Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class BookmarkFragment : Fragment() {
BookmarksSyncMiddleware(requireComponents.backgroundServices.syncStore, lifecycleScope),
BrowserToolbarSyncToBookmarksMiddleware(toolbarStore, lifecycleScope),
BookmarksMiddleware(
lifecycleScope = lifecycleScope,
bookmarksStorage = requireContext().bookmarkStorage,
clipboardManager = requireActivity().getSystemService(),
addNewTabUseCase = requireComponents.useCases.tabsUseCases.addTab,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.navigation.NavController
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.appservices.places.BookmarkRoot
Expand Down Expand Up @@ -51,6 +52,7 @@ private const val WARN_OPEN_ALL_SIZE = 15
* @param reportResultGlobally Invoked when an error occurs that needs to be reported even if the
* feature goes out of scope.
* @param ioDispatcher Coroutine dispatcher for IO operations.
* @param lifecycleScope lifecycle bound CoroutineScope scope used to cancel jobs when leaving bookmarks.
*/
@Suppress("LongParameterList", "LargeClass")
internal class BookmarksMiddleware(
Expand All @@ -73,9 +75,10 @@ internal class BookmarksMiddleware(
private val lastSavedFolderCache: LastSavedFolderCache,
private val reportResultGlobally: (BookmarksGlobalResultReport) -> Unit,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
lifecycleScope: CoroutineScope,
) : Middleware<BookmarksState, BookmarksAction> {

private val scope = CoroutineScope(ioDispatcher)
private val scope = CoroutineScope(lifecycleScope.coroutineContext + ioDispatcher)

@Suppress("LongMethod", "CognitiveComplexMethod", "CyclomaticComplexMethod")
override fun invoke(
Expand Down Expand Up @@ -278,6 +281,10 @@ internal class BookmarksMiddleware(
}
// list screen cases
preReductionState.selectedItems.isNotEmpty() -> { /* noop */ }
// User is clicking back before we've loaded anything
preReductionState.currentFolder.guid.isEmpty() -> {
exitBookmarks()
}
preReductionState.currentFolder.guid != BookmarkRoot.Mobile.id -> {
scope.launch {
val parentFolderGuid = withContext(ioDispatcher) {
Expand Down Expand Up @@ -516,6 +523,8 @@ internal class BookmarksMiddleware(
private fun Store<BookmarksState, BookmarksAction>.tryDispatchLoadFor(guid: String) =
scope.launch {
bookmarksStorage.getTree(guid).getOrNull()?.let { rootNode ->
ensureActive()

val folder = BookmarkItem.Folder(
guid = guid,
title = resolveFolderTitle(rootNode),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavHostController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
Expand Down Expand Up @@ -111,6 +112,7 @@ class EditBookmarkFragment : Fragment(R.layout.fragment_edit_bookmark) {
AppAction.BookmarkAction.BookmarkOperationResultReported(it),
)
},
lifecycleScope = lifecycleScope,
),
),
bookmarkToLoad = args.guidToEdit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import mozilla.appservices.places.BookmarkRoot
Expand Down Expand Up @@ -50,6 +52,8 @@ class BookmarksMiddlewareTest {
@get:Rule
val coroutineRule = MainCoroutineRule()

private val testDispatcher = StandardTestDispatcher()

private lateinit var bookmarksStorage: BookmarksStorage
private lateinit var clipboardManager: ClipboardManager
private lateinit var addNewTabUseCase: TabsUseCases.AddNewTabUseCase
Expand Down Expand Up @@ -2064,6 +2068,7 @@ class BookmarksMiddlewareTest {
saveBookmarkSortOrder = saveSortOrder,
lastSavedFolderCache = lastSavedFolderCache,
reportResultGlobally = reportResultGlobally,
lifecycleScope = CoroutineScope(testDispatcher),
Comment thread
boek marked this conversation as resolved.
)

private fun BookmarksMiddleware.makeStore(
Expand Down