Skip to content

Commit

Permalink
Added option to undo remove account
Browse files Browse the repository at this point in the history
  • Loading branch information
WSTxda committed Dec 5, 2024
1 parent 87a3dd4 commit f906f44
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import androidx.lifecycle.lifecycleScope
Expand All @@ -20,6 +19,7 @@ import androidx.preference.PreferenceFragmentCompat
import com.google.android.gms.R
import com.google.android.material.color.MaterialColors
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.transition.platform.MaterialSharedAxis
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -93,6 +93,7 @@ class AccountsFragment : PreferenceFragmentCompat() {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
Log.e(tag, errorMessage, e)
showSnackbar(errorMessage)
}
}

Expand Down Expand Up @@ -175,24 +176,42 @@ class AccountsFragment : PreferenceFragmentCompat() {
}

private fun removeAccount(accountName: String) {
lifecycleScope.launch(Dispatchers.Main) {
val accountManager = AccountManager.get(requireContext())
val accounts = accountManager.getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE)
val accountToRemove = accounts.firstOrNull { it.name == accountName }

accountToRemove?.let {
try {
val removedSuccessfully = withContext(Dispatchers.IO) {
accountManager.removeAccountExplicitly(it)
}
if (removedSuccessfully) {
refreshAccountSettings()
showToast(getString(R.string.toast_remove_account_success, accountName))
val rootView = view ?: return
val accountManager = AccountManager.get(requireContext())
val accounts = accountManager.getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE)
val accountToRemove = accounts.firstOrNull { it.name == accountName }

accountToRemove?.let {
val snackbar = Snackbar.make(
rootView,
getString(R.string.snackbar_remove_account, accountName),
Snackbar.LENGTH_LONG
)

var cancelRemoval = false

snackbar.setAction(getString(R.string.snackbar_undo_button)) {
cancelRemoval = true
}

snackbar.addCallback(object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
if (!cancelRemoval) {
lifecycleScope.launch(Dispatchers.IO) {
try {
val removedSuccessfully = accountManager.removeAccountExplicitly(it)
if (removedSuccessfully) {
withContext(Dispatchers.Main) {
refreshAccountSettings()
}
}
} catch (_: Exception) {
}
}
}
} catch (e: Exception) {
Log.e(tag, "Error removing account", e)
}
}
})
snackbar.show()
}
}

Expand All @@ -216,7 +235,9 @@ class AccountsFragment : PreferenceFragmentCompat() {
}
}

private fun showToast(message: String) {
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
private fun showSnackbar(message: String) {
view?.let {
Snackbar.make(it, message, Snackbar.LENGTH_LONG).show()
}
}
}
3 changes: 2 additions & 1 deletion play-services-core/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@
<string name="dialog_message_remove_account">هل أنت متأكد أنك تريد إزالة هذا الحساب؟\n\nبعض الخدمات والوظائف ستكون غير متاحة\n\nلن تُفقد بياناتك</string>
<string name="dialog_confirm_button">إزالة</string>
<string name="dialog_cancel_button">إلغاء</string>
<string name="toast_remove_account_success">تمت إزالة %s</string>
<string name="snackbar_remove_account">تمت إزالة %s</string>
<string name="snackbar_undo_button">تراجع</string>

<!-- Settings options strings-->

Expand Down
3 changes: 2 additions & 1 deletion play-services-core/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ Ini mungkin memerlukan beberapa menit"</string>
<string name="dialog_message_remove_account">Apakah Anda yakin ingin menghapus akun ini?\n\nBeberapa layanan dan fungsi akan tidak tersedia\n\nData Anda tidak akan hilang</string>
<string name="dialog_confirm_button">Hapus</string>
<string name="dialog_cancel_button">Batal</string>
<string name="toast_remove_account_success">Akun %s dihapus</string>
<string name="snackbar_remove_account">Akun %s dihapus</string>
<string name="snackbar_undo_button">Batalkan</string>

<!-- Settings options strings-->

Expand Down
3 changes: 2 additions & 1 deletion play-services-core/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ Questo potrebbe richiedere qualche minuto"</string>
<string name="dialog_message_remove_account">Sei sicuro di voler rimuovere questo account?\n\nAlcuni servizi e funzionalità non saranno più disponibili\n\nI tuoi dati non andranno persi</string>
<string name="dialog_confirm_button">Rimuovi</string>
<string name="dialog_cancel_button">Annulla</string>
<string name="toast_remove_account_success">Account %s rimosso</string>
<string name="snackbar_remove_account">Account %s rimosso</string>
<string name="snackbar_undo_button">Annulla</string>

