From 832fd71afc6545aa5f3cd41fe2627eda79620b03 Mon Sep 17 00:00:00 2001 From: Sawyer Blatz Date: Thu, 23 May 2019 12:16:40 -0700 Subject: [PATCH] For #2784: Removes undo from collection deletion (#2786) --- .../org/mozilla/fenix/home/HomeFragment.kt | 59 ++++++++----------- app/src/main/res/values/strings.xml | 6 ++ 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 280c3bc7a..b3e6c7205 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -4,12 +4,15 @@ package org.mozilla.fenix.home +import android.content.DialogInterface import android.content.res.Resources import android.graphics.drawable.BitmapDrawable import android.os.Bundle +import android.view.ContextThemeWrapper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams.PARENT_ID import androidx.fragment.app.Fragment @@ -85,7 +88,6 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver { var deleteAllSessionsJob: (suspend () -> Unit)? = null var deleteSessionJob: (suspend () -> Unit)? = null - var deleteCollectionJob: (suspend () -> Unit)? = null private val onboarding by lazy { FenixOnboarding(requireContext()) } private lateinit var sessionControlComponent: SessionControlComponent @@ -346,14 +348,29 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver { deleteAllSessionsJob = null } } + } - deleteCollectionJob?.let { - launch { - it.invoke() - }.invokeOnCompletion { - deleteCollectionJob = null + private fun createDeleteCollectionPrompt(tabCollection: TabCollection) { + AlertDialog.Builder( + ContextThemeWrapper( + activity, + R.style.DialogStyle + ) + ).apply { + val message = context.getString(R.string.tab_collection_dialog_message, tabCollection.title) + setMessage(message) + setNegativeButton(R.string.tab_collection_dialog_negative) { dialog: DialogInterface, _ -> + dialog.cancel() } - } + setPositiveButton(R.string.tab_collection_dialog_positive) { dialog: DialogInterface, _ -> + launch(Dispatchers.IO) { + requireComponents.core.tabCollectionStorage.removeCollection(tabCollection) + }.invokeOnCompletion { + dialog.dismiss() + } + } + create() + }.show() } @Suppress("ComplexMethod") @@ -368,7 +385,7 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver { .onNext(SessionControlChange.ExpansionChange(action.collection, false)) } is CollectionAction.Delete -> { - removeCollectionWithUndo(action.collection) + createDeleteCollectionPrompt(action.collection) } is CollectionAction.AddTab -> { ItsNotBrokenSnack(context!!).showSnackbar(issueNumber = "1575") @@ -569,32 +586,6 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver { } } - private fun removeCollectionWithUndo(tabCollection: TabCollection) { - // Update the UI with the tab collection removed, but don't remove it from storage yet - val updatedCollectionList = requireComponents.core.tabCollectionStorage.cachedTabCollections.toMutableList() - updatedCollectionList.remove(tabCollection) - getManagedEmitter().onNext(SessionControlChange.CollectionsChange(updatedCollectionList)) - - deleteCollectionJob = { - launch(Dispatchers.IO) { - requireComponents.core.tabCollectionStorage.removeCollection(tabCollection) - } - } - - CoroutineScope(Dispatchers.Main).allowUndo( - view!!, getString(R.string.snackbar_collection_deleted), - getString(R.string.snackbar_deleted_undo), { - deleteCollectionJob = null - getManagedEmitter().onNext(SessionControlChange - .CollectionsChange(requireComponents.core.tabCollectionStorage.cachedTabCollections)) - } - ) { - launch(Dispatchers.IO) { - requireComponents.core.tabCollectionStorage.removeCollection(tabCollection) - } - } - } - private fun emitSessionChanges() { val sessionManager = requireComponents.core.sessionManager diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d1808104e..23a6c16f5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -513,6 +513,12 @@ ALLOW DENY + + Are you sure you want to delete %1$s? + + Delete + + Cancel Entering full screen mode