mirror of https://github.com/tasks/tasks
Move sync accounts to top level prefs menu
Remove synchronization submenupull/1305/head
parent
b8005daa80
commit
64e845a6fe
@ -1,13 +0,0 @@
|
|||||||
package org.tasks.preferences
|
|
||||||
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import org.tasks.R
|
|
||||||
import org.tasks.preferences.fragments.Synchronization
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class SyncPreferences : BasePreferences() {
|
|
||||||
|
|
||||||
override fun getRootTitle() = R.string.synchronization
|
|
||||||
|
|
||||||
override fun getRootPreference() = Synchronization()
|
|
||||||
}
|
|
||||||
@ -1,190 +0,0 @@
|
|||||||
package org.tasks.preferences.fragments
|
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Bundle
|
|
||||||
import androidx.fragment.app.activityViewModels
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import androidx.preference.Preference
|
|
||||||
import androidx.preference.PreferenceCategory
|
|
||||||
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity
|
|
||||||
import com.todoroo.astrid.service.TaskDeleter
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.NonCancellable
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import org.tasks.R
|
|
||||||
import org.tasks.Strings.isNullOrEmpty
|
|
||||||
import org.tasks.caldav.BaseCaldavAccountSettingsActivity
|
|
||||||
import org.tasks.caldav.CaldavAccountSettingsActivity
|
|
||||||
import org.tasks.data.CaldavAccount.Companion.TYPE_LOCAL
|
|
||||||
import org.tasks.data.CaldavAccount.Companion.TYPE_TASKS
|
|
||||||
import org.tasks.data.CaldavDao
|
|
||||||
import org.tasks.data.GoogleTaskAccount
|
|
||||||
import org.tasks.data.GoogleTaskListDao
|
|
||||||
import org.tasks.data.OpenTaskDao.Companion.isDavx5
|
|
||||||
import org.tasks.data.OpenTaskDao.Companion.isDecSync
|
|
||||||
import org.tasks.data.OpenTaskDao.Companion.isEteSync
|
|
||||||
import org.tasks.etebase.EtebaseAccountSettingsActivity
|
|
||||||
import org.tasks.etesync.EteSyncAccountSettingsActivity
|
|
||||||
import org.tasks.injection.InjectingPreferenceFragment
|
|
||||||
import org.tasks.opentasks.OpenTaskAccountSettingsActivity
|
|
||||||
import org.tasks.preferences.Preferences
|
|
||||||
import org.tasks.preferences.PreferencesViewModel
|
|
||||||
import org.tasks.sync.AddAccountDialog.Companion.newAccountDialog
|
|
||||||
import org.tasks.sync.SyncAdapters
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class Synchronization : InjectingPreferenceFragment() {
|
|
||||||
|
|
||||||
@Inject lateinit var preferences: Preferences
|
|
||||||
@Inject lateinit var caldavDao: CaldavDao
|
|
||||||
@Inject lateinit var googleTaskListDao: GoogleTaskListDao
|
|
||||||
@Inject lateinit var taskDeleter: TaskDeleter
|
|
||||||
@Inject lateinit var syncAdapters: SyncAdapters
|
|
||||||
|
|
||||||
private val viewModel: PreferencesViewModel by activityViewModels()
|
|
||||||
|
|
||||||
override fun getPreferenceXml() = R.xml.preferences_synchronization
|
|
||||||
|
|
||||||
override suspend fun setupPreferences(savedInstanceState: Bundle?) {
|
|
||||||
findPreference(R.string.add_account)
|
|
||||||
.setOnPreferenceClickListener {
|
|
||||||
lifecycleScope.launch {
|
|
||||||
val hasTasksAccount = viewModel.tasksAccount() != null
|
|
||||||
newAccountDialog(this@Synchronization, REQUEST_ADD_ACCOUNT, hasTasksAccount)
|
|
||||||
.show(parentFragmentManager, FRAG_TAG_ADD_ACCOUNT)
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
||||||
if (requestCode == REQUEST_ADD_ACCOUNT) {
|
|
||||||
refresh()
|
|
||||||
} else {
|
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onResume() {
|
|
||||||
super.onResume()
|
|
||||||
|
|
||||||
refresh()
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun addGoogleTasksAccounts(category: PreferenceCategory): Boolean {
|
|
||||||
val accounts: List<GoogleTaskAccount> = googleTaskListDao.getAccounts()
|
|
||||||
for (googleTaskAccount in accounts) {
|
|
||||||
val account = googleTaskAccount.account
|
|
||||||
val preference = Preference(context)
|
|
||||||
preference.title = account
|
|
||||||
val error = googleTaskAccount.error
|
|
||||||
if (isNullOrEmpty(error)) {
|
|
||||||
preference.setSummary(R.string.gtasks_GPr_header)
|
|
||||||
} else {
|
|
||||||
preference.summary = error
|
|
||||||
}
|
|
||||||
preference.setOnPreferenceClickListener {
|
|
||||||
dialogBuilder
|
|
||||||
.newDialog(account)
|
|
||||||
.setItems(
|
|
||||||
listOf(
|
|
||||||
getString(R.string.reinitialize_account),
|
|
||||||
getString(R.string.logout)
|
|
||||||
)
|
|
||||||
) { _, which ->
|
|
||||||
if (which == 0) {
|
|
||||||
startActivityForResult(
|
|
||||||
Intent(context, GtasksLoginActivity::class.java),
|
|
||||||
REQUEST_GOOGLE_TASKS
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
logoutConfirmation(googleTaskAccount)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.setNegativeButton(R.string.cancel, null)
|
|
||||||
.show()
|
|
||||||
false
|
|
||||||
}
|
|
||||||
category.addPreference(preference)
|
|
||||||
}
|
|
||||||
return accounts.isNotEmpty()
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun addCaldavAccounts(category: PreferenceCategory): Boolean {
|
|
||||||
val accounts = caldavDao.getAccounts().filter {
|
|
||||||
it.accountType != TYPE_LOCAL && it.accountType != TYPE_TASKS
|
|
||||||
}
|
|
||||||
for (account in accounts) {
|
|
||||||
val preference = Preference(context)
|
|
||||||
preference.title = account.name
|
|
||||||
val error = account.error
|
|
||||||
if (isNullOrEmpty(error)) {
|
|
||||||
preference.setSummary(when {
|
|
||||||
account.isCaldavAccount -> R.string.caldav
|
|
||||||
account.isEteSyncAccount -> R.string.etesync_v1
|
|
||||||
account.isEtebaseAccount -> R.string.etesync
|
|
||||||
account.isOpenTasks && account.uuid.isEteSync() -> R.string.etesync
|
|
||||||
account.isOpenTasks && account.uuid.isDavx5() -> R.string.davx5
|
|
||||||
account.isOpenTasks && account.uuid.isDecSync() -> R.string.decsync
|
|
||||||
else -> 0
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
preference.summary = error
|
|
||||||
}
|
|
||||||
preference.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
|
||||||
val intent = Intent(context, when {
|
|
||||||
account.isCaldavAccount -> CaldavAccountSettingsActivity::class.java
|
|
||||||
account.isEteSyncAccount -> EteSyncAccountSettingsActivity::class.java
|
|
||||||
account.isEtebaseAccount -> EtebaseAccountSettingsActivity::class.java
|
|
||||||
account.isOpenTasks -> OpenTaskAccountSettingsActivity::class.java
|
|
||||||
else -> throw IllegalArgumentException("Unexpected account type: $account")
|
|
||||||
})
|
|
||||||
intent.putExtra(BaseCaldavAccountSettingsActivity.EXTRA_CALDAV_DATA, account)
|
|
||||||
startActivityForResult(intent, REQUEST_CALDAV_SETTINGS)
|
|
||||||
false
|
|
||||||
}
|
|
||||||
category.addPreference(preference)
|
|
||||||
}
|
|
||||||
return accounts.isNotEmpty()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun logoutConfirmation(account: GoogleTaskAccount) {
|
|
||||||
val name = account.account
|
|
||||||
val alertDialog = dialogBuilder
|
|
||||||
.newDialog()
|
|
||||||
.setMessage(R.string.logout_warning, name)
|
|
||||||
.setPositiveButton(R.string.logout) { _, _ ->
|
|
||||||
lifecycleScope.launch {
|
|
||||||
withContext(NonCancellable) {
|
|
||||||
taskDeleter.delete(account)
|
|
||||||
}
|
|
||||||
refresh()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.setNegativeButton(R.string.cancel, null)
|
|
||||||
.create()
|
|
||||||
alertDialog.setCanceledOnTouchOutside(false)
|
|
||||||
alertDialog.setCancelable(false)
|
|
||||||
alertDialog.show()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun refresh() {
|
|
||||||
lifecycleScope.launch {
|
|
||||||
val synchronizationPreferences = findPreference(R.string.accounts) as PreferenceCategory
|
|
||||||
synchronizationPreferences.removeAll()
|
|
||||||
val hasGoogleAccounts: Boolean = addGoogleTasksAccounts(synchronizationPreferences)
|
|
||||||
val hasCaldavAccounts = addCaldavAccounts(synchronizationPreferences)
|
|
||||||
findPreference(R.string.accounts).isVisible = hasGoogleAccounts || hasCaldavAccounts
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val REQUEST_CALDAV_SETTINGS = 10013
|
|
||||||
const val REQUEST_GOOGLE_TASKS = 10014
|
|
||||||
private const val REQUEST_ADD_ACCOUNT = 10015
|
|
||||||
const val REQUEST_TASKS_ORG = 10016
|
|
||||||
private const val FRAG_TAG_ADD_ACCOUNT = "frag_tag_add_account"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
<vector android:autoMirrored="true" android:height="36dp"
|
|
||||||
android:viewportHeight="1146" android:viewportWidth="1146"
|
|
||||||
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
<group>
|
|
||||||
<clip-path android:pathData="M0,0l1145.72,0l0,1145.72l-1145.72,0z"/>
|
|
||||||
<path android:fillColor="#2196F3" android:pathData="M572.65,571.88m-572.51,0a572.51,572.51 0,1 1,1145.02 0a572.51,572.51 45,1 1,-1145.02 0"/>
|
|
||||||
<group>
|
|
||||||
<clip-path android:pathData="M572.65,571.88m-572.51,0a572.51,572.51 0,1 1,1145.02 0a572.51,572.51 45,1 1,-1145.02 0"/>
|
|
||||||
<path android:fillAlpha="0.15" android:fillColor="#FF000000"
|
|
||||||
android:fillType="nonZero" android:pathData="M429.5,771.89L230.23,572.61L162.61,640.23L1146.31,1623.94L1719.76,1050.49L935.33,266.06L429.5,771.89Z"/>
|
|
||||||
</group>
|
|
||||||
</group>
|
|
||||||
<path android:fillColor="#ffffff" android:fillType="nonZero" android:pathData="M429.5,771.88L230.22,572.61L162.61,640.23L429.5,907.12L1002.95,333.67L935.33,266.05L429.5,771.88Z"/>
|
|
||||||
</vector>
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
<?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"
|
|
||||||
android:key="@string/preference_screen">
|
|
||||||
|
|
||||||
<PreferenceCategory
|
|
||||||
android:key="@string/accounts"
|
|
||||||
android:title="@string/accounts"
|
|
||||||
app:isPreferenceVisible="false" />
|
|
||||||
|
|
||||||
<Preference
|
|
||||||
android:key="@string/add_account"
|
|
||||||
android:title="@string/add_account"
|
|
||||||
app:icon="@drawable/ic_outline_add_24px" />
|
|
||||||
|
|
||||||
</PreferenceScreen>
|
|
||||||
Loading…
Reference in New Issue