<!-- Settings options strings-->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- Theming -->
<item name="android:textAppearanceListItem">@style/Theme.TextAppearance.PreferenceTitle</item>
<item name="alertDialogTheme">@style/Theme.Base.AlertDialog</item>
<item name="snackbarStyle">@style/AppTheme.Snackbar</item>
<!-- Layout padding -->
<item name="android:listPreferredItemHeightSmall">@dimen/settings_listPreferredItemHeightSmall</item>
<item name="android:listPreferredItemPaddingLeft">@dimen/settings_listPreferredItemPaddingStart</item>
Expand Down
1 change: 1 addition & 0 deletions play-services-core/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<!-- Theming -->
<item name="android:textAppearanceListItem">@style/Theme.TextAppearance.PreferenceTitle</item>
<item name="alertDialogTheme">@style/Theme.Base.AlertDialog</item>
<item name="snackbarStyle">@style/AppTheme.Snackbar</item>
<!-- Layout padding -->
<item name="android:listPreferredItemHeightSmall">@dimen/settings_listPreferredItemHeightSmall</item>
<item name="android:listPreferredItemPaddingLeft">@dimen/settings_listPreferredItemPaddingStart</item>
Expand Down
3 changes: 2 additions & 1 deletion play-services-core/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@
<string name="dialog_message_remove_account">Czy na pewno chcesz usunąć to konto?\n\nNiektóre usługi i funkcje będą niedostępne.\nDane nie zostaną usunięte.</string>
<string name="dialog_confirm_button">Usuń</string>
<string name="dialog_cancel_button">Anuluj</string>
<string name="toast_remove_account_success">Usunięto: %s</string>
<string name="snackbar_remove_account">Usunięto: %s</string>
<string name="snackbar_undo_button">Cofnij</string>

<!-- Informacje -->

Expand Down
3 changes: 2 additions & 1 deletion play-services-core/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ Isso pode levar alguns minutos."</string>
<string name="dialog_message_remove_account">Tem certeza de que deseja remover esta conta?\n\nAlguns serviços e funções não estarão disponíveis\n\nSeus dados não serão perdidos</string>
<string name="dialog_confirm_button">Remover</string>
<string name="dialog_cancel_button">Cancelar</string>
<string name="toast_remove_account_success">Conta %s removida</string>
<string name="snackbar_remove_account">Conta %s removida</string>
<string name="snackbar_undo_button">Desfazer</string>

<!-- Settings options strings-->

Expand Down
1 change: 1 addition & 0 deletions play-services-core/src/main/res/values-v31/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- Theming -->
<item name="android:textAppearanceListItem">@style/Theme.TextAppearance.PreferenceTitle</item>
<item name="alertDialogTheme">@style/Theme.Base.AlertDialog</item>
<item name="snackbarStyle">@style/AppTheme.Snackbar</item>
<!-- Layout padding -->
<item name="android:listPreferredItemHeightSmall">@dimen/settings_listPreferredItemHeightSmall</item>
<item name="android:listPreferredItemPaddingLeft">@dimen/settings_listPreferredItemPaddingStart</item>
Expand Down
3 changes: 2 additions & 1 deletion play-services-core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ This can take a couple of minutes"</string>
<string name="dialog_message_remove_account">Are you sure you want to remove this account?\n\nSome services and functions will be unavailable\n\nYour data will not be lost</string>
<string name="dialog_confirm_button">Remove</string>
<string name="dialog_cancel_button">Cancel</string>
<string name="toast_remove_account_success">Account %s removed</string>
<string name="snackbar_remove_account">Account %s removed</string>
<string name="snackbar_undo_button">Undo</string>

<!-- Settings options strings-->

Expand Down
6 changes: 5 additions & 1 deletion play-services-core/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
<item name="iconSpaceReserved">@bool/settings_config_icon_space_reserved</item>
</style>

<style name="AppTheme.Snackbar" parent="Widget.Material3.Snackbar">
<item name="android:layout_margin">24dp</item>
</style>

<!-- Text application theme -->

<style name="Theme.TextAppearance.PreferenceTitle" parent="@style/TextAppearance.Material3.TitleMedium" />
Expand Down Expand Up @@ -126,6 +130,7 @@
<!-- Theming -->
<item name="android:textAppearanceListItem">@style/Theme.TextAppearance.PreferenceTitle</item>
<item name="alertDialogTheme">@style/Theme.Base.AlertDialog</item>
<item name="snackbarStyle">@style/AppTheme.Snackbar</item>
<!-- Layout padding -->
<item name="android:listPreferredItemHeightSmall">@dimen/settings_listPreferredItemHeightSmall</item>
<item name="android:listPreferredItemPaddingLeft">@dimen/settings_listPreferredItemPaddingStart</item>
Expand Down Expand Up @@ -180,5 +185,4 @@
<item name="colorSurfaceContainerHigh">@color/md_theme_surfaceContainerHigh</item>
<item name="colorSurfaceContainerHighest">@color/md_theme_surfaceContainerHighest</item>
</style>

</resources>

0 comments on commit f906f44

Please sign in to comment.