diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/MediaDatabase.kt b/common/src/main/java/de/nicidienase/chaosflix/common/ChaosflixDatabase.kt similarity index 56% rename from common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/MediaDatabase.kt rename to common/src/main/java/de/nicidienase/chaosflix/common/ChaosflixDatabase.kt index 105d9959..86f987d0 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/MediaDatabase.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/ChaosflixDatabase.kt @@ -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.room.Database import android.arch.persistence.room.RoomDatabase import android.arch.persistence.room.TypeConverters 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.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.PlaybackProgressDao import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItem +import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItemDao @Database(entities = arrayOf( PersistentItem::class, @@ -17,11 +31,13 @@ import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItem PersistentRecording::class, PersistentRelatedEvent::class, ConferenceGroup::class, + PlaybackProgress::class, WatchlistItem::class, - OfflineEvent::class), version = 5, exportSchema = true) + OfflineEvent::class +), version = 5, exportSchema = true) @TypeConverters(Converters::class) -abstract class MediaDatabase : RoomDatabase() { +abstract class ChaosflixDatabase : RoomDatabase() { abstract fun conferenceGroupDao(): ConferenceGroupDao abstract fun conferenceDao(): ConferenceDao @@ -29,6 +45,11 @@ abstract class MediaDatabase : RoomDatabase() { abstract fun relatedEventDao(): RelatedEventDao abstract fun recordingDao(): RecordingDao + abstract fun playbackProgressDao(): PlaybackProgressDao + abstract fun watchlistItemDao(): WatchlistItemDao + abstract fun offlineEventDao(): OfflineEventDao + + companion object { val migration_2_3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { @@ -58,6 +79,4 @@ abstract class MediaDatabase : RoomDatabase() { } } -} - - +} \ No newline at end of file diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/DatabaseFactory.kt b/common/src/main/java/de/nicidienase/chaosflix/common/DatabaseFactory.kt new file mode 100644 index 00000000..b22fcdb7 --- /dev/null +++ b/common/src/main/java/de/nicidienase/chaosflix/common/DatabaseFactory.kt @@ -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() + } +} diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Conference.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Conference.kt index f3e8edb5..7bb3f643 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Conference.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Conference.kt @@ -34,36 +34,33 @@ data class Conference( } private fun getEventsMap(events: List?): Map>{ - val map = HashMap>() - val untagged = ArrayList() - if (events != null) { - for (event in events) { - if (event.tags?.isNotEmpty() ?: false) { - for (tag in event.tags!!) { - if (tag != null) { + val map = HashMap>() + val untagged = ArrayList() + if (events != null) { + for (event in events) { + if (event.tags?.isNotEmpty() ?: false) { + for (tag in event.tags!!) { - val list: MutableList - if (map.keys.contains(tag)) { - list = map[tag]!! - } else { - list = ArrayList() - map.put(tag, list) - } - list.add(event) - } else { - untagged.add(event) - } - } + val list: MutableList + if (map.keys.contains(tag)) { + list = map[tag]!! } else { - untagged.add(event) + list = ArrayList() + map.put(tag, list) } + list.add(event) + } - if (untagged.size > 0) { - map.put("untagged", untagged) - } + } else { + untagged.add(event) } - return map } + if (untagged.size > 0) { + map.put("untagged", untagged) + } + } + return map + } private fun getIdFromUrl(url: String = this.url): Long { @@ -85,7 +82,7 @@ data class Conference( return hashSet } - override fun compareTo(conference: Conference): Int { - return slug.compareTo(conference.slug) + override fun compareTo(other: Conference): Int { + return slug.compareTo(other.slug) } } diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Event.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Event.kt index 513a56a1..135a3f4c 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Event.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Event.kt @@ -57,15 +57,15 @@ data class Event(@SerializedName("conference_id") fun getSpeakerString(): String? = persons?.joinToString(", ") - override fun compareTo(event: Event): Int { - return slug.compareTo(event.slug) + override fun compareTo(other: Event): Int { + return slug.compareTo(other.slug) } - override fun equals(obj: Any?): Boolean { - return if (obj is Event) { - guid == obj.guid + override fun equals(other: Any?): Boolean { + return if (other is Event) { + guid == other.guid } else { - super.equals(obj) + super.equals(other) } } diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Recording.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Recording.kt index 5675861d..ba08ff83 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Recording.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/Recording.kt @@ -23,11 +23,10 @@ data class Recording( @SerializedName("event_url") var eventUrl: String = "", @SerializedName("conference_url") - var conferenceUrl: String = "" + var conferenceUrl: String = "", + var recordingID: Long ) { - var recordingID: Long - init { val strings = url.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() recordingID = (strings[strings.size - 1]).toLong() diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/ConferenceGroup.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/ConferenceGroup.kt index c6400788..aa712f1c 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/ConferenceGroup.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/ConferenceGroup.kt @@ -6,7 +6,7 @@ import android.os.Parcelable @Entity( tableName = "conference_group", - indices = [Index(value = "name", unique = true)]) + indices = [Index(value = ["name"], unique = true)]) data class ConferenceGroup( var name: String = "" ): PersistentItem(), Parcelable { diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentConference.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentConference.kt index 43d4a9e2..6498485f 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentConference.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentConference.kt @@ -1,10 +1,10 @@ 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.Parcelable -import de.nicidienase.chaosflix.common.entities.recording.Conference -import de.nicidienase.chaosflix.common.entities.recording.Event +import de.nicidienase.chaosflix.common.mediadata.entities.recording.Conference @Entity(tableName = "conference") data class PersistentConference( diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentEvent.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentEvent.kt index 1a2a081a..3dc76dcd 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentEvent.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentEvent.kt @@ -103,6 +103,7 @@ data class PersistentEvent( if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { return Html.fromHtml(description, Html.FROM_HTML_MODE_LEGACY) } else { + @Suppress("DEPRECATION") return Html.fromHtml(description) } } diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentItem.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentItem.kt index 5c30bbaa..6a68b60c 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentItem.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentItem.kt @@ -2,6 +2,7 @@ package de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence import android.arch.persistence.room.Entity import android.arch.persistence.room.PrimaryKey +import android.os.Parcelable @Entity abstract class PersistentItem(@PrimaryKey(autoGenerate = true) diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentRecording.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentRecording.kt index bbb99f20..578f174d 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentRecording.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/PersistentRecording.kt @@ -28,9 +28,11 @@ data class PersistentRecording( var recordingUrl: String = "", var url: String = "", var eventUrl: String = "", - var conferenceUrl: String = "" + var conferenceUrl: String = "", + var backendId: Long = 0 ) : PersistentItem(), Parcelable { + @Ignore constructor(parcel: Parcel) : this( parcel.readLong(), parcel.readInt(), @@ -67,7 +69,8 @@ data class PersistentRecording( recordingUrl = rec.recordingUrl, url = rec.url, eventUrl = rec.eventUrl, - conferenceUrl = rec.conferenceUrl) + conferenceUrl = rec.conferenceUrl, + backendId = rec.recordingID ) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeLong(eventId) @@ -101,4 +104,5 @@ data class PersistentRecording( return arrayOfNulls(size) } } + } \ No newline at end of file diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/DownloadJobService.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/DownloadJobService.kt index 886f1a02..7bed29f2 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/DownloadJobService.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/DownloadJobService.kt @@ -1,32 +1,41 @@ -package de.nicidienase.chaosflix.touch.sync +package de.nicidienase.chaosflix.common.mediadata.sync import android.content.Intent import android.support.v4.app.JobIntentService -import de.nicidienase.chaosflix.common.mediadata.sync.Downloader -import de.nicidienase.chaosflix.touch.ViewModelFactory +import de.nicidienase.chaosflix.common.DatabaseFactory +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() { override fun onHandleWork(intent: Intent) { - val downloader = Downloader(ViewModelFactory.recordingApi, ViewModelFactory.database) - val entity: String? = intent.getStringExtra(ENTITY_KEY) - val id: Long = intent.getLongExtra(ID_KEY, -1) - if (entity != null) { - when (entity) { -// ENTITY_KEY_EVERYTHING -> downloader.updateEverything() + val downloader = Downloader( + ApiFactory(resources).recordingApi, + DatabaseFactory(applicationContext).mediaDatabase) + val entityType: String? = intent.getStringExtra(ENTITY_KEY) + + if (entityType != null) { + when (entityType) { ENTITY_KEY_CONFERENCES -> downloader.updateConferencesAndGroups() - ENTITY_KEY_EVENTS -> downloader.updateEventsForConference(id) - ENTITY_KEY_RECORDINGS -> downloader.updateRecordingsForEvent(id) + ENTITY_KEY_EVENTS -> { + val item = intent.getParcelableExtra(ITEM_KEY) + downloader.updateEventsForConference(item as PersistentConference) + } + ENTITY_KEY_RECORDINGS -> { + val item = intent.getParcelableExtra(ITEM_KEY) + downloader.updateRecordingsForEvent(item as PersistentEvent) + } } } } companion object { 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_EVENTS: String = "events" val ENTITY_KEY_RECORDINGS: String = "recodings" - val ID_KEY: String = "id_key" } } \ No newline at end of file diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/Downloader.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/Downloader.kt index f7095a76..c8596c74 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/Downloader.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/sync/Downloader.kt @@ -4,7 +4,7 @@ import android.arch.lifecycle.LiveData import android.database.sqlite.SQLiteConstraintException import android.util.Log 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.Event 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.SingleLiveEvent import de.nicidienase.chaosflix.common.util.ThreadHandler -import io.reactivex.schedulers.Schedulers class Downloader(val recordingApi: RecordingService, - val database: MediaDatabase) { + val database: ChaosflixDatabase) { private val threadHandler = ThreadHandler() diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEvent.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEvent.kt index cf8a4e5a..1d8de37c 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEvent.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEvent.kt @@ -1,19 +1,54 @@ package de.nicidienase.chaosflix.common.userdata.entities.download import android.arch.persistence.room.* -import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentEvent -import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentRecording +import android.os.Parcel +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", - indices = arrayOf(Index(value = "event_id", unique = true))) + indices = arrayOf(Index(value = ["event_guid"], unique = true))) 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 = "download_reference") var downloadReference: Long, - @ColumnInfo(name = "local_path") var localPath: String){ - - @PrimaryKey(autoGenerate = true) var id: Long = 0 + @ColumnInfo(name = "local_path") var localPath: String): PersistentItem(), Parcelable { @Ignore var event: PersistentEvent? = 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 { + override fun createFromParcel(parcel: Parcel): OfflineEvent { + return OfflineEvent(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } \ No newline at end of file diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEventDao.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEventDao.kt index 1a0cd238..fc504273 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEventDao.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/download/OfflineEventDao.kt @@ -11,11 +11,11 @@ interface OfflineEventDao{ @Insert fun insert(vararg items: OfflineEvent) - @Query("SELECT * FROM offline_event WHERE event_id = :id LIMIT 1") - fun getByEventId(id: Long): LiveData + @Query("SELECT * FROM offline_event WHERE event_guid = :guid LIMIT 1") + fun getByEventId(guid: String): LiveData - @Query("SELECT * FROM offline_event WHERE event_id = :id LIMIT 1") - fun getByEventIdSynchronous(id: Long): OfflineEvent + @Query("SELECT * FROM offline_event WHERE event_guid = :guid LIMIT 1") + fun getByEventIdSynchronous(guid: String): OfflineEvent @Query("SELECT * FROM offline_event WHERE download_reference = :ref LIMIT 1") fun getByDownloadReference(ref: Long): LiveData diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt index bdf9798b..010c7cbc 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt @@ -6,7 +6,7 @@ import android.arch.persistence.room.Index import android.arch.persistence.room.PrimaryKey @Entity(tableName = "playback_progress", - indices = arrayOf(Index(value = "event_id",unique = true))) + indices = arrayOf(Index(value = ["event_id"],unique = true))) data class PlaybackProgress (@PrimaryKey @ColumnInfo(name = "event_id") var eventId: Long, diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItem.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItem.kt index 91724f02..190cf79e 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItem.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItem.kt @@ -6,7 +6,7 @@ import android.arch.persistence.room.Index import android.arch.persistence.room.PrimaryKey @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) var id: Long = 0, @ColumnInfo(name = "event_id")