Skip to content

Instantly share code, notes, and snippets.

View VictorAlbertos's full-sized avatar

Víctor Albertos VictorAlbertos

View GitHub Profile
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)
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 {
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 }
}
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)
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,
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
}
sealed class SampleViewEvents {
data class Edit(val sampleEntity: SampleEntity) : SampleViewEvents()
data class Remove(val sampleEntity: SampleEntity) : SampleViewEvents()
object InsertItemHeader : SampleViewEvents()
object InsertItemFooter : SampleViewEvents()
}
data class SampleEntity(val id: Int, val name: String)
//read from loader
api
.compose(rxCache.<User>.read("mock").toObservable())
.subscribe();
//read without loader
RxCache.<User>.read("mock")
.toObservable()
.subscribe();
{"idShop":1,"serverVersionCode":"1","hour":"00:00 - 00:30","orderNow":false,"action":"get_data","token":"sushigo2015!","lang":"en","day":"2015-12-05","customer":{"addresses":[{"address":"addres buena","addressName":"addresbue","associatedEstablishment":{"address":"Calle Joan Crespí, 53","availableDays":[{"availableHours":["00:00 - 00:30","00:30 - 01:00","01:00 - 01:30","01:30 - 02:00"],"day":"Saturday - 05","rawDay":"2015-12-05"},{"availableHours":["00:30 - 02:02","01:00 - 01:30","01:05 - 05:30","01:30 - 02:00","07:00 - 07:05","07:05 - 07:10","07:10 - 07:15","07:15 - 07:20","07:20 - 07:25","07:25 - 07:30","07:30 - 07:35","07:35 - 07:40","07:40 - 07:45","07:45 - 07:50","07:50 - 07:55","07:55 - 08:00","08:00 - 08:05","08:05 - 08:10","08:10 - 08:15","08:15 - 08:20","08:20 - 08:25","08:25 - 08:30","08:30 - 08:35","08:35 - 08:40","08:40 - 08:45","08:45 - 08:50","08:50 - 08:55","08:55 - 09:00"],"day":"Monday - 07","rawDay":"2015-12-07"},{"availableHours":["00:00 - 01:00","01:00 - 02:00"],"day":"Wednesday - 09","raw