try out LiveData

This commit is contained in:
Felix 2017-09-25 03:49:25 +02:00
parent e363879708
commit cd7039906e
2 changed files with 37 additions and 9 deletions

View file

@ -1,5 +1,8 @@
package de.nicidienase.chaosflix.touch;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelStore;
@ -7,6 +10,7 @@ import android.arch.lifecycle.ViewModelStoreOwner;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.List;
@ -20,6 +24,8 @@ import de.nicidienase.chaosflix.common.network.RecordingService;
import de.nicidienase.chaosflix.common.network.StreamingService;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
@ -58,6 +64,18 @@ public class ChaosflixViewModel extends ViewModel {
mStreamingApi = retrofigStreaming.create(StreamingService.class);
}
public LiveData<ConferencesWrapper> getConferencesWrapperAsLiveData(){
return new LiveData<ConferencesWrapper>() {
@Override
protected void onActive() {
super.onActive();
mRecordingApi.getConferences()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conferencesWrapper -> setValue(conferencesWrapper));
}
};
}
public Observable<ConferencesWrapper> getConferencesWrapper() {
return mRecordingApi.getConferences()
.doOnError(throwable -> Log.d(TAG, String.valueOf(throwable.getCause())))

View file

@ -58,15 +58,25 @@ public class BrowseActivity extends AppCompatActivity implements
mViewModel = ViewModelProviders.of(this,factory).get(ChaosflixViewModel.class);
if(savedInstanceState == null){
mDisposables.add(mViewModel.getConferencesWrapper()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conferencesWrapper -> {
ConferencesTabBrowseFragment browseFragment
= ConferencesTabBrowseFragment.newInstance(getNumColumns());
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container,browseFragment);
ft.commit();
}));
ConferencesTabBrowseFragment browseFragment
= ConferencesTabBrowseFragment.newInstance(getNumColumns());
mViewModel.getConferencesWrapperAsLiveData().observe(browseFragment,conferencesWrapper -> {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container,browseFragment);
ft.setReorderingAllowed(true);
ft.commit();
});
// mDisposables.add(mViewModel.getConferencesWrapper()
// .observeOn(AndroidSchedulers.mainThread())
// .doOnError(throwable -> Log.d(TAG, String.valueOf(throwable.getCause())))
// .subscribe(conferencesWrapper -> {
// ConferencesTabBrowseFragment browseFragment
// = ConferencesTabBrowseFragment.newInstance(getNumColumns());
// FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// ft.replace(R.id.fragment_container,browseFragment);
// ft.setReorderingAllowed(true);
// ft.commit();
// }));
}
}