private val _cat = MutableLiveData>()
fun updateCat(new: Cat) {
val currentCats = _cat.value ?: return
val updatedCats = currentCats.toMutableList()
.map { old -> if(old.id == new.id) new else old }
_cat.postValue(updatedCats)
// update storage and web service
...
}
Slide 70
Slide 70 text
private val _cat = MutableLiveData>()
fun updateCat(new: Cat) {
val currentCats = _cat.value ?: return
val updatedCats = currentCats.toMutableList()
.map { old -> if(old.id == new.id) new else old }
_cat.postValue(updatedCats)
// update storage and web service
...
}
Slide 71
Slide 71 text
private val _cat = MutableLiveData>()
fun updateCat(new: Cat) {
val currentCats = _cat.value ?: return
val updatedCats = currentCats.toMutableList()
.map { old -> if(old.id == new.id) new else old }
_cat.postValue(updatedCats)
// update storage and web service
...
}
Slide 72
Slide 72 text
@Entity
data class Cat(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
val name: String,
val age: Int,
val description: String,
val loved: Boolean
)
Slide 73
Slide 73 text
@Dao
interface CatDao {
@Query("SELECT * from Cat")
fun loadCats() : LiveData>
@Update
fun update(cat: Cat)
}
Slide 74
Slide 74 text
@Database(entities = [Cat::class], version = 1)
abstract class CatDatabase : RoomDatabase() {
abstract fun catDao() : CatDao
}
Slide 75
Slide 75 text
fun getCatDatabase(context: Context) =
Room.databaseBuilder(context, CatDatabase::class.java, "catDB").build()
Slide 76
Slide 76 text
fun updateCat(new: Cat) {
// update logic here
catDao.updateCat(new)
catService.updateData(new)
}
Cat Database
Cat table
room_master_table
Id, identity_hash
room_table_modification_log
InvalidationTracker
Version, table_id
Observer
ComputeLiveDat
a
addObserver
query
Cat Dao
Slide 80
Slide 80 text
Cat Database
Cat table
room_master_table
Id, identity_hash
room_table_modification_log
InvalidationTracker
Version, table_id
Observer
Cat Dao
ComputeLiveDat
a
addObserver
query
onInvalidated
Slide 81
Slide 81 text
Why use Paging library?
Slide 82
Slide 82 text
Repository
ViewModel
Activity /
Fragment
What do
I display?
I need cats
Give us
ALL Cats
OK,
1,000,000
cats here.
Here you are
Show this
1,000,000
cats
¯\_(π)_/¯
public abstract class PagedListAdapter ... {
final AsyncPagedListDiffer mDiffer;
@Nullable protected T getItem(int position) {
return mDiffer.getItem(position);
}
}
Slide 93
Slide 93 text
public class AsyncPagedListDiffer {
...
public T getItem(int index) {
...
mPagedList.loadAround(index);
return mPagedList.get(index);
}
}
Slide 94
Slide 94 text
public abstract class PagedList extends AbstractList {
...
public void loadAround(int index) {
...
loadAroundInternal(index);
...
}
}
Slide 95
Slide 95 text
class ContiguousPagedList extends PagedList ... {
protected void loadAroundInternal(int index) {
int appendItems = getAppendItemsRequested(...);
...
mAppendItemsRequested = Math.max(...);
if (mAppendItemsRequested > 0) {
scheduleAppend();
}
}