Skip to content

Commit

Permalink
sync with bs
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Jul 11, 2024
1 parent 99e1fa5 commit 3274d21
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,57 +54,41 @@ actual class BurningSeriesResolver(
actual val isAvailable: Boolean
get() = packageInfo != null

actual fun resolveWatchedEpisodes(): ImmutableSet<Episode> {
actual fun resolveWatchedEpisode(seriesHref: String): Int? = scopeCatching {
if (episodeClient == null) {
return persistentSetOf()
return@scopeCatching null
}

val episodeCursor = episodeClient.query(
episodesContentUri,
arrayOf("MAX(number) as number"),
"finished = 1 AND seriesHref = '$seriesHref'",
null,
"progress > 0 AND length > 0",
null,
null
) ?: return persistentSetOf()

val episodes = mutableSetOf<Episode>()
"number DESC"
) ?: return@scopeCatching null

var max: Int? = null
if (episodeCursor.moveToFirst()) {
while (!episodeCursor.isAfterLast) {
val progressIndex = episodeCursor.getColumnIndex("progress")
val lengthIndex = episodeCursor.getColumnIndex("length")
val numberIndex = episodeCursor.getColumnIndex("number")
val seriesHrefIndex = episodeCursor.getColumnIndex("seriesHref")

if (progressIndex == -1 || lengthIndex == -1 || numberIndex == -1 || seriesHrefIndex == -1) {
if (numberIndex == -1) {
episodeCursor.moveToNext()
continue
}

val progress = episodeCursor.getLong(progressIndex)
val length = episodeCursor.getLong(lengthIndex)
val number = episodeCursor.getString(numberIndex)
val seriesHref = episodeCursor.getString(seriesHrefIndex)

episodes.add(
Episode(
progress = progress,
length = length,
number = number,
series = Series(
title = seriesHref,
href = seriesHref
)
)
)
val number = episodeCursor.getInt(numberIndex)
if (max == null || number > max) {
max = number
}

episodeCursor.moveToNext()
}
}

episodeCursor.close()
return episodes.toImmutableSet()
}
return@scopeCatching max
}.getOrNull()

actual fun resolveByName(english: String?, romaji: String?): ImmutableSet<Series> {
val englishTrimmed = english?.trim()?.ifBlank { null }?.replace("'", "")?.trim()
Expand All @@ -115,29 +99,29 @@ actual class BurningSeriesResolver(
}

val selection = if (englishTrimmed != null && romajiTrimmed != null) {
"fullTitle LIKE '%$englishTrimmed%' OR fullTitle LIKE '%$romajiTrimmed%'"
"isAnime = 1 AND fullTitle LIKE '%$englishTrimmed%' OR fullTitle LIKE '%$romajiTrimmed%'"
} else if (englishTrimmed != null) {
"fullTitle LIKE '%$englishTrimmed%'"
"isAnime = 1 AND fullTitle LIKE '%$englishTrimmed%'"
} else {
"fullTitle LIKE '%$romajiTrimmed%'"
"isAnime = 1 AND fullTitle LIKE '%$romajiTrimmed%'"
}

return seriesBySelection(selection).ifEmpty {
val mainTitleSelection = if (englishTrimmed != null && romajiTrimmed != null) {
"mainTitle LIKE '%$englishTrimmed%' OR mainTitle LIKE '%$romajiTrimmed%'"
"isAnime = 1 AND mainTitle LIKE '%$englishTrimmed%' OR mainTitle LIKE '%$romajiTrimmed%'"
} else if (englishTrimmed != null) {
"mainTitle LIKE '%$englishTrimmed%'"
"isAnime = 1 AND mainTitle LIKE '%$englishTrimmed%'"
} else {
"mainTitle LIKE '%$romajiTrimmed%'"
"isAnime = 1 AND mainTitle LIKE '%$romajiTrimmed%'"
}

seriesBySelection(mainTitleSelection).ifEmpty {
val subTitleSelection = if (englishTrimmed != null && romajiTrimmed != null) {
"subTitle LIKE '%$englishTrimmed%' OR subTitle LIKE '%$romajiTrimmed%'"
"isAnime = 1 AND subTitle LIKE '%$englishTrimmed%' OR subTitle LIKE '%$romajiTrimmed%'"
} else if (englishTrimmed != null) {
"subTitle LIKE '%$englishTrimmed%'"
"isAnime = 1 AND subTitle LIKE '%$englishTrimmed%'"
} else {
"subTitle LIKE '%$romajiTrimmed%'"
"isAnime = 1 AND subTitle LIKE '%$romajiTrimmed%'"
}

seriesBySelection(subTitleSelection)
Expand All @@ -149,7 +133,7 @@ actual class BurningSeriesResolver(
val trimmed = value.trim().replace("'", "").trim()

return if (trimmed.length >= 3) {
seriesBySelection("fullTitle LIKE '%$trimmed%'")
seriesBySelection("isAnime = 1 AND fullTitle LIKE '%$trimmed%'")
} else {
persistentSetOf()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ expect class BurningSeriesResolver {
val versionCode: Int
val versionName: String?

fun resolveWatchedEpisodes(): ImmutableSet<Episode>
fun resolveWatchedEpisode(seriesHref: String): Int?
fun resolveByName(english: String?, romaji: String?): ImmutableSet<Series>
fun resolveByName(value: String): ImmutableSet<Series>

fun close()
}

data class Episode(
val progress: Long,
val length: Long,
val number: String,
val series: Series
)

data class Series(
val title: String,
val href: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ sealed class DialogConfig {
) : DialogConfig()

@Serializable
data object Edit : DialogConfig()
data class Edit(
val watched: Int? = null
) : DialogConfig()
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ interface MediumComponent : ContentHolderComponent {
fun edit()

suspend fun searchBS(value: String)
fun selectBS(series: Series)
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class MediumScreenComponent(
componentContext = context,
di = di,
episodesOrChapters = episodesOrChapters,
progress = watchProgress,
progress = config.watched?.let(::flowOf) ?: watchProgress,
listStatus = listStatus,
repeatCount = watchRepeat,
episodeStartDate = mediumSuccessState.mapLatest {
Expand Down Expand Up @@ -374,10 +374,16 @@ class MediumScreenComponent(
}

override fun edit() {
dialogNavigation.activate(DialogConfig.Edit)
dialogNavigation.activate(DialogConfig.Edit())
}

override suspend fun searchBS(value: String) {
bsSearch.update { value }
}

override fun selectBS(series: Series) {
val max = burningSeriesResolver.resolveWatchedEpisode(series.href)

dialogNavigation.activate(DialogConfig.Edit(max))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ fun BSDialog(
bsVersionCode: Int,
bsVersionName: String?,
bsOptions: ImmutableCollection<Series>,
onSearch: suspend (String) -> Unit
onSearch: suspend (String) -> Unit,
onSelect: (Series) -> Unit
) {
OptionDialog(
state = state,
Expand All @@ -45,7 +46,7 @@ fun BSDialog(
)
},
onSelectOption = { option, _ ->

onSelect(bsOptions.toList()[option])
}
),
header = Header.Default(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ fun FABContent(
bsVersionCode = component.bsVersionCode,
bsVersionName = component.bsVersionName,
bsOptions = bsOptions,
onSearch = component::searchBS
onSearch = component::searchBS,
onSelect = component::selectBS
)

RatingDialog(
Expand Down

0 comments on commit 3274d21

Please sign in to comment.