Skip to content

Commit

Permalink
created pixabay datasource and Factory - created pixabay viewModel an…
Browse files Browse the repository at this point in the history
…d Factory - edited pixabay repo - created pixabay viewPager adapter and fragment - added search capability to pixabay - added toolbar title lookup
  • Loading branch information
medyas committed Jan 13, 2019
1 parent 4d056d9 commit 217e4e0
Show file tree
Hide file tree
Showing 29 changed files with 739 additions and 220 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ml.medyas.wallbay.adapters.pexels;

public class PexelsDataSource {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ml.medyas.wallbay.adapters.pexels;

public class PexelsDataSourceFactory {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ml.medyas.wallbay.adapters.pixabay;

import android.arch.lifecycle.MutableLiveData;
import android.arch.paging.PageKeyedDataSource;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import ml.medyas.wallbay.entities.ImageEntity;
import ml.medyas.wallbay.entities.pixabay.Hit;
import ml.medyas.wallbay.entities.pixabay.PixabayEntity;
import ml.medyas.wallbay.repositories.PixabayRepository;
import ml.medyas.wallbay.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PixabayDataSource extends PageKeyedDataSource<Integer, ImageEntity> {
private PixabayRepository pixabayRepository;
private MutableLiveData<Utils.NetworkState> networkState;
private String query;
private String category;
private String colors;
private boolean editorsChoice;
private String orderBy;


public PixabayDataSource(Context context, String query, String category, String colors, boolean editorsChoice, String orderBy) {
pixabayRepository = new PixabayRepository(context);
networkState = new MutableLiveData<>();
this.query = query;
this.category = category;
this.colors = colors;
this.editorsChoice = editorsChoice;
this.orderBy = orderBy;
}

public MutableLiveData<Utils.NetworkState> getNetworkState() {
return networkState;
}

@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, ImageEntity> callback) {
networkState.postValue(Utils.NetworkState.LOADING);
pixabayRepository.getSearch(query, 1, category, colors, editorsChoice, orderBy).enqueue(new Callback<PixabayEntity>() {
@Override
public void onResponse(@NonNull Call<PixabayEntity> call, @NonNull Response<PixabayEntity> response) {
if (response.isSuccessful() && response.body() != null) {
List<ImageEntity> list = new ArrayList<>();
for (Hit item : response.body().getHits()) {
ImageEntity imageEntity = new ImageEntity(
String.valueOf(item.getId()),
item.getUser(),
item.getUserImageURL(),
Utils.webSite.PIXABAY,
item.getLikes(),
item.getViews(),
item.getDownloads(),
item.getImageWidth(),
item.getImageHeight(),
item.getPageURL(),
item.getImageURL(),
item.getImageURL(), item.getWebformatURL(),
item.getTags()
);
list.add(imageEntity);
}
callback.onResult(list, null, 2);
networkState.postValue(Utils.NetworkState.LOADED);

} else {
ImageEntity item = new ImageEntity();
item.setProvider(Utils.webSite.EMPTY);
List<ImageEntity> list = new ArrayList<>();
list.add(item);
callback.onResult(list, null, null);
networkState.postValue(Utils.NetworkState.EMPTY);
}
}

@Override
public void onFailure(@NonNull Call<PixabayEntity> call, @NonNull Throwable t) {
t.printStackTrace();
Log.d("mainactivity", "NetError: " + t.getMessage());
networkState.postValue(Utils.NetworkState.FAILED);
}
});
}

@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, ImageEntity> callback) {
}

@Override
public void loadAfter(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, ImageEntity> callback) {
networkState.postValue(Utils.NetworkState.LOADING);
pixabayRepository.getSearch(query, params.key, category, colors, editorsChoice, orderBy).enqueue(new Callback<PixabayEntity>() {
@Override
public void onResponse(@NonNull Call<PixabayEntity> call, @NonNull Response<PixabayEntity> response) {
if (response.isSuccessful() && response.body() != null) {
List<ImageEntity> list = new ArrayList<>();
for (Hit item : response.body().getHits()) {
ImageEntity imageEntity = new ImageEntity(
String.valueOf(item.getId()),
item.getUser(),
item.getUserImageURL(),
Utils.webSite.PIXABAY,
item.getLikes(),
item.getViews(),
item.getDownloads(),
item.getImageWidth(),
item.getImageHeight(),
item.getPageURL(),
item.getImageURL(),
item.getImageURL(), item.getWebformatURL(),
item.getTags()
);
list.add(imageEntity);
}
callback.onResult(list, params.key+1);
networkState.postValue(Utils.NetworkState.LOADED);

} else {
ImageEntity item = new ImageEntity();
item.setProvider(Utils.webSite.EMPTY);
List<ImageEntity> list = new ArrayList<>();
list.add(item);
callback.onResult(list, null);
networkState.postValue(Utils.NetworkState.EMPTY);
}
}

@Override
public void onFailure(@NonNull Call<PixabayEntity> call, @NonNull Throwable t) {
t.printStackTrace();
Log.d("mainactivity", "NetError: " + t.getMessage());
networkState.postValue(Utils.NetworkState.FAILED);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ml.medyas.wallbay.adapters.pixabay;

import android.arch.lifecycle.MutableLiveData;
import android.arch.paging.DataSource;
import android.content.Context;

public class PixabayDataSourceFactory extends DataSource.Factory {
private PixabayDataSource pixabayDataSource;
private MutableLiveData<PixabayDataSource> mutableLiveData;
private Context context;
private String query;
private String category;
private String colors;
private boolean editorsChoice;
private String orderBy;

public PixabayDataSourceFactory(Context context, String query, String category, String colors, boolean editorsChoice, String orderBy) {
this.mutableLiveData = new MutableLiveData<>();
this.context = context;
this.query = query;
this.category = category;
this.colors = colors;
this.editorsChoice = editorsChoice;
this.orderBy = orderBy;
}

@Override
public DataSource create() {
pixabayDataSource = new PixabayDataSource(context, query, category, colors, editorsChoice, orderBy);
mutableLiveData.postValue(pixabayDataSource);
return pixabayDataSource;
}

public MutableLiveData<PixabayDataSource> getMutableLiveData() {
return mutableLiveData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ml.medyas.wallbay.adapters.pixabay;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import ml.medyas.wallbay.R;
import ml.medyas.wallbay.ui.fragments.PixabayViewPagerFragment;

public class PixabayViewPagerAdapter extends FragmentStatePagerAdapter {
private Resources res;

public PixabayViewPagerAdapter(FragmentManager fm, Context context) {
super(fm);
res = context.getResources();
}

@Override
public Fragment getItem(int i) {
return PixabayViewPagerFragment.newInstance(i);
}

@Override
public int getCount() {
return 3;
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return res.getString(R.string.popular);
case 1:
return res.getString(R.string.latest);
case 2:
return res.getString(R.string.editors_choice);
}
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ml.medyas.wallbay.adapters.unsplash;

public class UnsplashDataSource {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ml.medyas.wallbay.adapters.unsplash;

public class UnsplashDataSourceFactory {
}
26 changes: 0 additions & 26 deletions app/src/main/java/ml/medyas/wallbay/models/PixabayViewModel.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ml.medyas.wallbay.models.pixabay;

import android.arch.core.util.Function;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Transformations;
import android.arch.lifecycle.ViewModel;
import android.arch.paging.LivePagedListBuilder;
import android.arch.paging.PagedList;
import android.content.Context;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import ml.medyas.wallbay.adapters.pixabay.PixabayDataSource;
import ml.medyas.wallbay.adapters.pixabay.PixabayDataSourceFactory;
import ml.medyas.wallbay.entities.ImageEntity;
import ml.medyas.wallbay.utils.Utils;

public class PixabayViewModel extends ViewModel {
private LiveData<Utils.NetworkState> networkStateLiveData;
private LiveData<PagedList<ImageEntity>> pagedListLiveData;

public PixabayViewModel(Context context, String query, String category, String colors, boolean editorsChoice, String orderBy) {

Executor executor = Executors.newFixedThreadPool(3);

PixabayDataSourceFactory pixabayDataSourceFactory = new PixabayDataSourceFactory(context, query, category, colors, editorsChoice, orderBy);

networkStateLiveData = Transformations.switchMap(pixabayDataSourceFactory.getMutableLiveData(), new Function<PixabayDataSource, LiveData<Utils.NetworkState>>() {
@Override
public LiveData<Utils.NetworkState> apply(PixabayDataSource input) {
return input.getNetworkState();
}
});

PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(30 * 2)
.setPrefetchDistance(60)
.setPageSize(30)
.build();

pagedListLiveData = new LivePagedListBuilder(pixabayDataSourceFactory, config).setFetchExecutor(executor).build();
}

public LiveData<Utils.NetworkState> getNetworkStateLiveData() {
return networkStateLiveData;
}

public LiveData<PagedList<ImageEntity>> getPagedListLiveData() {
return pagedListLiveData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ml.medyas.wallbay.models.pixabay;

import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.content.Context;
import android.support.annotation.NonNull;

public class PixabayViewModelFactory implements ViewModelProvider.Factory {
private String query;
private String category;
private String colors;
private boolean editorsChoice;
private String orderBy;
private Context context;

public PixabayViewModelFactory(Context context, String query, String category, String colors, boolean editorsChoice, String orderBy) {
this.query = query;
this.category = category;
this.colors = colors;
this.editorsChoice = editorsChoice;
this.orderBy = orderBy;
this.context = context;
}

@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new PixabayViewModel(context, query, category, colors, editorsChoice, orderBy);
}
}
Loading

0 comments on commit 217e4e0

Please sign in to comment.