refactor: clean up CollectionType
This commit is contained in:
parent
3960ab65e6
commit
763c11f590
5 changed files with 33 additions and 31 deletions
|
@ -18,7 +18,7 @@ fun BaseItemDto.toView(): View {
|
|||
return View(
|
||||
id = id,
|
||||
name = name ?: "",
|
||||
type = valueOf(collectionType, CollectionType.Movies),
|
||||
type = CollectionType.fromString(collectionType),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -48,15 +48,3 @@ fun Activity.restart() {
|
|||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
inline fun <reified T : Enum<T>> valueOf(type: String?, default: T): T {
|
||||
if (type == null) {
|
||||
return default
|
||||
}
|
||||
|
||||
return try {
|
||||
java.lang.Enum.valueOf(T::class.java, type)
|
||||
} catch (e: Exception) {
|
||||
default
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ class HomeViewModel @Inject internal constructor(
|
|||
private suspend fun loadLibraries(): HomeItem {
|
||||
val items = repository.getLibraries()
|
||||
val collections =
|
||||
items.filter { collection -> CollectionType.unsupportedCollections.none { it == collection.type } }
|
||||
items.filter { collection -> collection.type in CollectionType.supported }
|
||||
return HomeItem.Libraries(
|
||||
HomeSection(
|
||||
uuidLibraries,
|
||||
|
@ -112,7 +112,7 @@ class HomeViewModel @Inject internal constructor(
|
|||
|
||||
private suspend fun loadViews() = repository
|
||||
.getUserViews()
|
||||
.filter { view -> CollectionType.unsupportedCollections.none { it.type == view.collectionType } }
|
||||
.filter { view -> CollectionType.supported.any { it.type == view.collectionType } }
|
||||
.map { view -> view to repository.getLatestMedia(view.id) }
|
||||
.filter { (_, latest) -> latest.isNotEmpty() }
|
||||
.map { (view, latest) -> view.toView().apply { items = latest } }
|
||||
|
|
|
@ -37,7 +37,7 @@ constructor(
|
|||
try {
|
||||
val items = jellyfinRepository.getLibraries()
|
||||
val collections =
|
||||
items.filter { collection -> CollectionType.unsupportedCollections.none { it == collection.type } }
|
||||
items.filter { collection -> collection.type in CollectionType.supported }
|
||||
_uiState.emit(UiState.Normal(collections))
|
||||
} catch (e: Exception) {
|
||||
_uiState.emit(
|
||||
|
|
|
@ -9,15 +9,28 @@ enum class CollectionType(val type: String) {
|
|||
Books("books"),
|
||||
LiveTv("livetv"),
|
||||
BoxSets("boxsets"),
|
||||
Unknown("unknown"),
|
||||
;
|
||||
|
||||
companion object {
|
||||
val unsupportedCollections = listOf(
|
||||
HomeVideos,
|
||||
Music,
|
||||
Playlists,
|
||||
Books,
|
||||
LiveTv,
|
||||
val defaultValue = Unknown
|
||||
|
||||
val supported = listOf(
|
||||
Movies,
|
||||
TvShows,
|
||||
BoxSets,
|
||||
)
|
||||
|
||||
fun fromString(string: String?): CollectionType {
|
||||
if (string == null) {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return try {
|
||||
entries.first { it.type == string }
|
||||
} catch (e: NoSuchElementException) {
|
||||
defaultValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,14 +20,15 @@ data class FindroidCollection(
|
|||
) : FindroidItem
|
||||
|
||||
fun BaseItemDto.toFindroidCollection(): FindroidCollection? {
|
||||
val type = CollectionType.values().firstOrNull { it.type == collectionType }
|
||||
return if (type != null) {
|
||||
FindroidCollection(
|
||||
id = id,
|
||||
name = name.orEmpty(),
|
||||
type = type,
|
||||
)
|
||||
} else {
|
||||
null
|
||||
val type = CollectionType.fromString(collectionType)
|
||||
|
||||
if (type !in CollectionType.supported) {
|
||||
return null
|
||||
}
|
||||
|
||||
return FindroidCollection(
|
||||
id = id,
|
||||
name = name.orEmpty(),
|
||||
type = type,
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue