2019-02-09 00:33:50 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* 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/. */
|
2019-02-09 00:33:50 +01:00
|
|
|
|
|
|
|
package org.mozilla.fenix.library.history
|
|
|
|
|
2019-05-21 00:33:59 +02:00
|
|
|
import android.content.DialogInterface
|
2019-02-09 00:33:50 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuInflater
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-05-21 00:33:59 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2019-07-24 23:37:10 +02:00
|
|
|
import androidx.lifecycle.Observer
|
2019-06-13 02:14:46 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-02-09 01:00:33 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_history.view.*
|
2019-05-10 18:58:54 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2019-08-07 23:00:53 +02:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2019-02-09 01:21:55 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2019-11-25 20:07:21 +01:00
|
|
|
import mozilla.components.concept.engine.prompt.ShareData
|
2019-07-25 16:32:32 +02:00
|
|
|
import mozilla.components.lib.state.ext.consumeFrom
|
2019-11-28 00:02:47 +01:00
|
|
|
import mozilla.components.support.base.feature.UserInteractionHandler
|
2019-04-05 22:11:05 +02:00
|
|
|
import org.mozilla.fenix.BrowserDirection
|
|
|
|
import org.mozilla.fenix.HomeActivity
|
2019-02-09 00:33:50 +01:00
|
|
|
import org.mozilla.fenix.R
|
2019-09-13 19:18:25 +02:00
|
|
|
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
|
2019-05-10 18:58:54 +02:00
|
|
|
import org.mozilla.fenix.components.Components
|
2019-07-11 23:39:06 +02:00
|
|
|
import org.mozilla.fenix.components.StoreProvider
|
2019-07-24 23:37:10 +02:00
|
|
|
import org.mozilla.fenix.components.history.createSynchronousPagedHistoryProvider
|
2019-07-16 21:21:03 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-05-10 18:58:54 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-06-06 21:40:10 +02:00
|
|
|
import org.mozilla.fenix.ext.nav
|
2019-02-09 01:21:55 +01:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-11-25 21:36:47 +01:00
|
|
|
import org.mozilla.fenix.ext.showToolbar
|
2019-08-01 17:58:41 +02:00
|
|
|
import org.mozilla.fenix.library.LibraryPageFragment
|
2019-02-09 01:21:55 +01:00
|
|
|
|
2019-08-27 20:24:46 +02:00
|
|
|
@SuppressWarnings("TooManyFunctions", "LargeClass")
|
2019-11-28 00:02:47 +01:00
|
|
|
class HistoryFragment : LibraryPageFragment<HistoryItem>(), UserInteractionHandler {
|
2019-08-30 15:33:00 +02:00
|
|
|
private lateinit var historyStore: HistoryFragmentStore
|
2019-07-11 23:39:06 +02:00
|
|
|
private lateinit var historyView: HistoryView
|
|
|
|
private lateinit var historyInteractor: HistoryInteractor
|
2019-07-24 23:37:10 +02:00
|
|
|
private lateinit var viewModel: HistoryViewModel
|
2019-02-15 00:23:41 +01:00
|
|
|
|
2019-02-09 00:33:50 +01:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
2019-07-11 23:39:06 +02:00
|
|
|
): View? {
|
|
|
|
val view = inflater.inflate(R.layout.fragment_history, container, false)
|
2019-07-16 20:29:57 +02:00
|
|
|
historyStore = StoreProvider.get(this) {
|
2019-08-30 15:33:00 +02:00
|
|
|
HistoryFragmentStore(
|
|
|
|
HistoryFragmentState(
|
|
|
|
items = listOf(), mode = HistoryFragmentState.Mode.Normal
|
2019-05-29 13:40:56 +02:00
|
|
|
)
|
2019-05-15 08:16:48 +02:00
|
|
|
)
|
2019-07-16 20:29:57 +02:00
|
|
|
}
|
2019-08-27 19:33:40 +02:00
|
|
|
val historyController: HistoryController = DefaultHistoryController(
|
2019-07-11 23:39:06 +02:00
|
|
|
historyStore,
|
|
|
|
::openItem,
|
|
|
|
::displayDeleteAllDialog,
|
|
|
|
::invalidateOptionsMenu,
|
|
|
|
::deleteHistoryItems
|
|
|
|
)
|
2019-08-27 19:33:40 +02:00
|
|
|
historyInteractor = HistoryInteractor(
|
|
|
|
historyController
|
|
|
|
)
|
2019-08-03 10:14:35 +02:00
|
|
|
historyView = HistoryView(view.historyLayout, historyInteractor)
|
2019-07-24 23:37:10 +02:00
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
2019-08-01 17:58:41 +02:00
|
|
|
override val selectedItems get() = historyStore.state.mode.selectedItems
|
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
private fun invalidateOptionsMenu() {
|
|
|
|
activity?.invalidateOptionsMenu()
|
|
|
|
}
|
2019-02-09 00:33:50 +01:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2019-02-09 01:21:55 +01:00
|
|
|
|
2019-07-24 23:37:10 +02:00
|
|
|
viewModel = HistoryViewModel(
|
|
|
|
requireComponents.core.historyStorage.createSynchronousPagedHistoryProvider()
|
|
|
|
)
|
|
|
|
|
2019-07-31 02:16:31 +02:00
|
|
|
viewModel.userHasHistory.observe(this, Observer {
|
|
|
|
historyView.updateEmptyState(it)
|
|
|
|
})
|
|
|
|
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryOpened)
|
2019-07-24 23:37:10 +02:00
|
|
|
|
2019-02-09 00:33:50 +01:00
|
|
|
setHasOptionsMenu(true)
|
2019-02-25 20:37:20 +01:00
|
|
|
}
|
|
|
|
|
2019-08-01 17:58:41 +02:00
|
|
|
private fun deleteHistoryItems(items: Set<HistoryItem>) {
|
2019-07-11 23:39:06 +02:00
|
|
|
lifecycleScope.launch {
|
2019-08-04 00:23:15 +02:00
|
|
|
context?.components?.run {
|
|
|
|
for (item in items) {
|
|
|
|
analytics.metrics.track(Event.HistoryItemRemoved)
|
|
|
|
core.historyStorage.deleteVisit(item.url, item.visitedAt)
|
|
|
|
}
|
2019-07-11 23:39:06 +02:00
|
|
|
}
|
2019-07-24 23:37:10 +02:00
|
|
|
viewModel.invalidate()
|
2019-08-30 15:33:00 +02:00
|
|
|
historyStore.dispatch(HistoryFragmentAction.ExitDeletionMode)
|
2019-07-11 23:39:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 23:00:53 +02:00
|
|
|
@ExperimentalCoroutinesApi
|
2019-05-29 13:40:56 +02:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2019-07-25 16:32:32 +02:00
|
|
|
consumeFrom(historyStore) {
|
|
|
|
historyView.update(it)
|
2019-07-11 23:39:06 +02:00
|
|
|
}
|
2019-02-09 00:33:50 +01:00
|
|
|
|
2020-02-10 12:21:09 +01:00
|
|
|
viewModel.history.observe(viewLifecycleOwner, Observer {
|
2019-07-24 23:37:10 +02:00
|
|
|
historyView.historyAdapter.submitList(it)
|
|
|
|
})
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2019-11-25 21:36:47 +01:00
|
|
|
showToolbar(getString(R.string.library_history))
|
2019-02-13 23:36:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-09 00:33:50 +01:00
|
|
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
2019-10-05 07:20:58 +02:00
|
|
|
val menuRes = when (historyStore.state.mode) {
|
2019-08-30 15:33:00 +02:00
|
|
|
HistoryFragmentState.Mode.Normal -> R.menu.library_menu
|
|
|
|
is HistoryFragmentState.Mode.Editing -> R.menu.history_select_multi
|
2019-10-05 07:20:58 +02:00
|
|
|
else -> return
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-10-05 07:20:58 +02:00
|
|
|
|
|
|
|
inflater.inflate(menuRes, menu)
|
|
|
|
menu.findItem(R.id.share_history_multi_select)?.isVisible = true
|
2019-02-09 01:21:55 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
|
|
|
|
R.id.share_history_multi_select -> {
|
2019-08-01 17:58:41 +02:00
|
|
|
val selectedHistory = historyStore.state.mode.selectedItems
|
2019-11-25 20:07:21 +01:00
|
|
|
val shareTabs = selectedHistory.map { ShareData(url = it.url, title = it.title) }
|
|
|
|
share(shareTabs)
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.delete_history_multi_select -> {
|
2019-06-13 02:14:46 +02:00
|
|
|
lifecycleScope.launch(Main) {
|
2019-11-19 08:23:55 +01:00
|
|
|
deleteSelectedHistory(historyStore.state.mode.selectedItems, requireComponents)
|
2019-07-24 23:37:10 +02:00
|
|
|
viewModel.invalidate()
|
2019-08-30 15:33:00 +02:00
|
|
|
historyStore.dispatch(HistoryFragmentAction.ExitDeletionMode)
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.open_history_in_new_tabs_multi_select -> {
|
2019-08-01 17:58:41 +02:00
|
|
|
openItemsInNewTab { selectedItem ->
|
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemOpened)
|
|
|
|
selectedItem.url
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-05-10 18:58:54 +02:00
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
nav(
|
|
|
|
R.id.historyFragment,
|
|
|
|
HistoryFragmentDirections.actionHistoryFragmentToHomeFragment()
|
|
|
|
)
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.open_history_in_private_tabs_multi_select -> {
|
2019-08-01 17:58:41 +02:00
|
|
|
openItemsInNewTab(private = true) { selectedItem ->
|
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemOpened)
|
|
|
|
selectedItem.url
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-05-10 18:58:54 +02:00
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
(activity as HomeActivity).apply {
|
2020-02-10 22:57:29 +01:00
|
|
|
components.browsingModeManager.mode = BrowsingMode.Private
|
2019-05-29 13:40:56 +02:00
|
|
|
supportActionBar?.hide()
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-07-11 23:39:06 +02:00
|
|
|
nav(
|
|
|
|
R.id.historyFragment,
|
|
|
|
HistoryFragmentDirections.actionHistoryFragmentToHomeFragment()
|
|
|
|
)
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
2019-02-09 00:33:50 +01:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
else -> super.onOptionsItemSelected(item)
|
2019-02-09 00:33:50 +01:00
|
|
|
}
|
2019-02-15 00:23:41 +01:00
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
override fun onBackPressed(): Boolean = historyView.onBackPressed()
|
2019-05-29 13:40:56 +02:00
|
|
|
|
2019-08-01 17:58:41 +02:00
|
|
|
private fun openItem(item: HistoryItem) {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemOpened)
|
2019-05-29 13:40:56 +02:00
|
|
|
(activity as HomeActivity).openToBrowserAndLoad(
|
|
|
|
searchTermOrURL = item.url,
|
2019-07-01 23:31:30 +02:00
|
|
|
newTab = true,
|
2019-05-29 13:40:56 +02:00
|
|
|
from = BrowserDirection.FromHistory
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-04 00:23:15 +02:00
|
|
|
private fun displayDeleteAllDialog() {
|
2019-05-29 13:40:56 +02:00
|
|
|
activity?.let { activity ->
|
2019-05-31 23:26:34 +02:00
|
|
|
AlertDialog.Builder(activity).apply {
|
2019-08-06 23:40:17 +02:00
|
|
|
setMessage(R.string.delete_browsing_data_prompt_message)
|
|
|
|
setNegativeButton(R.string.delete_browsing_data_prompt_cancel) { dialog: DialogInterface, _ ->
|
2019-05-29 13:40:56 +02:00
|
|
|
dialog.cancel()
|
|
|
|
}
|
2019-08-06 23:40:17 +02:00
|
|
|
setPositiveButton(R.string.delete_browsing_data_prompt_allow) { dialog: DialogInterface, _ ->
|
2019-08-30 15:33:00 +02:00
|
|
|
historyStore.dispatch(HistoryFragmentAction.EnterDeletionMode)
|
2019-06-13 02:14:46 +02:00
|
|
|
lifecycleScope.launch {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryAllItemsRemoved)
|
2019-05-29 13:40:56 +02:00
|
|
|
requireComponents.core.historyStorage.deleteEverything()
|
2019-07-24 23:37:10 +02:00
|
|
|
launch(Main) {
|
|
|
|
viewModel.invalidate()
|
2019-08-30 15:33:00 +02:00
|
|
|
historyStore.dispatch(HistoryFragmentAction.ExitDeletionMode)
|
2019-05-29 19:59:05 +02:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-05-29 19:59:05 +02:00
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
dialog.dismiss()
|
|
|
|
}
|
|
|
|
create()
|
|
|
|
}.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:58:54 +02:00
|
|
|
private suspend fun deleteSelectedHistory(
|
2019-07-24 23:37:10 +02:00
|
|
|
selected: Set<HistoryItem>,
|
2019-05-10 18:58:54 +02:00
|
|
|
components: Components = requireComponents
|
|
|
|
) {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemRemoved)
|
2019-05-29 13:40:56 +02:00
|
|
|
val storage = components.core.historyStorage
|
|
|
|
for (item in selected) {
|
|
|
|
storage.deleteVisit(item.url, item.visitedAt)
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-23 19:48:22 +02:00
|
|
|
|
2019-11-25 20:07:21 +01:00
|
|
|
private fun share(data: List<ShareData>) {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemShared)
|
2019-11-25 20:07:21 +01:00
|
|
|
val directions = HistoryFragmentDirections.actionHistoryFragmentToShareFragment(
|
|
|
|
data = data.toTypedArray()
|
|
|
|
)
|
2019-06-06 21:40:10 +02:00
|
|
|
nav(R.id.historyFragment, directions)
|
2019-05-23 19:48:22 +02:00
|
|
|
}
|
2019-05-29 19:59:05 +02:00
|
|
|
}
|