Guest User

Untitled

a guest
Jul 17th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. /**
  2. * Data class that is necessary for a UI to show a listing and interact w/ the rest of the system
  3. */
  4. data class Listing<T>(
  5. // the LiveData of paged lists for the UI to observe
  6. val pagedList: LiveData<PagedList<T>>,
  7. // represents the network request status to show to the user
  8. val networkState: LiveData<NetworkState>,
  9. // represents the refresh status to show to the user. Separate from networkState, this
  10. // value is importantly only when refresh is requested.
  11. val refreshState: LiveData<NetworkState>,
  12. // refreshes the whole data and fetches it from scratch.
  13. val refresh: () -> Unit,
  14. // retries any failed requests.
  15. val retry: () -> Unit)
  16.  
  17.  
  18. class DataSource(
  19. private val apiService: ApiService,
  20. private val retryExecutor: Executor,
  21. //You can pass all other extra params that you need here.
  22. ) : PageKeyedDataSource<Int, Deliverable>() {
  23. // keep a function reference for the retry event
  24. private var retry: (() -> Any)? = null
  25.  
  26. /**
  27. * There is no sync on the state because paging will always call loadInitial first then wait
  28. * for it to return some success value before calling loadAfter.
  29. */
  30. val networkState = MutableLiveData<NetworkState>()
  31.  
  32. val initialLoad = SingleLiveEvent<NetworkState>()
  33.  
  34. fun retryAllFailed() {
  35. val prevRetry = retry
  36. retry = null
  37. prevRetry?.let {
  38. retryExecutor.execute {
  39. it.invoke()
  40. }
  41. }
  42. }
  43.  
  44.  
  45. override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Deliverable>) {
  46. Timber.d("Loading initial value")
  47.  
  48. }
  49.  
  50. override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Deliverable>) {
  51. Timber.d("Load after called. Offset: ${params.key}, Page size: ${params.requestedLoadSize}")
  52.  
  53. }
  54.  
  55. override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Deliverable>) {
  56. Timber.d("Load before called")
  57. //This is never used
  58. }
  59.  
  60. }
  61.  
  62. class DataSourceFactory(
  63. private val apiservice: ApiService,
  64. private val retryExecutor: Executor,
  65. //You can pass all other extra params that you need here.
  66. ) : DataSource.Factory<Int, Deliverable>() {
  67. val sourceLiveData = MutableLiveData<DataSource>()
  68. override fun create(): DataSource<Int, Deliverable> {
  69. val source = DataSource(//Pass all required params here.)
  70. sourceLiveData.postValue(source)
  71. return source
  72. }
  73.  
  74. }
  75.  
  76.  
  77. //Then on the repository or any other place you are creating your data source
  78.  
  79. fun history(//Pass params here): Listing<Deliverable> {
  80. val sourceFactory = DataSourceFactory(//Pass params)
  81.  
  82. val config = PagedList.Config.Builder()
  83. .setPageSize(10)
  84. .build()
  85. val livePageList = LivePagedListBuilder(sourceFactory, config)
  86. .build()
  87.  
  88. val refreshState = Transformations.switchMap(sourceFactory.sourceLiveData) {
  89. it.initialLoad
  90. }
  91.  
  92. return Listing(
  93. pagedList = livePageList,
  94. networkState = Transformations.switchMap(sourceFactory.sourceLiveData) {
  95. it.networkState
  96. },
  97. retry = { sourceFactory.sourceLiveData.value?.retryAllFailed() },
  98. refresh = { sourceFactory.sourceLiveData.value?.invalidate() },
  99. refreshState = refreshState
  100. )
  101. }
  102.  
  103. //The in viewModel, observe network state and trigger retries as follows
  104. private val repoResult = //create repo list from rrepository
  105.  
  106. private val transports = Transformations.switchMap(repoResult) { it.pagedList }!!
  107. private val networkState = Transformations.switchMap(repoResult) { it.networkState }!!
  108. private val refreshState = Transformations.switchMap(repoResult) { it.refreshState }!!
  109.  
  110.  
  111. //call refresh and retry as follows
  112. fun refresh() {
  113. repoResult.value?.refresh?.invoke()
  114. }
  115.  
  116.  
  117. fun retry() {
  118. val listing = repoResult?.value
  119. listing?.retry?.invoke()
  120. }
Add Comment
Please, Sign In to add comment