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 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))
@ -151,7 +151,7 @@ private fun assertBookmarkNameEditBox() =
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
private fun assertBookmarkFolderSelector() =
onView(withId(R.id.bookmarkFolderSelector))
onView(withId(R.id.bookmarkParentFolderSelector))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
private fun assertBookmarkURLEditBox() =

View File

@ -8,16 +8,19 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.View.GONE
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
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.Main
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.appservices.places.BookmarkRoot
import org.mozilla.fenix.R
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.
*/
class AddBookmarkFolderFragment : Fragment(R.layout.fragment_add_bookmark_folder) {
class AddBookmarkFolderFragment : Fragment(R.layout.fragment_edit_bookmark) {
private val sharedViewModel: BookmarksSharedViewModel by activityViewModels {
ViewModelProvider.NewInstanceFactory() // this is a workaround for #4652
@ -39,27 +42,36 @@ class AddBookmarkFolderFragment : Fragment(R.layout.fragment_add_bookmark_folder
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() {
super.onResume()
(activity as AppCompatActivity).title =
getString(R.string.bookmark_add_folder_fragment_label)
(activity as AppCompatActivity).supportActionBar?.show()
lifecycleScope.launch(IO) {
sharedViewModel.selectedFolder = sharedViewModel.selectedFolder
?: requireComponents.core.bookmarksStorage.getTree(BookmarkRoot.Mobile.id)
launch(Main) {
bookmarkAddFolderParentSelector.text = sharedViewModel.selectedFolder!!.title
bookmarkAddFolderParentSelector.setOnClickListener {
nav(
R.id.bookmarkAddFolderFragment,
AddBookmarkFolderFragmentDirections
.actionBookmarkAddFolderFragmentToBookmarkSelectFolderFragment(
BookmarkRoot.Root.id,
true
)
)
}
lifecycleScope.launch(Main) {
sharedViewModel.selectedFolder = withContext(IO) {
sharedViewModel.selectedFolder
?: requireComponents.core.bookmarksStorage.getTree(BookmarkRoot.Mobile.id)
}
bookmarkParentFolderSelector.text = sharedViewModel.selectedFolder!!.title
bookmarkParentFolderSelector.setOnClickListener {
nav(
R.id.bookmarkAddFolderFragment,
AddBookmarkFolderFragmentDirections
.actionBookmarkAddFolderFragmentToBookmarkSelectFolderFragment(
BookmarkRoot.Root.id,
true
)
)
}
}
}
@ -71,18 +83,18 @@ class AddBookmarkFolderFragment : Fragment(R.layout.fragment_add_bookmark_folder
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.confirm_add_folder_button -> {
if (bookmarkAddFolderTitleEdit.text.isNullOrBlank()) {
bookmarkAddFolderTitleEdit.error =
if (bookmarkNameEdit.text.isNullOrBlank()) {
bookmarkNameEdit.error =
getString(R.string.bookmark_empty_title_error)
return true
}
lifecycleScope.launch(IO) {
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)
requireComponents.analytics.metrics.track(Event.AddBookmarkFolder)
launch(Main) {
withContext(Main) {
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)
if (sharedViewModel.selectedFolder != null && bookmarkNode.title != null) {
updateBookmarkNode(bookmarkNode.title to bookmarkNode.url)
updateBookmarkNode(bookmarkNode.title, bookmarkNode.url)
}
}
bookmarkParent?.let { node ->
bookmarkFolderSelector.text = node.title
bookmarkFolderSelector.setOnClickListener {
bookmarkParentFolderSelector.text = node.title
bookmarkParentFolderSelector.setOnClickListener {
sharedViewModel.selectedFolder = null
nav(
R.id.bookmarkEditFragment,
@ -132,13 +132,13 @@ class EditBookmarkFragment : Fragment(R.layout.fragment_edit_bookmark) {
BiFunction { name: CharSequence, url: CharSequence ->
Pair(name.toString(), url.toString())
})
.filter { it.first.isNotBlank() }
.filter { (name) -> name.isNotBlank() }
.debounce(debouncePeriodInMs, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this@EditBookmarkFragment)))
.subscribe {
updateBookmarkNode(it)
.subscribe { (name, url) ->
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) {
try {
requireComponents.let {
if (pair != Pair(bookmarkNode?.title, bookmarkNode?.url)) {
it.analytics.metrics.track(Event.EditedBookmark)
requireComponents.let { components ->
if (title != bookmarkNode?.title || url != bookmarkNode?.url) {
components.analytics.metrics.track(Event.EditedBookmark)
}
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,
BookmarkInfo(
sharedViewModel.selectedFolder?.guid ?: bookmarkNode!!.parentGuid,
bookmarkNode?.position,
pair.first,
if (bookmarkNode?.type == BookmarkNodeType.ITEM) pair.second else null
title,
if (bookmarkNode?.type == BookmarkNodeType.ITEM) url else null
)
)
}
} catch (e: UrlParseFailed) {
launch(Main) {
withContext(Main) {
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
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="16dp">
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/bookmark_name_label"
@ -15,9 +16,9 @@
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:textAllCaps="true"
android:labelFor="@id/bookmarkNameEdit" />
<org.mozilla.fenix.utils.ClearableEditText
@ -68,19 +69,18 @@
android:textColor="?primaryText"
android:textSize="12sp"
android:textAllCaps="true"
android:labelFor="@id/bookmarkFolderSelector" />
android:labelFor="@id/bookmarkParentFolderSelector" />
<TextView
android:id="@+id/bookmarkFolderSelector"
android:id="@+id/bookmarkParentFolderSelector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:textColor="?secondaryText"
android:drawableStart="@drawable/ic_folder_icon"
android:drawablePadding="10dp"
android:drawableTint="?primaryText"
tools:text="Mobile Bookmarks"
tools:targetApi="m" />
app:drawableTint="?primaryText"
android:textColor="?secondaryText"
android:textSize="16sp"
tools:text="Mobile Bookmarks" />
</LinearLayout>