mirror of
https://github.com/NiciDieNase/chaosflix
synced 2024-11-26 22:20:24 +00:00
Common: introduce db-view for offline events
This commit is contained in:
parent
d4a60ae0a9
commit
91d3c300fc
5 changed files with 49 additions and 27 deletions
|
@ -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>>
|
||||
}
|
|
@ -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
|
||||
)
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -42,7 +42,6 @@ class OfflineEventAdapter(private val offlineItemManager: OfflineItemManager,
|
|||
}
|
||||
content?.setOnClickListener { _ ->
|
||||
eventSelectedListener(item.second)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue