|
|
|
@ -9,7 +9,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
import kotlinx.coroutines.CancellationException
|
|
|
|
import kotlinx.coroutines.CancellationException
|
|
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
|
|
|
|
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
|
|
|
|
import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar
|
|
|
|
import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar
|
|
|
|
@ -18,11 +20,13 @@ import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
import org.koitharu.kotatsu.local.data.CacheDir
|
|
|
|
import org.koitharu.kotatsu.local.data.CacheDir
|
|
|
|
import org.koitharu.kotatsu.local.data.LocalStorageManager
|
|
|
|
import org.koitharu.kotatsu.local.data.LocalStorageManager
|
|
|
|
import org.koitharu.kotatsu.scrobbling.anilist.data.AniListRepository
|
|
|
|
import org.koitharu.kotatsu.scrobbling.anilist.data.AniListRepository
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.scrobbling.data.ScrobblerRepository
|
|
|
|
import org.koitharu.kotatsu.scrobbling.shikimori.data.ShikimoriRepository
|
|
|
|
import org.koitharu.kotatsu.scrobbling.shikimori.data.ShikimoriRepository
|
|
|
|
import org.koitharu.kotatsu.search.domain.MangaSearchRepository
|
|
|
|
import org.koitharu.kotatsu.search.domain.MangaSearchRepository
|
|
|
|
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
|
|
|
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
|
|
|
import org.koitharu.kotatsu.utils.FileSize
|
|
|
|
import org.koitharu.kotatsu.utils.FileSize
|
|
|
|
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
|
|
|
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
|
|
|
import org.koitharu.kotatsu.utils.ext.viewLifecycleScope
|
|
|
|
import org.koitharu.kotatsu.utils.ext.viewLifecycleScope
|
|
|
|
import javax.inject.Inject
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
|
|
@ -78,8 +82,8 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
|
|
|
|
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
super.onResume()
|
|
|
|
bindShikimoriSummary()
|
|
|
|
bindScrobblerSummary(AppSettings.KEY_SHIKIMORI, shikimoriRepository)
|
|
|
|
bindAniListSummary()
|
|
|
|
bindScrobblerSummary(AppSettings.KEY_ANILIST, aniListRepository)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onPreferenceTreeClick(preference: Preference): Boolean {
|
|
|
|
override fun onPreferenceTreeClick(preference: Preference): Boolean {
|
|
|
|
@ -120,7 +124,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
|
|
|
|
|
|
|
|
|
|
|
|
AppSettings.KEY_SHIKIMORI -> {
|
|
|
|
AppSettings.KEY_SHIKIMORI -> {
|
|
|
|
if (!shikimoriRepository.isAuthorized) {
|
|
|
|
if (!shikimoriRepository.isAuthorized) {
|
|
|
|
launchShikimoriAuth()
|
|
|
|
launchScrobblerAuth(shikimoriRepository)
|
|
|
|
true
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
super.onPreferenceTreeClick(preference)
|
|
|
|
super.onPreferenceTreeClick(preference)
|
|
|
|
@ -129,7 +133,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
|
|
|
|
|
|
|
|
|
|
|
|
AppSettings.KEY_ANILIST -> {
|
|
|
|
AppSettings.KEY_ANILIST -> {
|
|
|
|
if (!aniListRepository.isAuthorized) {
|
|
|
|
if (!aniListRepository.isAuthorized) {
|
|
|
|
launchAniListAuth()
|
|
|
|
launchScrobblerAuth(aniListRepository)
|
|
|
|
true
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
super.onPreferenceTreeClick(preference)
|
|
|
|
super.onPreferenceTreeClick(preference)
|
|
|
|
@ -199,36 +203,34 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
|
|
|
|
}.show()
|
|
|
|
}.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun bindShikimoriSummary() {
|
|
|
|
private fun bindScrobblerSummary(key: String, repository: ScrobblerRepository) {
|
|
|
|
findPreference<Preference>(AppSettings.KEY_SHIKIMORI)?.summary = if (shikimoriRepository.isAuthorized) {
|
|
|
|
val pref = findPreference<Preference>(key) ?: return
|
|
|
|
getString(R.string.logged_in_as, shikimoriRepository.cachedUser?.nickname)
|
|
|
|
if (!repository.isAuthorized) {
|
|
|
|
} else {
|
|
|
|
pref.setSummary(R.string.disabled)
|
|
|
|
getString(R.string.disabled)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val username = repository.cachedUser?.nickname
|
|
|
|
private fun bindAniListSummary() {
|
|
|
|
if (username != null) {
|
|
|
|
findPreference<Preference>(AppSettings.KEY_ANILIST)?.summary = if (aniListRepository.isAuthorized) {
|
|
|
|
pref.summary = getString(R.string.logged_in_as, username)
|
|
|
|
getString(R.string.logged_in_as, aniListRepository.cachedUser?.nickname)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
getString(R.string.disabled)
|
|
|
|
pref.setSummary(R.string.loading_)
|
|
|
|
|
|
|
|
viewLifecycleScope.launch {
|
|
|
|
|
|
|
|
pref.summary = withContext(Dispatchers.Default) {
|
|
|
|
|
|
|
|
runCatching {
|
|
|
|
|
|
|
|
repository.loadUser().nickname
|
|
|
|
|
|
|
|
}.getOrElse {
|
|
|
|
|
|
|
|
it.printStackTraceDebug()
|
|
|
|
|
|
|
|
it.getDisplayMessage(resources)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun launchShikimoriAuth() {
|
|
|
|
|
|
|
|
runCatching {
|
|
|
|
|
|
|
|
val intent = Intent(Intent.ACTION_VIEW)
|
|
|
|
|
|
|
|
intent.data = Uri.parse(shikimoriRepository.oauthUrl)
|
|
|
|
|
|
|
|
startActivity(intent)
|
|
|
|
|
|
|
|
}.onFailure {
|
|
|
|
|
|
|
|
Snackbar.make(listView, it.getDisplayMessage(resources), Snackbar.LENGTH_LONG).show()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun launchAniListAuth() {
|
|
|
|
private fun launchScrobblerAuth(repository: ScrobblerRepository) {
|
|
|
|
runCatching {
|
|
|
|
runCatching {
|
|
|
|
val intent = Intent(Intent.ACTION_VIEW)
|
|
|
|
val intent = Intent(Intent.ACTION_VIEW)
|
|
|
|
intent.data = Uri.parse(aniListRepository.oauthUrl)
|
|
|
|
intent.data = Uri.parse(repository.oauthUrl)
|
|
|
|
startActivity(intent)
|
|
|
|
startActivity(intent)
|
|
|
|
}.onFailure {
|
|
|
|
}.onFailure {
|
|
|
|
Snackbar.make(listView, it.getDisplayMessage(resources), Snackbar.LENGTH_LONG).show()
|
|
|
|
Snackbar.make(listView, it.getDisplayMessage(resources), Snackbar.LENGTH_LONG).show()
|
|
|
|
|