Commit 58e24b81 authored by Mihai Eduard Badea's avatar Mihai Eduard Badea Committed by Emily Kager
Browse files

For issue #12400 - Refresh swiped collection tab view

Item is now refreshed by calling notifyDataSetChanged on the adapter when the last tab from the collection has been swiped away and the user cancels the deletion by pressing the cancel button from the dialog.
Also added a "wasSwiped" flag to onCollectionRemoveTab in order to check if the tab was deleted from a swipe action and not by pressing the "X" button.
parent bffb5663
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -211,7 +211,8 @@ class HomeFragment : Fragment() {
                hideOnboarding = ::hideOnboardingAndOpenSearch,
                registerCollectionStorageObserver = ::registerCollectionStorageObserver,
                showDeleteCollectionPrompt = ::showDeleteCollectionPrompt,
                showTabTray = ::openTabTray
                showTabTray = ::openTabTray,
                handleSwipedItemDeletionCancel = ::handleSwipedItemDeletionCancel
            )
        )
        updateLayout(view)
@@ -557,12 +558,21 @@ class HomeFragment : Fragment() {
        }
    }

    private fun showDeleteCollectionPrompt(tabCollection: TabCollection, title: String?, message: String) {
    private fun showDeleteCollectionPrompt(
        tabCollection: TabCollection,
        title: String?,
        message: String,
        wasSwiped: Boolean,
        handleSwipedItemDeletionCancel: () -> Unit
    ) {
        val context = context ?: return
        AlertDialog.Builder(context).apply {
            setTitle(title)
            setMessage(message)
            setNegativeButton(R.string.tab_collection_dialog_negative) { dialog: DialogInterface, _ ->
                if (wasSwiped) {
                    handleSwipedItemDeletionCancel()
                }
                dialog.cancel()
            }
            setPositiveButton(R.string.tab_collection_dialog_positive) { dialog: DialogInterface, _ ->
@@ -951,6 +961,10 @@ class HomeFragment : Fragment() {
        view?.add_tabs_to_collections_button?.isVisible = tabCount > 0
    }

    private fun handleSwipedItemDeletionCancel() {
        view?.sessionControlRecyclerView?.adapter?.notifyDataSetChanged()
    }

    companion object {
        const val ALL_NORMAL_TABS = "all_normal"
        const val ALL_PRIVATE_TABS = "all_private"
+13 −6
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ interface SessionControlController {
    /**
     * @see [CollectionInteractor.onCollectionRemoveTab]
     */
    fun handleCollectionRemoveTab(collection: TabCollection, tab: ComponentTab)
    fun handleCollectionRemoveTab(collection: TabCollection, tab: ComponentTab, wasSwiped: Boolean)

    /**
     * @see [CollectionInteractor.onCollectionShareTabsClicked]
@@ -160,8 +160,15 @@ class DefaultSessionControlController(
    private val viewLifecycleScope: CoroutineScope,
    private val hideOnboarding: () -> Unit,
    private val registerCollectionStorageObserver: () -> Unit,
    private val showDeleteCollectionPrompt: (tabCollection: TabCollection, title: String?, message: String) -> Unit,
    private val showTabTray: () -> Unit
    private val showDeleteCollectionPrompt: (
        tabCollection: TabCollection,
        title: String?,
        message: String,
        wasSwiped: Boolean,
        handleSwipedItemDeletionCancel: () -> Unit
    ) -> Unit,
    private val showTabTray: () -> Unit,
    private val handleSwipedItemDeletionCancel: () -> Unit
) : SessionControlController {

    override fun handleCollectionAddTabTapped(collection: TabCollection) {
@@ -206,7 +213,7 @@ class DefaultSessionControlController(
        metrics.track(Event.CollectionAllTabsRestored)
    }

    override fun handleCollectionRemoveTab(collection: TabCollection, tab: ComponentTab) {
    override fun handleCollectionRemoveTab(collection: TabCollection, tab: ComponentTab, wasSwiped: Boolean) {
        metrics.track(Event.CollectionTabRemoved)

        if (collection.tabs.size == 1) {
@@ -216,7 +223,7 @@ class DefaultSessionControlController(
            )
            val message =
                activity.resources.getString(R.string.delete_tab_and_collection_dialog_message)
            showDeleteCollectionPrompt(collection, title, message)
            showDeleteCollectionPrompt(collection, title, message, wasSwiped, handleSwipedItemDeletionCancel)
        } else {
            viewLifecycleScope.launch(Dispatchers.IO) {
                tabCollectionStorage.removeTabFromCollection(collection, tab)
@@ -232,7 +239,7 @@ class DefaultSessionControlController(
    override fun handleDeleteCollectionTapped(collection: TabCollection) {
        val message =
            activity.resources.getString(R.string.tab_collection_dialog_message, collection.title)
        showDeleteCollectionPrompt(collection, null, message)
        showDeleteCollectionPrompt(collection, null, message, false, handleSwipedItemDeletionCancel)
    }

    override fun handleOpenInPrivateTabClicked(topSite: TopSite) {
+3 −3
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ interface CollectionInteractor {
     * @param collection The collection of tabs that will be modified.
     * @param tab The tab to remove from the tab collection.
     */
    fun onCollectionRemoveTab(collection: TabCollection, tab: Tab)
    fun onCollectionRemoveTab(collection: TabCollection, tab: Tab, wasSwiped: Boolean)

    /**
     * Shares the tabs in the given tab collection. Called when a user clicks on the Collection
@@ -189,8 +189,8 @@ class SessionControlInteractor(
        controller.handleCollectionOpenTabsTapped(collection)
    }

    override fun onCollectionRemoveTab(collection: TabCollection, tab: Tab) {
        controller.handleCollectionRemoveTab(collection, tab)
    override fun onCollectionRemoveTab(collection: TabCollection, tab: Tab, wasSwiped: Boolean) {
        controller.handleCollectionRemoveTab(collection, tab, wasSwiped)
    }

    override fun onCollectionShareTabsClicked(collection: TabCollection) {
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ class SwipeToDeleteCallback(
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        when (viewHolder) {
            is TabInCollectionViewHolder -> {
                interactor.onCollectionRemoveTab(viewHolder.collection, viewHolder.tab)
                interactor.onCollectionRemoveTab(viewHolder.collection, viewHolder.tab, wasSwiped = true)
            }
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ class TabInCollectionViewHolder(

        list_item_action_button.increaseTapArea(buttonIncreaseDps)
        list_item_action_button.setOnClickListener {
            interactor.onCollectionRemoveTab(collection, tab)
            interactor.onCollectionRemoveTab(collection, tab, wasSwiped = false)
        }
    }

Loading