Fix google login issues

* Display error toasts
* Enable background sync after adding accounts
* Initiate sync after adding accounts
pull/1061/head
Alex Baker 4 years ago
parent 3fbbd95395
commit cb236bc0e8

@ -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) {

@ -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)
}
}

@ -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();

@ -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)
}
}
}

@ -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)
}

@ -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)
}

Loading…
Cancel
Save