Common: introduce db-view for offline events

This commit is contained in:
Felix 2019-04-29 23:50:00 +02:00
parent d4a60ae0a9
commit 91d3c300fc
5 changed files with 49 additions and 27 deletions

View file

@ -31,4 +31,7 @@ interface OfflineEventDao{
@Query("DELETE FROM offline_event WHERE id=:id")
fun deleteById(id: Long)
@Query("SELECT o.event_guid,o.recording_id,o.download_reference,o.local_path,e.title,e.subtitle,e.length,e.thumbUrl FROM offline_event o JOIN event e WHERE o.event_guid = e.guid")
fun getOfflineEventsDisplay(): LiveData<List<OfflineEventView>>
}

View file

@ -0,0 +1,16 @@
package de.nicidienase.chaosflix.common.userdata.entities.download
import android.arch.persistence.room.ColumnInfo
import android.support.annotation.Keep
@Keep
data class OfflineEventView(
@ColumnInfo(name = "event_guid") var eventGuid: String,
@ColumnInfo(name = "recording_id") var recordingId: Long,
@ColumnInfo(name = "download_reference") var downloadReference: Long,
@ColumnInfo(name = "local_path") var localPath: String,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "subtitle") var subtitle: String?,
@ColumnInfo(name = "length") val length: Long,
@ColumnInfo(name = "thumbUrl") var thumbUrl: String
)

View file

@ -112,28 +112,27 @@ class BrowseViewModel(
return result
}
fun getOfflineEvents(): LiveData<List<Pair<OfflineEvent,Event>>> {
val result = MutableLiveData<List<Pair<OfflineEvent, Event>>>()
handler.runOnBackgroundThread {
val offlineEventMap = database.offlineEventDao().getAllSync()
.map { it.eventGuid to it }.toMap()
val persistentEventMap = database.eventDao().findEventsByGUIDsSync(offlineEventMap.keys.toList())
.map { it.guid to it }.toMap()
fun getOfflineEvents() = database.offlineEventDao().getAll()
val resultList = ArrayList<Pair<OfflineEvent, Event>>()
for (key in offlineEventMap.keys){
val offlineEvent = offlineEventMap[key]
var event: Event? = persistentEventMap[key]
if(event == null){
event = downloader.updateSingleEvent(key)
}
if(event != null && offlineEvent != null){
resultList.add(Pair(offlineEvent, event))
}
fun getOfflineDisplayEvents() = database.offlineEventDao().getOfflineEventsDisplay()
fun mapOfflineEvents(offlineEvents: List<OfflineEvent>): List<Pair<OfflineEvent,Event>> {
val offlineEventMap = offlineEvents.map { it.eventGuid to it }.toMap()
val persistentEventMap = database.eventDao().findEventsByGUIDsSync(offlineEventMap.keys.toList())
.map { it.guid to it }.toMap()
val resultList = ArrayList<Pair<OfflineEvent, Event>>()
for (key in offlineEventMap.keys){
val offlineEvent = offlineEventMap[key]
var event: Event? = persistentEventMap[key]
if(event == null){
event = downloader.updateSingleEvent(key)
}
if(event != null && offlineEvent != null){
resultList.add(Pair(offlineEvent, event))
}
result.postValue(resultList)
}
return result
return resultList
}
fun getEventById(eventId: Long) = database.eventDao().findEventById(eventId)

View file

@ -5,6 +5,7 @@ import android.os.Bundle
import android.os.Handler
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -39,13 +40,16 @@ class DownloadsListFragment : BrowseFragment() {
} else {
list.layoutManager = GridLayoutManager(context, columnCount - 1)
}
viewModel.getOfflineEvents().observe(this@DownloadsListFragment, Observer { events ->
if (events != null) {
offlineEventAdapter.items = events
} else {
offlineEventAdapter.items = emptyList()
}
setLoadingOverlayVisibility(false)
// viewModel.getOfflineEvents().observe(this@DownloadsListFragment, Observer { events ->
// if (events != null) {
// offlineEventAdapter.items = viewModel.mapOfflineEvents(events)
// } else {
// offlineEventAdapter.items = emptyList()
// }
// setLoadingOverlayVisibility(false)
// })
viewModel.getOfflineDisplayEvents().observe(this@DownloadsListFragment, Observer {
Log.d(TAG,"size: ${it?.size}")
})
return root
}
@ -75,6 +79,7 @@ class DownloadsListFragment : BrowseFragment() {
}
companion object {
private val TAG = DownloadsListFragment::class.java.simpleName
private const val ARG_COLUMN_COUNT = "column_count"
private const val UPDATE_DELAY = 700L

View file

@ -42,7 +42,6 @@ class OfflineEventAdapter(private val offlineItemManager: OfflineItemManager,
}
content?.setOnClickListener { _ ->
eventSelectedListener(item.second)
}
}
}