├── README.md ├── javacode ├── .gitignore ├── .idea │ ├── encodings.xml │ ├── gradle.xml │ ├── misc.xml │ ├── modules.xml │ └── runConfigurations.xml ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── androidcodehub │ │ │ └── androidpagingsample │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── androidcodehub │ │ │ │ └── androidpagingsample │ │ │ │ ├── AppsExecutor.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── api │ │ │ │ ├── ProductResponse.java │ │ │ │ ├── Service.java │ │ │ │ └── WalmartApi.java │ │ │ │ ├── model │ │ │ │ ├── DataLoadState.java │ │ │ │ └── Product.java │ │ │ │ └── ui │ │ │ │ ├── ProductAdapter.java │ │ │ │ ├── ProductDataFactory.java │ │ │ │ ├── ProductDataSource.java │ │ │ │ ├── ProductRepository.java │ │ │ │ ├── ProductRepositoryImpl.java │ │ │ │ └── ProductViewModel.java │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ ├── product_content.xml │ │ │ └── productlist.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── androidcodehub │ │ └── androidpagingsample │ │ └── ExampleUnitTest.java ├── build.gradle ├── device-2018-01-31-231940.mp4 ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle └── versions.gradle /README.md: -------------------------------------------------------------------------------- 1 | # android-paging-sample 2 | Android Architecture Components Paging Library Sample 3 | 4 | more info at http://androidcodehub.com/paginglibrary/ 5 | -------------------------------------------------------------------------------- /javacode/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /javacode/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /javacode/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /javacode/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | General 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 49 | -------------------------------------------------------------------------------- /javacode/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /javacode/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /javacode/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /javacode/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | buildToolsVersion "26.0.3" 6 | defaultConfig { 7 | applicationId "com.androidcodehub.androidpagingsample" 8 | minSdkVersion 15 9 | targetSdkVersion 27 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | debug { 16 | testCoverageEnabled !project.hasProperty('android.injected.invoked.from.ide') 17 | } 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | dataBinding { 24 | enabled = true 25 | } 26 | compileOptions { 27 | sourceCompatibility JavaVersion.VERSION_1_8 28 | targetCompatibility JavaVersion.VERSION_1_8 29 | } 30 | sourceSets { 31 | androidTest.java.srcDirs += "src/test-common/java" 32 | test.java.srcDirs += "src/test-common/java" 33 | } 34 | lintOptions { 35 | lintConfig rootProject.file('lint.xml') 36 | } 37 | } 38 | 39 | 40 | dependencies { 41 | implementation deps.support.app_compat 42 | implementation deps.support.recyclerview 43 | implementation deps.support.cardview 44 | implementation deps.support.design 45 | implementation deps.room.runtime 46 | implementation deps.lifecycle.runtime 47 | implementation deps.lifecycle.extensions 48 | implementation deps.lifecycle.java8 49 | implementation deps.retrofit.runtime 50 | implementation deps.retrofit.gson 51 | implementation deps.glide 52 | implementation "android.arch.paging:runtime:1.0.0-alpha4-1" 53 | /* 54 | implementation deps.dagger.runtime 55 | implementation deps.dagger.android 56 | implementation deps.dagger.android_support*/ 57 | implementation deps.constraint_layout 58 | 59 | implementation deps.timber 60 | 61 | annotationProcessor deps.dagger.android_support_compiler 62 | annotationProcessor deps.dagger.compiler 63 | annotationProcessor deps.room.compiler 64 | annotationProcessor deps.lifecycle.compiler 65 | 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /javacode/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /javacode/app/src/androidTest/java/com/androidcodehub/androidpagingsample/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.androidcodehub.androidpagingsample", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /javacode/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/AppsExecutor.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample; 2 | 3 | 4 | import android.os.Handler; 5 | import android.os.Looper; 6 | import android.support.annotation.NonNull; 7 | 8 | import java.util.concurrent.Executor; 9 | import java.util.concurrent.Executors; 10 | 11 | public class AppsExecutor { 12 | 13 | private final static Executor networkIO = Executors.newFixedThreadPool(3); 14 | 15 | private final static Executor mainThread = new MainThreadExecutor(); 16 | 17 | private AppsExecutor() { 18 | 19 | } 20 | 21 | public static Executor networkIO() { 22 | return networkIO; 23 | } 24 | 25 | public static Executor mainThread() { 26 | return mainThread; 27 | } 28 | 29 | private static class MainThreadExecutor implements Executor { 30 | private Handler mainThreadHandler = new Handler(Looper.getMainLooper()); 31 | @Override 32 | public void execute(@NonNull Runnable command) { 33 | mainThreadHandler.post(command); 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample; 2 | 3 | import android.arch.lifecycle.ViewModelProviders; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.support.v7.widget.DividerItemDecoration; 8 | import android.support.v7.widget.RecyclerView; 9 | import android.support.v7.widget.Toolbar; 10 | import android.util.Log; 11 | import android.view.View; 12 | import android.widget.ProgressBar; 13 | import android.widget.Toast; 14 | 15 | import com.androidcodehub.androidpagingsample.ui.ProductAdapter; 16 | import com.androidcodehub.androidpagingsample.ui.ProductViewModel; 17 | 18 | 19 | public class MainActivity extends AppCompatActivity { 20 | 21 | private static final String TAG = "ProductListActivity"; 22 | private boolean mTwoPane; 23 | private ProductViewModel mProductsViewModel; 24 | private ProductAdapter mAdapter; 25 | 26 | private ProgressBar mLoadProgressBar; 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | 31 | setContentView(R.layout.activity_main); 32 | 33 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 34 | setSupportActionBar(toolbar); 35 | toolbar.setTitle(getTitle()); 36 | 37 | mLoadProgressBar = findViewById(R.id.progress_bar); 38 | 39 | View recyclerView = findViewById(R.id.product_list); 40 | assert recyclerView != null; 41 | 42 | 43 | setupRecyclerView((RecyclerView) recyclerView); 44 | 45 | mProductsViewModel = ViewModelProviders.of(this).get(ProductViewModel.class); 46 | mProductsViewModel.dataLoadStatus().observe(this, loadStatus -> { 47 | switch (loadStatus) { 48 | case LOADING: 49 | mLoadProgressBar.setVisibility(View.VISIBLE); 50 | break; 51 | case LOADED: 52 | mLoadProgressBar.setVisibility(View.GONE); 53 | break; 54 | case FAILED: 55 | mLoadProgressBar.setVisibility(View.GONE); 56 | Toast.makeText(this,"Failed to connect to Walmart Service", 57 | Toast.LENGTH_LONG); 58 | break; 59 | } 60 | }); 61 | mProductsViewModel.getProducts().observe(this, pagedList -> { 62 | mAdapter.setList(pagedList); 63 | 64 | }); 65 | } 66 | 67 | private void setupRecyclerView(@NonNull RecyclerView recyclerView) { 68 | mAdapter = new ProductAdapter(this, mTwoPane); 69 | recyclerView.setAdapter(mAdapter); 70 | recyclerView.setHasFixedSize(true); 71 | 72 | recyclerView.addItemDecoration(new DividerItemDecoration(this, 73 | DividerItemDecoration.VERTICAL)); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/api/ProductResponse.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.api; 2 | 3 | 4 | 5 | import com.androidcodehub.androidpagingsample.model.Product; 6 | 7 | import java.util.List; 8 | 9 | 10 | public class ProductResponse { 11 | public List products; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/api/Service.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.api; 2 | 3 | import retrofit2.Retrofit; 4 | import retrofit2.converter.gson.GsonConverterFactory; 5 | 6 | 7 | 8 | public class Service { 9 | 10 | private static final Service service = new Service(); 11 | private static WalmartApi api; 12 | 13 | private static final String API_URL 14 | = "https://walmartlabs-test.appspot.com/_ah/api/walmart/v1/walmartproducts/"+ 15 | "56330880-82c2-47eb-9d5d-03db3a49e3bf/"; 16 | 17 | private Service() { 18 | 19 | 20 | api = new Retrofit.Builder() 21 | .baseUrl(API_URL) 22 | .addConverterFactory(GsonConverterFactory.create()) 23 | //.client(client) 24 | .build() 25 | .create(WalmartApi.class); 26 | } 27 | 28 | public static WalmartApi get() { 29 | return service.api; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/api/WalmartApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.androidcodehub.androidpagingsample.api; 18 | 19 | import retrofit2.Call; 20 | import retrofit2.http.GET; 21 | import retrofit2.http.Path; 22 | 23 | public interface WalmartApi { 24 | 25 | @GET("{page}/{size}") 26 | Call getProducts(@Path("page") int pageNumber, @Path("size") int pageSize); 27 | } 28 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/model/DataLoadState.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.model; 2 | 3 | 4 | 5 | public enum DataLoadState { 6 | 7 | LOADING, 8 | LOADED, 9 | FAILED 10 | 11 | } 12 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/model/Product.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.model; 2 | 3 | 4 | import android.os.Parcel; 5 | import android.os.Parcelable; 6 | 7 | 8 | public class Product implements Parcelable { 9 | public String productId; 10 | public String productName; 11 | public String shortDescription; 12 | public String longDescription; 13 | public String price; 14 | public String productImage; 15 | public float reviewRating; 16 | public int reviewCount; 17 | public boolean inStock; 18 | 19 | public Product(Parcel in){ 20 | this.productId = in.readString(); 21 | this.productName = in.readString(); 22 | this.shortDescription = in.readString(); 23 | this.longDescription = in.readString(); 24 | this.price = in.readString(); 25 | this.productImage = in.readString(); 26 | this.reviewRating = in.readFloat(); 27 | this.reviewCount = in.readInt(); 28 | this.inStock = in.readInt() == 1; 29 | } 30 | 31 | @Override 32 | public int describeContents() { 33 | return 0; 34 | } 35 | 36 | @Override 37 | public void writeToParcel(Parcel dest, int flags) { 38 | dest.writeString(productId); 39 | dest.writeString(productName); 40 | dest.writeString(shortDescription); 41 | dest.writeString(longDescription); 42 | dest.writeString(price); 43 | dest.writeString(productImage); 44 | dest.writeFloat(reviewRating); 45 | dest.writeInt(reviewCount); 46 | dest.writeInt(inStock ? 1 : 0); 47 | 48 | } 49 | @Override 50 | public String toString() { 51 | 52 | return "id="+productId 53 | +",\nimg="+productImage 54 | +",\ninstock="+inStock; 55 | } 56 | 57 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 58 | 59 | @Override 60 | public Product createFromParcel(Parcel source) { 61 | return new Product(source); 62 | } 63 | 64 | @Override 65 | public Product[] newArray(int size) { 66 | return new Product[size]; 67 | } 68 | }; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/ui/ProductAdapter.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.ui; 2 | 3 | 4 | import android.arch.paging.PagedListAdapter; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.net.Uri; 8 | import android.os.Bundle; 9 | import android.support.annotation.NonNull; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.support.v7.recyclerview.extensions.DiffCallback; 12 | import android.support.v7.widget.RecyclerView; 13 | import android.view.LayoutInflater; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | import android.widget.ImageView; 17 | import android.widget.RatingBar; 18 | import android.widget.TextView; 19 | import android.widget.Toast; 20 | 21 | import com.androidcodehub.androidpagingsample.R; 22 | import com.androidcodehub.androidpagingsample.model.Product; 23 | import com.bumptech.glide.Glide; 24 | import com.bumptech.glide.load.engine.DiskCacheStrategy; 25 | 26 | public class ProductAdapter extends PagedListAdapter { 27 | 28 | private Context mContext; 29 | 30 | 31 | public ProductAdapter(Context context, boolean twoPane) { 32 | super(DIFF_CALLBACK); 33 | mContext = context; 34 | 35 | } 36 | 37 | @Override 38 | public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 39 | View view = LayoutInflater.from(parent.getContext()) 40 | .inflate(R.layout.product_content, parent, false); 41 | return new ProductViewHolder(view); 42 | } 43 | 44 | @Override 45 | public void onBindViewHolder(ProductViewHolder holder, int position) { 46 | Product product = getItem(position); 47 | if (product != null) { 48 | holder.bindTo(product); 49 | } else { 50 | 51 | holder.clear(); 52 | } 53 | } 54 | public static final DiffCallback DIFF_CALLBACK = new DiffCallback() { 55 | @Override 56 | public boolean areItemsTheSame(@NonNull Product oldProduct, @NonNull Product newProduct) { 57 | 58 | return oldProduct.productId == newProduct.productId; 59 | } 60 | @Override 61 | public boolean areContentsTheSame(@NonNull Product oldProduct, @NonNull Product newProduct) { 62 | 63 | return oldProduct.equals(newProduct); 64 | } 65 | }; 66 | 67 | public class ProductViewHolder extends RecyclerView.ViewHolder { 68 | private View itemView; 69 | private ImageView productImage; 70 | private TextView title; 71 | private TextView price; 72 | 73 | 74 | public ProductViewHolder(View itemView) { 75 | super(itemView); 76 | this.itemView = itemView; 77 | this.productImage = (ImageView) itemView.findViewById(R.id.product); 78 | this.title = (TextView) itemView.findViewById(R.id.title); 79 | this.price = (TextView) itemView.findViewById(R.id.price); 80 | 81 | 82 | } 83 | 84 | public void bindTo(Product product) { 85 | 86 | productImage.setImageURI(Uri.parse(product.productImage)); 87 | title.setText(product.productName); 88 | price.setText(product.price); 89 | 90 | 91 | Glide.with(mContext).load(product.productImage) 92 | .thumbnail(0.5f) 93 | .crossFade() 94 | .diskCacheStrategy(DiskCacheStrategy.ALL) 95 | .into(productImage); 96 | 97 | itemView.setOnClickListener(new View.OnClickListener() { 98 | @Override 99 | public void onClick(View v) { 100 | 101 | Toast.makeText(mContext,product.productName,Toast.LENGTH_LONG).show(); 102 | 103 | } 104 | 105 | 106 | }); 107 | } 108 | 109 | public void clear() { 110 | //TODO 111 | 112 | productImage.setImageURI(null); 113 | title.setText(null); 114 | price.setText(null); 115 | 116 | 117 | } 118 | } 119 | 120 | 121 | 122 | } 123 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/ui/ProductDataFactory.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.ui; 2 | 3 | import android.arch.lifecycle.MutableLiveData; 4 | import android.arch.paging.DataSource; 5 | 6 | import com.androidcodehub.androidpagingsample.api.Service; 7 | import com.androidcodehub.androidpagingsample.model.Product; 8 | 9 | 10 | 11 | public class ProductDataFactory implements DataSource.Factory { 12 | 13 | public MutableLiveData datasourceLiveData = new MutableLiveData<>(); 14 | 15 | @Override 16 | public DataSource create() { 17 | 18 | ProductDataSource dataSource = new ProductDataSource(Service.get()); 19 | datasourceLiveData.postValue(dataSource); 20 | return dataSource; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/ui/ProductDataSource.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.ui; 2 | 3 | import android.arch.lifecycle.MutableLiveData; 4 | import android.arch.paging.PageKeyedDataSource; 5 | import android.support.annotation.NonNull; 6 | 7 | 8 | import com.androidcodehub.androidpagingsample.api.ProductResponse; 9 | import com.androidcodehub.androidpagingsample.api.WalmartApi; 10 | import com.androidcodehub.androidpagingsample.model.DataLoadState; 11 | import com.androidcodehub.androidpagingsample.model.Product; 12 | 13 | import java.io.IOException; 14 | 15 | import retrofit2.Call; 16 | import retrofit2.Response; 17 | 18 | 19 | public class ProductDataSource extends PageKeyedDataSource { 20 | 21 | private WalmartApi walmartApi; 22 | 23 | public final MutableLiveData loadState; 24 | 25 | public ProductDataSource(WalmartApi walmartApi) { 26 | 27 | this.walmartApi = walmartApi; 28 | loadState = new MutableLiveData(); 29 | 30 | } 31 | 32 | @Override 33 | public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) { 34 | 35 | loadState.postValue(DataLoadState.LOADING); 36 | 37 | Call request = walmartApi.getProducts(1, params.requestedLoadSize); 38 | 39 | Response response = null; 40 | try{ 41 | response = request.execute(); 42 | if(response != null) { 43 | callback.onResult(response.body().products, 1, 2); 44 | }else { 45 | callback.onResult(null, null,2); 46 | } 47 | loadState.postValue(DataLoadState.LOADED); 48 | }catch (IOException ex) { 49 | loadState.postValue(DataLoadState.FAILED); 50 | } 51 | 52 | } 53 | 54 | @Override 55 | public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) { 56 | 57 | 58 | loadState.postValue(DataLoadState.LOADING); 59 | 60 | Call request = walmartApi.getProducts(params.key, params.requestedLoadSize); 61 | 62 | Response response = null; 63 | try{ 64 | response = request.execute(); 65 | if(response != null) { 66 | Integer adjacentKey = (params.key > 1) ? params.key - 1 : null; 67 | callback.onResult(response.body().products, adjacentKey); 68 | }else { 69 | callback.onResult(null, params.key - 1); 70 | } 71 | loadState.postValue(DataLoadState.LOADED); 72 | }catch (IOException ex) { 73 | //networkState.postValue(); 74 | } 75 | } 76 | 77 | @Override 78 | public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) { 79 | 80 | loadState.postValue(DataLoadState.LOADING); 81 | 82 | Call request = walmartApi.getProducts(params.key , params.requestedLoadSize); 83 | 84 | Response response = null; 85 | try{ 86 | response = request.execute(); 87 | if(response != null) { 88 | callback.onResult(response.body().products, params.key + 1); 89 | }else { 90 | callback.onResult(null, params.key + 1 ); 91 | } 92 | loadState.postValue(DataLoadState.LOADED); 93 | }catch (IOException ex) { 94 | //networkState.postValue(); 95 | loadState.postValue(DataLoadState.FAILED); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/ui/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.ui; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.paging.PagedList; 5 | 6 | import com.androidcodehub.androidpagingsample.model.DataLoadState; 7 | import com.androidcodehub.androidpagingsample.model.Product; 8 | 9 | 10 | public interface ProductRepository { 11 | 12 | public LiveData> getProducts(); 13 | public LiveData getDataLoadStatus(); 14 | } 15 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/ui/ProductRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.ui; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.paging.LivePagedListBuilder; 5 | import android.arch.paging.PagedList; 6 | import android.arch.paging.PagedList.Config.Builder; 7 | import android.support.annotation.MainThread; 8 | 9 | 10 | import com.androidcodehub.androidpagingsample.AppsExecutor; 11 | import com.androidcodehub.androidpagingsample.model.DataLoadState; 12 | import com.androidcodehub.androidpagingsample.model.Product; 13 | 14 | import static android.arch.lifecycle.Transformations.switchMap; 15 | 16 | 17 | public class ProductRepositoryImpl implements ProductRepository{ 18 | 19 | //private Executor networkExecutor; 20 | ProductDataFactory dataSourceFactory; 21 | private static final int PAGE_SIZE = 30; 22 | private LiveData> products; 23 | 24 | public ProductRepositoryImpl() { 25 | dataSourceFactory = new ProductDataFactory(); 26 | //this.networkExecutor = networkExecutor; 27 | } 28 | 29 | @Override 30 | @MainThread 31 | public LiveData> getProducts() { 32 | 33 | PagedList.Config config = new Builder() 34 | .setInitialLoadSizeHint(PAGE_SIZE) 35 | .setPageSize(PAGE_SIZE) 36 | .build(); 37 | 38 | 39 | products = new LivePagedListBuilder(dataSourceFactory, config) 40 | .setInitialLoadKey(1) 41 | .setBackgroundThreadExecutor(AppsExecutor.networkIO()) 42 | .build(); 43 | 44 | return products; 45 | } 46 | 47 | public LiveData getDataLoadStatus(){ 48 | return switchMap(dataSourceFactory.datasourceLiveData, 49 | dataSource -> dataSource.loadState); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /javacode/app/src/main/java/com/androidcodehub/androidpagingsample/ui/ProductViewModel.java: -------------------------------------------------------------------------------- 1 | package com.androidcodehub.androidpagingsample.ui; 2 | 3 | import android.app.Application; 4 | import android.arch.lifecycle.AndroidViewModel; 5 | import android.arch.lifecycle.LiveData; 6 | import android.arch.paging.PagedList; 7 | import android.support.annotation.NonNull; 8 | 9 | import com.androidcodehub.androidpagingsample.model.DataLoadState; 10 | import com.androidcodehub.androidpagingsample.model.Product; 11 | 12 | 13 | public class ProductViewModel extends AndroidViewModel { 14 | 15 | private ProductRepository repository; 16 | 17 | public ProductViewModel(@NonNull Application application) { 18 | super(application); 19 | repository = new ProductRepositoryImpl(); 20 | } 21 | 22 | public LiveData> getProducts() { 23 | return repository.getProducts(); 24 | } 25 | 26 | public LiveData dataLoadStatus() { 27 | return repository.getDataLoadStatus(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 21 | 22 | 23 | 24 | 29 | 30 | 31 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/layout/product_content.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 21 | 22 | 33 | 34 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/layout/productlist.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/androidcodehub/android-paging-sample/633e76fe4400dbae63535a682613f365e7cafe39/javacode/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /javacode/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 200dp 5 | 200dp 6 | 16dp 7 | 8 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidPagingSample 3 | 4 | -------------------------------------------------------------------------------- /javacode/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | 16 | 17 |