From cb236bc0e8ad37116862ef2fa324db4292ac629a Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 30 Jul 2020 17:08:14 -0500 Subject: [PATCH] Fix google login issues * Display error toasts * Enable background sync after adding accounts * Initiate sync after adding accounts --- .../astrid/gtasks/auth/GtasksLoginActivity.kt | 36 +++++++++------ .../tasks/data/GoogleTaskListDaoBlocking.kt | 12 ----- .../org/tasks/drive/DriveLoginActivity.java | 4 +- .../org/tasks/preferences/MainPreferences.kt | 44 +++++++++++++++++++ .../tasks/preferences/fragments/Backups.kt | 9 +--- .../preferences/fragments/Synchronization.kt | 11 ----- 6 files changed, 70 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.kt b/app/src/main/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.kt index d7765e041..05bd4f25b 100644 --- a/app/src/main/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.kt @@ -10,12 +10,16 @@ import android.app.Activity import android.app.ProgressDialog import android.content.Intent import android.os.Bundle +import androidx.lifecycle.lifecycleScope import com.todoroo.andlib.utility.DialogUtilities import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.tasks.PermissionUtil.verifyPermissions import org.tasks.R import org.tasks.data.GoogleTaskAccount -import org.tasks.data.GoogleTaskListDaoBlocking +import org.tasks.data.GoogleTaskListDao import org.tasks.dialogs.DialogBuilder import org.tasks.gtasks.GoogleAccountManager import org.tasks.injection.InjectingAppCompatActivity @@ -34,7 +38,7 @@ import javax.inject.Inject class GtasksLoginActivity : InjectingAppCompatActivity() { @Inject lateinit var dialogBuilder: DialogBuilder @Inject lateinit var googleAccountManager: GoogleAccountManager - @Inject lateinit var googleTaskListDao: GoogleTaskListDaoBlocking + @Inject lateinit var googleTaskListDao: GoogleTaskListDao @Inject lateinit var permissionRequestor: ActivityPermissionRequestor override fun onCreate(savedInstanceState: Bundle?) { @@ -62,19 +66,23 @@ class GtasksLoginActivity : InjectingAppCompatActivity() { a, object : AuthResultHandler { override fun authenticationSuccessful(accountName: String) { - var account = googleTaskListDao.getAccount(accountName) - if (account == null) { - account = GoogleTaskAccount() - account.account = accountName - googleTaskListDao.insert(account) - } else { - account.error = "" - googleTaskListDao.update(account) - googleTaskListDao.resetLastSync(accountName) + lifecycleScope.launch { + withContext(NonCancellable) { + var account = googleTaskListDao.getAccount(accountName) + if (account == null) { + account = GoogleTaskAccount() + account.account = accountName + googleTaskListDao.insert(account) + } else { + account.error = "" + googleTaskListDao.update(account) + googleTaskListDao.resetLastSync(accountName) + } + } + setResult(Activity.RESULT_OK) + DialogUtilities.dismissDialog(this@GtasksLoginActivity, pd) + finish() } - setResult(Activity.RESULT_OK) - DialogUtilities.dismissDialog(this@GtasksLoginActivity, pd) - finish() } override fun authenticationFailed(message: String) { diff --git a/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt b/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt index bb9e298ac..82b3f104e 100644 --- a/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt @@ -9,10 +9,6 @@ class GoogleTaskListDaoBlocking @Inject constructor(private val dao: GoogleTaskL dao.getAccounts() } - fun getAccount(account: String): GoogleTaskAccount? = runBlocking { - dao.getAccount(account) - } - fun getById(id: Long): GoogleTaskList? = runBlocking { dao.getById(id) } @@ -37,10 +33,6 @@ class GoogleTaskListDaoBlocking @Inject constructor(private val dao: GoogleTaskL dao.getAllLists() } - fun resetLastSync(account: String) = runBlocking { - dao.resetLastSync(account) - } - fun insertOrReplace(googleTaskList: GoogleTaskList): Long = runBlocking { dao.insertOrReplace(googleTaskList) } @@ -48,8 +40,4 @@ class GoogleTaskListDaoBlocking @Inject constructor(private val dao: GoogleTaskL fun insert(googleTaskAccount: GoogleTaskAccount) = runBlocking { dao.insert(googleTaskAccount) } - - fun update(account: GoogleTaskAccount) = runBlocking { - dao.update(account) - } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/drive/DriveLoginActivity.java b/app/src/main/java/org/tasks/drive/DriveLoginActivity.java index 78031d306..0cd71a275 100644 --- a/app/src/main/java/org/tasks/drive/DriveLoginActivity.java +++ b/app/src/main/java/org/tasks/drive/DriveLoginActivity.java @@ -28,7 +28,7 @@ import org.tasks.preferences.Preferences; @AndroidEntryPoint public class DriveLoginActivity extends InjectingAppCompatActivity { - private static final String EXTRA_ERROR = "extra_error"; + public static final String EXTRA_ERROR = "extra_error"; private static final int RC_CHOOSE_ACCOUNT = 10988; @Inject DialogBuilder dialogBuilder; @Inject GoogleAccountManager googleAccountManager; @@ -58,6 +58,7 @@ public class DriveLoginActivity extends InjectingAppCompatActivity { @Override public void authenticationSuccessful(String accountName) { preferences.setString(R.string.p_google_drive_backup_account, accountName); + preferences.setBoolean(R.string.p_google_drive_backup, true); setResult(RESULT_OK); DialogUtilities.dismissDialog(DriveLoginActivity.this, pd); finish(); @@ -65,6 +66,7 @@ public class DriveLoginActivity extends InjectingAppCompatActivity { @Override public void authenticationFailed(final String message) { + preferences.setBoolean(R.string.p_google_drive_backup, false); setResult(RESULT_CANCELED, new Intent().putExtra(EXTRA_ERROR, message)); DialogUtilities.dismissDialog(DriveLoginActivity.this, pd); finish(); diff --git a/app/src/main/java/org/tasks/preferences/MainPreferences.kt b/app/src/main/java/org/tasks/preferences/MainPreferences.kt index dc1c10287..9ebb3efc9 100644 --- a/app/src/main/java/org/tasks/preferences/MainPreferences.kt +++ b/app/src/main/java/org/tasks/preferences/MainPreferences.kt @@ -1,13 +1,57 @@ 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.NonCancellable +import kotlinx.coroutines.launch import org.tasks.R +import org.tasks.drive.DriveLoginActivity +import org.tasks.jobs.WorkManager import org.tasks.preferences.fragments.MainSettingsFragment +import org.tasks.preferences.fragments.REQUEST_CALDAV_SETTINGS +import org.tasks.preferences.fragments.REQUEST_DRIVE_BACKUP +import org.tasks.preferences.fragments.REQUEST_GOOGLE_TASKS +import org.tasks.sync.SyncAdapters +import org.tasks.ui.Toaster +import javax.inject.Inject @AndroidEntryPoint class MainPreferences : BasePreferences() { + @Inject lateinit var syncAdapters: SyncAdapters + @Inject lateinit var workManager: WorkManager + @Inject lateinit var toaster: Toaster + override fun getRootTitle() = R.string.TLA_menu_settings override fun getRootPreference() = MainSettingsFragment() + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == REQUEST_CALDAV_SETTINGS) { + if (resultCode == RESULT_OK) { + lifecycleScope.launch(NonCancellable) { + syncAdapters.sync(true) + workManager.updateBackgroundSync() + } + } + } else if (requestCode == REQUEST_GOOGLE_TASKS) { + if (resultCode == Activity.RESULT_OK) { + lifecycleScope.launch(NonCancellable) { + syncAdapters.sync(true) + workManager.updateBackgroundSync() + } + } else { + data?.getStringExtra(GtasksLoginActivity.EXTRA_ERROR)?.let { toaster.longToast(it) } + } + } else if (requestCode == REQUEST_DRIVE_BACKUP) { + if (resultCode != Activity.RESULT_OK) { + data?.getStringExtra(DriveLoginActivity.EXTRA_ERROR)?.let { toaster.longToast(it) } + } + } else { + super.onActivityResult(requestCode, resultCode, data) + } + } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/preferences/fragments/Backups.kt b/app/src/main/java/org/tasks/preferences/fragments/Backups.kt index a42bb5ea9..5c5402861 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Backups.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Backups.kt @@ -23,7 +23,7 @@ import org.tasks.ui.Toaster import javax.inject.Inject private const val REQUEST_CODE_BACKUP_DIR = 10001 -private const val REQUEST_DRIVE_BACKUP = 10002 +const val REQUEST_DRIVE_BACKUP = 12002 private const val REQUEST_PICKER = 10003 private const val FRAG_TAG_EXPORT_TASKS = "frag_tag_export_tasks" private const val FRAG_TAG_IMPORT_TASKS = "frag_tag_import_tasks" @@ -105,13 +105,6 @@ class Backups : InjectingPreferenceFragment() { .show(parentFragmentManager, FRAG_TAG_IMPORT_TASKS) } } - } else if (requestCode == REQUEST_DRIVE_BACKUP) { - val success = resultCode == RESULT_OK - preferences.setBoolean(R.string.p_google_drive_backup, success) - updateGoogleDriveCheckbox() - if (!success && data != null) { - toaster.longToast(data.getStringExtra(GtasksLoginActivity.EXTRA_ERROR)) - } } else { super.onActivityResult(requestCode, resultCode, data) } 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 92193ebd6..4695867af 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt @@ -29,7 +29,6 @@ import org.tasks.jobs.WorkManager import org.tasks.preferences.Preferences import org.tasks.sync.AddAccountDialog import org.tasks.sync.SyncAdapters -import org.tasks.ui.Toaster import javax.inject.Inject const val REQUEST_CALDAV_SETTINGS = 10013 @@ -40,7 +39,6 @@ class Synchronization : InjectingPreferenceFragment() { @Inject lateinit var workManager: WorkManager @Inject lateinit var preferences: Preferences - @Inject lateinit var toaster: Toaster @Inject lateinit var caldavDao: CaldavDao @Inject lateinit var googleTaskListDao: GoogleTaskListDao @Inject lateinit var taskDeleter: TaskDeleter @@ -101,15 +99,6 @@ class Synchronization : InjectingPreferenceFragment() { workManager.updateBackgroundSync() } } - } else if (requestCode == REQUEST_GOOGLE_TASKS) { - if (resultCode == Activity.RESULT_OK) { - lifecycleScope.launch(NonCancellable) { - syncAdapters.sync(true) - workManager.updateBackgroundSync() - } - } else if (data != null) { - toaster.longToast(data.getStringExtra(GtasksLoginActivity.EXTRA_ERROR)) - } } else { super.onActivityResult(requestCode, resultCode, data) }