@@ -24,6 +24,22 @@ interface MainContract {
2424 ?.state is PlaceholderState .Error
2525 }
2626
27+ fun canLoadNextPageHorizontal (): Boolean {
28+ val horizontalList =
29+ items.singleOrNull { it is Item .HorizontalList } as ? Item .HorizontalList ? : return false
30+ return ! horizontalList.isLoading &&
31+ horizontalList.error == null &&
32+ horizontalList.postItems.isNotEmpty() &&
33+ (horizontalList.items.singleOrNull { it is HorizontalItem .Placeholder } as ? HorizontalItem .Placeholder )
34+ ?.state == PlaceholderState .Idle
35+ }
36+
37+ fun getHorizontalListCount (): Int {
38+ val horizontalList =
39+ items.singleOrNull { it is Item .HorizontalList } as ? Item .HorizontalList ? : return 0
40+ return horizontalList.postItems.size
41+ }
42+
2743 companion object Factory {
2844 @JvmStatic
2945 fun initial () = ViewState (
@@ -110,8 +126,8 @@ interface MainContract {
110126 object LoadNextPage : ViewIntent()
111127 object RetryLoadPage : ViewIntent()
112128
113- object LoadNextPageHorizontal: ViewIntent()
114- object RetryLoadPageHorizontal: ViewIntent()
129+ object LoadNextPageHorizontal : ViewIntent()
130+ object RetryLoadPageHorizontal : ViewIntent()
115131 }
116132
117133 sealed class PartialStateChange {
@@ -239,6 +255,63 @@ interface MainContract {
239255 }
240256 }
241257 }
258+
259+ sealed class PostNextPage : PartialStateChange () {
260+ data class Data (val posts : List <PostVS >) : PostNextPage()
261+ data class Error (val error : Throwable ) : PostNextPage()
262+ object Loading : PostNextPage()
263+
264+ override fun reduce (vs : ViewState ): ViewState {
265+ return when (this ) {
266+ is Data -> {
267+ vs.copy(
268+ items = vs.items.map { item ->
269+ if (item is Item .HorizontalList ) {
270+ val postItems = item.items.filterIsInstance<HorizontalItem .Post >() +
271+ this .posts.map { HorizontalItem .Post (it) }
272+ item.copy(
273+ items = postItems + HorizontalItem .Placeholder (PlaceholderState .Idle ),
274+ postItems = postItems
275+ )
276+ } else {
277+ item
278+ }
279+ }
280+ )
281+ }
282+ is Error -> {
283+ vs.copy(
284+ items = vs.items.map { item ->
285+ if (item is Item .HorizontalList ) {
286+ val postItems = item.items.filterIsInstance<HorizontalItem .Post >()
287+ item.copy(
288+ items = postItems + HorizontalItem .Placeholder (PlaceholderState .Error (error)),
289+ postItems = postItems
290+ )
291+ } else {
292+ item
293+ }
294+ }
295+ )
296+ }
297+ Loading -> {
298+ vs.copy(
299+ items = vs.items.map { item ->
300+ if (item is Item .HorizontalList ) {
301+ val postItems = item.items.filterIsInstance<HorizontalItem .Post >()
302+ item.copy(
303+ items = postItems + HorizontalItem .Placeholder (PlaceholderState .Loading ),
304+ postItems = postItems
305+ )
306+ } else {
307+ item
308+ }
309+ }
310+ )
311+ }
312+ }
313+ }
314+ }
242315 }
243316
244317 sealed class SingleEvent {
@@ -250,5 +323,6 @@ interface MainContract {
250323 fun photoFirstPageChanges (limit : Int ): Observable <PartialStateChange .PhotoFirstPage >
251324
252325 fun postFirstPageChanges (limit : Int ): Observable <PartialStateChange .PostFirstPage >
326+ fun postNextPageChanges (start : Int , limit : Int ): Observable <PartialStateChange .PostNextPage >
253327 }
254328}
0 commit comments