diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/BrowseViewModel.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/BrowseViewModel.kt index bf8a4ea0..4cfda653 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/BrowseViewModel.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/BrowseViewModel.kt @@ -3,16 +3,10 @@ package de.nicidienase.chaosflix.touch.browse import android.arch.lifecycle.LiveData import android.arch.lifecycle.ViewModel import de.nicidienase.chaosflix.common.entities.ChaosflixDatabase -import de.nicidienase.chaosflix.common.entities.recording.persistence.ConferenceGroup -import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentConference import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentEvent -import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentRecording -import de.nicidienase.chaosflix.common.entities.userdata.WatchlistItem import de.nicidienase.chaosflix.common.network.RecordingService import de.nicidienase.chaosflix.common.network.StreamingService import de.nicidienase.chaosflix.touch.sync.Downloader -import io.reactivex.Completable -import io.reactivex.schedulers.Schedulers class BrowseViewModel( val database: ChaosflixDatabase, @@ -22,25 +16,30 @@ class BrowseViewModel( val downloader = Downloader(recordingApi, database) - fun getConferenceGroups(): LiveData> { - downloader.updateConferencesAndGroups() - return database.conferenceGroupDao().getAll() - } + fun getConferenceGroups() + = database.conferenceGroupDao().getAll() - fun getConference(conferenceId: Long): LiveData + fun getConference(conferenceId: Long) = database.conferenceDao().findConferenceById(conferenceId) - fun getConferencesByGroup(groupId: Long): LiveData> + fun getConferencesByGroup(groupId: Long) = database.conferenceDao().findConferenceByGroup(groupId) - fun getEventsforConference(conferenceId: Long): LiveData> { - downloader.updateEventsForConference(conferenceId) - return database.eventDao().findEventsByConference(conferenceId) - } + fun getEventsforConference(conferenceId: Long) + = database.eventDao().findEventsByConference(conferenceId) - fun getBookmarkedEvents(): LiveData> = database.eventDao().findBookmarkedEvents() + fun updateConferences() + = downloader.updateConferencesAndGroups() - fun getInProgressEvents(): LiveData> = database.eventDao().findInProgressEvents() + fun updateEventsForConference(conferenceId: Long) + = downloader.updateEventsForConference(conferenceId) - fun getLivestreams() = streamingApi.getStreamingConferences() + fun getBookmarkedEvents() + = database.eventDao().findBookmarkedEvents() + + fun getInProgressEvents() + = database.eventDao().findInProgressEvents() + + fun getLivestreams() + = streamingApi.getStreamingConferences() } \ No newline at end of file diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferencesTabBrowseFragment.java b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferencesTabBrowseFragment.java index 6e29f892..4e26fa67 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferencesTabBrowseFragment.java +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferencesTabBrowseFragment.java @@ -74,6 +74,7 @@ public class ConferencesTabBrowseFragment extends BrowseFragment { TabLayout tabLayout = view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(mViewPager); }); + getViewModel().updateConferences(); // TODO show and dismiss loading-spinner return view; } diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java index 12859715..1e14f6fa 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java @@ -108,6 +108,7 @@ public class EventsListFragment extends BrowseFragment implements SearchView.OnQ }); getViewModel().getEventsforConference(conferenceId).observe(this, listObserver); + getViewModel().updateEventsForConference(conferenceId); // TODO show/dismiss loading-spinner } } return view; diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/sync/Downloader.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/sync/Downloader.kt index bf9ac19a..148f900e 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/sync/Downloader.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/sync/Downloader.kt @@ -1,5 +1,7 @@ package de.nicidienase.chaosflix.touch.sync +import android.arch.lifecycle.LiveData +import android.arch.lifecycle.MutableLiveData import android.util.Log import de.nicidienase.chaosflix.common.Util import de.nicidienase.chaosflix.common.entities.ChaosflixDatabase @@ -18,9 +20,9 @@ class Downloader(val recordingApi: RecordingService, private fun updateEverything() { updateConferencesAndGroups { conferenceIds -> - for(id in conferenceIds){ - updateEventsForConference(id){ eventIds -> - for(id in eventIds){ + for (id in conferenceIds) { + updateEventsForConference(id) { eventIds -> + for (id in eventIds) { updateRecordingsForEvent(id) } } @@ -28,41 +30,50 @@ class Downloader(val recordingApi: RecordingService, } } - fun updateConferencesAndGroups(listener: ((List) -> Unit)? = null) { + fun updateConferencesAndGroups(listener: ((List) -> Unit)? = null): LiveData { + val updateFinished = MutableLiveData() + updateFinished.value = false recordingApi.getConferencesWrapper() .subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) - .subscribe( { con: ConferencesWrapper? -> saveConferences(con, listener) },{ - t: Throwable? -> Log.d(TAG,t?.message,t) + .subscribe({ con: ConferencesWrapper? -> + saveConferences(con, listener) + updateFinished.postValue(true) + }, { t: Throwable? -> + Log.d(TAG, t?.message, t) }) + return updateFinished } private val TAG: String? = Downloader::class.simpleName fun updateEventsForConference(conferenceId: Long, listener: ((List) -> Unit)? = null) { - if(conferenceId < 0) + if (conferenceId < 0) return + val updateFinished = MutableLiveData() + updateFinished.value = false recordingApi.getConference(conferenceId) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) .subscribe({ conference: Conference? -> saveEvents(conference, listener) - }, { - t: Throwable? -> Log.d(TAG,t?.message,t) + updateFinished.postValue(true) + }, { t: Throwable? -> + Log.d(TAG, t?.message, t) }) } fun updateRecordingsForEvent(eventId: Long, listener: ((List) -> Unit)? = null) { - if(eventId < 0) + if (eventId < 0) return recordingApi.getEvent(eventId) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) - .subscribe ({ event: Event? -> + .subscribe({ event: Event? -> saveRecordings(event, listener) - }, { - t: Throwable? -> Log.d(TAG,t?.message,t) + }, { t: Throwable? -> + Log.d(TAG, t?.message, t) }) }