mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
enhance: Rework ViewModel structure
This commit is contained in:
parent
534642ea57
commit
067621cd51
3 changed files with 54 additions and 32 deletions
7
app/src/main/java/dev/yokai/domain/Result.kt
Normal file
7
app/src/main/java/dev/yokai/domain/Result.kt
Normal file
|
@ -0,0 +1,7 @@
|
|||
package dev.yokai.domain
|
||||
|
||||
sealed class Result<out T> {
|
||||
data class Success<out T>(val data: T? = null) : Result<T>()
|
||||
data class Error(val message: String? = null) : Result<Nothing>()
|
||||
data object Loading : Result<Nothing>()
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package dev.yokai.domain.extension.repo
|
||||
|
||||
import dev.yokai.domain.source.SourcePreferences
|
||||
import dev.yokai.domain.Result
|
||||
import eu.kanade.tachiyomi.data.preference.minusAssign
|
||||
import eu.kanade.tachiyomi.data.preference.plusAssign
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class ExtensionRepoRepository(private val sourcePreferences: SourcePreferences) {
|
||||
fun addRepo(url: String): Result<Nothing> {
|
||||
if (!url.matches(repoRegex))
|
||||
return Result.Error("Invalid URL")
|
||||
|
||||
sourcePreferences.extensionRepos() += url.substringBeforeLast("/index.min.json")
|
||||
|
||||
return Result.Success()
|
||||
}
|
||||
|
||||
fun deleteRepo(repo: String) {
|
||||
sourcePreferences.extensionRepos() -= repo
|
||||
}
|
||||
|
||||
fun getRepo() =
|
||||
sourcePreferences.extensionRepos().changes()
|
||||
.map { it.sortedWith(String.CASE_INSENSITIVE_ORDER) }
|
||||
}
|
||||
|
||||
private val repoRegex = """^https://.*/index\.min\.json$""".toRegex()
|
|
@ -3,55 +3,44 @@ package dev.yokai.presentation.extension.repo
|
|||
import androidx.compose.runtime.Immutable
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dev.yokai.domain.source.SourcePreferences
|
||||
import eu.kanade.tachiyomi.data.preference.minusAssign
|
||||
import eu.kanade.tachiyomi.data.preference.plusAssign
|
||||
import dev.yokai.domain.Result
|
||||
import dev.yokai.domain.extension.repo.ExtensionRepoRepository
|
||||
import eu.kanade.tachiyomi.util.system.launchIO
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import okhttp3.internal.toImmutableList
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
|
||||
class ExtensionRepoViewModel :
|
||||
ViewModel() {
|
||||
|
||||
private val sourcePreferences: SourcePreferences by injectLazy()
|
||||
private val repository = ExtensionRepoRepository(Injekt.get())
|
||||
private val _repoState: MutableStateFlow<ExtensionRepoState> = MutableStateFlow(ExtensionRepoState.Loading)
|
||||
val repoState: StateFlow<ExtensionRepoState> = _repoState.asStateFlow()
|
||||
|
||||
init {
|
||||
refresh()
|
||||
}
|
||||
|
||||
fun addRepo(url: String) {
|
||||
viewModelScope.launchIO {
|
||||
getRepo().collectLatest { repos ->
|
||||
val result = repository.addRepo(url)
|
||||
if (result is Result.Error) return@launchIO
|
||||
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
|
||||
fun refresh() {
|
||||
viewModelScope.launchIO {
|
||||
repository.getRepo().collectLatest { repos ->
|
||||
_repoState.value = ExtensionRepoState.Success(repos = repos.toImmutableList())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fun addRepo(url: String): Result {
|
||||
viewModelScope.launchIO {
|
||||
if (!url.matches(repoRegex))
|
||||
return Result.InvalidUrl
|
||||
|
||||
sourcePreferences.extensionRepos() += url.substringBeforeLast("/index.min.json")
|
||||
|
||||
return Result.Success
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
fun deleteRepo(repo: String) {
|
||||
viewModelScope.launchIO {
|
||||
sourcePreferences.extensionRepos() -= repo
|
||||
}
|
||||
}
|
||||
|
||||
fun getRepo() =
|
||||
sourcePreferences.extensionRepos().changes()
|
||||
.map { it.sortedWith(String.CASE_INSENSITIVE_ORDER) }
|
||||
}
|
||||
|
||||
sealed class ExtensionRepoState {
|
||||
|
@ -68,5 +57,3 @@ sealed class ExtensionRepoState {
|
|||
get() = repos.isEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
private val repoRegex = """^https://.*/index\.min\.json$""".toRegex()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue