For #1578: Adds delete collection with undo
parent
c2339e9669
commit
e7ecd49353
|
@ -81,6 +81,7 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
private var homeMenu: HomeMenu? = null
|
private var homeMenu: HomeMenu? = null
|
||||||
|
|
||||||
var deleteSessionJob: (suspend () -> Unit)? = null
|
var deleteSessionJob: (suspend () -> Unit)? = null
|
||||||
|
var deleteCollectionJob: (suspend () -> Unit)? = null
|
||||||
|
|
||||||
private val onboarding by lazy { FenixOnboarding(requireContext()) }
|
private val onboarding by lazy { FenixOnboarding(requireContext()) }
|
||||||
private lateinit var sessionControlComponent: SessionControlComponent
|
private lateinit var sessionControlComponent: SessionControlComponent
|
||||||
|
@ -159,9 +160,8 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
(toolbarPaddingDp * Resources.getSystem().displayMetrics.density).roundToInt()
|
(toolbarPaddingDp * Resources.getSystem().displayMetrics.density).roundToInt()
|
||||||
view.toolbar.compoundDrawablePadding = roundToInt
|
view.toolbar.compoundDrawablePadding = roundToInt
|
||||||
view.toolbar.setOnClickListener {
|
view.toolbar.setOnClickListener {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
onboarding.finish()
|
onboarding.finish()
|
||||||
|
|
||||||
val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null)
|
val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null)
|
||||||
Navigation.findNavController(it).navigate(directions)
|
Navigation.findNavController(it).navigate(directions)
|
||||||
|
|
||||||
|
@ -269,10 +269,11 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
if ((activity as HomeActivity).browsingModeManager.isPrivate) {
|
if ((activity as HomeActivity).browsingModeManager.isPrivate) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
invokePendingDeleteJobs()
|
||||||
showCollectionCreationFragment(action.selectedTabSessionId)
|
showCollectionCreationFragment(action.selectedTabSessionId)
|
||||||
}
|
}
|
||||||
is TabAction.Select -> {
|
is TabAction.Select -> {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
val session =
|
val session =
|
||||||
requireComponents.core.sessionManager.findSessionById(action.sessionId)
|
requireComponents.core.sessionManager.findSessionById(action.sessionId)
|
||||||
requireComponents.core.sessionManager.select(session!!)
|
requireComponents.core.sessionManager.select(session!!)
|
||||||
|
@ -291,6 +292,7 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is TabAction.Share -> {
|
is TabAction.Share -> {
|
||||||
|
invokePendingDeleteJobs()
|
||||||
requireComponents.core.sessionManager.findSessionById(action.sessionId)
|
requireComponents.core.sessionManager.findSessionById(action.sessionId)
|
||||||
?.let { session ->
|
?.let { session ->
|
||||||
requireContext().share(session.url)
|
requireContext().share(session.url)
|
||||||
|
@ -308,11 +310,12 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
is TabAction.Add -> {
|
is TabAction.Add -> {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null)
|
val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null)
|
||||||
Navigation.findNavController(view!!).navigate(directions)
|
Navigation.findNavController(view!!).navigate(directions)
|
||||||
}
|
}
|
||||||
is TabAction.ShareTabs -> {
|
is TabAction.ShareTabs -> {
|
||||||
|
invokePendingDeleteJobs()
|
||||||
val shareText = requireComponents.core.sessionManager.sessions.joinToString("\n") {
|
val shareText = requireComponents.core.sessionManager.sessions.joinToString("\n") {
|
||||||
it.url
|
it.url
|
||||||
}
|
}
|
||||||
|
@ -321,7 +324,7 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun invokePendingDeleteSessionJob() {
|
private fun invokePendingDeleteJobs() {
|
||||||
deleteSessionJob?.let {
|
deleteSessionJob?.let {
|
||||||
launch {
|
launch {
|
||||||
it.invoke()
|
it.invoke()
|
||||||
|
@ -329,6 +332,14 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
deleteSessionJob = null
|
deleteSessionJob = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deleteCollectionJob?.let {
|
||||||
|
launch {
|
||||||
|
it.invoke()
|
||||||
|
}.invokeOnCompletion {
|
||||||
|
deleteCollectionJob = null
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("ComplexMethod")
|
@Suppress("ComplexMethod")
|
||||||
|
@ -343,9 +354,7 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
.onNext(SessionControlChange.ExpansionChange(action.collection, false))
|
.onNext(SessionControlChange.ExpansionChange(action.collection, false))
|
||||||
}
|
}
|
||||||
is CollectionAction.Delete -> {
|
is CollectionAction.Delete -> {
|
||||||
launch(Dispatchers.IO) {
|
removeCollectionWithUndo(action.collection)
|
||||||
requireComponents.core.tabCollectionStorage.removeCollection(action.collection)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
is CollectionAction.AddTab -> {
|
is CollectionAction.AddTab -> {
|
||||||
ItsNotBrokenSnack(context!!).showSnackbar(issueNumber = "1575")
|
ItsNotBrokenSnack(context!!).showSnackbar(issueNumber = "1575")
|
||||||
|
@ -357,14 +366,14 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
is CollectionAction.OpenTab -> {
|
is CollectionAction.OpenTab -> {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
(activity as HomeActivity).openToBrowserAndLoad(
|
(activity as HomeActivity).openToBrowserAndLoad(
|
||||||
searchTermOrURL = action.tab.url,
|
searchTermOrURL = action.tab.url,
|
||||||
newTab = true,
|
newTab = true,
|
||||||
from = BrowserDirection.FromHome)
|
from = BrowserDirection.FromHome)
|
||||||
}
|
}
|
||||||
is CollectionAction.OpenTabs -> {
|
is CollectionAction.OpenTabs -> {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
action.collection.tabs.forEach {
|
action.collection.tabs.forEach {
|
||||||
requireComponents.useCases.tabsUseCases.addTab.invoke(it.url)
|
requireComponents.useCases.tabsUseCases.addTab.invoke(it.url)
|
||||||
}
|
}
|
||||||
|
@ -400,21 +409,21 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
homeMenu = HomeMenu(requireContext()) {
|
homeMenu = HomeMenu(requireContext()) {
|
||||||
when (it) {
|
when (it) {
|
||||||
HomeMenu.Item.Settings -> {
|
HomeMenu.Item.Settings -> {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
onboarding.finish()
|
onboarding.finish()
|
||||||
Navigation.findNavController(homeLayout).navigate(
|
Navigation.findNavController(homeLayout).navigate(
|
||||||
HomeFragmentDirections.actionHomeFragmentToSettingsFragment()
|
HomeFragmentDirections.actionHomeFragmentToSettingsFragment()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
HomeMenu.Item.Library -> {
|
HomeMenu.Item.Library -> {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
onboarding.finish()
|
onboarding.finish()
|
||||||
Navigation.findNavController(homeLayout).navigate(
|
Navigation.findNavController(homeLayout).navigate(
|
||||||
HomeFragmentDirections.actionHomeFragmentToLibraryFragment()
|
HomeFragmentDirections.actionHomeFragmentToLibraryFragment()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
HomeMenu.Item.Help -> {
|
HomeMenu.Item.Help -> {
|
||||||
invokePendingDeleteSessionJob()
|
invokePendingDeleteJobs()
|
||||||
(activity as HomeActivity).openToBrowserAndLoad(
|
(activity as HomeActivity).openToBrowserAndLoad(
|
||||||
searchTermOrURL = SupportUtils.getSumoURLForTopic(
|
searchTermOrURL = SupportUtils.getSumoURLForTopic(
|
||||||
context!!,
|
context!!,
|
||||||
|
@ -486,6 +495,8 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
|
|
||||||
private fun removeTabWithUndo(sessionId: String) {
|
private fun removeTabWithUndo(sessionId: String) {
|
||||||
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(
|
getManagedEmitter<SessionControlChange>().onNext(
|
||||||
SessionControlChange.TabsChange(
|
SessionControlChange.TabsChange(
|
||||||
sessionManager.sessions
|
sessionManager.sessions
|
||||||
|
@ -527,6 +538,32 @@ class HomeFragment : Fragment(), CoroutineScope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun removeCollectionWithUndo(tabCollection: TabCollection) {
|
||||||
|
// Update the UI with the tab collection removed, but don't remove it from storage yet
|
||||||
|
val updatedCollectionList = requireComponents.core.tabCollectionStorage.cachedTabCollections.toMutableList()
|
||||||
|
updatedCollectionList.remove(tabCollection)
|
||||||
|
getManagedEmitter<SessionControlChange>().onNext(SessionControlChange.CollectionsChange(updatedCollectionList))
|
||||||
|
|
||||||
|
deleteCollectionJob = {
|
||||||
|
launch(Dispatchers.IO) {
|
||||||
|
requireComponents.core.tabCollectionStorage.removeCollection(tabCollection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CoroutineScope(Dispatchers.Main).allowUndo(
|
||||||
|
view!!, getString(R.string.snackbar_collection_deleted),
|
||||||
|
getString(R.string.snackbar_deleted_undo), {
|
||||||
|
deleteCollectionJob = null
|
||||||
|
getManagedEmitter<SessionControlChange>().onNext(SessionControlChange
|
||||||
|
.CollectionsChange(requireComponents.core.tabCollectionStorage.cachedTabCollections))
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
launch(Dispatchers.IO) {
|
||||||
|
requireComponents.core.tabCollectionStorage.removeCollection(tabCollection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun emitSessionChanges() {
|
private fun emitSessionChanges() {
|
||||||
val sessionManager = requireComponents.core.sessionManager
|
val sessionManager = requireComponents.core.sessionManager
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue