1
0
Fork 0

For #3366 - Use one layout for edit/add bookmark (#5900)

master
Tiger Oakes 2019-10-21 10:06:08 -07:00 committed by Jeff Boek
parent 5ce1968d67
commit 4e3a3665d6
5 changed files with 60 additions and 104 deletions

View File

@ -101,7 +101,7 @@ private fun folderTitle() = onView(withId(R.id.title))
private fun addFolderButton() = onView(withId(R.id.add_bookmark_folder)) private fun addFolderButton() = onView(withId(R.id.add_bookmark_folder))
private fun addFolderTitleField() = onView(withId(R.id.bookmarkAddFolderTitleEdit)) private fun addFolderTitleField() = onView(withId(R.id.bookmarkNameEdit))
private fun saveFolderButton() = onView(withId(R.id.confirm_add_folder_button)) private fun saveFolderButton() = onView(withId(R.id.confirm_add_folder_button))
@ -151,7 +151,7 @@ private fun assertBookmarkNameEditBox() =
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
private fun assertBookmarkFolderSelector() = private fun assertBookmarkFolderSelector() =
onView(withId(R.id.bookmarkFolderSelector)) onView(withId(R.id.bookmarkParentFolderSelector))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
private fun assertBookmarkURLEditBox() = private fun assertBookmarkURLEditBox() =

View File

@ -8,16 +8,19 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import android.view.View.GONE
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_add_bookmark_folder.* import kotlinx.android.synthetic.main.fragment_edit_bookmark.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.appservices.places.BookmarkRoot import mozilla.appservices.places.BookmarkRoot
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.metrics.Event
@ -28,7 +31,7 @@ import org.mozilla.fenix.library.bookmarks.BookmarksSharedViewModel
/** /**
* Menu to create a new bookmark folder. * Menu to create a new bookmark folder.
*/ */
class AddBookmarkFolderFragment : Fragment(R.layout.fragment_add_bookmark_folder) { class AddBookmarkFolderFragment : Fragment(R.layout.fragment_edit_bookmark) {
private val sharedViewModel: BookmarksSharedViewModel by activityViewModels { private val sharedViewModel: BookmarksSharedViewModel by activityViewModels {
ViewModelProvider.NewInstanceFactory() // this is a workaround for #4652 ViewModelProvider.NewInstanceFactory() // this is a workaround for #4652
@ -39,27 +42,36 @@ class AddBookmarkFolderFragment : Fragment(R.layout.fragment_add_bookmark_folder
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
/**
* Hides fields for bookmark items present in the shared layout file.
*/
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
bookmarkUrlLabel.visibility = GONE
bookmarkUrlEdit.visibility = GONE
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
(activity as AppCompatActivity).title = (activity as AppCompatActivity).title =
getString(R.string.bookmark_add_folder_fragment_label) getString(R.string.bookmark_add_folder_fragment_label)
(activity as AppCompatActivity).supportActionBar?.show() (activity as AppCompatActivity).supportActionBar?.show()
lifecycleScope.launch(IO) { lifecycleScope.launch(Main) {
sharedViewModel.selectedFolder = sharedViewModel.selectedFolder sharedViewModel.selectedFolder = withContext(IO) {
?: requireComponents.core.bookmarksStorage.getTree(BookmarkRoot.Mobile.id) sharedViewModel.selectedFolder
launch(Main) { ?: requireComponents.core.bookmarksStorage.getTree(BookmarkRoot.Mobile.id)
bookmarkAddFolderParentSelector.text = sharedViewModel.selectedFolder!!.title }
bookmarkAddFolderParentSelector.setOnClickListener {
nav( bookmarkParentFolderSelector.text = sharedViewModel.selectedFolder!!.title
R.id.bookmarkAddFolderFragment, bookmarkParentFolderSelector.setOnClickListener {
AddBookmarkFolderFragmentDirections nav(
.actionBookmarkAddFolderFragmentToBookmarkSelectFolderFragment( R.id.bookmarkAddFolderFragment,
BookmarkRoot.Root.id, AddBookmarkFolderFragmentDirections
true .actionBookmarkAddFolderFragmentToBookmarkSelectFolderFragment(
) BookmarkRoot.Root.id,
) true
} )
)
} }
} }
} }
@ -71,18 +83,18 @@ class AddBookmarkFolderFragment : Fragment(R.layout.fragment_add_bookmark_folder
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.confirm_add_folder_button -> { R.id.confirm_add_folder_button -> {
if (bookmarkAddFolderTitleEdit.text.isNullOrBlank()) { if (bookmarkNameEdit.text.isNullOrBlank()) {
bookmarkAddFolderTitleEdit.error = bookmarkNameEdit.error =
getString(R.string.bookmark_empty_title_error) getString(R.string.bookmark_empty_title_error)
return true return true
} }
lifecycleScope.launch(IO) { lifecycleScope.launch(IO) {
val newGuid = requireComponents.core.bookmarksStorage.addFolder( val newGuid = requireComponents.core.bookmarksStorage.addFolder(
sharedViewModel.selectedFolder!!.guid, bookmarkAddFolderTitleEdit.text.toString(), null sharedViewModel.selectedFolder!!.guid, bookmarkNameEdit.text.toString(), null
) )
sharedViewModel.selectedFolder = requireComponents.core.bookmarksStorage.getTree(newGuid) sharedViewModel.selectedFolder = requireComponents.core.bookmarksStorage.getTree(newGuid)
requireComponents.analytics.metrics.track(Event.AddBookmarkFolder) requireComponents.analytics.metrics.track(Event.AddBookmarkFolder)
launch(Main) { withContext(Main) {
Navigation.findNavController(requireActivity(), R.id.container).popBackStack() Navigation.findNavController(requireActivity(), R.id.container).popBackStack()
} }
} }

View File

@ -99,13 +99,13 @@ class EditBookmarkFragment : Fragment(R.layout.fragment_edit_bookmark) {
bookmarkUrlEdit.setText(bookmarkNode.url) bookmarkUrlEdit.setText(bookmarkNode.url)
if (sharedViewModel.selectedFolder != null && bookmarkNode.title != null) { if (sharedViewModel.selectedFolder != null && bookmarkNode.title != null) {
updateBookmarkNode(bookmarkNode.title to bookmarkNode.url) updateBookmarkNode(bookmarkNode.title, bookmarkNode.url)
} }
} }
bookmarkParent?.let { node -> bookmarkParent?.let { node ->
bookmarkFolderSelector.text = node.title bookmarkParentFolderSelector.text = node.title
bookmarkFolderSelector.setOnClickListener { bookmarkParentFolderSelector.setOnClickListener {
sharedViewModel.selectedFolder = null sharedViewModel.selectedFolder = null
nav( nav(
R.id.bookmarkEditFragment, R.id.bookmarkEditFragment,
@ -132,13 +132,13 @@ class EditBookmarkFragment : Fragment(R.layout.fragment_edit_bookmark) {
BiFunction { name: CharSequence, url: CharSequence -> BiFunction { name: CharSequence, url: CharSequence ->
Pair(name.toString(), url.toString()) Pair(name.toString(), url.toString())
}) })
.filter { it.first.isNotBlank() } .filter { (name) -> name.isNotBlank() }
.debounce(debouncePeriodInMs, TimeUnit.MILLISECONDS) .debounce(debouncePeriodInMs, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this@EditBookmarkFragment))) .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this@EditBookmarkFragment)))
.subscribe { .subscribe { (name, url) ->
updateBookmarkNode(it) updateBookmarkNode(name, url)
} }
} }
@ -190,28 +190,28 @@ class EditBookmarkFragment : Fragment(R.layout.fragment_edit_bookmark) {
} }
} }
private fun updateBookmarkNode(pair: Pair<String?, String?>) { private fun updateBookmarkNode(title: String?, url: String?) {
lifecycleScope.launch(IO) { lifecycleScope.launch(IO) {
try { try {
requireComponents.let { requireComponents.let { components ->
if (pair != Pair(bookmarkNode?.title, bookmarkNode?.url)) { if (title != bookmarkNode?.title || url != bookmarkNode?.url) {
it.analytics.metrics.track(Event.EditedBookmark) components.analytics.metrics.track(Event.EditedBookmark)
} }
if (sharedViewModel.selectedFolder != null) { if (sharedViewModel.selectedFolder != null) {
it.analytics.metrics.track(Event.MovedBookmark) components.analytics.metrics.track(Event.MovedBookmark)
} }
it.core.bookmarksStorage.updateNode( components.core.bookmarksStorage.updateNode(
guidToEdit, guidToEdit,
BookmarkInfo( BookmarkInfo(
sharedViewModel.selectedFolder?.guid ?: bookmarkNode!!.parentGuid, sharedViewModel.selectedFolder?.guid ?: bookmarkNode!!.parentGuid,
bookmarkNode?.position, bookmarkNode?.position,
pair.first, title,
if (bookmarkNode?.type == BookmarkNodeType.ITEM) pair.second else null if (bookmarkNode?.type == BookmarkNodeType.ITEM) url else null
) )
) )
} }
} catch (e: UrlParseFailed) { } catch (e: UrlParseFailed) {
launch(Main) { withContext(Main) {
bookmarkUrlEdit.error = getString(R.string.bookmark_invalid_url_error) bookmarkUrlEdit.error = getString(R.string.bookmark_invalid_url_error)
} }
} }

View File

@ -1,56 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/bookmark_add_folder_title_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/bookmark_name_label"
android:textAllCaps="true"
android:textColor="?primaryText"
android:textSize="12sp"
android:labelFor="@id/bookmarkAddFolderTitleEdit" />
<org.mozilla.fenix.utils.ClearableEditText
android:id="@+id/bookmarkAddFolderTitleEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:inputType="textAutoComplete"
android:textColor="?secondaryText"
android:textSize="15sp"
tools:text="News" />
<TextView
android:id="@+id/bookmark_add_folder_parent_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/bookmark_folder_label"
android:textAllCaps="true"
android:textColor="?primaryText"
android:textSize="12sp" />
<TextView
android:id="@+id/bookmarkAddFolderParentSelector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:drawableStart="@drawable/ic_folder_icon"
android:drawablePadding="10dp"
android:drawableTint="?primaryText"
android:textColor="?secondaryText"
android:textSize="16sp"
tools:targetApi="m"
tools:text="Mobile Bookmarks" />
</LinearLayout>

View File

@ -3,11 +3,12 @@
- License, v. 2.0. If a copy of the MPL was not distributed with this - License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:layout_margin="16dp"
android:layout_margin="16dp"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/bookmark_name_label" android:id="@+id/bookmark_name_label"
@ -15,9 +16,9 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:text="@string/bookmark_name_label" android:text="@string/bookmark_name_label"
android:textAllCaps="true"
android:textColor="?primaryText" android:textColor="?primaryText"
android:textSize="12sp" android:textSize="12sp"
android:textAllCaps="true"
android:labelFor="@id/bookmarkNameEdit" /> android:labelFor="@id/bookmarkNameEdit" />
<org.mozilla.fenix.utils.ClearableEditText <org.mozilla.fenix.utils.ClearableEditText
@ -68,19 +69,18 @@
android:textColor="?primaryText" android:textColor="?primaryText"
android:textSize="12sp" android:textSize="12sp"
android:textAllCaps="true" android:textAllCaps="true"
android:labelFor="@id/bookmarkFolderSelector" /> android:labelFor="@id/bookmarkParentFolderSelector" />
<TextView <TextView
android:id="@+id/bookmarkFolderSelector" android:id="@+id/bookmarkParentFolderSelector"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:textSize="16sp"
android:textColor="?secondaryText"
android:drawableStart="@drawable/ic_folder_icon" android:drawableStart="@drawable/ic_folder_icon"
android:drawablePadding="10dp" android:drawablePadding="10dp"
android:drawableTint="?primaryText" app:drawableTint="?primaryText"
tools:text="Mobile Bookmarks" android:textColor="?secondaryText"
tools:targetApi="m" /> android:textSize="16sp"
tools:text="Mobile Bookmarks" />
</LinearLayout> </LinearLayout>