Skip to content

Commit aaa0d58

Browse files
committed
Simplify bookmarking logic
1 parent fc06e36 commit aaa0d58

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
@@ -450,7 +450,7 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
450450
is CloseAllTabsRequest -> showCloseAllTabsConfirmation()
451451
is Command.ShareLinks -> launchShareMultipleLinkChooser(command.links)
452452
is Command.ShareLink -> launchShareLinkChooser(command.link, command.title)
453-
is Command.BookmarkTabsRequest -> showBookmarkTabsConfirmation(command.numTabs)
453+
is Command.BookmarkTabsRequest -> showBookmarkTabsConfirmation(command.tabIds)
454454
is Command.ShowBookmarkToast -> showBookmarkToast(command.numBookmarks)
455455
}
456456
}
@@ -816,7 +816,8 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine
816816
.show()
817817
}
818818

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

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
@@ -107,7 +107,7 @@ class TabSwitcherViewModel @Inject constructor(
107107
data object CloseAllTabsRequest : Command()
108108
data class ShareLink(val link: String, val title: String) : Command()
109109
data class ShareLinks(val links: List<String>) : Command()
110-
data class BookmarkTabsRequest(val numTabs: Int) : Command()
110+
data class BookmarkTabsRequest(val tabIds: List<String>) : Command()
111111
data class ShowBookmarkToast(val numBookmarks: Int) : Command()
112112
}
113113

@@ -229,17 +229,21 @@ class TabSwitcherViewModel @Inject constructor(
229229
fun onBookmarkSelectedTabs() {
230230
when (val mode = selectionViewState.value.mode) {
231231
is SelectionViewState.Mode.Normal -> {
232-
command.value = BookmarkTabsRequest(1)
232+
activeTab.value?.tabId?.let { tabId ->
233+
command.value = BookmarkTabsRequest(listOf(tabId))
234+
}
233235
}
234236

235237
is SelectionViewState.Mode.Selection -> {
236-
command.value = BookmarkTabsRequest(mode.selectedTabs.size)
238+
command.value = BookmarkTabsRequest(mode.selectedTabs)
237239
}
238240
}
239241
}
240242

241243
fun onBookmarkAllTabs() {
242-
command.value = BookmarkTabsRequest(tabSwitcherItems.value?.size ?: 0)
244+
tabSwitcherItems.value?.map { it.id }?.let { tabIds ->
245+
command.value = BookmarkTabsRequest(tabIds)
246+
}
243247
}
244248

245249
fun onSelectionModeRequested() {
@@ -252,36 +256,13 @@ class TabSwitcherViewModel @Inject constructor(
252256
fun onCloseOtherTabs() {
253257
}
254258

255-
fun onBookmarkTabsConfirmed(numTabs: Int) {
259+
fun onBookmarkTabsConfirmed(tabIds: List<String>) {
256260
viewModelScope.launch {
257-
val numBookmarkedTabs = when (val mode = selectionViewState.value.mode) {
258-
is SelectionViewState.Mode.Selection -> {
259-
// bookmark selected tabs (or all tabs if none selected)
260-
if (mode.selectedTabs.isNotEmpty()) {
261-
bookmarkTabs(mode.selectedTabs)
262-
} else {
263-
bookmarkAllTabs()
264-
}
265-
}
266-
267-
SelectionViewState.Mode.Normal -> {
268-
if (numTabs == 1) {
269-
activeTab.value?.tabId?.let { bookmarkTabs(listOf(it)) } ?: 0
270-
} else {
271-
bookmarkAllTabs()
272-
}
273-
}
274-
}
261+
val numBookmarkedTabs = bookmarkTabs(tabIds)
275262
command.value = ShowBookmarkToast(numBookmarkedTabs)
276263
}
277264
}
278265

279-
private suspend fun bookmarkAllTabs(): Int {
280-
return tabSwitcherItems.value?.filterIsInstance<TabSwitcherItem.Tab>()?.let { tabIds ->
281-
bookmarkTabs(tabIds.map { it.id })
282-
} ?: 0
283-
}
284-
285266
private suspend fun bookmarkTabs(tabIds: List<String>): Int {
286267
val results = tabIds.map { tabId ->
287268
viewModelScope.async {
@@ -303,16 +284,20 @@ class TabSwitcherViewModel @Inject constructor(
303284
pixel.fire(AppPixelName.TAB_MANAGER_MENU_CLOSE_ALL_TABS_CONFIRMED)
304285

305286
// Trigger a normal mode when there are no tabs
306-
_selectionViewState.update { it.copy(mode = SelectionViewState.Mode.Normal) }
287+
triggerNormalMode()
307288
}
308289
}
309290

310291
fun onEmptyAreaClicked() {
311292
if (tabManagerFeatureFlags.multiSelection().isEnabled() && _selectionViewState.value.mode is SelectionViewState.Mode.Selection) {
312-
_selectionViewState.update { it.copy(mode = SelectionViewState.Mode.Normal) }
293+
triggerNormalMode()
313294
}
314295
}
315296

297+
private fun triggerNormalMode() {
298+
_selectionViewState.update { it.copy(mode = SelectionViewState.Mode.Normal) }
299+
}
300+
316301
fun onUpButtonPressed() {
317302
pixel.fire(AppPixelName.TAB_MANAGER_UP_BUTTON_PRESSED)
318303
}

0 commit comments

Comments
 (0)