Skip to content

Instantly share code, notes, and snippets.

Avatar

Víctor Albertos VictorAlbertos

View GitHub Profile
View SampleFragment.kt
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)
View SampleAdapter.kt
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 {
View onViewEvent.kt
fun onViewEvent(sampleViewEvents: SampleViewEvents) {
val paingData = pagingDataViewStates.value ?: return
when (sampleViewEvents) {
is SampleViewEvents.Remove -> {
paingData
.filter { sampleViewEvents.sampleEntity.id != it.id }
.let { _pagingDataViewStates.value = it }
}
is SampleViewEvents.Edit -> {
View SampleViewModel.kt
class SampleViewModel : ViewModel() {
private val _pagingDataViewStates =
Pager(PagingConfig(pageSize = SampleRepository.PAGE_SIZE)) { SamplePagingSource(SampleRepository()) }.flow
.cachedIn(viewModelScope)
.asLiveData()
.let { it as MutableLiveData<PagingData<SampleEntity>> }
val pagingDataViewStates: LiveData<PagingData<SampleEntity>> = _pagingDataViewStates
}
View SamplePagingSource.kt
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,
View SampleRepository.kt
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
}
View SampleViewEvents.kt
sealed class SampleViewEvents {
data class Edit(val sampleEntity: SampleEntity) : SampleViewEvents()
data class Remove(val sampleEntity: SampleEntity) : SampleViewEvents()
object InsertItemHeader : SampleViewEvents()
object InsertItemFooter : SampleViewEvents()
}
View SampleEntity.kt
data class SampleEntity(val id: Int, val name: String)
View RxCache.java
//read from loader
api
.compose(rxCache.<User>.read("mock").toObservable())
.subscribe();
//read without loader
RxCache.<User>.read("mock")
.toObservable()
.subscribe();
View bug male always true in get data
{"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
You can’t perform that action at this time.