paging3 InsertSeparators()

나고수·2022년 5월 8일
0

1일1공부

목록 보기
40/67

paging은 adpater에 pagingData가 바로 submitData되기 때문에, adapter에서 viewType에 따라 view를 따로 바인딩해줄 수 없다.
submitData 하기 전에 insertSeparators 함수를 이용해 pagingData를 조작?하면 된다.

   private fun searchRepo(queryString: String): Flow<PagingData<UiModel>> =
        repository.getSearchResultStream(queryString)
            .map { pagingData -> pagingData.map { UiModel.RepoItem(it) } }
            .map {
                it.insertSeparators { before, after ->
                //맨 첫번째 데이터부터 순서대로 before, after로 들어온다.
                //ex) listOf(0,1,2,3,4,5) 이면
                //null,0
                //0,1
                //1,2
                //2,3
                //3,4
                //4,5
                //5,null
                    if (after == null) {
                        // we're at the end of the list 👈
                        return@insertSeparators null
                    }

                    if (before == null) {
                        // we're at the beginning of the list 👈
                        return@insertSeparators UiModel.SeparatorItem("${after.roundedStarCount}0.000+ stars")
                    }
                    // check between 2 items 👈
                    if (before.roundedStarCount > after.roundedStarCount) {
                        if (after.roundedStarCount >= 1) {
                            UiModel.SeparatorItem("${after.roundedStarCount}0.000+ stars")
                        } else {
                            UiModel.SeparatorItem("< 10.000+ stars")
                        }
                    } else {
                        // no separator
                        null
                    }
                }
            }

코드랩

profile
되고싶다

0개의 댓글