This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pepepepepe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleFragment : Fragment(R.layout.sample_fragment) { | |
private val viewModel: SampleViewModel by viewModels() | |
private val sampleAdapter by lazy { SampleAdapter(viewModel) } | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val rvItems = view.findViewById<RecyclerView>(R.id.rvItems) | |
rvItems.layoutManager = LinearLayoutManager(context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleAdapter(private val sampleViewModel: SampleViewModel) : | |
PagingDataAdapter<SampleEntity, SampleAdapter.ViewHolder>(Comparator) { | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
val sampleEntity = getItem(position) ?: return | |
holder.itemView.findViewById<TextView>(R.id.tvItem).text = sampleEntity.name | |
holder.itemView.findViewById<View>(R.id.ivRemove).setOnClickListener { | |
sampleViewModel.onViewEvent(SampleViewEvents.Remove(sampleEntity)) | |
} | |
holder.itemView.findViewById<View>(R.id.ivEdit).setOnClickListener { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun onViewEvent(sampleViewEvents: SampleViewEvents) { | |
modificationEvents.value += sampleViewEvents | |
} | |
private fun applyEvents(paging: PagingData<SampleEntity>, sampleViewEvents: SampleViewEvents): PagingData<SampleEntity> { | |
return when (sampleViewEvents) { | |
is SampleViewEvents.Remove -> { | |
paging | |
.filter { sampleViewEvents.sampleEntity.id != it.id } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleViewModel : ViewModel() { | |
private val modificationEvents = MutableStateFlow<List<SampleViewEvents>>(emptyList()) | |
// combine them with the data coming from paging | |
private val combined = | |
Pager(PagingConfig(pageSize = SampleRepository.PAGE_SIZE)) { SamplePagingSource(SampleRepository()) }.flow | |
.cachedIn(viewModelScope) | |
.combine(modificationEvents) { pagingData, modifications -> | |
modifications.fold(pagingData) { acc, event -> | |
applyEvents(acc, event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SamplePagingSource( | |
private val sampleRepository: SampleRepository | |
) : PagingSource<Int, SampleEntity>() { | |
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, SampleEntity> { | |
return try { | |
val data = sampleRepository.getNextPage(lastSeenId = params.key ?: 1) | |
.map { SampleEntity(it, "Page number: $it") } | |
LoadResult.Page( | |
data = data, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleRepository { | |
suspend fun getNextPage(lastSeenId: Int): List<Int> { | |
return withContext(Dispatchers.IO) { | |
(lastSeenId..lastSeenId + PAGE_SIZE).map { it } | |
} | |
} | |
companion object { | |
const val PAGE_SIZE = 20 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class SampleViewEvents { | |
data class Edit(val sampleEntity: SampleEntity) : SampleViewEvents() | |
data class Remove(val sampleEntity: SampleEntity) : SampleViewEvents() | |
object InsertItemHeader : SampleViewEvents() | |
object InsertItemFooter : SampleViewEvents() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class SampleEntity(val id: Int, val name: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//read from loader | |
api | |
.compose(rxCache.<User>.read("mock").toObservable()) | |
.subscribe(); | |
//read without loader | |
RxCache.<User>.read("mock") | |
.toObservable() | |
.subscribe(); | |
NewerOlder