undo database-split and other fixes

This commit is contained in:
Felix 2018-09-06 19:47:18 +02:00
parent c2a83fae2d
commit 76a2a204a8
16 changed files with 158 additions and 77 deletions

View file

@ -1,14 +1,28 @@
package de.nicidienase.chaosflix.common.mediadata.entities package de.nicidienase.chaosflix.common
import android.arch.persistence.db.SupportSQLiteDatabase import android.arch.persistence.db.SupportSQLiteDatabase
import android.arch.persistence.room.Database import android.arch.persistence.room.Database
import android.arch.persistence.room.RoomDatabase import android.arch.persistence.room.RoomDatabase
import android.arch.persistence.room.TypeConverters import android.arch.persistence.room.TypeConverters
import android.arch.persistence.room.migration.Migration import android.arch.persistence.room.migration.Migration
import de.nicidienase.chaosflix.common.mediadata.entities.Converters
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.ConferenceDao
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.ConferenceGroup
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.ConferenceGroupDao
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.EventDao
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentConference
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentEvent
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentItem
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentRecording
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentRelatedEvent
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.RecordingDao
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.RelatedEventDao
import de.nicidienase.chaosflix.common.userdata.entities.download.OfflineEvent import de.nicidienase.chaosflix.common.userdata.entities.download.OfflineEvent
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.* import de.nicidienase.chaosflix.common.userdata.entities.download.OfflineEventDao
import de.nicidienase.chaosflix.common.userdata.entities.progress.PlaybackProgress import de.nicidienase.chaosflix.common.userdata.entities.progress.PlaybackProgress
import de.nicidienase.chaosflix.common.userdata.entities.progress.PlaybackProgressDao
import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItem import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItem
import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItemDao
@Database(entities = arrayOf( @Database(entities = arrayOf(
PersistentItem::class, PersistentItem::class,
@ -17,11 +31,13 @@ import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItem
PersistentRecording::class, PersistentRecording::class,
PersistentRelatedEvent::class, PersistentRelatedEvent::class,
ConferenceGroup::class, ConferenceGroup::class,
PlaybackProgress::class, PlaybackProgress::class,
WatchlistItem::class, WatchlistItem::class,
OfflineEvent::class), version = 5, exportSchema = true) OfflineEvent::class
), version = 5, exportSchema = true)
@TypeConverters(Converters::class) @TypeConverters(Converters::class)
abstract class MediaDatabase : RoomDatabase() { abstract class ChaosflixDatabase : RoomDatabase() {
abstract fun conferenceGroupDao(): ConferenceGroupDao abstract fun conferenceGroupDao(): ConferenceGroupDao
abstract fun conferenceDao(): ConferenceDao abstract fun conferenceDao(): ConferenceDao
@ -29,6 +45,11 @@ abstract class MediaDatabase : RoomDatabase() {
abstract fun relatedEventDao(): RelatedEventDao abstract fun relatedEventDao(): RelatedEventDao
abstract fun recordingDao(): RecordingDao abstract fun recordingDao(): RecordingDao
abstract fun playbackProgressDao(): PlaybackProgressDao
abstract fun watchlistItemDao(): WatchlistItemDao
abstract fun offlineEventDao(): OfflineEventDao
companion object { companion object {
val migration_2_3 = object : Migration(2, 3) { val migration_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
@ -59,5 +80,3 @@ abstract class MediaDatabase : RoomDatabase() {
} }
} }
} }

View file

@ -0,0 +1,17 @@
package de.nicidienase.chaosflix.common
import android.arch.persistence.room.Room
import android.content.Context
class DatabaseFactory (context: Context) {
val mediaDatabase by lazy {
Room.databaseBuilder(
context.applicationContext,
ChaosflixDatabase::class.java, "mediaccc.de")
.addMigrations(
ChaosflixDatabase.migration_2_3,
ChaosflixDatabase.migration_3_4)
.fallbackToDestructiveMigration()
.build()
}
}

View file

@ -40,7 +40,6 @@ data class Conference(
for (event in events) { for (event in events) {
if (event.tags?.isNotEmpty() ?: false) { if (event.tags?.isNotEmpty() ?: false) {
for (tag in event.tags!!) { for (tag in event.tags!!) {
if (tag != null) {
val list: MutableList<Event> val list: MutableList<Event>
if (map.keys.contains(tag)) { if (map.keys.contains(tag)) {
@ -50,9 +49,7 @@ data class Conference(
map.put(tag, list) map.put(tag, list)
} }
list.add(event) list.add(event)
} else {
untagged.add(event)
}
} }
} else { } else {
untagged.add(event) untagged.add(event)
@ -85,7 +82,7 @@ data class Conference(
return hashSet return hashSet
} }
override fun compareTo(conference: Conference): Int { override fun compareTo(other: Conference): Int {
return slug.compareTo(conference.slug) return slug.compareTo(other.slug)
} }
} }

View file

@ -57,15 +57,15 @@ data class Event(@SerializedName("conference_id")
fun getSpeakerString(): String? fun getSpeakerString(): String?
= persons?.joinToString(", ") = persons?.joinToString(", ")
override fun compareTo(event: Event): Int { override fun compareTo(other: Event): Int {
return slug.compareTo(event.slug) return slug.compareTo(other.slug)
} }
override fun equals(obj: Any?): Boolean { override fun equals(other: Any?): Boolean {
return if (obj is Event) { return if (other is Event) {
guid == obj.guid guid == other.guid
} else { } else {
super.equals(obj) super.equals(other)
} }
} }

View file

@ -23,10 +23,9 @@ data class Recording(
@SerializedName("event_url") @SerializedName("event_url")
var eventUrl: String = "", var eventUrl: String = "",
@SerializedName("conference_url") @SerializedName("conference_url")
var conferenceUrl: String = "" var conferenceUrl: String = "",
) {
var recordingID: Long var recordingID: Long
) {
init { init {
val strings = url.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() val strings = url.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()

View file

@ -6,7 +6,7 @@ import android.os.Parcelable
@Entity( @Entity(
tableName = "conference_group", tableName = "conference_group",
indices = [Index(value = "name", unique = true)]) indices = [Index(value = ["name"], unique = true)])
data class ConferenceGroup( data class ConferenceGroup(
var name: String = "" var name: String = ""
): PersistentItem(), Parcelable { ): PersistentItem(), Parcelable {

View file

@ -1,10 +1,10 @@
package de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence package de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence
import android.arch.persistence.room.* import android.arch.persistence.room.Entity
import android.arch.persistence.room.Ignore
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import de.nicidienase.chaosflix.common.entities.recording.Conference import de.nicidienase.chaosflix.common.mediadata.entities.recording.Conference
import de.nicidienase.chaosflix.common.entities.recording.Event
@Entity(tableName = "conference") @Entity(tableName = "conference")
data class PersistentConference( data class PersistentConference(

View file

@ -103,6 +103,7 @@ data class PersistentEvent(
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return Html.fromHtml(description, Html.FROM_HTML_MODE_LEGACY) return Html.fromHtml(description, Html.FROM_HTML_MODE_LEGACY)
} else { } else {
@Suppress("DEPRECATION")
return Html.fromHtml(description) return Html.fromHtml(description)
} }
} }

View file

@ -2,6 +2,7 @@ package de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence
import android.arch.persistence.room.Entity import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey import android.arch.persistence.room.PrimaryKey
import android.os.Parcelable
@Entity @Entity
abstract class PersistentItem(@PrimaryKey(autoGenerate = true) abstract class PersistentItem(@PrimaryKey(autoGenerate = true)

View file

@ -28,9 +28,11 @@ data class PersistentRecording(
var recordingUrl: String = "", var recordingUrl: String = "",
var url: String = "", var url: String = "",
var eventUrl: String = "", var eventUrl: String = "",
var conferenceUrl: String = "" var conferenceUrl: String = "",
var backendId: Long = 0
) : PersistentItem(), Parcelable { ) : PersistentItem(), Parcelable {
@Ignore
constructor(parcel: Parcel) : this( constructor(parcel: Parcel) : this(
parcel.readLong(), parcel.readLong(),
parcel.readInt(), parcel.readInt(),
@ -67,7 +69,8 @@ data class PersistentRecording(
recordingUrl = rec.recordingUrl, recordingUrl = rec.recordingUrl,
url = rec.url, url = rec.url,
eventUrl = rec.eventUrl, eventUrl = rec.eventUrl,
conferenceUrl = rec.conferenceUrl) conferenceUrl = rec.conferenceUrl,
backendId = rec.recordingID )
override fun writeToParcel(parcel: Parcel, flags: Int) { override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeLong(eventId) parcel.writeLong(eventId)
@ -101,4 +104,5 @@ data class PersistentRecording(
return arrayOfNulls(size) return arrayOfNulls(size)
} }
} }
} }

View file

@ -1,32 +1,41 @@
package de.nicidienase.chaosflix.touch.sync package de.nicidienase.chaosflix.common.mediadata.sync
import android.content.Intent import android.content.Intent
import android.support.v4.app.JobIntentService import android.support.v4.app.JobIntentService
import de.nicidienase.chaosflix.common.mediadata.sync.Downloader import de.nicidienase.chaosflix.common.DatabaseFactory
import de.nicidienase.chaosflix.touch.ViewModelFactory import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentConference
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentEvent
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentItem
import de.nicidienase.chaosflix.common.mediadata.network.ApiFactory
class DownloadJobService : JobIntentService() { class DownloadJobService : JobIntentService() {
override fun onHandleWork(intent: Intent) { override fun onHandleWork(intent: Intent) {
val downloader = Downloader(ViewModelFactory.recordingApi, ViewModelFactory.database) val downloader = Downloader(
val entity: String? = intent.getStringExtra(ENTITY_KEY) ApiFactory(resources).recordingApi,
val id: Long = intent.getLongExtra(ID_KEY, -1) DatabaseFactory(applicationContext).mediaDatabase)
if (entity != null) { val entityType: String? = intent.getStringExtra(ENTITY_KEY)
when (entity) {
// ENTITY_KEY_EVERYTHING -> downloader.updateEverything() if (entityType != null) {
when (entityType) {
ENTITY_KEY_CONFERENCES -> downloader.updateConferencesAndGroups() ENTITY_KEY_CONFERENCES -> downloader.updateConferencesAndGroups()
ENTITY_KEY_EVENTS -> downloader.updateEventsForConference(id) ENTITY_KEY_EVENTS -> {
ENTITY_KEY_RECORDINGS -> downloader.updateRecordingsForEvent(id) val item = intent.getParcelableExtra<PersistentConference>(ITEM_KEY)
downloader.updateEventsForConference(item as PersistentConference)
}
ENTITY_KEY_RECORDINGS -> {
val item = intent.getParcelableExtra<PersistentEvent>(ITEM_KEY)
downloader.updateRecordingsForEvent(item as PersistentEvent)
}
} }
} }
} }
companion object { companion object {
val ENTITY_KEY: String = "entity_key" val ENTITY_KEY: String = "entity_key"
// val ENTITY_KEY_EVERYTHING = "everything" val ITEM_KEY: String = "item_key"
val ENTITY_KEY_CONFERENCES: String = "conferences" val ENTITY_KEY_CONFERENCES: String = "conferences"
val ENTITY_KEY_EVENTS: String = "events" val ENTITY_KEY_EVENTS: String = "events"
val ENTITY_KEY_RECORDINGS: String = "recodings" val ENTITY_KEY_RECORDINGS: String = "recodings"
val ID_KEY: String = "id_key"
} }
} }

View file

@ -4,7 +4,7 @@ import android.arch.lifecycle.LiveData
import android.database.sqlite.SQLiteConstraintException import android.database.sqlite.SQLiteConstraintException
import android.util.Log import android.util.Log
import de.nicidienase.chaosflix.common.Util import de.nicidienase.chaosflix.common.Util
import de.nicidienase.chaosflix.common.mediadata.entities.MediaDatabase import de.nicidienase.chaosflix.common.ChaosflixDatabase
import de.nicidienase.chaosflix.common.mediadata.entities.recording.ConferencesWrapper import de.nicidienase.chaosflix.common.mediadata.entities.recording.ConferencesWrapper
import de.nicidienase.chaosflix.common.mediadata.entities.recording.Event import de.nicidienase.chaosflix.common.mediadata.entities.recording.Event
import de.nicidienase.chaosflix.common.mediadata.entities.recording.Recording import de.nicidienase.chaosflix.common.mediadata.entities.recording.Recording
@ -16,10 +16,9 @@ import de.nicidienase.chaosflix.common.mediadata.network.RecordingService
import de.nicidienase.chaosflix.common.util.LiveEvent import de.nicidienase.chaosflix.common.util.LiveEvent
import de.nicidienase.chaosflix.common.util.SingleLiveEvent import de.nicidienase.chaosflix.common.util.SingleLiveEvent
import de.nicidienase.chaosflix.common.util.ThreadHandler import de.nicidienase.chaosflix.common.util.ThreadHandler
import io.reactivex.schedulers.Schedulers
class Downloader(val recordingApi: RecordingService, class Downloader(val recordingApi: RecordingService,
val database: MediaDatabase) { val database: ChaosflixDatabase) {
private val threadHandler = ThreadHandler() private val threadHandler = ThreadHandler()

View file

@ -1,19 +1,54 @@
package de.nicidienase.chaosflix.common.userdata.entities.download package de.nicidienase.chaosflix.common.userdata.entities.download
import android.arch.persistence.room.* import android.arch.persistence.room.*
import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentEvent import android.os.Parcel
import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentRecording import android.os.Parcelable
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentEvent
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentItem
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentRecording
@Entity(tableName = "offline_event", @Entity(tableName = "offline_event",
indices = arrayOf(Index(value = "event_id", unique = true))) indices = arrayOf(Index(value = ["event_guid"], unique = true)))
data class OfflineEvent( data class OfflineEvent(
@ColumnInfo(name = "event_id") var eventId: Long, @ColumnInfo(name = "event_guid") var eventGuid: String,
@ColumnInfo(name = "recording_id") var recordingId: Long, @ColumnInfo(name = "recording_id") var recordingId: Long,
@ColumnInfo(name = "download_reference") var downloadReference: Long, @ColumnInfo(name = "download_reference") var downloadReference: Long,
@ColumnInfo(name = "local_path") var localPath: String){ @ColumnInfo(name = "local_path") var localPath: String): PersistentItem(), Parcelable {
@PrimaryKey(autoGenerate = true) var id: Long = 0
@Ignore var event: PersistentEvent? = null @Ignore var event: PersistentEvent? = null
@Ignore var recording: PersistentRecording? = null @Ignore var recording: PersistentRecording? = null
@Ignore
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readLong(),
parcel.readLong(),
parcel.readString()) {
event = parcel.readParcelable(PersistentEvent::class.java.classLoader)
recording = parcel.readParcelable(PersistentRecording::class.java.classLoader)
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(eventGuid)
parcel.writeLong(recordingId)
parcel.writeLong(downloadReference)
parcel.writeString(localPath)
parcel.writeParcelable(event, flags)
parcel.writeParcelable(recording, flags)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<OfflineEvent> {
override fun createFromParcel(parcel: Parcel): OfflineEvent {
return OfflineEvent(parcel)
}
override fun newArray(size: Int): Array<OfflineEvent?> {
return arrayOfNulls(size)
}
}
} }

View file

@ -11,11 +11,11 @@ interface OfflineEventDao{
@Insert @Insert
fun insert(vararg items: OfflineEvent) fun insert(vararg items: OfflineEvent)
@Query("SELECT * FROM offline_event WHERE event_id = :id LIMIT 1") @Query("SELECT * FROM offline_event WHERE event_guid = :guid LIMIT 1")
fun getByEventId(id: Long): LiveData<OfflineEvent> fun getByEventId(guid: String): LiveData<OfflineEvent>
@Query("SELECT * FROM offline_event WHERE event_id = :id LIMIT 1") @Query("SELECT * FROM offline_event WHERE event_guid = :guid LIMIT 1")
fun getByEventIdSynchronous(id: Long): OfflineEvent fun getByEventIdSynchronous(guid: String): OfflineEvent
@Query("SELECT * FROM offline_event WHERE download_reference = :ref LIMIT 1") @Query("SELECT * FROM offline_event WHERE download_reference = :ref LIMIT 1")
fun getByDownloadReference(ref: Long): LiveData<OfflineEvent> fun getByDownloadReference(ref: Long): LiveData<OfflineEvent>

View file

@ -6,7 +6,7 @@ import android.arch.persistence.room.Index
import android.arch.persistence.room.PrimaryKey import android.arch.persistence.room.PrimaryKey
@Entity(tableName = "playback_progress", @Entity(tableName = "playback_progress",
indices = arrayOf(Index(value = "event_id",unique = true))) indices = arrayOf(Index(value = ["event_id"],unique = true)))
data class PlaybackProgress (@PrimaryKey data class PlaybackProgress (@PrimaryKey
@ColumnInfo(name = "event_id") @ColumnInfo(name = "event_id")
var eventId: Long, var eventId: Long,

View file

@ -6,7 +6,7 @@ import android.arch.persistence.room.Index
import android.arch.persistence.room.PrimaryKey import android.arch.persistence.room.PrimaryKey
@Entity(tableName = "watchlist_item", @Entity(tableName = "watchlist_item",
indices = arrayOf(Index(value = "event_id",unique = true))) indices = arrayOf(Index(value = ["event_id"],unique = true)))
data class WatchlistItem(@PrimaryKey(autoGenerate = true) data class WatchlistItem(@PrimaryKey(autoGenerate = true)
var id: Long = 0, var id: Long = 0,
@ColumnInfo(name = "event_id") @ColumnInfo(name = "event_id")