Skip to content

Commit c80ecea

Browse files
committed
Simplify bookmarking logic
1 parent ca72063 commit c80ecea

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherActivity.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
449449
is CloseAllTabsRequest -> showCloseAllTabsConfirmation()
450450
is Command.ShareLinks -> launchShareMultipleLinkChooser(command.links)
451451
is Command.ShareLink -> launchShareLinkChooser(command.link, command.title)
452-
is Command.BookmarkTabsRequest -> showBookmarkTabsConfirmation(command.numTabs)
452+
is Command.BookmarkTabsRequest -> showBookmarkTabsConfirmation(command.tabIds)
453453
is Command.ShowBookmarkToast -> showBookmarkToast(command.numBookmarks)
454454
}
455455
}
@@ -817,7 +817,8 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
817817
.show()
818818
}
819819

820-
private fun showBookmarkTabsConfirmation(numTabs: Int) {
820+
private fun showBookmarkTabsConfirmation(tabIds: List<String>) {
821+
val numTabs = tabIds.size
821822
val title = resources.getQuantityString(R.plurals.tabSwitcherBookmarkDialogTitle, numTabs, numTabs)
822823
TextAlertDialogBuilder(this)
823824
.setTitle(title)
@@ -827,7 +828,7 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
827828
.addEventListener(
828829
object : TextAlertDialogBuilder.EventListener() {
829830
override fun onPositiveButtonClicked() {
830-
viewModel.onBookmarkTabsConfirmed(numTabs)
831+
viewModel.onBookmarkTabsConfirmed(tabIds)
831832
}
832833
},
833834
)

app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherViewModel.kt

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class TabSwitcherViewModel @Inject constructor(
111111
data object CloseAllTabsRequest : Command()
112112
data class ShareLink(val link: String, val title: String) : Command()
113113
data class ShareLinks(val links: List<String>) : Command()
114-
data class BookmarkTabsRequest(val numTabs: Int) : Command()
114+
data class BookmarkTabsRequest(val tabIds: List<String>) : Command()
115115
data class ShowBookmarkToast(val numBookmarks: Int) : Command()
116116
}
117117

@@ -237,17 +237,21 @@ class TabSwitcherViewModel @Inject constructor(
237237
fun onBookmarkSelectedTabs() {
238238
when (val mode = selectionViewState.value.mode) {
239239
is SelectionViewState.Mode.Normal -> {
240-
command.value = BookmarkTabsRequest(1)
240+
activeTab.value?.tabId?.let { tabId ->
241+
command.value = BookmarkTabsRequest(listOf(tabId))
242+
}
241243
}
242244

243245
is SelectionViewState.Mode.Selection -> {
244-
command.value = BookmarkTabsRequest(mode.selectedTabs.size)
246+
command.value = BookmarkTabsRequest(mode.selectedTabs)
245247
}
246248
}
247249
}
248250

249251
fun onBookmarkAllTabs() {
250-
command.value = BookmarkTabsRequest(tabSwitcherItems.value?.size ?: 0)
252+
tabSwitcherItems.value?.map { it.id }?.let { tabIds ->
253+
command.value = BookmarkTabsRequest(tabIds)
254+
}
251255
}
252256

253257
fun onSelectionModeRequested() {
@@ -260,36 +264,13 @@ class TabSwitcherViewModel @Inject constructor(
260264
fun onCloseOtherTabs() {
261265
}
262266

263-
fun onBookmarkTabsConfirmed(numTabs: Int) {
267+
fun onBookmarkTabsConfirmed(tabIds: List<String>) {
264268
viewModelScope.launch {
265-
val numBookmarkedTabs = when (val mode = selectionViewState.value.mode) {
266-
is SelectionViewState.Mode.Selection -> {
267-
// bookmark selected tabs (or all tabs if none selected)
268-
if (mode.selectedTabs.isNotEmpty()) {
269-
bookmarkTabs(mode.selectedTabs)
270-
} else {
271-
bookmarkAllTabs()
272-
}
273-
}
274-
275-
SelectionViewState.Mode.Normal -> {
276-
if (numTabs == 1) {
277-
activeTab.value?.tabId?.let { bookmarkTabs(listOf(it)) } ?: 0
278-
} else {
279-
bookmarkAllTabs()
280-
}
281-
}
282-
}
269+
val numBookmarkedTabs = bookmarkTabs(tabIds)
283270
command.value = ShowBookmarkToast(numBookmarkedTabs)
284271
}
285272
}
286273

287-
private suspend fun bookmarkAllTabs(): Int {
288-
return tabSwitcherItems.value?.filterIsInstance<TabSwitcherItem.Tab>()?.let { tabIds ->
289-
bookmarkTabs(tabIds.map { it.id })
290-
} ?: 0
291-
}
292-
293274
private suspend fun bookmarkTabs(tabIds: List<String>): Int {
294275
val results = tabIds.map { tabId ->
295276
viewModelScope.async {
@@ -311,16 +292,20 @@ class TabSwitcherViewModel @Inject constructor(
311292
pixel.fire(AppPixelName.TAB_MANAGER_MENU_CLOSE_ALL_TABS_CONFIRMED)
312293

313294
// Trigger a normal mode when there are no tabs
314-
_selectionViewState.update { it.copy(mode = SelectionViewState.Mode.Normal) }
295+
triggerNormalMode()
315296
}
316297
}
317298

318299
fun onEmptyAreaClicked() {
319300
if (tabManagerFeatureFlags.multiSelection().isEnabled() && _selectionViewState.value.mode is SelectionViewState.Mode.Selection) {
320-
_selectionViewState.update { it.copy(mode = SelectionViewState.Mode.Normal) }
301+
triggerNormalMode()
321302
}
322303
}
323304

305+
private fun triggerNormalMode() {
306+
_selectionViewState.update { it.copy(mode = SelectionViewState.Mode.Normal) }
307+
}
308+
324309
fun onUpButtonPressed() {
325310
pixel.fire(AppPixelName.TAB_MANAGER_UP_BUTTON_PRESSED)
326311
}

0 commit comments

Comments
 (0)