From e5a5c9b264e07eb7cb81970ad1694aba1d1c7307 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 31 Jul 2020 16:56:44 -0500 Subject: [PATCH] Convert AddAccountDialog to DialogFragment --- .../org/tasks/preferences/MainPreferences.kt | 10 +---- .../preferences/fragments/Synchronization.kt | 17 +------- .../java/org/tasks/sync/AddAccountDialog.kt | 43 +++++++++++-------- 3 files changed, 28 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/org/tasks/preferences/MainPreferences.kt b/app/src/main/java/org/tasks/preferences/MainPreferences.kt index f168054ca..b24a44561 100644 --- a/app/src/main/java/org/tasks/preferences/MainPreferences.kt +++ b/app/src/main/java/org/tasks/preferences/MainPreferences.kt @@ -2,10 +2,8 @@ package org.tasks.preferences import android.app.Activity import android.content.Intent -import androidx.lifecycle.lifecycleScope import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.launch import org.tasks.R import org.tasks.drive.DriveLoginActivity import org.tasks.jobs.WorkManager @@ -32,16 +30,12 @@ class MainPreferences : BasePreferences() { if (requestCode == REQUEST_CALDAV_SETTINGS) { if (resultCode == RESULT_OK) { syncAdapters.sync(true) - lifecycleScope.launch { - workManager.updateBackgroundSync() - } + workManager.updateBackgroundSync() } } else if (requestCode == REQUEST_GOOGLE_TASKS) { if (resultCode == Activity.RESULT_OK) { syncAdapters.sync(true) - lifecycleScope.launch { - workManager.updateBackgroundSync() - } + workManager.updateBackgroundSync() } else { data?.getStringExtra(GtasksLoginActivity.EXTRA_ERROR)?.let { toaster.longToast(it) } } diff --git a/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt b/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt index 2c838a440..ff9afc14b 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt @@ -1,6 +1,5 @@ package org.tasks.preferences.fragments -import android.app.Activity import android.content.Intent import android.os.Bundle import androidx.lifecycle.lifecycleScope @@ -33,6 +32,7 @@ import javax.inject.Inject const val REQUEST_CALDAV_SETTINGS = 10013 const val REQUEST_GOOGLE_TASKS = 10014 +private const val FRAG_TAG_ADD_ACCOUNT = "frag_tag_add_account" @AndroidEntryPoint class Synchronization : InjectingPreferenceFragment() { @@ -80,7 +80,7 @@ class Synchronization : InjectingPreferenceFragment() { findPreference(R.string.add_account) .setOnPreferenceClickListener { - AddAccountDialog.showAddAccountDialog(requireActivity(), dialogBuilder) + AddAccountDialog().show(parentFragmentManager, FRAG_TAG_ADD_ACCOUNT) false } } @@ -91,19 +91,6 @@ class Synchronization : InjectingPreferenceFragment() { refresh() } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (requestCode == REQUEST_CALDAV_SETTINGS) { - if (resultCode == Activity.RESULT_OK) { - syncAdapters.sync(true) - lifecycleScope.launch { - workManager.updateBackgroundSync() - } - } - } else { - super.onActivityResult(requestCode, resultCode, data) - } - } - private suspend fun addGoogleTasksAccounts(category: PreferenceCategory): Boolean { val accounts: List = googleTaskListDao.getAccounts() for (googleTaskAccount in accounts) { diff --git a/app/src/main/java/org/tasks/sync/AddAccountDialog.kt b/app/src/main/java/org/tasks/sync/AddAccountDialog.kt index f83066a49..137b16bd6 100644 --- a/app/src/main/java/org/tasks/sync/AddAccountDialog.kt +++ b/app/src/main/java/org/tasks/sync/AddAccountDialog.kt @@ -1,15 +1,17 @@ package org.tasks.sync -import android.app.Activity -import android.content.DialogInterface +import android.app.Dialog import android.content.Intent import android.net.Uri +import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter import android.widget.ImageView import android.widget.TextView +import androidx.fragment.app.DialogFragment import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity +import dagger.hilt.android.AndroidEntryPoint import org.tasks.R import org.tasks.caldav.CaldavAccountSettingsActivity import org.tasks.dialogs.DialogBuilder @@ -17,19 +19,24 @@ import org.tasks.etesync.EteSyncAccountSettingsActivity import org.tasks.preferences.fragments.REQUEST_CALDAV_SETTINGS import org.tasks.preferences.fragments.REQUEST_GOOGLE_TASKS import org.tasks.themes.DrawableUtil +import javax.inject.Inject -object AddAccountDialog { - fun showAddAccountDialog(activity: Activity, dialogBuilder: DialogBuilder) { - val services = activity.resources.getStringArray(R.array.synchronization_services) - val descriptions = activity.resources.getStringArray(R.array.synchronization_services_description) - val typedArray = activity.resources.obtainTypedArray(R.array.synchronization_services_icons) +@AndroidEntryPoint +class AddAccountDialog : DialogFragment() { + + @Inject lateinit var dialogBuilder: DialogBuilder + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val services = requireActivity().resources.getStringArray(R.array.synchronization_services) + val descriptions = requireActivity().resources.getStringArray(R.array.synchronization_services_description) + val typedArray = requireActivity().resources.obtainTypedArray(R.array.synchronization_services_icons) val icons = IntArray(typedArray.length()) for (i in icons.indices) { icons[i] = typedArray.getResourceId(i, 0) } typedArray.recycle() val adapter: ArrayAdapter = object : ArrayAdapter( - activity, R.layout.simple_list_item_2_themed, R.id.text1, services) { + requireActivity(), R.layout.simple_list_item_2_themed, R.id.text1, services) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val view = super.getView(position, convertView, parent) view.findViewById(R.id.text1).text = services[position] @@ -42,32 +49,30 @@ object AddAccountDialog { return view } } - dialogBuilder + return dialogBuilder .newDialog() .setTitle(R.string.choose_synchronization_service) - .setSingleChoiceItems( - adapter, - -1 - ) { dialog: DialogInterface, which: Int -> + .setSingleChoiceItems(adapter, -1) { dialog, which -> when (which) { - 0 -> activity.startActivityForResult( + 0 -> activity?.startActivityForResult( Intent(activity, GtasksLoginActivity::class.java), REQUEST_GOOGLE_TASKS) - 1 -> activity.startActivityForResult( + 1 -> activity?.startActivityForResult( Intent(activity, CaldavAccountSettingsActivity::class.java), REQUEST_CALDAV_SETTINGS) - 2 -> activity.startActivityForResult( + 2 -> activity?.startActivityForResult( Intent(activity, EteSyncAccountSettingsActivity::class.java), REQUEST_CALDAV_SETTINGS) } dialog.dismiss() } .setNeutralButton(R.string.help) { _, _ -> - activity.startActivity( - Intent( - Intent.ACTION_VIEW, Uri.parse(activity.getString(R.string.help_url_sync)))) + activity?.startActivity(Intent( + Intent.ACTION_VIEW, + Uri.parse(context?.getString(R.string.help_url_sync)))) } .setNegativeButton(android.R.string.cancel, null) .show() } + } \ No newline at end of file