For #747. Improve `HistoryFragment` readability.

nightly-build-test
Denys M 5 years ago committed by Jeff Boek
parent 13f144f212
commit 39df4c8522

@ -43,9 +43,6 @@ import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.getAutoDisposeObservable import org.mozilla.fenix.mvi.getAutoDisposeObservable
import org.mozilla.fenix.mvi.getManagedEmitter import org.mozilla.fenix.mvi.getManagedEmitter
import org.mozilla.fenix.share.ShareTab import org.mozilla.fenix.share.ShareTab
import java.net.MalformedURLException
import java.net.URL
import kotlin.coroutines.CoroutineContext
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@SuppressWarnings("TooManyFunctions") @SuppressWarnings("TooManyFunctions")
@ -58,19 +55,18 @@ class HistoryFragment : Fragment(), CoroutineScope by MainScope(), BackHandler {
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? = inflater
val view = inflater.inflate(R.layout.fragment_history, container, false) .inflate(R.layout.fragment_history, container, false).also { view ->
historyComponent = HistoryComponent( historyComponent = HistoryComponent(
view.history_layout, view.history_layout,
ActionBusFactory.get(this), ActionBusFactory.get(this),
FenixViewModelProvider.create( FenixViewModelProvider.create(
this, this,
HistoryViewModel::class.java, HistoryViewModel::class.java,
HistoryViewModel.Companion::create HistoryViewModel.Companion::create
)
) )
) }
return view
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -78,17 +74,24 @@ class HistoryFragment : Fragment(), CoroutineScope by MainScope(), BackHandler {
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
override fun onResume() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onResume() super.onViewCreated(view, savedInstanceState)
(activity as AppCompatActivity).title = getString(R.string.library_history)
(activity as AppCompatActivity).supportActionBar?.show() launch { reloadData() }
} }
private fun openItem(item: HistoryItem) { override fun onStart() {
(activity as HomeActivity).openToBrowserAndLoad( super.onStart()
searchTermOrURL = item.url, getAutoDisposeObservable<HistoryAction>()
newTab = false, .subscribe(this::handleNewHistoryAction)
from = BrowserDirection.FromHistory) }
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).apply {
title = getString(R.string.library_history)
supportActionBar?.show()
}
} }
override fun onDestroy() { override fun onDestroy() {
@ -97,139 +100,154 @@ class HistoryFragment : Fragment(), CoroutineScope by MainScope(), BackHandler {
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
when (val mode = (historyComponent.uiView as HistoryUIView).mode) { val mode = (historyComponent.uiView as HistoryUIView).mode
HistoryState.Mode.Normal -> inflater.inflate(R.menu.library_menu, menu) when (mode) {
is HistoryState.Mode.Editing -> { HistoryState.Mode.Normal ->
inflater.inflate(R.menu.history_select_multi, menu) R.menu.library_menu
menu.findItem(R.id.share_history_multi_select)?.run { is HistoryState.Mode.Editing ->
isVisible = mode.selectedItems.isNotEmpty() R.menu.history_select_multi
icon.colorFilter = PorterDuffColorFilter( }.let { inflater.inflate(it, menu) }
ContextCompat.getColor(context!!, R.color.white_color),
PorterDuff.Mode.SRC_IN
)
}
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
launch { reloadData() } if (mode is HistoryState.Mode.Editing) {
} menu.findItem(R.id.share_history_multi_select)?.run {
isVisible = mode.selectedItems.isNotEmpty()
// This method triggers the complexity warning. However it's actually not that hard to understand. icon.colorFilter = PorterDuffColorFilter(
@SuppressWarnings("ComplexMethod") ContextCompat.getColor(context!!, R.color.white_color),
override fun onStart() { PorterDuff.Mode.SRC_IN
super.onStart() )
getAutoDisposeObservable<HistoryAction>()
.subscribe {
when (it) {
is HistoryAction.Open -> openItem(it.item)
is HistoryAction.EnterEditMode -> getManagedEmitter<HistoryChange>()
.onNext(HistoryChange.EnterEditMode(it.item))
is HistoryAction.AddItemForRemoval -> getManagedEmitter<HistoryChange>()
.onNext(HistoryChange.AddItemForRemoval(it.item))
is HistoryAction.RemoveItemForRemoval -> getManagedEmitter<HistoryChange>()
.onNext(HistoryChange.RemoveItemForRemoval(it.item))
is HistoryAction.BackPressed -> getManagedEmitter<HistoryChange>()
.onNext(HistoryChange.ExitEditMode)
is HistoryAction.Delete.All -> {
activity?.let { activity ->
AlertDialog.Builder(
ContextThemeWrapper(
activity,
R.style.DeleteDialogStyle
)
).apply {
setMessage(R.string.history_delete_all_dialog)
setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _ ->
dialog.cancel()
}
setPositiveButton(R.string.history_clear_dialog) { dialog: DialogInterface, _ ->
launch {
requireComponents.core.historyStorage.deleteEverything()
reloadData()
}
dialog.dismiss()
}
create()
}.show()
}
}
is HistoryAction.Delete.One -> launch {
requireComponents.core.historyStorage.deleteVisit(it.item.url, it.item.visitedAt)
reloadData()
}
is HistoryAction.Delete.Some -> launch {
val storage = requireComponents.core.historyStorage
for (item in it.items) {
storage.deleteVisit(item.url, item.visitedAt)
}
reloadData()
}
is HistoryAction.SwitchMode -> activity?.invalidateOptionsMenu()
}
} }
}
} }
@Suppress("ComplexMethod") @Suppress("ComplexMethod")
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
return when (item.itemId) { R.id.share_history_multi_select -> {
R.id.share_history_multi_select -> { val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected()
val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected() when {
when { selectedHistory.size == 1 ->
selectedHistory.size == 1 -> share(selectedHistory.first().url) share(selectedHistory.first().url)
selectedHistory.size > 1 -> { selectedHistory.size > 1 -> {
val shareTabs = selectedHistory.map { ShareTab(it.url, it.title) } val shareTabs = selectedHistory.map { ShareTab(it.url, it.title) }
share(tabs = shareTabs) share(tabs = shareTabs)
}
} }
true
}
R.id.libraryClose -> {
Navigation.findNavController(requireActivity(), R.id.container)
.popBackStack(R.id.libraryFragment, true)
true
} }
R.id.delete_history_multi_select -> { true
val components = context?.applicationContext?.components!! }
val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected() R.id.libraryClose -> {
Navigation.findNavController(requireActivity(), R.id.container)
.popBackStack(R.id.libraryFragment, true)
true
}
R.id.delete_history_multi_select -> {
val components = context?.applicationContext?.components!!
val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected()
GlobalScope.launch(Main) { GlobalScope.launch(Main) {
deleteSelectedHistory(selectedHistory, components) deleteSelectedHistory(selectedHistory, components)
reloadData() reloadData()
}
true
} }
R.id.open_history_in_new_tabs_multi_select -> { true
val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected() }
selectedHistory.forEach { R.id.open_history_in_new_tabs_multi_select -> {
requireComponents.useCases.tabsUseCases.addTab.invoke(it.url) val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected()
requireComponents.useCases.tabsUseCases.addTab.let { useCase ->
for (selectedItem in selectedHistory) {
useCase.invoke(selectedItem.url)
} }
}
(activity as HomeActivity).browsingModeManager.mode = BrowsingModeManager.Mode.Normal (activity as HomeActivity).apply {
(activity as HomeActivity).supportActionBar?.hide() browsingModeManager.mode = BrowsingModeManager.Mode.Normal
navigation.navigate(HistoryFragmentDirections.actionHistoryFragmentToHomeFragment()) supportActionBar?.hide()
true
} }
R.id.open_history_in_private_tabs_multi_select -> { navigation.navigate(HistoryFragmentDirections.actionHistoryFragmentToHomeFragment())
val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected() true
selectedHistory.forEach { }
requireComponents.useCases.tabsUseCases.addPrivateTab.invoke(it.url) R.id.open_history_in_private_tabs_multi_select -> {
val selectedHistory = (historyComponent.uiView as HistoryUIView).getSelected()
requireComponents.useCases.tabsUseCases.addPrivateTab.let { useCase ->
for (selectedItem in selectedHistory) {
useCase.invoke(selectedItem.url)
} }
}
(activity as HomeActivity).browsingModeManager.mode = BrowsingModeManager.Mode.Private (activity as HomeActivity).apply {
(activity as HomeActivity).supportActionBar?.hide() browsingModeManager.mode = BrowsingModeManager.Mode.Private
navigation.navigate(HistoryFragmentDirections.actionHistoryFragmentToHomeFragment()) supportActionBar?.hide()
true
} }
else -> super.onOptionsItemSelected(item) navigation.navigate(HistoryFragmentDirections.actionHistoryFragmentToHomeFragment())
true
} }
else -> super.onOptionsItemSelected(item)
} }
override fun onBackPressed(): Boolean = (historyComponent.uiView as HistoryUIView).onBackPressed() override fun onBackPressed(): Boolean = (historyComponent.uiView as HistoryUIView).onBackPressed()
@SuppressWarnings("ComplexMethod")
private fun handleNewHistoryAction(action: HistoryAction) {
when (action) {
is HistoryAction.Open ->
openItem(action.item)
is HistoryAction.EnterEditMode ->
emitChange { HistoryChange.EnterEditMode(action.item) }
is HistoryAction.AddItemForRemoval ->
emitChange { HistoryChange.AddItemForRemoval(action.item) }
is HistoryAction.RemoveItemForRemoval ->
emitChange { HistoryChange.RemoveItemForRemoval(action.item) }
is HistoryAction.BackPressed ->
emitChange { HistoryChange.ExitEditMode }
is HistoryAction.Delete.All ->
displayDeleteAllDialog()
is HistoryAction.Delete.One -> launch {
requireComponents.core
.historyStorage
.deleteVisit(action.item.url, action.item.visitedAt)
reloadData()
}
is HistoryAction.Delete.Some -> launch {
val storage = requireComponents.core.historyStorage
for (item in action.items) {
storage.deleteVisit(item.url, item.visitedAt)
}
reloadData()
}
is HistoryAction.SwitchMode ->
activity?.invalidateOptionsMenu()
}
}
private fun openItem(item: HistoryItem) {
(activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = item.url,
newTab = false,
from = BrowserDirection.FromHistory
)
}
private fun displayDeleteAllDialog() {
activity?.let { activity ->
AlertDialog.Builder(
ContextThemeWrapper(
activity,
R.style.DeleteDialogStyle
)
).apply {
setMessage(R.string.history_delete_all_dialog)
setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _ ->
dialog.cancel()
}
setPositiveButton(R.string.history_clear_dialog) { dialog: DialogInterface, _ ->
launch {
requireComponents.core.historyStorage.deleteEverything()
reloadData()
}
dialog.dismiss()
}
create()
}.show()
}
}
private suspend fun reloadData() { private suspend fun reloadData() {
val excludeTypes = listOf( val excludeTypes = listOf(
VisitType.NOT_A_VISIT, VisitType.NOT_A_VISIT,
@ -247,7 +265,7 @@ class HistoryFragment : Fragment(), CoroutineScope by MainScope(), BackHandler {
val items = requireComponents.core.historyStorage val items = requireComponents.core.historyStorage
.getDetailedVisits(sinceTimeMs, excludeTypes = excludeTypes) .getDetailedVisits(sinceTimeMs, excludeTypes = excludeTypes)
// We potentially have a large amount of visits, and multiple processing steps. // We potentially have a large amount of visits, and multiple processing steps.
// Wrapping iterator in a sequence should make this a little more efficient. // Wrapping iterator in a sequence should make this a little memory-more efficient.
.asSequence() .asSequence()
.sortedByDescending { it.visitTime } .sortedByDescending { it.visitTime }
.mapIndexed { id, item -> .mapIndexed { id, item ->
@ -261,8 +279,7 @@ class HistoryFragment : Fragment(), CoroutineScope by MainScope(), BackHandler {
.toList() .toList()
withContext(Main) { withContext(Main) {
getManagedEmitter<HistoryChange>() emitChange { HistoryChange.Change(items) }
.onNext(HistoryChange.Change(items))
} }
} }
@ -270,8 +287,9 @@ class HistoryFragment : Fragment(), CoroutineScope by MainScope(), BackHandler {
selected: List<HistoryItem>, selected: List<HistoryItem>,
components: Components = requireComponents components: Components = requireComponents
) { ) {
selected.forEach { val storage = components.core.historyStorage
components.core.historyStorage.deleteVisit(it.url, it.visitedAt) for (item in selected) {
storage.deleteVisit(item.url, item.visitedAt)
} }
} }
@ -280,6 +298,10 @@ class HistoryFragment : Fragment(), CoroutineScope by MainScope(), BackHandler {
HistoryFragmentDirections.actionHistoryFragmentToShareFragment(url = url, tabs = tabs?.toTypedArray()) HistoryFragmentDirections.actionHistoryFragmentToShareFragment(url = url, tabs = tabs?.toTypedArray())
Navigation.findNavController(view!!).navigate(directions) Navigation.findNavController(view!!).navigate(directions)
} }
private inline fun emitChange(producer: () -> HistoryChange) {
getManagedEmitter<HistoryChange>().onNext(producer())
}
} }
private const val HISTORY_TIME_DAYS = 3L private const val HISTORY_TIME_DAYS = 3L

Loading…
Cancel
Save