|
|
|
|
@ -4,16 +4,15 @@ import android.os.Bundle
|
|
|
|
|
import android.view.View
|
|
|
|
|
import androidx.fragment.app.viewModels
|
|
|
|
|
import androidx.preference.Preference
|
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
|
import org.koitharu.kotatsu.core.exceptions.resolve.ExceptionResolver
|
|
|
|
|
import org.koitharu.kotatsu.core.exceptions.resolve.SnackbarErrorObserver
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
|
import org.koitharu.kotatsu.core.ui.BasePreferenceFragment
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.getDisplayMessage
|
|
|
|
|
import org.koitharu.kotatsu.core.ui.util.ReversibleActionObserver
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.observe
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.observeEvent
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.viewLifecycleScope
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.withArgs
|
|
|
|
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
|
|
|
import org.koitharu.kotatsu.settings.sources.auth.SourceAuthActivity
|
|
|
|
|
@ -49,10 +48,18 @@ class SourceSettingsFragment : BasePreferenceFragment(0) {
|
|
|
|
|
getString(R.string.logged_in_as, it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
viewModel.onError.observeEvent(viewLifecycleOwner, ::onError)
|
|
|
|
|
viewModel.onError.observeEvent(
|
|
|
|
|
viewLifecycleOwner,
|
|
|
|
|
SnackbarErrorObserver(
|
|
|
|
|
listView,
|
|
|
|
|
this,
|
|
|
|
|
exceptionResolver,
|
|
|
|
|
) { viewModel.onResume() },
|
|
|
|
|
)
|
|
|
|
|
viewModel.isLoading.observe(viewLifecycleOwner) { isLoading ->
|
|
|
|
|
findPreference<Preference>(KEY_AUTH)?.isEnabled = !isLoading
|
|
|
|
|
}
|
|
|
|
|
viewModel.onActionDone.observeEvent(viewLifecycleOwner, ReversibleActionObserver(listView))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onPreferenceTreeClick(preference: Preference): Boolean {
|
|
|
|
|
@ -61,32 +68,15 @@ class SourceSettingsFragment : BasePreferenceFragment(0) {
|
|
|
|
|
startActivity(SourceAuthActivity.newIntent(preference.context, viewModel.source))
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
AppSettings.KEY_COOKIES_CLEAR -> {
|
|
|
|
|
viewModel.clearCookies()
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else -> super.onPreferenceTreeClick(preference)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun onError(error: Throwable) {
|
|
|
|
|
val snackbar = Snackbar.make(
|
|
|
|
|
listView ?: return,
|
|
|
|
|
error.getDisplayMessage(resources),
|
|
|
|
|
Snackbar.LENGTH_INDEFINITE,
|
|
|
|
|
)
|
|
|
|
|
if (ExceptionResolver.canResolve(error)) {
|
|
|
|
|
snackbar.setAction(ExceptionResolver.getResolveStringId(error)) { resolveError(error) }
|
|
|
|
|
}
|
|
|
|
|
snackbar.show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun resolveError(error: Throwable) {
|
|
|
|
|
view ?: return
|
|
|
|
|
viewLifecycleScope.launch {
|
|
|
|
|
if (exceptionResolver.resolve(error)) {
|
|
|
|
|
viewModel.onResume()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
|
|
private const val KEY_AUTH = "auth"
|
|
|
|
|
|