Skip to content

Commit

Permalink
refactor: implement content kind enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
urFate committed Jun 30, 2024
1 parent d00c5a6 commit 5d81f31
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 55 deletions.
27 changes: 27 additions & 0 deletions app/src/main/java/live/shirabox/shirabox/ValuesHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package live.shirabox.shirabox

import android.content.Context
import live.shirabox.core.model.ContentKind
import live.shirabox.core.model.ReleaseStatus

object ValuesHelper {
fun decodeKind(contentKind: ContentKind, context: Context): String = when (contentKind) {
ContentKind.TV -> context.getString(R.string.kind_tv)
ContentKind.MOVIE -> context.getString(R.string.kind_movie)
ContentKind.OVA -> context.getString(R.string.kind_ova)
ContentKind.ONA -> context.getString(R.string.kind_ona)
ContentKind.SPECIAL -> context.getString(R.string.kind_special)
ContentKind.OTHER -> context.getString(R.string.kind_other)
}

fun decodeStatus(releaseStatus: ReleaseStatus, context: Context): String =
when (releaseStatus) {
ReleaseStatus.FINISHED -> context.getString(R.string.release_status_finished)
ReleaseStatus.RELEASING -> context.getString(R.string.release_status_releasing)
ReleaseStatus.ANNOUNCED -> context.getString(R.string.release_status_announced)
ReleaseStatus.PAUSED -> context.getString(R.string.release_status_paused)
ReleaseStatus.DISCOUNTED -> context.getString(R.string.release_status_discounted)
ReleaseStatus.UNKNOWN -> context.getString(R.string.release_status_unknown)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ import coil.compose.AsyncImage
import coil.request.ImageRequest
import dagger.hilt.android.AndroidEntryPoint
import live.shirabox.core.model.ContentType
import live.shirabox.core.model.ReleaseStatus
import live.shirabox.core.util.round
import live.shirabox.shirabox.R
import live.shirabox.shirabox.ValuesHelper
import live.shirabox.shirabox.ui.component.general.ContentCard
import live.shirabox.shirabox.ui.component.general.ExpandableBox
import live.shirabox.shirabox.ui.component.general.ExtendedListItem
Expand Down Expand Up @@ -438,7 +438,11 @@ fun Resource(
)
ResourceDataLabel(
icon = Icons.Outlined.EventAvailable,
text = "${content.kind}, ${decodeStatus(content.status, context)}"
text = "${
ValuesHelper.decodeKind(content.kind, context)
}, ${
ValuesHelper.decodeStatus(content.status, context)
}"
)
if (type == ContentType.ANIME) {
ResourceDataLabel(
Expand Down Expand Up @@ -636,17 +640,6 @@ private fun shareVia(context: Context, shikimoriId: Int) {
context.startActivity(shareIntent)
}

private fun decodeStatus(releaseStatus: ReleaseStatus, context: Context): String {
return when(releaseStatus) {
ReleaseStatus.FINISHED -> context.getString(R.string.release_status_finished)
ReleaseStatus.RELEASING -> context.getString(R.string.release_status_releasing)
ReleaseStatus.ANNOUNCED -> context.getString(R.string.release_status_announced)
ReleaseStatus.PAUSED -> context.getString(R.string.release_status_paused)
ReleaseStatus.DISCOUNTED -> context.getString(R.string.release_status_discounted)
ReleaseStatus.UNKNOWN -> context.getString(R.string.release_status_unknown)
}
}

@Composable
fun CommentComponent(username: String, avatar: String, timestamp: String, text: String) {
ExtendedListItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import live.shirabox.core.model.Content
import live.shirabox.core.model.ContentType
import live.shirabox.data.catalog.shikimori.ShikimoriRepository
import live.shirabox.shirabox.R
import live.shirabox.shirabox.ValuesHelper
import live.shirabox.shirabox.ui.activity.resource.ResourceActivity
import live.shirabox.shirabox.ui.component.general.DespondencyEmoticon
import live.shirabox.shirabox.ui.component.general.ListItem
Expand Down Expand Up @@ -226,7 +227,7 @@ fun SearchScreen() {
Text(
text = it.name, fontWeight = FontWeight.Bold
)
}, supportingString = "${it.releaseYear}, ${it.kind}", coverImage = it.image
}, supportingString = "${it.releaseYear}, ${ValuesHelper.decodeKind(it.kind, context)}", coverImage = it.image
) {
context.startActivity(Intent(
context, ResourceActivity::class.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import live.shirabox.core.model.ContentType
import live.shirabox.shirabox.R
import live.shirabox.shirabox.ValuesHelper
import live.shirabox.shirabox.ui.activity.resource.ResourceActivity
import live.shirabox.shirabox.ui.component.general.DespondencyEmoticon
import live.shirabox.shirabox.ui.component.general.ListItem
Expand Down Expand Up @@ -100,7 +101,7 @@ fun History(
)
},
supportingString = "${contentEntity.production?.let { "$it\n" }}" +
"${contentEntity.releaseYear}, ${contentEntity.kind}",
"${contentEntity.releaseYear}, ${ValuesHelper.decodeKind(contentEntity.kind, context)}",
coverImage = contentEntity.image
) {
context.startActivity(
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
<string name="release_status_paused">Выпуск приостановлен</string>
<string name="release_status_discounted">Выпуск прекращён</string>
<string name="release_status_unknown">Статус неизвестен</string>
<string name="kind_tv">Сериал</string>
<string name="kind_movie">Фильм</string>
<string name="kind_ova">OVA</string>
<string name="kind_ona">ONA</string>
<string name="kind_special">Спецвыпуск</string>
<string name="kind_other">Другое</string>

<!--
Player Activity
Expand Down
15 changes: 14 additions & 1 deletion core/src/main/java/live/shirabox/core/db/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import kotlinx.serialization.json.Json
import live.shirabox.core.model.ActingTeam
import live.shirabox.core.model.ContentKind
import live.shirabox.core.model.ReleaseStatus

object Migrations {
Expand Down Expand Up @@ -43,16 +44,18 @@ object Migrations {
} while (episodesCursor.moveToNext())
}

// Map content cyrillic status to enum values
// Map content cyrillic values to enum classes
if(contentCursor.moveToFirst()) {
do {
val uidIndex = contentCursor.getColumnIndex("uid")
val statusIndex = contentCursor.getColumnIndex("status")
val kindIndex = contentCursor.getColumnIndex("kind")

if (uidIndex < 0) continue

val contentUid = contentCursor.getLong(uidIndex)
val oldStatus = contentCursor.getString(statusIndex)
val oldKind = contentCursor.getString(kindIndex)

val releaseStatus = when (oldStatus) {
"Анонс" -> ReleaseStatus.ANNOUNCED
Expand All @@ -63,8 +66,18 @@ object Migrations {
else -> ReleaseStatus.UNKNOWN
}

val contentKind = when(oldKind) {
"Сериал" -> ContentKind.TV
"Фильм" -> ContentKind.MOVIE
"Спешл" -> ContentKind.SPECIAL
"OVA" -> ContentKind.OVA
"ONA" -> ContentKind.ONA
else -> ContentKind.OTHER
}

val values = ContentValues().apply {
put("status", releaseStatus.toString())
put("kind", contentKind.toString())
}

db.update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import live.shirabox.core.model.ContentKind
import live.shirabox.core.model.ContentType
import live.shirabox.core.model.Rating
import live.shirabox.core.model.ReleaseStatus
Expand All @@ -19,7 +20,7 @@ data class ContentEntity(
@ColumnInfo(name = "production") val production: String?,
@ColumnInfo(name = "release_year") val releaseYear: String?,
@ColumnInfo(name = "type") val type: ContentType,
@ColumnInfo(name = "kind") val kind: String,
@ColumnInfo(name = "kind") val kind: ContentKind,
@ColumnInfo(name = "status") val status: ReleaseStatus,
@ColumnInfo(name = "episodes") val episodes: Int,
@ColumnInfo(name = "episodes_aired") val episodesAired: Int?,
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/live/shirabox/core/model/Content.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data class Content(
val production: String? = null,
val releaseYear: String?,
val type: ContentType,
val kind: String,
val kind: ContentKind,
val status: ReleaseStatus,
val episodes: Int,
val episodesAired: Int? = null,
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/live/shirabox/core/model/ContentKind.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package live.shirabox.core.model

enum class ContentKind {
TV, MOVIE, OVA, ONA, SPECIAL, OTHER
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.serialization.json.Json
import live.shirabox.core.model.Content
import live.shirabox.core.model.ContentKind
import live.shirabox.core.model.ContentType
import live.shirabox.core.model.Rating
import live.shirabox.core.model.ReleaseStatus
Expand Down Expand Up @@ -197,15 +198,15 @@ object ShikimoriRepository : AbstractCatalogRepository("Shikimori", "https://shi
val response = myClient.newCall(request).execute().body.string()
val data = json.decodeFromString<List<RelatedItem>>(response)

val contents = data.filter { it.relation != "Other" }.map {
val contents = data.filter { it.relation != "Other" }.mapNotNull {
when {
it.anime != null -> Content(
name = it.anime.russian,
enName = it.anime.name,
image = "$url/${it.anime.image.original}",
releaseYear = it.anime.releasedOn,
type = ContentType.ANIME,
kind = it.anime.kind.toString(),
kind = decodeKind(it.anime.kind.toString()),
status = decodeStatus(it.anime.status),
episodes = it.anime.episodes,
episodesAired = it.anime.episodesAired,
Expand All @@ -219,7 +220,7 @@ object ShikimoriRepository : AbstractCatalogRepository("Shikimori", "https://shi
image = "$url/${it.manga.image.original}",
releaseYear = it.manga.releasedOn ?: "",
type = ContentType.MANGA,
kind = it.manga.kind.toString(),
kind = decodeKind(it.manga.kind.toString()),
status = decodeStatus(it.manga.status),
episodes = it.manga.chapters,
rating = Rating(average = it.manga.score.toDouble(), scores = mapOf()),
Expand All @@ -228,7 +229,7 @@ object ShikimoriRepository : AbstractCatalogRepository("Shikimori", "https://shi

else -> null
}
}.filterNotNull()
}

emit(contents)
} catch (ex: Exception) { throw ex}
Expand Down Expand Up @@ -300,31 +301,22 @@ object ShikimoriRepository : AbstractCatalogRepository("Shikimori", "https://shi
}
}

private fun decodeKind(str: String?): String {
return when(str) {
"tv" -> "Сериал"
"movie" -> "Фильм"
"special", "tv_special" -> "Спешл"
"manga" -> "Манга"
"manhua" -> "Маньхуа"
"light_novel" -> "Ранобэ"
"novel" -> "Новелла"
"one_shot" -> "Ван-шот"
"doujin" -> "Додзинси"
null, "null" -> "Неизвестно"
else -> str.uppercase()
}
private fun decodeKind(str: String?): ContentKind = when(str) {
"tv" -> ContentKind.TV
"movie" -> ContentKind.MOVIE
"special", "tv_special" -> ContentKind.SPECIAL
"ova" -> ContentKind.OVA
"ona" -> ContentKind.ONA
else -> ContentKind.OTHER
}

private fun decodeStatus(str: String): ReleaseStatus {
return when(str) {
"anons" -> ReleaseStatus.ANNOUNCED
"ongoing" -> ReleaseStatus.RELEASING
"released" -> ReleaseStatus.FINISHED
"paused" -> ReleaseStatus.PAUSED
"discontinued" -> ReleaseStatus.DISCOUNTED
else -> ReleaseStatus.UNKNOWN
}
private fun decodeStatus(str: String): ReleaseStatus = when(str) {
"anons" -> ReleaseStatus.ANNOUNCED
"ongoing" -> ReleaseStatus.RELEASING
"released" -> ReleaseStatus.FINISHED
"paused" -> ReleaseStatus.PAUSED
"discontinued" -> ReleaseStatus.DISCOUNTED
else -> ReleaseStatus.UNKNOWN
}

private fun currentSeason(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import live.shirabox.core.entity.EpisodeEntity
import live.shirabox.core.model.Content
import live.shirabox.core.model.ContentKind
import live.shirabox.core.model.ContentType
import live.shirabox.core.model.Quality
import live.shirabox.data.content.AbstractContentRepository
Expand Down Expand Up @@ -41,7 +42,7 @@ class LibriaRepository : AbstractContentRepository(

private suspend fun advancedSearch(content: Content): List<EpisodeEntity> {
val altNamesListQuery = "(${content.altNames.joinToString { "\"${it}\"" }})"
if(libriaKind(content.kind) == null) return emptyList()
if (libriaKind(content.kind) == null) return emptyList()

try {
val response = "$API_ENDPOINT/v3/title/search/advanced"
Expand Down Expand Up @@ -91,14 +92,12 @@ class LibriaRepository : AbstractContentRepository(
)
}

private fun libriaKind(kind: String): Int? {
return when(kind) {
"Фильм" -> 0
"Сериал" -> 1
"OVA" -> 2
"ONA" -> 3
"Спешл" -> 4
else -> null
}
private fun libriaKind(kind: ContentKind) : Int? = when(kind) {
ContentKind.MOVIE -> 0
ContentKind.TV -> 1
ContentKind.OVA -> 2
ContentKind.ONA -> 3
ContentKind.SPECIAL -> 4
else -> null
}
}

0 comments on commit 5d81f31

Please sign in to comment.