Commit
I use Ctags+vim in addition to android studio so I updated gitignore to ignore tags files.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,6 @@ captures/ | |
|
||
# IntelliJ | ||
*.iml | ||
|
||
# Ctags | ||
**/tags |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,51 +17,62 @@ | |
package com.google.android.gnd.ui.recorddetails; | ||
|
||
import android.arch.lifecycle.LiveData; | ||
import android.arch.lifecycle.MutableLiveData; | ||
import android.databinding.ObservableInt; | ||
import android.arch.lifecycle.LiveDataReactiveStreams; | ||
import android.view.View; | ||
|
||
import com.google.android.gnd.repository.DataRepository; | ||
import com.google.android.gnd.repository.Resource; | ||
import com.google.android.gnd.ui.common.AbstractViewModel; | ||
import com.google.android.gnd.vo.Record; | ||
|
||
import io.reactivex.BackpressureStrategy; | ||
import io.reactivex.Observable; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class RecordDetailsViewModel extends AbstractViewModel { | ||
|
||
private final DataRepository dataRepository; | ||
private final MutableLiveData<Resource<Record>> record; | ||
public final ObservableInt progressBarVisibility = new ObservableInt(); | ||
private LiveData<Resource<Record>> record; | ||
private LiveData<Integer> progressBarVisibility; | ||
|
||
@Inject | ||
RecordDetailsViewModel(DataRepository dataRepository) { | ||
this.dataRepository = dataRepository; | ||
this.record = new MutableLiveData<>(); | ||
} | ||
|
||
public LiveData<Resource<Record>> getRecord() { | ||
return record; | ||
} | ||
|
||
public LiveData<Integer> getProgressBarVisibility() { | ||
return progressBarVisibility; | ||
} | ||
|
||
public void loadRecordDetails(String projectId, String featureId, String recordId) { | ||
disposeOnClear( | ||
dataRepository | ||
.getRecordDetails(projectId, featureId, recordId) | ||
.subscribe(this::onRecordUpdate)); | ||
Observable<Resource<Record>> recordDetials = | ||
dataRepository.getRecordDetails(projectId, featureId, recordId); | ||
|
||
progressBarVisibility = | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
scolsen
Author
Contributor
|
||
LiveDataReactiveStreams.fromPublisher( | ||
recordDetials | ||
.map(this::toProgressBarVisibility) | ||
.onErrorReturn(err -> View.GONE) | ||
.toFlowable(BackpressureStrategy.LATEST)); | ||
|
||
record = | ||
LiveDataReactiveStreams.fromPublisher( | ||
recordDetials | ||
.map(r -> r) | ||
.onErrorReturn(err -> Resource.error(err)) | ||
.toFlowable(BackpressureStrategy.LATEST)); | ||
} | ||
|
||
private void onRecordUpdate(Resource<Record> r) { | ||
switch (r.operationState().get()) { | ||
case LOADING: | ||
progressBarVisibility.set(View.VISIBLE); | ||
break; | ||
case LOADED: | ||
progressBarVisibility.set(View.GONE); | ||
break; | ||
case NOT_FOUND: | ||
case ERROR: | ||
break; | ||
private Integer toProgressBarVisibility(Resource<Record> r) { | ||
if(r.operationState().get() == Resource.Status.LOADING) { | ||
This comment has been minimized.
Sorry, something went wrong.
gino-m
Collaborator
|
||
return View.VISIBLE; | ||
} else { | ||
return View.GONE; | ||
} | ||
record.setValue(r); | ||
} | ||
} |
1 comment
on commit e678555
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few thoughts, and more questions. Thanks for your patience!
Deleted all my comments because I just realized we can't just replace the LiveDatas mid-lifecycle; previous LiveDatas will continue to live since there will already be observers, and the view will not know to look at the new LiveData.
For hot Observables, we would normally initialize these in the constructor/lifecycle method. But since these are cold (started and finished by the user action), that won't work unless we create something akin to a hot Observable "LoadRecordDetailsRequest" that would initiate getRecordDetails on emissions, switchMapping the result. While this feels a bit contrived, it might make sense if the UI clicks were treated as a hot Observable.