update viewmodel

This commit is contained in:
Felix 2017-10-24 21:40:23 +02:00
parent 56f3706b46
commit dca80e2fc5
4 changed files with 169 additions and 5 deletions

View file

@ -59,14 +59,15 @@ dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile "com.android.support:design:${rootProject.ext.supportLibraryVersion}"
implementation "android.arch.lifecycle:runtime:1.0.0"
implementation "android.arch.lifecycle:runtime:1.0.3"
implementation "android.arch.lifecycle:extensions:${rootProject.ext.archCompVersion}"
implementation "android.arch.lifecycle:common-java8:1.0.0-beta1"
implementation "android.arch.lifecycle:common-java8:1.0.0-rc1"
implementation "android.arch.persistence.room:runtime:${rootProject.ext.archCompVersion}"
implementation "android.arch.persistence.room:rxjava2:${rootProject.ext.archCompVersion}"
kapt 'com.android.databinding:compiler:3.0.0-beta7'
kapt 'com.android.databinding:compiler:3.0.0-rc2'
kapt "android.arch.lifecycle:compiler:${rootProject.ext.archCompVersion}"
kapt "android.arch.persistence.room:compiler:${rootProject.ext.archCompVersion}"
compile "org.jetbrains.kotlin:kotlin-reflect:1.1.50"
compile 'com.google.android.exoplayer:exoplayer:r2.5.2'
compile 'com.squareup.picasso:picasso:2.5.2'

View file

@ -0,0 +1,63 @@
package de.nicidienase.chaosflix.touch
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.persistence.room.Room
import de.nicidienase.chaosflix.ChaosflixApplication
import de.nicidienase.chaosflix.R
import de.nicidienase.chaosflix.common.entities.ChaosflixDatabase
import de.nicidienase.chaosflix.common.network.RecordingService
import de.nicidienase.chaosflix.common.network.StreamingService
import de.nicidienase.chaosflix.touch.BrowseViewModel
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
object ViewModelFactory: ViewModelProvider.Factory{
val database: ChaosflixDatabase
val recordingApi: RecordingService
val streamingApi: StreamingService
init {
val res = ChaosflixApplication.getContext().getResources()
val recordingUrl = res.getString(R.string.api_media_ccc_url)
val streamingUrl = res.getString(R.string.streaming_media_ccc_url)
val client = OkHttpClient()
val gsonConverterFactory = GsonConverterFactory.create()
val rxJava2CallAdapterFactory = RxJava2CallAdapterFactory.create()
val retrofitRecordings = Retrofit.Builder()
.baseUrl(recordingUrl)
.client(client)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build()
recordingApi = retrofitRecordings.create(RecordingService::class.java)
val retrofigStreaming = Retrofit.Builder()
.baseUrl(streamingUrl)
.client(client)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build()
streamingApi = retrofigStreaming.create(StreamingService::class.java)
database = Room.databaseBuilder(ChaosflixApplication.getContext(),
ChaosflixDatabase::class.java,"mediaccc.de").build()
}
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if(modelClass.isAssignableFrom(BrowseViewModel::class.java)){
return BrowseViewModel(database, recordingApi, streamingApi) as T
} else {
throw UnsupportedOperationException("The requested ViewModel is currently unsupported. " +
"Please make sure to implement are correct creation of it. " +
" Request: ${modelClass.getCanonicalName()}");
}
}
}

View file

