Handle add account dialog clicks in settings

pull/1997/head
Alex Baker 2 years ago
parent 3c16a0c99a
commit 9522c14891

@ -7,18 +7,23 @@ import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity
import com.todoroo.astrid.service.TaskDeleter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.tasks.BuildConfig
import org.tasks.R
import org.tasks.auth.SignInActivity
import org.tasks.billing.BillingClient
import org.tasks.billing.Inventory
import org.tasks.billing.Purchase
import org.tasks.billing.PurchaseActivity
import org.tasks.caldav.BaseCaldavAccountSettingsActivity
import org.tasks.caldav.CaldavAccountSettingsActivity
import org.tasks.data.CaldavAccount
import org.tasks.data.GoogleTaskAccount
import org.tasks.etebase.EtebaseAccountSettingsActivity
import org.tasks.extensions.Context.openUri
import org.tasks.extensions.Context.toast
import org.tasks.injection.InjectingPreferenceFragment
import org.tasks.preferences.IconPreference
@ -27,7 +32,10 @@ import org.tasks.preferences.Preferences
import org.tasks.preferences.PreferencesViewModel
import org.tasks.preferences.fragments.GoogleTasksAccount.Companion.newGoogleTasksAccountPreference
import org.tasks.preferences.fragments.TasksAccount.Companion.newTasksAccountPreference
import org.tasks.sync.AddAccountDialog
import org.tasks.sync.AddAccountDialog.Companion.EXTRA_SELECTED
import org.tasks.sync.AddAccountDialog.Companion.newAccountDialog
import org.tasks.sync.AddAccountDialog.Platform
import org.tasks.widget.AppWidgetManager
import javax.inject.Inject
@ -82,6 +90,44 @@ class MainSettingsFragment : InjectingPreferenceFragment() {
} else {
inventory.subscription.observe(this) { refreshSubscription(it) }
}
parentFragmentManager.setFragmentResultListener(
AddAccountDialog.ADD_ACCOUNT,
this
) { _, result ->
val platform =
result.getSerializable(EXTRA_SELECTED, Platform::class.java)
?: return@setFragmentResultListener
when (platform) {
Platform.TASKS_ORG ->
startActivityForResult(
Intent(requireContext(), SignInActivity::class.java),
REQUEST_TASKS_ORG
)
Platform.GOOGLE_TASKS ->
startActivityForResult(
Intent(requireContext(), GtasksLoginActivity::class.java),
REQUEST_GOOGLE_TASKS
)
Platform.MICROSOFT -> {
}
Platform.DAVX5 ->
context?.openUri(R.string.url_davx5)
Platform.CALDAV ->
startActivityForResult(
Intent(requireContext(), CaldavAccountSettingsActivity::class.java),
REQUEST_CALDAV_SETTINGS
)
Platform.ETESYNC ->
startActivityForResult(
Intent(requireContext(), EtebaseAccountSettingsActivity::class.java),
REQUEST_CALDAV_SETTINGS
)
Platform.DECSYNC_CC ->
context?.openUri(R.string.url_decsync)
}
}
}
override fun onResume() {
@ -123,9 +169,8 @@ class MainSettingsFragment : InjectingPreferenceFragment() {
}
private fun addAccount(): Boolean {
val hasTasksAccount = viewModel.tasksAccount() != null
newAccountDialog(this@MainSettingsFragment, REQUEST_ADD_ACCOUNT, hasTasksAccount)
.show(parentFragmentManager, FRAG_TAG_ADD_ACCOUNT)
newAccountDialog(hasTasksAccount = viewModel.tasksAccount() != null)
.show(parentFragmentManager, FRAG_TAG_ADD_ACCOUNT)
return false
}
@ -244,7 +289,6 @@ class MainSettingsFragment : InjectingPreferenceFragment() {
}
companion object {
private const val REQUEST_ADD_ACCOUNT = 10015
private const val FRAG_TAG_ADD_ACCOUNT = "frag_tag_add_account"
const val REQUEST_CALDAV_SETTINGS = 10013
const val REQUEST_GOOGLE_TASKS = 10014

@ -1,7 +1,6 @@
package org.tasks.sync
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.rememberScrollState
@ -9,22 +8,16 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ContentAlpha
import androidx.compose.material.MaterialTheme
import androidx.compose.ui.Modifier
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
import com.google.android.material.composethemeadapter.MdcTheme
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity
import dagger.hilt.android.AndroidEntryPoint
import org.tasks.BuildConfig
import org.tasks.R
import org.tasks.auth.SignInActivity
import org.tasks.caldav.CaldavAccountSettingsActivity
import org.tasks.compose.SyncAccount
import org.tasks.dialogs.DialogBuilder
import org.tasks.etebase.EtebaseAccountSettingsActivity
import org.tasks.extensions.Context.openUri
import org.tasks.preferences.fragments.MainSettingsFragment.Companion.REQUEST_CALDAV_SETTINGS
import org.tasks.preferences.fragments.MainSettingsFragment.Companion.REQUEST_GOOGLE_TASKS
import org.tasks.preferences.fragments.MainSettingsFragment.Companion.REQUEST_TASKS_ORG
import javax.inject.Inject
@AndroidEntryPoint
@ -35,6 +28,16 @@ class AddAccountDialog : DialogFragment() {
private val hasTasksAccount: Boolean
get() = arguments?.getBoolean(EXTRA_HAS_TASKS_ACCOUNT) ?: false
enum class Platform {
TASKS_ORG,
GOOGLE_TASKS,
MICROSOFT,
DAVX5,
CALDAV,
ETESYNC,
DECSYNC_CC
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return dialogBuilder
.newDialog()
@ -47,45 +50,28 @@ class AddAccountDialog : DialogFragment() {
title = R.string.tasks_org,
description = R.string.tasks_org_description,
icon = R.drawable.ic_round_icon,
onClick = {
activity?.startActivityForResult(
Intent(activity, SignInActivity::class.java),
REQUEST_TASKS_ORG
)
dismiss()
}
onClick = { selected(Platform.TASKS_ORG) }
)
}
SyncAccount(
title = R.string.gtasks_GPr_header,
description = R.string.google_tasks_selection_description,
icon = R.drawable.ic_google,
onClick = {
activity?.startActivityForResult(
Intent(activity, GtasksLoginActivity::class.java),
REQUEST_GOOGLE_TASKS
)
dismiss()
}
onClick = { selected(Platform.GOOGLE_TASKS) }
)
if (BuildConfig.DEBUG) {
SyncAccount(
title = R.string.microsoft,
description = R.string.microsoft_selection_description,
icon = R.drawable.ic_microsoft_tasks,
onClick = {
dismiss()
}
onClick = { selected(Platform.MICROSOFT) }
)
}
SyncAccount(
title = R.string.davx5,
description = R.string.davx5_selection_description,
icon = R.drawable.ic_davx5_icon_green_bg,
onClick = {
activity?.openUri(R.string.url_davx5)
dismiss()
}
onClick = { selected(Platform.DAVX5) }
)
SyncAccount(
title = R.string.caldav,
@ -94,37 +80,19 @@ class AddAccountDialog : DialogFragment() {
tint = MaterialTheme.colors.onSurface.copy(
alpha = ContentAlpha.medium
),
onClick = {
activity?.startActivityForResult(
Intent(activity, CaldavAccountSettingsActivity::class.java),
REQUEST_CALDAV_SETTINGS
)
dismiss()
}
onClick = { selected(Platform.CALDAV) }
)
SyncAccount(
title = R.string.etesync,
description = R.string.etesync_selection_description,
icon = R.drawable.ic_etesync,
onClick = {
activity?.startActivityForResult(
Intent(
activity,
EtebaseAccountSettingsActivity::class.java
),
REQUEST_CALDAV_SETTINGS
)
dismiss()
}
onClick = { selected(Platform.ETESYNC) }
)
SyncAccount(
title = R.string.decsync,
description = R.string.decsync_selection_description,
icon = R.drawable.ic_decsync,
onClick = {
activity?.openUri(R.string.url_decsync)
dismiss()
}
onClick = { selected(Platform.DECSYNC_CC) }
)
}
}
@ -134,20 +102,19 @@ class AddAccountDialog : DialogFragment() {
.show()
}
private fun selected(platform: Platform) {
setFragmentResult(ADD_ACCOUNT, bundleOf(EXTRA_SELECTED to platform))
dismiss()
}
companion object {
const val ADD_ACCOUNT = "add_account"
const val EXTRA_SELECTED = "selected"
private const val EXTRA_HAS_TASKS_ACCOUNT = "extra_has_tasks_account"
fun newAccountDialog(
targetFragment: Fragment,
rc: Int,
hasTasksAccount: Boolean
): AddAccountDialog {
val dialog = AddAccountDialog()
dialog.arguments = Bundle().apply {
putBoolean(EXTRA_HAS_TASKS_ACCOUNT, hasTasksAccount)
fun newAccountDialog(hasTasksAccount: Boolean) =
AddAccountDialog().apply {
arguments = bundleOf(EXTRA_HAS_TASKS_ACCOUNT to hasTasksAccount)
}
dialog.setTargetFragment(targetFragment, rc)
return dialog
}
}
}
Loading…
Cancel
Save