save and restore playback progress

This commit is contained in:
Felix 2017-09-26 17:03:02 +02:00
parent 1f1e2a9817
commit 91319a83fb
2 changed files with 24 additions and 1 deletions

View file

@ -7,6 +7,7 @@ import android.util.Log;
import java.util.List;
import de.nicidienase.chaosflix.common.entities.PlaybackProgress;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
import de.nicidienase.chaosflix.common.entities.recording.ConferencesWrapper;
import de.nicidienase.chaosflix.common.entities.recording.Event;
@ -98,6 +99,22 @@ public class ChaosflixViewModel extends ViewModel {
.subscribeOn(Schedulers.io());
}
public void setPlaybackProgress(int apiId, long progress){
new PlaybackProgress(apiId,progress,0).save();
}
public Observable<Long> getPlaybackProgress(int apiID) {
return Observable.fromCallable(() -> {
List<PlaybackProgress> progresses
= PlaybackProgress.find(PlaybackProgress.class, "api_id = ?", Integer.toString(apiID));
if(progresses.size() > 0){
return progresses.get(0).getProgress();
} else {
return 0l;
}
});
}
public static class Factory extends ViewModelProvider.NewInstanceFactory{
private final String recordingUrl;

View file

@ -50,8 +50,10 @@ import butterknife.ButterKnife;
import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Event;
import de.nicidienase.chaosflix.common.entities.recording.Recording;
import de.nicidienase.chaosflix.touch.ChaosflixViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
public class ExoPlayerFragment extends Fragment {
public class ExoPlayerFragment extends ChaosflixFragment {
private static final String TAG = ExoPlayerFragment.class.getSimpleName();
public static final String PLAYBACK_STATE = "playback_state";
private static final String ARG_EVENT = "event";
@ -133,6 +135,9 @@ public class ExoPlayerFragment extends Fragment {
getView().setSystemUiVisibility(View.INVISIBLE);
if(mPlayer != null){
mPlayer.setPlayWhenReady(mPlaybackState);
getViewModel().getPlaybackProgress(mEvent.getApiID())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(aLong -> mPlayer.seekTo(aLong));
}
}
@ -141,6 +146,7 @@ public class ExoPlayerFragment extends Fragment {
super.onPause();
getView().setSystemUiVisibility(View.VISIBLE);
if(mPlayer != null){
getViewModel().setPlaybackProgress(mEvent.getApiID(),mPlayer.getCurrentPosition());
mPlayer.setPlayWhenReady(false);
}
}