@ -0,0 +1,98 @@
package de.nicidienase.chaosflix.touch
import android.arch.lifecycle.ViewModel
import android.util.Log
import de.nicidienase.chaosflix.common.entities.ChaosflixDatabase
import de.nicidienase.chaosflix.common.entities.PlaybackProgress
import de.nicidienase.chaosflix.common.entities.WatchlistItem
import de.nicidienase.chaosflix.common.entities.recording.Conference
import de.nicidienase.chaosflix.common.entities.recording.ConferencesWrapper
import de.nicidienase.chaosflix.common.entities.recording.Event
import de.nicidienase.chaosflix.common.entities.recording.Recording
import de.nicidienase.chaosflix.common.entities.streaming.LiveConference
import de.nicidienase.chaosflix.common.network.RecordingService
import de.nicidienase.chaosflix.common.network.StreamingService
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
/**
* Created by felix on 12.10.17.
*/
class BrowseViewModel(
val database: ChaosflixDatabase,
val recordingApi: RecordingService,
val streamingApi: StreamingService
) : ViewModel(){
private val TAG = BrowseViewModel::class.simpleName
fun getConferencesWrapper(): Observable<ConferencesWrapper> {
return recordingApi.getConferences()
.doOnError({ throwable -> Log.d(TAG, throwable.cause.toString()) })
.subscribeOn(Schedulers.io())
}
fun getConferencesByGroup(group: String): Observable<List<Conference>> {
return recordingApi.conferences!!.map { conf -> conf?.conferencesBySeries.get(group) }
}
fun getConference(mConferenceId: Int): Observable<Conference> {
return recordingApi.getConference(mConferenceId.toLong())
.subscribeOn(Schedulers.io())
}
fun getEvent(apiID: Int): Observable<Event> {
return recordingApi.getEvent(apiID.toLong())
.subscribeOn(Schedulers.io())
}
fun getRecording(id: Long): Observable<Recording> {
return recordingApi.getRecording(id)
.subscribeOn(Schedulers.io())
}
fun getStreamingConferences(): Observable<List<LiveConference>> {
return streamingApi.getStreamingConferences()
.subscribeOn(Schedulers.io())
}
fun setPlaybackProgress(apiId: Int, progress: Long) {
database.playbackProgressDao().getProgressForEvent(apiId)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe { playbackProgress: PlaybackProgress? ->
if(playbackProgress != null){
playbackProgress.progress = progress
database.playbackProgressDao().saveProgress(playbackProgress)
} else {
database.playbackProgressDao().saveProgress(
PlaybackProgress(apiId,progress))
}
}
}
fun getPlaybackProgress(apiID: Int): Flowable<PlaybackProgress> {
return database.playbackProgressDao().getProgressForEvent(apiID)
}
fun createBookmark(apiId: Int) {
database.watchlistItemDao().getItemForEvent(apiId)
.subscribe { watchlistItem: WatchlistItem? ->
if (watchlistItem == null) {
database.watchlistItemDao()
.saveItem(WatchlistItem(apiId, apiId))
}
}
}
fun getBookmark(apiId: Int): Flowable<WatchlistItem> {
return database.watchlistItemDao().getItemForEvent(apiId)
}
fun removeBookmark(apiID: Int) {
getBookmark(apiID).subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe { watchlistItem -> database.watchlistItemDao().deleteItem(watchlistItem) }
}
}

View file

@ -23,7 +23,9 @@ import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
import de.nicidienase.chaosflix.common.entities.recording.Event;
import de.nicidienase.chaosflix.common.entities.recording.Recording;
import de.nicidienase.chaosflix.touch.BrowseViewModel;
import de.nicidienase.chaosflix.touch.ChaosflixViewModel;
import de.nicidienase.chaosflix.touch.ViewModelFactory;
import de.nicidienase.chaosflix.touch.fragments.ConferencesTabBrowseFragment;
import de.nicidienase.chaosflix.touch.fragments.EventDetailsFragment;
import de.nicidienase.chaosflix.touch.fragments.EventsFragment;
@ -43,14 +45,14 @@ public class BrowseActivity extends AppCompatActivity implements
private static final String TAG = BrowseActivity.class.getSimpleName();
CompositeDisposable mDisposables = new CompositeDisposable();
private ChaosflixViewModel mViewModel;
private BrowseViewModel mViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container_layout);
mViewModel = ViewModelProviders.of(this).get(ChaosflixViewModel.class);
mViewModel = ViewModelProviders.of(this, ViewModelFactory.INSTANCE).get(BrowseViewModel.class);
if(savedInstanceState == null){
ConferencesTabBrowseFragment browseFragment