|
|
|
|
@ -11,13 +11,14 @@ import org.koitharu.kotatsu.R
|
|
|
|
|
import org.koitharu.kotatsu.core.model.MangaSource
|
|
|
|
|
import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar
|
|
|
|
|
import org.koitharu.kotatsu.core.parser.MangaRepository
|
|
|
|
|
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
|
|
|
|
import org.koitharu.kotatsu.core.parser.ParserMangaRepository
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.SourceSettings
|
|
|
|
|
import org.koitharu.kotatsu.core.ui.BaseViewModel
|
|
|
|
|
import org.koitharu.kotatsu.core.ui.util.ReversibleAction
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.MutableEventFlow
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.call
|
|
|
|
|
import org.koitharu.kotatsu.explore.data.MangaSourcesRepository
|
|
|
|
|
import org.koitharu.kotatsu.parsers.MangaParserAuthProvider
|
|
|
|
|
import org.koitharu.kotatsu.parsers.exception.AuthRequiredException
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
@ -30,7 +31,7 @@ class SourceSettingsViewModel @Inject constructor(
|
|
|
|
|
) : BaseViewModel(), SharedPreferences.OnSharedPreferenceChangeListener {
|
|
|
|
|
|
|
|
|
|
val source = MangaSource(savedStateHandle.get<String>(SourceSettingsFragment.EXTRA_SOURCE))
|
|
|
|
|
val repository = mangaRepositoryFactory.create(source) as RemoteMangaRepository
|
|
|
|
|
val repository = mangaRepositoryFactory.create(source)
|
|
|
|
|
|
|
|
|
|
val onActionDone = MutableEventFlow<ReversibleAction>()
|
|
|
|
|
val username = MutableStateFlow<String?>(null)
|
|
|
|
|
@ -38,28 +39,41 @@ class SourceSettingsViewModel @Inject constructor(
|
|
|
|
|
private var usernameLoadJob: Job? = null
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
|
repository.getConfig().subscribe(this)
|
|
|
|
|
loadUsername()
|
|
|
|
|
when (repository) {
|
|
|
|
|
is ParserMangaRepository -> {
|
|
|
|
|
repository.getConfig().subscribe(this)
|
|
|
|
|
loadUsername(repository.getAuthProvider())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onCleared() {
|
|
|
|
|
repository.getConfig().unsubscribe(this)
|
|
|
|
|
when (repository) {
|
|
|
|
|
is ParserMangaRepository -> {
|
|
|
|
|
repository.getConfig().unsubscribe(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
super.onCleared()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
|
|
|
|
if (key != SourceSettings.KEY_SLOWDOWN && key != SourceSettings.KEY_SORT_ORDER) {
|
|
|
|
|
repository.invalidateCache()
|
|
|
|
|
when (repository) {
|
|
|
|
|
is ParserMangaRepository -> {
|
|
|
|
|
if (key != SourceSettings.KEY_SLOWDOWN && key != SourceSettings.KEY_SORT_ORDER) {
|
|
|
|
|
repository.invalidateCache()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun onResume() {
|
|
|
|
|
if (usernameLoadJob?.isActive != true) {
|
|
|
|
|
loadUsername()
|
|
|
|
|
if (usernameLoadJob?.isActive != true && repository is ParserMangaRepository) {
|
|
|
|
|
loadUsername(repository.getAuthProvider())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun clearCookies() {
|
|
|
|
|
if (repository !is ParserMangaRepository) return
|
|
|
|
|
launchLoadingJob(Dispatchers.Default) {
|
|
|
|
|
val url = HttpUrl.Builder()
|
|
|
|
|
.scheme("https")
|
|
|
|
|
@ -67,7 +81,7 @@ class SourceSettingsViewModel @Inject constructor(
|
|
|
|
|
.build()
|
|
|
|
|
cookieJar.removeCookies(url, null)
|
|
|
|
|
onActionDone.call(ReversibleAction(R.string.cookies_cleared, null))
|
|
|
|
|
loadUsername()
|
|
|
|
|
loadUsername(repository.getAuthProvider())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -77,11 +91,11 @@ class SourceSettingsViewModel @Inject constructor(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun loadUsername() {
|
|
|
|
|
private fun loadUsername(authProvider: MangaParserAuthProvider?) {
|
|
|
|
|
launchLoadingJob(Dispatchers.Default) {
|
|
|
|
|
try {
|
|
|
|
|
username.value = null
|
|
|
|
|
username.value = repository.getAuthProvider()?.getUsername()
|
|
|
|
|
username.value = authProvider?.getUsername()
|
|
|
|
|
} catch (_: AuthRequiredException) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|