Reorganize settings
parent
2acbff487e
commit
ea9ae2263c
@ -0,0 +1,150 @@
|
||||
package org.koitharu.kotatsu.settings
|
||||
|
||||
import android.accounts.AccountManager
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import androidx.preference.Preference
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.scrobbling.anilist.data.AniListRepository
|
||||
import org.koitharu.kotatsu.scrobbling.common.domain.model.ScrobblerService
|
||||
import org.koitharu.kotatsu.scrobbling.common.ui.config.ScrobblerConfigActivity
|
||||
import org.koitharu.kotatsu.scrobbling.mal.data.MALRepository
|
||||
import org.koitharu.kotatsu.scrobbling.shikimori.data.ShikimoriRepository
|
||||
import org.koitharu.kotatsu.sync.ui.SyncSettingsIntent
|
||||
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.viewLifecycleScope
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class ServicesSettingsFragment : BasePreferenceFragment(R.string.services) {
|
||||
|
||||
@Inject
|
||||
lateinit var shikimoriRepository: ShikimoriRepository
|
||||
|
||||
@Inject
|
||||
lateinit var aniListRepository: AniListRepository
|
||||
|
||||
@Inject
|
||||
lateinit var malRepository: MALRepository
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
addPreferencesFromResource(R.xml.pref_services)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
bindScrobblerSummary(AppSettings.KEY_SHIKIMORI, shikimoriRepository)
|
||||
bindScrobblerSummary(AppSettings.KEY_ANILIST, aniListRepository)
|
||||
bindScrobblerSummary(AppSettings.KEY_MAL, malRepository)
|
||||
bindSyncSummary()
|
||||
}
|
||||
|
||||
override fun onPreferenceTreeClick(preference: Preference): Boolean {
|
||||
return when (preference.key) {
|
||||
AppSettings.KEY_SHIKIMORI -> {
|
||||
if (!shikimoriRepository.isAuthorized) {
|
||||
launchScrobblerAuth(shikimoriRepository)
|
||||
} else {
|
||||
startActivity(ScrobblerConfigActivity.newIntent(preference.context, ScrobblerService.SHIKIMORI))
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
AppSettings.KEY_MAL -> {
|
||||
if (!malRepository.isAuthorized) {
|
||||
launchScrobblerAuth(malRepository)
|
||||
} else {
|
||||
startActivity(ScrobblerConfigActivity.newIntent(preference.context, ScrobblerService.MAL))
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
AppSettings.KEY_ANILIST -> {
|
||||
if (!aniListRepository.isAuthorized) {
|
||||
launchScrobblerAuth(aniListRepository)
|
||||
} else {
|
||||
startActivity(ScrobblerConfigActivity.newIntent(preference.context, ScrobblerService.ANILIST))
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
AppSettings.KEY_SYNC -> {
|
||||
val am = AccountManager.get(requireContext())
|
||||
val accountType = getString(R.string.account_type_sync)
|
||||
val account = am.getAccountsByType(accountType).firstOrNull()
|
||||
if (account == null) {
|
||||
am.addAccount(accountType, accountType, null, null, requireActivity(), null, null)
|
||||
} else {
|
||||
try {
|
||||
startActivity(SyncSettingsIntent(account))
|
||||
} catch (_: ActivityNotFoundException) {
|
||||
Snackbar.make(listView, R.string.operation_not_supported, Snackbar.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
else -> super.onPreferenceTreeClick(preference)
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindScrobblerSummary(
|
||||
key: String,
|
||||
repository: org.koitharu.kotatsu.scrobbling.common.data.ScrobblerRepository
|
||||
) {
|
||||
val pref = findPreference<Preference>(key) ?: return
|
||||
if (!repository.isAuthorized) {
|
||||
pref.setSummary(R.string.disabled)
|
||||
return
|
||||
}
|
||||
val username = repository.cachedUser?.nickname
|
||||
if (username != null) {
|
||||
pref.summary = getString(R.string.logged_in_as, username)
|
||||
} else {
|
||||
pref.setSummary(R.string.loading_)
|
||||
viewLifecycleScope.launch {
|
||||
pref.summary = withContext(Dispatchers.Default) {
|
||||
runCatching {
|
||||
val user = repository.loadUser()
|
||||
getString(R.string.logged_in_as, user.nickname)
|
||||
}.getOrElse {
|
||||
it.printStackTraceDebug()
|
||||
it.getDisplayMessage(resources)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun launchScrobblerAuth(repository: org.koitharu.kotatsu.scrobbling.common.data.ScrobblerRepository) {
|
||||
runCatching {
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
intent.data = Uri.parse(repository.oauthUrl)
|
||||
startActivity(intent)
|
||||
}.onFailure {
|
||||
Snackbar.make(listView, it.getDisplayMessage(resources), Snackbar.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindSyncSummary() {
|
||||
viewLifecycleScope.launch {
|
||||
val account = withContext(Dispatchers.Default) {
|
||||
val type = getString(R.string.account_type_sync)
|
||||
AccountManager.get(requireContext()).getAccountsByType(type).firstOrNull()
|
||||
}
|
||||
findPreference<Preference>(AppSettings.KEY_SYNC)?.run {
|
||||
summary = account?.name ?: getString(R.string.sync_title)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
<vector
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="#000000"
|
||||
android:pathData="M20 8H17V6C17 4.9 16.1 4 15 4H9C7.9 4 7 4.9 7 6V8H4C2.9 8 2 8.9 2 10V20H22V10C22 8.9 21.1 8 20 8M9 6H15V8H9V6M20 18H4V15H6V16H8V15H16V16H18V15H20V18M18 13V12H16V13H8V12H6V13H4V10H20V13H18Z" />
|
||||
</vector>
|
||||
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<Preference
|
||||
android:key="sync"
|
||||
android:persistent="false"
|
||||
android:summary="@string/sync_title"
|
||||
android:title="@string/sync" />
|
||||
|
||||
<PreferenceCategory android:title="@string/tracking">
|
||||
|
||||
<Preference
|
||||
android:key="shikimori"
|
||||
android:title="@string/shikimori"
|
||||
app:icon="@drawable/ic_shikimori" />
|
||||
|
||||
<Preference
|
||||
android:key="anilist"
|
||||
android:title="@string/anilist"
|
||||
app:icon="@drawable/ic_anilist" />
|
||||
|
||||
<Preference
|
||||
android:key="mal"
|
||||
android:title="@string/mal"
|
||||
app:icon="@drawable/ic_mal" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
Loading…
Reference in New Issue