1 /*
2  * Copyright 2019 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 androidx.paging
18 
19 /**
20  * LoadState of a PagedList load - associated with a [LoadType]
21  *
22  * [LoadState] of any [LoadType] may be observed for UI purposes by registering a listener via
23  * [androidx.paging.PagingDataAdapter.addLoadStateListener] or
24  * [androidx.paging.AsyncPagingDataDiffer.addLoadStateListener]
25  *
26  * @param endOfPaginationReached `false` if there is more data to load in the [LoadType] this
27  *   [LoadState] is associated with, `true` otherwise. This parameter informs [Pager] if it should
28  *   continue to make requests for additional data in this direction or if it should halt as the end
29  *   of the dataset has been reached.
30  *
31  * Note: The [LoadType] [REFRESH][LoadType.REFRESH] always has [LoadState.endOfPaginationReached]
32  * set to `false`.
33  *
34  * @see LoadType
35  */
36 public sealed class LoadState(public val endOfPaginationReached: Boolean) {
37     /**
38      * Indicates the [PagingData] is not currently loading, and no error currently observed.
39      *
40      * @param endOfPaginationReached `false` if there is more data to load in the [LoadType] this
41      *   [LoadState] is associated with, `true` otherwise. This parameter informs [Pager] if it
42      *   should continue to make requests for additional data in this direction or if it should halt
43      *   as the end of the dataset has been reached.
44      */
45     public class NotLoading(endOfPaginationReached: Boolean) : LoadState(endOfPaginationReached) {
toStringnull46         override fun toString(): String {
47             return "NotLoading(endOfPaginationReached=$endOfPaginationReached)"
48         }
49 
equalsnull50         override fun equals(other: Any?): Boolean {
51             return other is NotLoading && endOfPaginationReached == other.endOfPaginationReached
52         }
53 
hashCodenull54         override fun hashCode(): Int {
55             return endOfPaginationReached.hashCode()
56         }
57 
58         internal companion object {
59             internal val Complete = NotLoading(endOfPaginationReached = true)
60             internal val Incomplete = NotLoading(endOfPaginationReached = false)
61         }
62     }
63 
64     /** Loading is in progress. */
65     public object Loading : LoadState(false) {
toStringnull66         override fun toString(): String {
67             return "Loading(endOfPaginationReached=$endOfPaginationReached)"
68         }
69 
equalsnull70         override fun equals(other: Any?): Boolean {
71             return other is Loading && endOfPaginationReached == other.endOfPaginationReached
72         }
73 
hashCodenull74         override fun hashCode(): Int {
75             return endOfPaginationReached.hashCode()
76         }
77     }
78 
79     /**
80      * Loading hit an error.
81      *
82      * @param error [Throwable] that caused the load operation to generate this error state.
83      * @see androidx.paging.PagedList#retry
84      */
85     public class Error(public val error: Throwable) : LoadState(false) {
equalsnull86         override fun equals(other: Any?): Boolean {
87             return other is Error &&
88                 endOfPaginationReached == other.endOfPaginationReached &&
89                 error == other.error
90         }
91 
hashCodenull92         override fun hashCode(): Int {
93             return endOfPaginationReached.hashCode() + error.hashCode()
94         }
95 
toStringnull96         override fun toString(): String {
97             return "Error(endOfPaginationReached=$endOfPaginationReached, error=$error)"
98         }
99     }
100 }
101