Skip to content

Instantly share code, notes, and snippets.

@VictorAlbertos
Last active October 4, 2021 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VictorAlbertos/0d4ef7571003f57a5c5cc691f1406f30 to your computer and use it in GitHub Desktop.
Save VictorAlbertos/0d4ef7571003f57a5c5cc691f1406f30 to your computer and use it in GitHub Desktop.
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 }
}
is SampleViewEvents.Edit -> {
paging
.map {
if (sampleViewEvents.sampleEntity.id == it.id) return@map it.copy(name = "${it.name} (updated)")
else return@map it
}
}
SampleViewEvents.InsertItemHeader -> {
paging.insertHeaderItem(
item = SampleEntity(
id = Random.nextInt(0, 1000),
name = "New item added at the top"
)
)
}
SampleViewEvents.InsertItemFooter -> {
paging.insertFooterItem(
item = SampleEntity(
id = Random.nextInt(0, 1000),
name = "New item added at the bottom"
)
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment