forked from jellyfin/jellyfin-android
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from neBM/download
Refactor and simplify Room database
- Loading branch information
Showing
31 changed files
with
492 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 78 additions & 19 deletions
97
app/src/main/java/org/jellyfin/mobile/data/entity/DownloadEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,102 @@ | ||
package org.jellyfin.mobile.data.entity | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.Ignore | ||
import androidx.room.Index | ||
import androidx.room.PrimaryKey | ||
import androidx.room.TypeConverter | ||
import androidx.room.TypeConverters | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.Json.Default.decodeFromString | ||
import org.jellyfin.mobile.data.entity.DownloadEntity.Key.ITEM_ID | ||
import org.jellyfin.mobile.data.entity.DownloadEntity.Key.TABLE_NAME | ||
import org.jellyfin.mobile.player.source.LocalJellyfinMediaSource | ||
import org.jellyfin.mobile.utils.Constants | ||
import java.io.File | ||
import java.util.Locale | ||
|
||
@Entity( | ||
tableName = TABLE_NAME, | ||
indices = [ | ||
Index(value = [ITEM_ID], unique = true), | ||
], | ||
) | ||
@TypeConverters(LocalJellyfinMediaSourceConverter::class) | ||
data class DownloadEntity( | ||
@PrimaryKey(autoGenerate = true) | ||
@ColumnInfo(name = ID) | ||
val id: Long, | ||
@PrimaryKey | ||
@ColumnInfo(name = ITEM_ID) | ||
val itemId: String, | ||
@ColumnInfo(name = MEDIA_URI) | ||
val mediaUri: String, | ||
@ColumnInfo(name = MEDIA_SOURCE) | ||
val mediaSource: String, | ||
@ColumnInfo(name = DOWNLOAD_FOLDER_URI) | ||
val downloadFolderUri: String, | ||
@ColumnInfo(name = DOWNLOAD_LENGTH) | ||
val downloadLength: Long | ||
val mediaSource: LocalJellyfinMediaSource, | ||
) { | ||
constructor(itemId: String, mediaUri: String, mediaSource: String, downloadFolderUri: String, downloadLength: Long) : | ||
this(0, itemId, mediaUri, mediaSource, downloadFolderUri, downloadLength) | ||
/** | ||
* Converts the [mediaSource] string to a [LocalJellyfinMediaSource] object. | ||
* | ||
* @param startTimeMs The start time in milliseconds. If null, the default start time is used. | ||
* @param audioStreamIndex The index of the audio stream to select. If null, the default audio stream is used. | ||
* @param subtitleStreamIndex The index of the subtitle stream to select. If -1, subtitles are disabled. If null, the default subtitle stream is used. | ||
*/ | ||
fun asMediaSource( | ||
startTimeMs: Long? = null, | ||
audioStreamIndex: Int? = null, | ||
subtitleStreamIndex: Int? = null, | ||
): LocalJellyfinMediaSource = mediaSource | ||
.also { localJellyfinMediaSource -> | ||
startTimeMs | ||
?.let { localJellyfinMediaSource.startTimeMs = it } | ||
audioStreamIndex | ||
?.let { localJellyfinMediaSource.mediaStreams[it] } | ||
?.let(localJellyfinMediaSource::selectAudioStream) | ||
subtitleStreamIndex | ||
?.run { | ||
takeUnless { it == -1 } | ||
?.let { localJellyfinMediaSource.mediaStreams[it] } | ||
?: localJellyfinMediaSource.selectSubtitleStream(null) | ||
} | ||
} | ||
|
||
constructor(mediaSource: LocalJellyfinMediaSource) : | ||
this(mediaSource.id, mediaSource) | ||
|
||
@Ignore | ||
val thumbnail: Bitmap? = BitmapFactory.decodeFile( | ||
File(mediaSource.localDirectoryUri, Constants.DOWNLOAD_THUMBNAIL_FILENAME).canonicalPath, | ||
) | ||
|
||
@Ignore | ||
val fileSize: String = formatFileSize(mediaSource.downloadSize) | ||
|
||
@Ignore | ||
private fun formatFileSize(bytes: Long): String { | ||
val units = arrayOf("B", "KB", "MB", "GB", "TB") | ||
var size = bytes.toDouble() | ||
var unitIndex = 0 | ||
|
||
while (size >= BYTES_PER_BINARY_UNIT && unitIndex < units.lastIndex) { | ||
size /= BYTES_PER_BINARY_UNIT | ||
unitIndex++ | ||
} | ||
|
||
return "%.1f %s".format(Locale.ROOT, size, units[unitIndex]) | ||
} | ||
|
||
companion object Key { | ||
const val TABLE_NAME = "Download" | ||
const val ID = "id" | ||
const val ITEM_ID = "item_id" | ||
const val MEDIA_URI = "media_uri" | ||
const val MEDIA_SOURCE = "media_source" | ||
const val DOWNLOAD_FOLDER_URI = "download_folder_uri" | ||
const val DOWNLOAD_LENGTH = "download_length" | ||
const val BYTES_PER_BINARY_UNIT: Int = 1024 | ||
const val TABLE_NAME: String = "Download" | ||
const val ID: String = "id" | ||
const val ITEM_ID: String = "item_id" | ||
const val MEDIA_SOURCE: String = "media_source" | ||
} | ||
} | ||
|
||
class LocalJellyfinMediaSourceConverter { | ||
@TypeConverter | ||
fun toLocalJellyfinMediaSource(value: String): LocalJellyfinMediaSource = decodeFromString(value) | ||
|
||
@TypeConverter | ||
fun fromLocalJellyfinMediaSource(value: LocalJellyfinMediaSource): String = Json.encodeToString(value) | ||
} |
31 changes: 0 additions & 31 deletions
31
app/src/main/java/org/jellyfin/mobile/downloads/DownloadItem.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.