Share
Embed
Transcript
62 slides
-
@jossiwolf ”LiveData is an observable data holder class” - Android
Developers Documentation -
@jossiwolf class HomeViewModel { val latestData: LiveData<String> = MutableLiveData() }
LiveData - an example -
@jossiwolf class HomeViewModel { val latestData: LiveData<String> = MutableLiveData() fun
refresh() { val data = fetchData() latestData.post(data) } } LiveData - an example -
@jossiwolf class HomeFragment: Fragment() { fun setupUI() { latestData.observe(viewLifecycleOwner) {
data #-> ##... } } } LiveData - an example -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map -
@jossiwolf ”The events are dispatched on the main thread.” -
Android Developers Documentation -
@jossiwolf fun fetchOpeningHours(id: SupermarketId): LiveData<OpenHoursInformation> { val liveData = MutableLiveData<OpenHoursInformation>()
val openingHours = poiService.downloadOpeningHours(poiType = SUPERMARKET, id).enqueue( object: Callback<OpenHoursInformation>() { override fun onResponse(call: Call<OpenHoursInformation>, response: Response<OpenHoursInformation>) { liveData.postValue(response.body()) } ##... } ) liveData.postValue(openingHours) return liveData } Repository with LiveData + Callbacks -
@jossiwolf suspend fun fetchOpeningHours(id: SupermarketId): LiveData<OpenHoursInformation> { val liveData =
MutableLiveData<OpenHoursInformation>() val openingHours = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) liveData.postValue(openingHours) return liveData } Repository with LiveData + Coroutines -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData(context = Dispatchers.IO) { emit(fetchOpeningHours(id)) } -
@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ):
ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf Repository with Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) =
poiService.downloadOpeningHours(poiType = SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData(context = Dispatchers.IO) { emit(fetchOpeningHours(id)) } -
@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ):
ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ):
ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = liveData(context = Dispatchers.IO) { val openingHours = repo.fetchOpeningHours(id) val filteredHours = openingHours.hours.filter { it #!= null } emit(filteredHours) } } -
@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData<SupermarketDetails> { val
liveData = MutableLiveData<OpenHoursInformation>() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData -
@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData<SupermarketDetails> { val
liveData = MutableLiveData<OpenHoursInformation>() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData -
@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData<SupermarketDetails> { val
liveData = MutableLiveData<OpenHoursInformation>() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData() -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO) -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO) -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO)