mListeners = new CopyOnWriteArrayList<>();
48 | private final Object mLock = new Object();
49 | private final Executor mRetryService;
50 | @GuardedBy("mLock")
51 | private final RequestQueue[] mRequestQueues = new RequestQueue[]
52 | {new RequestQueue(RequestType.INITIAL),
53 | new RequestQueue(RequestType.BEFORE),
54 | new RequestQueue(RequestType.AFTER)};
55 |
56 | /**
57 | * Creates a new PagingRequestHelper with the given {@link Executor} which is used to run
58 | * retry actions.
59 | *
60 | * @param retryService The {@link Executor} that can run the retry actions.
61 | */
62 | public PagingRequestHelper(@NonNull Executor retryService) {
63 | mRetryService = retryService;
64 | }
65 |
66 | /**
67 | * Adds a new listener that will be notified when any request changes {@link Status state}.
68 | *
69 | * @param listener The listener that will be notified each time a request's status changes.
70 | * @return True if it is added, false otherwise (e.g. it already exists in the list).
71 | */
72 | @AnyThread
73 | public boolean addListener(@NonNull Listener listener) {
74 | return mListeners.add(listener);
75 | }
76 |
77 | /**
78 | * Removes the given listener from the listeners list.
79 | *
80 | * @param listener The listener that will be removed.
81 | * @return True if the listener is removed, false otherwise (e.g. it never existed)
82 | */
83 | public boolean removeListener(@NonNull Listener listener) {
84 | return mListeners.remove(listener);
85 | }
86 |
87 | /**
88 | * Runs the given {@link Request} if no other requests in the given request type is already
89 | * running.
90 | *
91 | * If run, the request will be run in the current thread.
92 | *
93 | * @param type The type of the request.
94 | * @param request The request to run.
95 | * @return True if the request is run, false otherwise.
96 | */
97 | @SuppressWarnings("WeakerAccess")
98 | @AnyThread
99 | public boolean runIfNotRunning(@NonNull RequestType type, @NonNull Request request) {
100 | boolean hasListeners = !mListeners.isEmpty();
101 | StatusReport report = null;
102 | synchronized (mLock) {
103 | RequestQueue queue = mRequestQueues[type.ordinal()];
104 | if (queue.mRunning != null) {
105 | return false;
106 | }
107 | queue.mRunning = request;
108 | queue.mStatus = Status.RUNNING;
109 | queue.mFailed = null;
110 | queue.mLastError = null;
111 | if (hasListeners) {
112 | report = prepareStatusReportLocked();
113 | }
114 | }
115 | if (report != null) {
116 | dispatchReport(report);
117 | }
118 | final RequestWrapper wrapper = new RequestWrapper(request, this, type);
119 | wrapper.run();
120 | return true;
121 | }
122 |
123 | @GuardedBy("mLock")
124 | private StatusReport prepareStatusReportLocked() {
125 | Throwable[] errors = new Throwable[]{
126 | mRequestQueues[0].mLastError,
127 | mRequestQueues[1].mLastError,
128 | mRequestQueues[2].mLastError
129 | };
130 | return new StatusReport(
131 | getStatusForLocked(RequestType.INITIAL),
132 | getStatusForLocked(RequestType.BEFORE),
133 | getStatusForLocked(RequestType.AFTER),
134 | errors
135 | );
136 | }
137 |
138 | @GuardedBy("mLock")
139 | private Status getStatusForLocked(RequestType type) {
140 | return mRequestQueues[type.ordinal()].mStatus;
141 | }
142 |
143 | @AnyThread
144 | @VisibleForTesting
145 | void recordResult(@NonNull RequestWrapper wrapper, @Nullable Throwable throwable) {
146 | StatusReport report = null;
147 | final boolean success = throwable == null;
148 | boolean hasListeners = !mListeners.isEmpty();
149 | synchronized (mLock) {
150 | RequestQueue queue = mRequestQueues[wrapper.mType.ordinal()];
151 | queue.mRunning = null;
152 | queue.mLastError = throwable;
153 | if (success) {
154 | queue.mFailed = null;
155 | queue.mStatus = Status.SUCCESS;
156 | } else {
157 | queue.mFailed = wrapper;
158 | queue.mStatus = Status.FAILED;
159 | }
160 | if (hasListeners) {
161 | report = prepareStatusReportLocked();
162 | }
163 | }
164 | if (report != null) {
165 | dispatchReport(report);
166 | }
167 | }
168 |
169 | private void dispatchReport(StatusReport report) {
170 | for (Listener listener : mListeners) {
171 | listener.onStatusChange(report);
172 | }
173 | }
174 |
175 | /**
176 | * Retries all failed requests.
177 | *
178 | * @return True if any request is retried, false otherwise.
179 | */
180 | public boolean retryAllFailed() {
181 | final RequestWrapper[] toBeRetried = new RequestWrapper[RequestType.values().length];
182 | boolean retried = false;
183 | synchronized (mLock) {
184 | for (int i = 0; i < RequestType.values().length; i++) {
185 | toBeRetried[i] = mRequestQueues[i].mFailed;
186 | mRequestQueues[i].mFailed = null;
187 | }
188 | }
189 | for (RequestWrapper failed : toBeRetried) {
190 | if (failed != null) {
191 | failed.retry(mRetryService);
192 | retried = true;
193 | }
194 | }
195 | return retried;
196 | }
197 |
198 | /**
199 | * Represents the status of a Request for each {@link RequestType}.
200 | */
201 | public enum Status {
202 | /**
203 | * There is current a running request.
204 | */
205 | RUNNING,
206 | /**
207 | * The last request has succeeded or no such requests have ever been run.
208 | */
209 | SUCCESS,
210 | /**
211 | * The last request has failed.
212 | */
213 | FAILED
214 | }
215 |
216 | /**
217 | * Available request types.
218 | */
219 | public enum RequestType {
220 | /**
221 | * Corresponds to an initial request made to a {@link DataSource} or the empty state for
222 | * a {@link android.arch.paging.PagedList.BoundaryCallback BoundaryCallback}.
223 | */
224 | INITIAL,
225 | /**
226 | * Corresponds to the {@code loadBefore} calls in {@link DataSource} or
227 | * {@code onItemAtFrontLoaded} in
228 | * {@link android.arch.paging.PagedList.BoundaryCallback BoundaryCallback}.
229 | */
230 | BEFORE,
231 | /**
232 | * Corresponds to the {@code loadAfter} calls in {@link DataSource} or
233 | * {@code onItemAtEndLoaded} in
234 | * {@link android.arch.paging.PagedList.BoundaryCallback BoundaryCallback}.
235 | */
236 | AFTER
237 | }
238 |
239 | /**
240 | * Runner class that runs a request tracked by the {@link PagingRequestHelper}.
241 | *
242 | * When a request is invoked, it must call one of {@link Callback#recordFailure(Throwable)}
243 | * or {@link Callback#recordSuccess()} once and only once. This call
244 | * can be made any time. Until that method call is made, {@link PagingRequestHelper} will
245 | * consider the request is running.
246 | */
247 | @FunctionalInterface
248 | public interface Request {
249 | /**
250 | * Should run the request and call the given {@link Callback} with the result of the
251 | * request.
252 | *
253 | * @param callback The callback that should be invoked with the result.
254 | */
255 | void run(Callback callback);
256 |
257 | /**
258 | * Callback class provided to the {@link #run(Callback)} method to report the result.
259 | */
260 | class Callback {
261 | private final AtomicBoolean mCalled = new AtomicBoolean();
262 | private final RequestWrapper mWrapper;
263 | private final PagingRequestHelper mHelper;
264 |
265 | Callback(RequestWrapper wrapper, PagingRequestHelper helper) {
266 | mWrapper = wrapper;
267 | mHelper = helper;
268 | }
269 |
270 | /**
271 | * Call this method when the request succeeds and new data is fetched.
272 | */
273 | @SuppressWarnings("unused")
274 | public final void recordSuccess() {
275 | if (mCalled.compareAndSet(false, true)) {
276 | mHelper.recordResult(mWrapper, null);
277 | } else {
278 | throw new IllegalStateException(
279 | "already called recordSuccess or recordFailure");
280 | }
281 | }
282 |
283 | /**
284 | * Call this method with the failure message and the request can be retried via
285 | * {@link #retryAllFailed()}.
286 | *
287 | * @param throwable The error that occured while carrying out the request.
288 | */
289 | @SuppressWarnings("unused")
290 | public final void recordFailure(@NonNull Throwable throwable) {
291 | //noinspection ConstantConditions
292 | if (throwable == null) {
293 | throw new IllegalArgumentException("You must provide a throwable describing"
294 | + " the error to record the failure");
295 | }
296 | if (mCalled.compareAndSet(false, true)) {
297 | mHelper.recordResult(mWrapper, throwable);
298 | } else {
299 | throw new IllegalStateException(
300 | "already called recordSuccess or recordFailure");
301 | }
302 | }
303 | }
304 | }
305 |
306 | /**
307 | * Listener interface to get notified by request status changes.
308 | */
309 | public interface Listener {
310 | /**
311 | * Called when the status for any of the requests has changed.
312 | *
313 | * @param report The current status report that has all the information about the requests.
314 | */
315 | void onStatusChange(@NonNull StatusReport report);
316 | }
317 |
318 | static class RequestWrapper implements Runnable {
319 | @NonNull
320 | final Request mRequest;
321 | @NonNull
322 | final PagingRequestHelper mHelper;
323 | @NonNull
324 | final RequestType mType;
325 |
326 | RequestWrapper(@NonNull Request request, @NonNull PagingRequestHelper helper,
327 | @NonNull RequestType type) {
328 | mRequest = request;
329 | mHelper = helper;
330 | mType = type;
331 | }
332 |
333 | @Override
334 | public void run() {
335 | mRequest.run(new Request.Callback(this, mHelper));
336 | }
337 |
338 | void retry(Executor service) {
339 | service.execute(new Runnable() {
340 | @Override
341 | public void run() {
342 | mHelper.runIfNotRunning(mType, mRequest);
343 | }
344 | });
345 | }
346 | }
347 |
348 | /**
349 | * Data class that holds the information about the current status of the ongoing requests
350 | * using this helper.
351 | */
352 | public static final class StatusReport {
353 | /**
354 | * Status of the latest request that were submitted with {@link RequestType#INITIAL}.
355 | */
356 | @NonNull
357 | public final Status initial;
358 | /**
359 | * Status of the latest request that were submitted with {@link RequestType#BEFORE}.
360 | */
361 | @NonNull
362 | public final Status before;
363 | /**
364 | * Status of the latest request that were submitted with {@link RequestType#AFTER}.
365 | */
366 | @NonNull
367 | public final Status after;
368 | @NonNull
369 | private final Throwable[] mErrors;
370 |
371 | StatusReport(@NonNull Status initial, @NonNull Status before, @NonNull Status after,
372 | @NonNull Throwable[] errors) {
373 | this.initial = initial;
374 | this.before = before;
375 | this.after = after;
376 | this.mErrors = errors;
377 | }
378 |
379 | /**
380 | * Convenience method to check if there are any running requests.
381 | *
382 | * @return True if there are any running requests, false otherwise.
383 | */
384 | public boolean hasRunning() {
385 | return initial == Status.RUNNING
386 | || before == Status.RUNNING
387 | || after == Status.RUNNING;
388 | }
389 |
390 | /**
391 | * Convenience method to check if there are any requests that resulted in an error.
392 | *
393 | * @return True if there are any requests that finished with error, false otherwise.
394 | */
395 | public boolean hasError() {
396 | return initial == Status.FAILED
397 | || before == Status.FAILED
398 | || after == Status.FAILED;
399 | }
400 |
401 | /**
402 | * Returns the error for the given request type.
403 | *
404 | * @param type The request type for which the error should be returned.
405 | * @return The {@link Throwable} returned by the failing request with the given type or
406 | * {@code null} if the request for the given type did not fail.
407 | */
408 | @Nullable
409 | public Throwable getErrorFor(@NonNull RequestType type) {
410 | return mErrors[type.ordinal()];
411 | }
412 |
413 | @Override
414 | public String toString() {
415 | return "StatusReport{"
416 | + "initial=" + initial
417 | + ", before=" + before
418 | + ", after=" + after
419 | + ", mErrors=" + Arrays.toString(mErrors)
420 | + '}';
421 | }
422 |
423 | @Override
424 | public boolean equals(Object o) {
425 | if (this == o) return true;
426 | if (o == null || getClass() != o.getClass()) return false;
427 | StatusReport that = (StatusReport) o;
428 | if (initial != that.initial) return false;
429 | if (before != that.before) return false;
430 | if (after != that.after) return false;
431 | // Probably incorrect - comparing Object[] arrays with Arrays.equals
432 | return Arrays.equals(mErrors, that.mErrors);
433 | }
434 |
435 | @Override
436 | public int hashCode() {
437 | int result = initial.hashCode();
438 | result = 31 * result + before.hashCode();
439 | result = 31 * result + after.hashCode();
440 | result = 31 * result + Arrays.hashCode(mErrors);
441 | return result;
442 | }
443 | }
444 |
445 | class RequestQueue {
446 | @NonNull
447 | final RequestType mRequestType;
448 | @Nullable
449 | RequestWrapper mFailed;
450 | @Nullable
451 | Request mRunning;
452 | @Nullable
453 | Throwable mLastError;
454 | @NonNull
455 | Status mStatus = Status.SUCCESS;
456 |
457 | RequestQueue(@NonNull RequestType requestType) {
458 | mRequestType = requestType;
459 | }
460 | }
461 | }
462 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spiraldev/mvvmpaging/data/remote/vo/MovieModel.kt:
--------------------------------------------------------------------------------
1 | package com.spiraldev.mvvmpaging.data.remote.vo
2 |
3 | import com.google.gson.annotations.SerializedName
4 | import com.spiraldev.mvvmpaging.data.local.MovieEntity
5 | import com.spiraldev.mvvmpaging.data.remote.Api.IMAGES_URL
6 |
7 | data class MovieModel(
8 | @SerializedName("id") val id: Int,
9 | @SerializedName("title") val title: String,
10 | @SerializedName("popularity") val popularity: Double,
11 | @SerializedName("vote_average") val voteAverage: Double,
12 | @SerializedName("poster_path") val posterPath: String,
13 | @SerializedName("release_date") val releaseDate: String
14 | )
15 |
16 | fun MovieModel.toMovieEntity() =
17 | MovieEntity(0, title, popularity, voteAverage, getPosterURL(posterPath), releaseDate)
18 |
19 | private fun getPosterURL(posterPath: String) = IMAGES_URL + posterPath
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spiraldev/mvvmpaging/data/remote/vo/ResponseModel.kt:
--------------------------------------------------------------------------------
1 | package com.spiraldev.mvvmpaging.data.remote.vo
2 |
3 | import com.google.gson.annotations.SerializedName
4 |
5 |
6 | data class ResponseModel(
7 | val page: Int,
8 | @SerializedName("results")
9 | val movieList: List,
10 | @SerializedName("total_pages")
11 | val totalPages: Int,
12 | @SerializedName("total_results")
13 | val totalResults: Int
14 | )
--------------------------------------------------------------------------------
/app/src/main/java/com/spiraldev/mvvmpaging/di/modules/ApplicationModule.kt:
--------------------------------------------------------------------------------
1 | package com.example.seemenstask.di.modules
2 |
3 | import android.app.Application
4 | import android.content.Context
5 | import com.spiraldev.mvvmpaging.data.local.MoviesDao
6 | import com.spiraldev.mvvmpaging.data.local.MoviesDatabase
7 | import com.spiraldev.mvvmpaging.data.remote.Api
8 | import com.spiraldev.mvvmpaging.data.remote.ApiService
9 | import dagger.Module
10 | import dagger.Provides
11 | import dagger.hilt.InstallIn
12 | import dagger.hilt.android.components.ApplicationComponent
13 | import dagger.hilt.android.qualifiers.ApplicationContext
14 | import okhttp3.Interceptor
15 | import okhttp3.OkHttpClient
16 | import okhttp3.logging.HttpLoggingInterceptor
17 | import retrofit2.Retrofit
18 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
19 | import retrofit2.converter.gson.GsonConverterFactory
20 | import java.util.concurrent.TimeUnit
21 | import javax.inject.Singleton
22 |
23 |
24 | @Module
25 | @InstallIn(ApplicationComponent::class)
26 | class ApplicationModule {
27 |
28 | @Provides
29 | @Singleton
30 | fun provideOkHttpClient(): OkHttpClient {
31 | val requestInterceptor = Interceptor { chain ->
32 |
33 | val url = chain.request()
34 | .url()
35 | .newBuilder()
36 | .build()
37 |
38 | val request = chain.request()
39 | .newBuilder()
40 | .url(url)
41 | .build()
42 |
43 | return@Interceptor chain.proceed(request)
44 | }
45 |
46 | return OkHttpClient.Builder()
47 | .addInterceptor(requestInterceptor)
48 | .connectTimeout(60, TimeUnit.SECONDS)
49 | .build()
50 | }
51 |
52 | @Provides
53 | @Singleton
54 | fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
55 |
56 | return Retrofit.Builder()
57 | .addConverterFactory(GsonConverterFactory.create())
58 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
59 | .baseUrl(Api.THE_MOVIE_URL)
60 | .client(okHttpClient)
61 | .build()
62 | }
63 |
64 | @Provides
65 | @Singleton
66 | fun provideApiClient(retrofit: Retrofit): ApiService {
67 | return retrofit.create(ApiService::class.java)
68 | }
69 |
70 | @Singleton
71 | @Provides
72 | fun providesMoviesDatabase(@ApplicationContext context: Context): MoviesDatabase =
73 | MoviesDatabase.buildDatabase(context)
74 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spiraldev/mvvmpaging/ui/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.spiraldev.mvvmpaging.ui
2 |
3 | import android.os.Bundle
4 | import android.view.View
5 | import androidx.activity.viewModels
6 | import androidx.appcompat.app.AppCompatActivity
7 | import androidx.lifecycle.Observer
8 | import androidx.recyclerview.widget.GridLayoutManager
9 | import androidx.recyclerview.widget.RecyclerView
10 | import com.spiraldev.mvvmpaging.R
11 | import com.spiraldev.mvvmpaging.adapters.MoviesPagedListAdapter
12 | import com.spiraldev.mvvmpaging.data.remote.NetworkState
13 | import dagger.hilt.android.AndroidEntryPoint
14 | import kotlinx.android.synthetic.main.activity_main.*
15 |
16 | @AndroidEntryPoint
17 | class MainActivity : AppCompatActivity() {
18 |
19 | val viewModel: MainActivityViewModel by viewModels()
20 |
21 | lateinit var moviesAdapter: MoviesPagedListAdapter
22 |
23 | override fun onCreate(savedInstanceState: Bundle?) {
24 | super.onCreate(savedInstanceState)
25 | setContentView(R.layout.activity_main)
26 | initRecycler()
27 | initObserver()
28 | }
29 |
30 | private fun initRecycler() {
31 | moviesAdapter = MoviesPagedListAdapter {
32 | viewModel.retry()
33 | }
34 |
35 | val gridLayoutManager = GridLayoutManager(this, 4, RecyclerView.VERTICAL, false)
36 |
37 | gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
38 | override fun getSpanSize(position: Int): Int {
39 | val viewType = moviesAdapter.getItemViewType(position)
40 | if (viewType == R.layout.movie_list_item) return 1
41 | else return 4
42 | }
43 | }
44 |
45 | movies_recycler.adapter = moviesAdapter
46 | movies_recycler.layoutManager = gridLayoutManager
47 | movies_recycler.setHasFixedSize(true)
48 | }
49 |
50 | private fun initObserver() {
51 | viewModel.moviePagedList.observe(this, Observer {
52 | moviesAdapter.submitList(it)
53 | })
54 |
55 | viewModel.getNetworkState()
56 | .observe(this, Observer {
57 | progress_bar_main.visibility =
58 | if (viewModel.listIsEmpty() && it == NetworkState.LOADING) View.VISIBLE else View.GONE
59 |
60 | txt_error_main.visibility =
61 | if (viewModel.listIsEmpty() && it.message != null) View.VISIBLE else View.GONE
62 |
63 | moviesAdapter.setNetworkState(it)
64 | })
65 | }
66 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spiraldev/mvvmpaging/ui/MainActivityViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.spiraldev.mvvmpaging.ui
2 |
3 | import androidx.hilt.lifecycle.ViewModelInject
4 | import androidx.lifecycle.LiveData
5 | import androidx.lifecycle.ViewModel
6 | import androidx.paging.LivePagedListBuilder
7 | import androidx.paging.PagedList
8 | import com.spiraldev.mvvmpaging.data.local.MovieEntity
9 | import com.spiraldev.mvvmpaging.data.local.MoviesDatabase
10 | import com.spiraldev.mvvmpaging.data.remote.ApiService
11 | import com.spiraldev.mvvmpaging.data.remote.NetworkState
12 | import com.spiraldev.mvvmpaging.data.remote.POST_PER_PAGE
13 | import com.spiraldev.mvvmpaging.data.remote.datasources.PagedListMovieBoundaryCallback
14 | import io.reactivex.disposables.CompositeDisposable
15 | import javax.inject.Inject
16 |
17 | class MainActivityViewModel @ViewModelInject constructor(
18 | private val apiService: ApiService,
19 | private val moviesDb: MoviesDatabase
20 | ) : ViewModel() {
21 |
22 | private val compositeDisposable = CompositeDisposable()
23 | var moviePagedList: LiveData>
24 |
25 | private val boundaryCallback: PagedListMovieBoundaryCallback =
26 | PagedListMovieBoundaryCallback(
27 | apiService,
28 | moviesDb,
29 | compositeDisposable
30 | )
31 |
32 | init {
33 | val config = PagedList.Config.Builder()
34 | .setPageSize(POST_PER_PAGE)
35 | .setEnablePlaceholders(false)
36 | .build()
37 |
38 | moviePagedList =
39 | LivePagedListBuilder(moviesDb.moviesDao().allMovies(), config)
40 | .setBoundaryCallback(boundaryCallback)
41 | .build()
42 | }
43 |
44 | fun listIsEmpty(): Boolean {
45 | return moviePagedList.value?.isEmpty() ?: true
46 | }
47 |
48 | fun retry() {
49 | boundaryCallback.retry()
50 | }
51 |
52 | fun getNetworkState(): LiveData {
53 | return boundaryCallback.networkState
54 | }
55 |
56 | override fun onCleared() {
57 | super.onCleared()
58 | compositeDisposable.dispose()
59 | }
60 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/poster_placeholder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/drawable/poster_placeholder.png
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
22 |
23 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/movie_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
19 |
20 |
27 |
28 |
29 |
30 |
40 |
41 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/network_state_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
21 |
22 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MVVMPaging
3 | Retry
4 | Connection Problem!
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/spiraldev/mvvmpaging/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.spiraldev.mvvmpaging
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | ext.versions = [
4 | 'compileSdk' : 29,
5 | 'minSdk' : 19,
6 | 'targetSdk' : 29,
7 | 'androidGradle' : '4.0.0',
8 | 'androidx' : '1.1.0',
9 | 'material' : '1.1.0',
10 | 'androidxAppcompat': '1.1.0',
11 | 'lifecycle' : '2.2.0',
12 | 'persistence' : '2.2.5',
13 | 'paging' : '2.1.2',
14 | 'retrofit' : '2.5.0',
15 | 'okhttp' : '3.12.0',
16 | 'glide' : '4.11.0',
17 | 'rxAndroid' : '2.1.1',
18 | 'rxJava' : '2.2.8',
19 | 'kotlin' : '1.3.72',
20 | 'daggerHilt' : '2.28-alpha',
21 | 'androidXHilt' : '1.0.0-alpha01'
22 | ]
23 |
24 | repositories {
25 | google()
26 | jcenter()
27 | }
28 | dependencies {
29 | classpath "com.android.tools.build:gradle:${versions.androidGradle}"
30 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
31 | classpath "com.google.dagger:hilt-android-gradle-plugin:${versions.daggerHilt}"
32 |
33 | // NOTE: Do not place your application dependencies here; they belong
34 | // in the individual module build.gradle files
35 | }
36 | }
37 |
38 | allprojects {
39 | repositories {
40 | google()
41 | jcenter()
42 | }
43 | }
44 |
45 | task clean(type: Delete) {
46 | delete rootProject.buildDir
47 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jun 29 16:18:43 UZT 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/offline_app_diagram.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/offline_app_diagram.png
--------------------------------------------------------------------------------
/online_app_diagram.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/online_app_diagram.png
--------------------------------------------------------------------------------
/screenshots/sc1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/screenshots/sc1.jpg
--------------------------------------------------------------------------------
/screenshots/sc2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpiralDevelopment/MVVMPaging/e1c9aa9586c7635ae1086a53f22e382336f3a6f0/screenshots/sc2.jpg
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name = "MVVMPaging"
--------------------------------------------------------------------------------