1
0
Fork 0

Hide add folder option when in Desktop Bookmarks folder

master
Afzal Najam 2019-12-12 00:49:33 -05:00 committed by Emily Kager
parent 31f2e80ab0
commit b9656a978e
7 changed files with 48 additions and 23 deletions

View File

@ -25,7 +25,7 @@ class BookmarkAdapter(val emptyView: View, val interactor: BookmarkViewInteracto
RecyclerView.Adapter<BookmarkNodeViewHolder>(), SelectionHolder<BookmarkNode> {
private var tree: List<BookmarkNode> = listOf()
private var mode: BookmarkFragmentState.Mode = BookmarkFragmentState.Mode.Normal
private var mode: BookmarkFragmentState.Mode = BookmarkFragmentState.Mode.Normal()
override val selectedItems: Set<BookmarkNode> get() = mode.selectedItems
private var isFirstRun = true

View File

@ -154,8 +154,10 @@ class BookmarkFragment : LibraryPageFragment<BookmarkNode>(), UserInteractionHan
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
when (val mode = bookmarkStore.state.mode) {
BookmarkFragmentState.Mode.Normal -> {
inflater.inflate(R.menu.bookmarks_menu, menu)
is BookmarkFragmentState.Mode.Normal -> {
if (mode.showMenu) {
inflater.inflate(R.menu.bookmarks_menu, menu)
}
}
is BookmarkFragmentState.Mode.Selecting -> {
if (mode.selectedItems.any { it.type != BookmarkNodeType.ITEM }) {

View File

@ -4,6 +4,7 @@
package org.mozilla.fenix.library.bookmarks
import mozilla.appservices.places.BookmarkRoot
import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.lib.state.Action
import mozilla.components.lib.state.State
@ -22,13 +23,13 @@ class BookmarkFragmentStore(
*/
data class BookmarkFragmentState(
val tree: BookmarkNode?,
val mode: Mode = Mode.Normal,
val mode: Mode = Mode.Normal(),
val isLoading: Boolean = true
) : State {
sealed class Mode {
open val selectedItems = emptySet<BookmarkNode>()
object Normal : Mode()
data class Normal(val showMenu: Boolean = true) : Mode()
data class Selecting(override val selectedItems: Set<BookmarkNode>) : Mode()
}
}
@ -58,8 +59,10 @@ private fun bookmarkFragmentStateReducer(
val items = state.mode.selectedItems.filter { it in action.tree }
state.copy(
tree = action.tree,
mode = if (items.isEmpty()) {
BookmarkFragmentState.Mode.Normal
mode = if (BookmarkRoot.Root.id == action.tree.guid) {
BookmarkFragmentState.Mode.Normal(false)
} else if (items.isEmpty()) {
BookmarkFragmentState.Mode.Normal()
} else {
BookmarkFragmentState.Mode.Selecting(items.toSet())
},
@ -72,14 +75,14 @@ private fun bookmarkFragmentStateReducer(
val items = state.mode.selectedItems - action.item
state.copy(
mode = if (items.isEmpty()) {
BookmarkFragmentState.Mode.Normal
BookmarkFragmentState.Mode.Normal()
} else {
BookmarkFragmentState.Mode.Selecting(items)
}
)
}
BookmarkFragmentAction.DeselectAll ->
state.copy(mode = BookmarkFragmentState.Mode.Normal)
state.copy(mode = BookmarkFragmentState.Mode.Normal())
}
}

View File

@ -100,7 +100,7 @@ class BookmarkView(
val view: View = LayoutInflater.from(container.context)
.inflate(R.layout.component_bookmark, container, true)
private var mode: BookmarkFragmentState.Mode = BookmarkFragmentState.Mode.Normal
private var mode: BookmarkFragmentState.Mode = BookmarkFragmentState.Mode.Normal()
private var tree: BookmarkNode? = null
private var canGoBack = false

View File

@ -46,12 +46,12 @@ internal class BookmarkAdapterTest {
)
)
)
bookmarkAdapter.updateData(tree, BookmarkFragmentState.Mode.Normal)
bookmarkAdapter.updateData(null, BookmarkFragmentState.Mode.Normal)
bookmarkAdapter.updateData(tree, BookmarkFragmentState.Mode.Normal())
bookmarkAdapter.updateData(null, BookmarkFragmentState.Mode.Normal())
verifyOrder {
bookmarkAdapter.updateData(tree, BookmarkFragmentState.Mode.Normal)
bookmarkAdapter.updateData(tree, BookmarkFragmentState.Mode.Normal())
bookmarkAdapter.notifyItemRangeInserted(0, 3)
bookmarkAdapter.updateData(null, BookmarkFragmentState.Mode.Normal)
bookmarkAdapter.updateData(null, BookmarkFragmentState.Mode.Normal())
bookmarkAdapter.notifyItemRangeRemoved(0, 3)
}
}

View File

@ -87,7 +87,7 @@ class BookmarkFragmentInteractorTest {
@Test
fun `switch between bookmark selection modes`() {
interactor.onSelectionModeSwitch(BookmarkFragmentState.Mode.Normal)
interactor.onSelectionModeSwitch(BookmarkFragmentState.Mode.Normal())
verify {
bookmarkController.handleSelectionModeSwitch()

View File

@ -21,7 +21,7 @@ class BookmarkFragmentStoreTest {
val initialState = BookmarkFragmentState(null)
val store = BookmarkFragmentStore(initialState)
assertThat(BookmarkFragmentState(null, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
assertThat(BookmarkFragmentState(null, BookmarkFragmentState.Mode.Normal())).isEqualTo(store.state)
store.dispatch(BookmarkFragmentAction.Change(tree)).join()
@ -34,7 +34,7 @@ class BookmarkFragmentStoreTest {
val initialState = BookmarkFragmentState(tree)
val store = BookmarkFragmentStore(initialState)
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal())).isEqualTo(store.state)
store.dispatch(BookmarkFragmentAction.Change(newTree)).join()
@ -47,7 +47,7 @@ class BookmarkFragmentStoreTest {
val initialState = BookmarkFragmentState(tree)
val store = BookmarkFragmentStore(initialState)
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal())).isEqualTo(store.state)
store.dispatch(BookmarkFragmentAction.Change(tree)).join()
@ -77,7 +77,7 @@ class BookmarkFragmentStoreTest {
store.dispatch(BookmarkFragmentAction.Deselect(childItem)).join()
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal())).isEqualTo(store.state)
}
@Test
@ -102,7 +102,7 @@ class BookmarkFragmentStoreTest {
@Test
fun `deselecting while not in selecting mode does nothing`() = runBlocking {
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal())
val store = BookmarkFragmentStore(initialState)
store.dispatch(BookmarkFragmentAction.Deselect(item)).join()
@ -117,12 +117,12 @@ class BookmarkFragmentStoreTest {
store.dispatch(BookmarkFragmentAction.DeselectAll).join()
assertThat(initialState.copy(mode = BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
assertThat(initialState.copy(mode = BookmarkFragmentState.Mode.Normal())).isEqualTo(store.state)
}
@Test
fun `deselect all bookmarks when none are selected`() = runBlocking {
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal())
val store = BookmarkFragmentStore(initialState)
store.dispatch(BookmarkFragmentAction.DeselectAll)
@ -139,7 +139,7 @@ class BookmarkFragmentStoreTest {
store.state.run {
assertThat(newTree).isEqualTo(tree)
assertThat(BookmarkFragmentState.Mode.Normal).isEqualTo(mode)
assertThat(BookmarkFragmentState.Mode.Normal()).isEqualTo(mode)
}
}
@ -167,6 +167,17 @@ class BookmarkFragmentStoreTest {
assertFalse(store.state.isLoading)
}
@Test
fun `switching to Desktop Bookmarks folder sets showMenu state to false`() = runBlocking {
val initialState = BookmarkFragmentState(tree)
val store = BookmarkFragmentStore(initialState)
store.dispatch(BookmarkFragmentAction.Change(rootFolder)).join()
assertThat(rootFolder).isEqualTo(store.state.tree)
assertThat(BookmarkFragmentState.Mode.Normal(false)).isEqualTo(store.state.mode)
}
private val item = BookmarkNode(BookmarkNodeType.ITEM, "456", "123", 0, "Mozilla", "http://mozilla.org", null)
private val separator = BookmarkNode(BookmarkNodeType.SEPARATOR, "789", "123", 1, null, null, null)
private val subfolder = BookmarkNode(BookmarkNodeType.FOLDER, "987", "123", 0, "Subfolder", null, listOf())
@ -191,4 +202,13 @@ class BookmarkFragmentStoreTest {
null,
listOf(separator, subfolder)
)
private val rootFolder = BookmarkNode(
BookmarkNodeType.FOLDER,
"root________",
null,
0,
"Desktop Bookmarks",
null,
listOf(subfolder)
)
}