@@ -132,7 +132,7 @@ class TabSwitcherViewModel @Inject constructor(
132132 data object CloseAllTabsRequest : Command ()
133133 data class ShareLink (val link : String , val title : String ) : Command()
134134 data class ShareLinks (val links : List <String >) : Command()
135- data class BookmarkTabsRequest (val numTabs : Int ) : Command()
135+ data class BookmarkTabsRequest (val tabIds : List < String > ) : Command()
136136 data class ShowBookmarkToast (val numBookmarks : Int ) : Command()
137137 }
138138
@@ -164,9 +164,9 @@ class TabSwitcherViewModel @Inject constructor(
164164 _selectionViewState .update {
165165 val selectionMode = it.mode as Selection
166166 if (tab.tabId in selectionMode.selectedTabs) {
167- it.copy(mode = Selection (selectionMode.selectedTabs - tab.tabId))
167+ it.copy(mode = Selection (selectedTabs = selectionMode.selectedTabs - tab.tabId))
168168 } else {
169- it.copy(mode = Selection (selectionMode.selectedTabs + tab.tabId))
169+ it.copy(mode = Selection (selectedTabs = selectionMode.selectedTabs + tab.tabId))
170170 }
171171 }
172172 } else {
@@ -193,7 +193,7 @@ class TabSwitcherViewModel @Inject constructor(
193193 (_selectionViewState .value.mode as ? Selection )?.let { selectionMode ->
194194 if (tab.tabId in selectionMode.selectedTabs) {
195195 _selectionViewState .update {
196- it.copy(mode = Selection (selectionMode.selectedTabs - tab.tabId))
196+ it.copy(mode = Selection (selectedTabs = selectionMode.selectedTabs - tab.tabId))
197197 }
198198 }
199199 }
@@ -204,7 +204,7 @@ class TabSwitcherViewModel @Inject constructor(
204204
205205 (_selectionViewState .value.mode as ? Selection )?.let { selectionMode ->
206206 _selectionViewState .update {
207- it.copy(mode = Selection (selectionMode.selectedTabs + tab.tabId))
207+ it.copy(mode = Selection (selectedTabs = selectionMode.selectedTabs + tab.tabId))
208208 }
209209 }
210210 }
@@ -222,7 +222,7 @@ class TabSwitcherViewModel @Inject constructor(
222222 }
223223
224224 fun onSelectAllTabs () {
225- _selectionViewState .update { it.copy(mode = Selection (tabItems.map { tab -> tab.id })) }
225+ _selectionViewState .update { it.copy(mode = Selection (selectedTabs = tabItems.map { tab -> tab.id })) }
226226 }
227227
228228 fun onDeselectAllTabs () {
@@ -248,12 +248,14 @@ class TabSwitcherViewModel @Inject constructor(
248248
249249 fun onBookmarkSelectedTabs () {
250250 when (val mode = selectionViewState.value.mode) {
251- is SelectionViewState .Mode .Normal -> {
252- command.value = BookmarkTabsRequest (1 )
251+ is Normal -> {
252+ activeTab.value?.tabId?.let { tabId ->
253+ command.value = BookmarkTabsRequest (listOf (tabId))
254+ }
253255 }
254256
255- is SelectionViewState . Mode . Selection -> {
256- command.value = BookmarkTabsRequest (mode.selectedTabs.size )
257+ is Selection -> {
258+ command.value = BookmarkTabsRequest (mode.selectedTabs)
257259 }
258260 }
259261 }
@@ -272,36 +274,13 @@ class TabSwitcherViewModel @Inject constructor(
272274 fun onCloseOtherTabs () {
273275 }
274276
275- fun onBookmarkTabsConfirmed (numTabs : Int ) {
277+ fun onBookmarkTabsConfirmed (tabIds : List < String > ) {
276278 viewModelScope.launch {
277- val numBookmarkedTabs = when (val mode = selectionViewState.value.mode) {
278- is SelectionViewState .Mode .Selection -> {
279- // bookmark selected tabs (or all tabs if none selected)
280- if (mode.selectedTabs.isNotEmpty()) {
281- bookmarkTabs(mode.selectedTabs)
282- } else {
283- bookmarkAllTabs()
284- }
285- }
286-
287- SelectionViewState .Mode .Normal -> {
288- if (numTabs == 1 ) {
289- activeTab.value?.tabId?.let { bookmarkTabs(listOf (it)) } ? : 0
290- } else {
291- bookmarkAllTabs()
292- }
293- }
294- }
279+ val numBookmarkedTabs = bookmarkTabs(tabIds)
295280 command.value = ShowBookmarkToast (numBookmarkedTabs)
296281 }
297282 }
298283
299- private suspend fun bookmarkAllTabs (): Int {
300- return tabSwitcherItems.value?.filterIsInstance<TabSwitcherItem .Tab >()?.let { tabIds ->
301- bookmarkTabs(tabIds.map { it.id })
302- } ? : 0
303- }
304-
305284 private suspend fun bookmarkTabs (tabIds : List <String >): Int {
306285 val results = tabIds.map { tabId ->
307286 viewModelScope.async {
@@ -323,21 +302,25 @@ class TabSwitcherViewModel @Inject constructor(
323302 pixel.fire(AppPixelName .TAB_MANAGER_MENU_CLOSE_ALL_TABS_CONFIRMED )
324303
325304 // Trigger a normal mode when there are no tabs
326- _selectionViewState .update { it.copy(mode = Normal ) }
305+ triggerNormalMode()
327306 }
328307 }
329308
330309 fun onEmptyAreaClicked () {
331310 if (tabManagerFeatureFlags.multiSelection().isEnabled() && _selectionViewState .value.mode is Selection ) {
332- _selectionViewState .update { it.copy(mode = Normal ) }
311+ triggerNormalMode()
333312 }
334313 }
335314
315+ private fun triggerNormalMode () {
316+ _selectionViewState .update { it.copy(mode = Normal ) }
317+ }
318+
336319 fun onUpButtonPressed () {
337320 pixel.fire(AppPixelName .TAB_MANAGER_UP_BUTTON_PRESSED )
338321
339322 if (tabManagerFeatureFlags.multiSelection().isEnabled() && _selectionViewState .value.mode is Selection ) {
340- _selectionViewState .update { it.copy(mode = Normal ) }
323+ triggerNormalMode()
341324 } else {
342325 command.value = Command .Close
343326 }
@@ -347,7 +330,7 @@ class TabSwitcherViewModel @Inject constructor(
347330 pixel.fire(AppPixelName .TAB_MANAGER_BACK_BUTTON_PRESSED )
348331
349332 if (tabManagerFeatureFlags.multiSelection().isEnabled() && _selectionViewState .value.mode is Selection ) {
350- _selectionViewState .update { it.copy(mode = Normal ) }
333+ triggerNormalMode()
351334 } else {
352335 command.value = Command .Close
353336 }
@@ -409,7 +392,7 @@ class TabSwitcherViewModel @Inject constructor(
409392
410393 private suspend fun saveSiteBookmark (tabId : String ) = withContext(dispatcherProvider.io()) {
411394 var bookmark: Bookmark ? = null
412- (tabSwitcherItems.value?.firstOrNull { it.id == tabId } as ? TabSwitcherItem . Tab )?.let { tab ->
395+ (tabSwitcherItems.value?.firstOrNull { it.id == tabId } as ? Tab )?.let { tab ->
413396 tab.tabEntity.url?.let { url ->
414397 if (url.isNotBlank()) {
415398 // Only bookmark new sites
0 commit comments