1
0
Fork 0

For #3323 Runs PublicSuffixList synchronously

master
Sawyer Blatz 2019-06-10 13:46:55 -07:00 committed by Emily Kager
parent a58a77317e
commit decacbfc97
10 changed files with 136 additions and 154 deletions

View File

@ -57,6 +57,8 @@ open class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
components.publicSuffixList.prefetch()
browsingModeManager = createBrowsingModeManager()
themeManager = createThemeManager(
when (browsingModeManager.isPrivate) {

View File

@ -747,22 +747,20 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope {
private fun showSaveToCollection() {
val context = context ?: return
getSessionById()?.let {
launch(Dispatchers.Main) {
val tabs = Tab(it.id, it.url, it.url.urlToTrimmedHost(context), it.title)
val viewModel = activity?.run {
ViewModelProviders.of(this).get(CreateCollectionViewModel::class.java)
}
viewModel?.tabs = listOf(tabs)
val selectedSet = mutableSetOf(tabs)
viewModel?.selectedTabs = selectedSet
viewModel?.tabCollections = requireComponents.core.tabCollectionStorage.cachedTabCollections.reversed()
viewModel?.saveCollectionStep =
viewModel?.tabCollections?.getStepForCollectionsSize() ?: SaveCollectionStep.SelectCollection
viewModel?.snackbarAnchorView = nestedScrollQuickAction
view?.let {
val directions = BrowserFragmentDirections.actionBrowserFragmentToCreateCollectionFragment()
nav(R.id.browserFragment, directions)
}
val tabs = Tab(it.id, it.url, it.url.urlToTrimmedHost(context), it.title)
val viewModel = activity?.run {
ViewModelProviders.of(this).get(CreateCollectionViewModel::class.java)
}
viewModel?.tabs = listOf(tabs)
val selectedSet = mutableSetOf(tabs)
viewModel?.selectedTabs = selectedSet
viewModel?.tabCollections = requireComponents.core.tabCollectionStorage.cachedTabCollections.reversed()
viewModel?.saveCollectionStep =
viewModel?.tabCollections?.getStepForCollectionsSize() ?: SaveCollectionStep.SelectCollection
viewModel?.snackbarAnchorView = nestedScrollQuickAction
view?.let {
val directions = BrowserFragmentDirections.actionBrowserFragmentToCreateCollectionFragment()
nav(R.id.browserFragment, directions)
}
}
}

View File

@ -25,7 +25,6 @@ import kotlinx.android.synthetic.main.component_collection_creation.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import mozilla.components.support.ktx.android.view.hideKeyboard
import mozilla.components.support.ktx.android.view.showKeyboard
import org.mozilla.fenix.R
@ -268,18 +267,16 @@ class CollectionCreationUIView(
is SaveCollectionStep.RenameCollection -> {
view.tab_list.isClickable = false
launch(Dispatchers.Main) {
it.selectedTabCollection?.let { tabCollection ->
tabCollection.tabs.map { tab ->
Tab(
tab.id.toString(),
tab.url,
tab.url.urlToTrimmedHost(view.context),
tab.title
)
}.let { tabs ->
collectionCreationTabListAdapter.updateData(tabs, tabs.toSet(), true)
}
it.selectedTabCollection?.let { tabCollection ->
tabCollection.tabs.map { tab ->
Tab(
tab.id.toString(),
tab.url,
tab.url.urlToTrimmedHost(view.context),
tab.title
)
}.let { tabs ->
collectionCreationTabListAdapter.updateData(tabs, tabs.toSet(), true)
}
}

View File

@ -16,7 +16,6 @@ import kotlinx.android.synthetic.main.collections_list_item.view.collection_icon
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.mozilla.fenix.R
import org.mozilla.fenix.components.description
import org.mozilla.fenix.home.sessioncontrol.Tab
@ -82,18 +81,16 @@ class CollectionViewHolder(
fun bind(collection: TabCollection) {
this.collection = collection
launch(Dispatchers.Main) {
view.collection_item.text = collection.title
view.collection_description.text = collection.description(view.context)
view.collection_item.text = collection.title
view.collection_description.text = collection.description(view.context)
view.collection_icon.setColorFilter(
ContextCompat.getColor(
view.context,
getIconColor(collection.id)
),
android.graphics.PorterDuff.Mode.SRC_IN
)
}
view.collection_icon.setColorFilter(
ContextCompat.getColor(
view.context,
getIconColor(collection.id)
),
android.graphics.PorterDuff.Mode.SRC_IN
)
}
@Suppress("ComplexMethod", "MagicNumber")

View File

@ -91,7 +91,7 @@ class TabCollectionStorage(
}
}
suspend fun TabCollection.description(context: Context): String {
fun TabCollection.description(context: Context): String {
return this.tabs
.map { it.url.urlToTrimmedHost(context).capitalize() }
.map {

View File

@ -8,6 +8,7 @@ package org.mozilla.fenix.ext
import android.content.Context
import androidx.core.net.toUri
import kotlinx.coroutines.runBlocking
import java.net.MalformedURLException
import java.net.URL
import mozilla.components.support.ktx.android.net.hostWithoutCommonPrefixes
@ -34,10 +35,12 @@ fun String?.getHostFromUrl(): String? = try {
/**
* Trim a host's prefix and suffix
*/
suspend fun String.urlToTrimmedHost(context: Context): String {
fun String.urlToTrimmedHost(context: Context): String {
return try {
val host = toUri().hostWithoutCommonPrefixes ?: return this
context.components.publicSuffixList.stripPublicSuffix(host).await()
runBlocking {
context.components.publicSuffixList.stripPublicSuffix(host).await()
}
} catch (e: MalformedURLException) {
this
}

View File

@ -283,15 +283,13 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver {
}
}
launch(Dispatchers.Main) {
getManagedEmitter<SessionControlChange>().onNext(
SessionControlChange.Change(
tabs = getListOfTabs(sessionManager = requireComponents.core.sessionManager),
mode = currentMode(),
collections = requireComponents.core.tabCollectionStorage.cachedTabCollections
)
getManagedEmitter<SessionControlChange>().onNext(
SessionControlChange.Change(
tabs = getListOfTabs(sessionManager = requireComponents.core.sessionManager),
mode = currentMode(),
collections = requireComponents.core.tabCollectionStorage.cachedTabCollections
)
}
)
requireComponents.core.tabCollectionStorage.register(collectionStorageObserver, this)
sessionObserver.onStart()
@ -566,63 +564,59 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver {
}
private fun removeTabWithUndo(sessionId: String) {
launch(Dispatchers.Main) {
val sessionManager = requireComponents.core.sessionManager
val sessionManager = requireComponents.core.sessionManager
// Update the UI with the tab removed, but don't remove it from storage yet
getManagedEmitter<SessionControlChange>().onNext(
// Update the UI with the tab removed, but don't remove it from storage yet
getManagedEmitter<SessionControlChange>().onNext(
SessionControlChange.TabsChange(
sessionManager.sessions
.filter { (activity as HomeActivity).browsingModeManager.isPrivate == it.private }
.filter { it.id != sessionId }
.map {
val selected = it == sessionManager.selectedSession
Tab(
it.id,
it.url,
it.url.urlToTrimmedHost(context!!),
it.title,
selected,
it.thumbnail
)
}
)
)
val deleteOperation: (suspend () -> Unit) = {
sessionManager.findSessionById(sessionId)
?.let { session ->
sessionManager.remove(session)
SessionControlChange.TabsChange(
sessionManager.sessions
.filter { (activity as HomeActivity).browsingModeManager.isPrivate == it.private }
.filter { it.id != sessionId }
.map {
val selected = it == sessionManager.selectedSession
Tab(
it.id,
it.url,
it.url.urlToTrimmedHost(context!!),
it.title,
selected,
it.thumbnail
)
}
}
deleteSessionJob = deleteOperation
allowUndo(
view!!, getString(R.string.snackbar_tab_deleted),
getString(R.string.snackbar_deleted_undo), {
deleteSessionJob = null
emitSessionChanges()
},
operation = deleteOperation
)
)
val deleteOperation: (suspend () -> Unit) = {
sessionManager.findSessionById(sessionId)
?.let { session ->
sessionManager.remove(session)
}
}
deleteSessionJob = deleteOperation
allowUndo(
view!!, getString(R.string.snackbar_tab_deleted),
getString(R.string.snackbar_deleted_undo), {
deleteSessionJob = null
emitSessionChanges()
},
operation = deleteOperation
)
}
private fun emitSessionChanges() {
val sessionManager = context?.components?.core?.sessionManager ?: return
launch(Dispatchers.Main) {
getManagedEmitter<SessionControlChange>().onNext(
SessionControlChange.TabsChange(
getListOfTabs(sessionManager)
)
getManagedEmitter<SessionControlChange>().onNext(
SessionControlChange.TabsChange(
getListOfTabs(sessionManager)
)
}
)
}
private suspend fun getListOfTabs(sessionManager: SessionManager): List<Tab> {
private fun getListOfTabs(sessionManager: SessionManager): List<Tab> {
val context = context ?: return listOf()
return sessionManager.sessions
.filter { (activity as HomeActivity).browsingModeManager.isPrivate == it.private }
@ -653,27 +647,25 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver {
val context = context?.let { it } ?: return
launch(Dispatchers.Main) {
val tabs = requireComponents.core.sessionManager.sessions.filter { !it.private }
.map { Tab(it.id, it.url, it.url.urlToTrimmedHost(context), it.title) }
val tabs = requireComponents.core.sessionManager.sessions.filter { !it.private }
.map { Tab(it.id, it.url, it.url.urlToTrimmedHost(context), it.title) }
val viewModel = activity?.run {
ViewModelProviders.of(this).get(CreateCollectionViewModel::class.java)
}
viewModel?.tabs = tabs
val selectedTabs =
tabs.find { tab -> tab.sessionId == selectedTabId } ?: if (tabs.size == 1) tabs[0] else null
val selectedSet = if (selectedTabs == null) mutableSetOf() else mutableSetOf(selectedTabs)
viewModel?.selectedTabs = selectedSet
viewModel?.tabCollections = requireComponents.core.tabCollectionStorage.cachedTabCollections.reversed()
viewModel?.selectedTabCollection = selectedTabCollection
viewModel?.saveCollectionStep =
step ?: viewModel?.getStepForTabsAndCollectionSize() ?: SaveCollectionStep.SelectTabs
val viewModel = activity?.run {
ViewModelProviders.of(this).get(CreateCollectionViewModel::class.java)
}
viewModel?.tabs = tabs
val selectedTabs =
tabs.find { tab -> tab.sessionId == selectedTabId } ?: if (tabs.size == 1) tabs[0] else null
val selectedSet = if (selectedTabs == null) mutableSetOf() else mutableSetOf(selectedTabs)
viewModel?.selectedTabs = selectedSet
viewModel?.tabCollections = requireComponents.core.tabCollectionStorage.cachedTabCollections.reversed()
viewModel?.selectedTabCollection = selectedTabCollection
viewModel?.saveCollectionStep =
step ?: viewModel?.getStepForTabsAndCollectionSize() ?: SaveCollectionStep.SelectTabs
view?.let {
val directions = HomeFragmentDirections.actionHomeFragmentToCreateCollectionFragment()
nav(R.id.homeFragment, directions)
}
view?.let {
val directions = HomeFragmentDirections.actionHomeFragmentToCreateCollectionFragment()
nav(R.id.homeFragment, directions)
}
}

View File

@ -16,7 +16,6 @@ import kotlinx.android.synthetic.main.collection_home_list_row.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import org.mozilla.fenix.R
@ -82,33 +81,31 @@ class CollectionViewHolder(
}
private fun updateCollectionUI() {
launch(Dispatchers.Main) {
view.collection_title.text = collection.title
view.collection_description.text = collection.description(view.context)
view.collection_title.text = collection.title
view.collection_description.text = collection.description(view.context)
if (expanded) {
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = 0
collection_title.setPadding(0, 0, 0, EXPANDED_PADDING)
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_top_corners)
view.collection_description.visibility = View.GONE
if (expanded) {
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = 0
collection_title.setPadding(0, 0, 0, EXPANDED_PADDING)
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_top_corners)
view.collection_description.visibility = View.GONE
view.chevron.setBackgroundResource(R.drawable.ic_chevron_up)
} else {
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = COLLAPSED_MARGIN
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_all_corners)
view.collection_description.visibility = View.VISIBLE
view.chevron.setBackgroundResource(R.drawable.ic_chevron_up)
} else {
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = COLLAPSED_MARGIN
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_all_corners)
view.collection_description.visibility = View.VISIBLE
view.chevron.setBackgroundResource(R.drawable.ic_chevron_down)
}
view.collection_icon.setColorFilter(
ContextCompat.getColor(
view.context,
getIconColor(collection.id)
),
android.graphics.PorterDuff.Mode.SRC_IN
)
view.chevron.setBackgroundResource(R.drawable.ic_chevron_down)
}
view.collection_icon.setColorFilter(
ContextCompat.getColor(
view.context,
getIconColor(collection.id)
),
android.graphics.PorterDuff.Mode.SRC_IN
)
}
private fun handleExpansion(isExpanded: Boolean) {

View File

@ -79,9 +79,8 @@ class TabInCollectionViewHolder(
}
private fun updateTabUI() {
launch(Dispatchers.Main) {
collection_tab_hostname.text = tab.url.urlToTrimmedHost(view.context)
}
collection_tab_hostname.text = tab.url.urlToTrimmedHost(view.context)
collection_tab_title.text = tab.title
launch(Dispatchers.IO) {
val bitmap = collection_tab_icon.context.components.core.icons

View File

@ -25,7 +25,6 @@ import androidx.navigation.NavController
import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_bookmark.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
@ -268,17 +267,15 @@ class BookmarkFragment : Fragment(), CoroutineScope, BackHandler, AccountObserve
getManagedEmitter<BookmarkChange>()
.onNext(BookmarkChange.Change(currentRoot - it.item.guid))
launch(Dispatchers.Main) {
allowUndo(
view!!,
getString(R.string.bookmark_deletion_snackbar_message,
it.item.url?.urlToTrimmedHost(context)),
getString(R.string.bookmark_undo_deletion), { refreshBookmarks() }
) {
bookmarkStorage()?.deleteNode(it.item.guid)
metrics()?.track(Event.RemoveBookmark)
refreshBookmarks()
}
allowUndo(
view!!,
getString(R.string.bookmark_deletion_snackbar_message,
it.item.url?.urlToTrimmedHost(context)),
getString(R.string.bookmark_undo_deletion), { refreshBookmarks() }
) {
bookmarkStorage()?.deleteNode(it.item.guid)
metrics()?.track(Event.RemoveBookmark)
refreshBookmarks()
}
}
}