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(
|
return View(
|
||||||
id = id,
|
id = id,
|
||||||
name = name ?: "",
|
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
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||||
startActivity(intent)
|
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 {
|
private suspend fun loadLibraries(): HomeItem {
|
||||||
val items = repository.getLibraries()
|
val items = repository.getLibraries()
|
||||||
val collections =
|
val collections =
|
||||||
items.filter { collection -> CollectionType.unsupportedCollections.none { it == collection.type } }
|
items.filter { collection -> collection.type in CollectionType.supported }
|
||||||
return HomeItem.Libraries(
|
return HomeItem.Libraries(
|
||||||
HomeSection(
|
HomeSection(
|
||||||
uuidLibraries,
|
uuidLibraries,
|
||||||
|
@ -112,7 +112,7 @@ class HomeViewModel @Inject internal constructor(
|
||||||
|
|
||||||
private suspend fun loadViews() = repository
|
private suspend fun loadViews() = repository
|
||||||
.getUserViews()
|
.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) }
|
.map { view -> view to repository.getLatestMedia(view.id) }
|
||||||
.filter { (_, latest) -> latest.isNotEmpty() }
|
.filter { (_, latest) -> latest.isNotEmpty() }
|
||||||
.map { (view, latest) -> view.toView().apply { items = latest } }
|
.map { (view, latest) -> view.toView().apply { items = latest } }
|
||||||
|
|
|
@ -37,7 +37,7 @@ constructor(
|
||||||
try {
|
try {
|
||||||
val items = jellyfinRepository.getLibraries()
|
val items = jellyfinRepository.getLibraries()
|
||||||
val collections =
|
val collections =
|
||||||
items.filter { collection -> CollectionType.unsupportedCollections.none { it == collection.type } }
|
items.filter { collection -> collection.type in CollectionType.supported }
|
||||||
_uiState.emit(UiState.Normal(collections))
|
_uiState.emit(UiState.Normal(collections))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
_uiState.emit(
|
_uiState.emit(
|
||||||
|
|
|
@ -9,15 +9,28 @@ enum class CollectionType(val type: String) {
|
||||||
Books("books"),
|
Books("books"),
|
||||||
LiveTv("livetv"),
|
LiveTv("livetv"),
|
||||||
BoxSets("boxsets"),
|
BoxSets("boxsets"),
|
||||||
|
Unknown("unknown"),
|
||||||
;
|
;
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val unsupportedCollections = listOf(
|
val defaultValue = Unknown
|
||||||
HomeVideos,
|
|
||||||
Music,
|
val supported = listOf(
|
||||||
Playlists,
|
Movies,
|
||||||
Books,
|
TvShows,
|
||||||
LiveTv,
|
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
|
) : FindroidItem
|
||||||
|
|
||||||
fun BaseItemDto.toFindroidCollection(): FindroidCollection? {
|
fun BaseItemDto.toFindroidCollection(): FindroidCollection? {
|
||||||
val type = CollectionType.values().firstOrNull { it.type == collectionType }
|
val type = CollectionType.fromString(collectionType)
|
||||||
return if (type != null) {
|
|
||||||
FindroidCollection(
|
if (type !in CollectionType.supported) {
|
||||||
id = id,
|
return null
|
||||||
name = name.orEmpty(),
|
|
||||||
type = type,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return FindroidCollection(
|
||||||
|
id = id,
|
||||||
|
name = name.orEmpty(),
|
||||||
|
type = type,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue