Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 43 additions & 38 deletions app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR
import androidx.appcompat.widget.Toolbar
Expand Down Expand Up @@ -63,6 +62,7 @@ import com.duckduckgo.app.tabs.ui.TabSwitcherViewModel.SelectionViewState.Mode
import com.duckduckgo.appbuildconfig.api.AppBuildConfig
import com.duckduckgo.common.ui.DuckDuckGoActivity
import com.duckduckgo.common.ui.menu.PopupMenu
import com.duckduckgo.common.ui.view.button.ButtonType
import com.duckduckgo.common.ui.view.button.ButtonType.DESTRUCTIVE
import com.duckduckgo.common.ui.view.button.ButtonType.GHOST_ALT
import com.duckduckgo.common.ui.view.dialog.TextAlertDialogBuilder
Expand All @@ -74,8 +74,6 @@ import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.ActivityScope
import com.duckduckgo.duckchat.api.DuckChat
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -437,16 +435,25 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
when (command) {
is Close -> finishAfterTransition()
is CloseAllTabsRequest -> showCloseAllTabsConfirmation()
is Command.ShareLinks -> {
launchShareMultipleLinkChooser(command.links)
}

is Command.ShareLink -> {
launchShareLinkChooser(command.link, command.title)
}
is Command.ShareLinks -> launchShareMultipleLinkChooser(command.links)
is Command.ShareLink -> launchShareLinkChooser(command.link, command.title)
is Command.BookmarkTabsRequest -> showBookmarkTabsConfirmation(command.tabIds)
is Command.ShowUndoBookmarkMessage -> showBookmarkSnackbarWithUndo(command.numBookmarks)
}
}

private fun showBookmarkSnackbarWithUndo(numBookmarks: Int) {
val message = resources.getQuantityString(R.plurals.tabSwitcherBookmarkToast, numBookmarks, numBookmarks)
TabSwitcherSnackbar(
anchorView = toolbar,
message = message,
action = getString(R.string.undoSnackbarAction),
showAction = numBookmarks > 0,
onAction = viewModel::undoBookmarkAction,
onDismiss = viewModel::finishBookmarkAction,
).show()
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
if (tabManagerFeatureFlags.multiSelection().isEnabled()) {
menuInflater.inflate(R.menu.menu_tab_switcher_activity_with_selection, menu)
Expand Down Expand Up @@ -602,34 +609,14 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
}

private fun onDeletableTab(tab: TabEntity) {
Snackbar.make(toolbar, getString(R.string.tabClosed), Snackbar.LENGTH_LONG)
.setDuration(3500) // 3.5 seconds
.setAction(R.string.tabClosedUndo) {
// noop, handled in onDismissed callback
}
.addCallback(
object : Snackbar.Callback() {
override fun onDismissed(
transientBottomBar: Snackbar?,
event: Int,
) {
when (event) {
// handle the UNDO action here as we only have one
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_ACTION -> launch { viewModel.undoDeletableTab(tab) }
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_SWIPE,
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_TIMEOUT,
-> launch { viewModel.purgeDeletableTabs() }
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_CONSECUTIVE,
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_MANUAL,
-> { /* noop */
}
}
}
},
)
.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE)
.apply { view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text).maxLines = 1 }
.show()
TabSwitcherSnackbar(
anchorView = toolbar,
message = getString(R.string.tabClosed),
action = getString(R.string.tabClosedUndo),
showAction = true,
onAction = { launch { viewModel.undoDeletableTab(tab) } },
onDismiss = { launch { viewModel.purgeDeletableTabs() } },
).show()
}

private fun launchShareLinkChooser(
Expand Down Expand Up @@ -715,6 +702,24 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
.show()
}

private fun showBookmarkTabsConfirmation(tabIds: List<String>) {
val numTabs = tabIds.size
val title = resources.getQuantityString(R.plurals.tabSwitcherBookmarkDialogTitle, numTabs, numTabs)
TextAlertDialogBuilder(this)
.setTitle(title)
.setMessage(R.string.tabSwitcherBookmarkDialogDescription)
.setPositiveButton(R.string.tabSwitcherBookmarkDialogPositiveButton, ButtonType.PRIMARY)
.setNegativeButton(R.string.cancel, GHOST_ALT)
.addEventListener(
object : TextAlertDialogBuilder.EventListener() {
override fun onPositiveButtonClicked() {
viewModel.onBookmarkTabsConfirmed(tabIds)
}
},
)
.show()
}

private fun configureOnBackPressedListener() {
onBackPressedDispatcher.addCallback(
this,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.tabs.ui

import android.view.View
import android.widget.TextView
import com.google.android.material.R as materialR
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar

class TabSwitcherSnackbar(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

anchorView: View,
message: String,
private val action: String? = null,
private val showAction: Boolean = false,
private val onAction: () -> Unit = {},
private val onDismiss: () -> Unit = {},
) {
companion object {
private const val SNACKBAR_DISPLAY_TIME_MS = 3500
}

private val snackbar = Snackbar.make(anchorView, message, Snackbar.LENGTH_LONG)
.setDuration(SNACKBAR_DISPLAY_TIME_MS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

.apply {
if (showAction) {
setAction(action) {
// noop, handled in onDismissed callback
}
}
}
.addCallback(
object : Snackbar.Callback() {
override fun onDismissed(
transientBottomBar: Snackbar?,
event: Int,
) {
when (event) {
// handle the UNDO action here as we only have one
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_ACTION -> onAction()
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_SWIPE,
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_TIMEOUT,
-> onDismiss()
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_CONSECUTIVE,
BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_MANUAL,
-> { /* noop */ }
}
}
},
)
.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE)
.apply { view.findViewById<TextView>(materialR.id.snackbar_text).maxLines = 1 }

fun show() {
snackbar.show()
}
}
Loading
Loading