Fixes #870 - Fixes logic for the session bottom sheet fragment for private mode
Also fixes #871master
parent
3a558f6f78
commit
79395631d7
|
@ -32,7 +32,6 @@ import org.mozilla.fenix.BrowserDirection
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
import org.mozilla.fenix.ext.requireComponents
|
import org.mozilla.fenix.ext.requireComponents
|
||||||
import org.mozilla.fenix.home.sessions.ArchivedSession
|
import org.mozilla.fenix.home.sessions.ArchivedSession
|
||||||
import org.mozilla.fenix.home.sessions.SessionBottomSheetFragment
|
|
||||||
import org.mozilla.fenix.home.sessions.SessionsAction
|
import org.mozilla.fenix.home.sessions.SessionsAction
|
||||||
import org.mozilla.fenix.home.sessions.SessionsChange
|
import org.mozilla.fenix.home.sessions.SessionsChange
|
||||||
import org.mozilla.fenix.home.sessions.SessionsComponent
|
import org.mozilla.fenix.home.sessions.SessionsComponent
|
||||||
|
@ -196,9 +195,18 @@ class HomeFragment : Fragment() {
|
||||||
requireComponents.core.sessionStorage.archive(requireComponents.core.sessionManager)
|
requireComponents.core.sessionStorage.archive(requireComponents.core.sessionManager)
|
||||||
}
|
}
|
||||||
is TabsAction.MenuTapped -> {
|
is TabsAction.MenuTapped -> {
|
||||||
requireComponents.core.sessionStorage.current()
|
val isPrivate = (activity as HomeActivity).browsingModeManager.isPrivate
|
||||||
?.let { ArchivedSession(it.id!!, it, it.lastSavedAt, it.urls) }
|
val titles = requireComponents.core.sessionManager.sessions
|
||||||
?.also { openSessionMenu(it) }
|
.filter { session -> session.private == isPrivate }
|
||||||
|
.map { session -> session.title }
|
||||||
|
|
||||||
|
val sessionType = if (isPrivate) {
|
||||||
|
SessionBottomSheetFragment.SessionType.Private(titles)
|
||||||
|
} else {
|
||||||
|
SessionBottomSheetFragment.SessionType.Current(titles)
|
||||||
|
}
|
||||||
|
|
||||||
|
openSessionMenu(sessionType)
|
||||||
}
|
}
|
||||||
is TabsAction.Select -> {
|
is TabsAction.Select -> {
|
||||||
val session = requireComponents.core.sessionManager.findSessionById(it.sessionId)
|
val session = requireComponents.core.sessionManager.findSessionById(it.sessionId)
|
||||||
|
@ -230,7 +238,8 @@ class HomeFragment : Fragment() {
|
||||||
is SessionsAction.Delete -> {
|
is SessionsAction.Delete -> {
|
||||||
requireComponents.core.sessionStorage.remove(it.archivedSession.bundle)
|
requireComponents.core.sessionStorage.remove(it.archivedSession.bundle)
|
||||||
}
|
}
|
||||||
is SessionsAction.MenuTapped -> openSessionMenu(it.archivedSession)
|
is SessionsAction.MenuTapped ->
|
||||||
|
openSessionMenu(SessionBottomSheetFragment.SessionType.Archived(it.archivedSession))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -347,22 +356,26 @@ class HomeFragment : Fragment() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openSessionMenu(archivedSession: ArchivedSession) {
|
private fun openSessionMenu(sessionType: SessionBottomSheetFragment.SessionType) {
|
||||||
val isCurrentSession = archivedSession.bundle.id == requireComponents.core.sessionStorage.current()?.id
|
SessionBottomSheetFragment.create(sessionType).apply {
|
||||||
SessionBottomSheetFragment().also {
|
onArchive = {
|
||||||
it.archivedSession = archivedSession
|
requireComponents.core.sessionStorage.archive(requireComponents.core.sessionManager)
|
||||||
it.isCurrentSession = isCurrentSession
|
|
||||||
it.onArchive = {
|
|
||||||
if (isCurrentSession) {
|
|
||||||
requireComponents.core.sessionStorage.archive(requireComponents.core.sessionManager)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
it.onDelete = {
|
onDelete = {
|
||||||
if (isCurrentSession) {
|
when (it) {
|
||||||
requireComponents.useCases.tabsUseCases.removeAllTabsOfType.invoke(false)
|
is SessionBottomSheetFragment.SessionType.Archived -> {
|
||||||
|
requireComponents.core.sessionStorage.remove(it.archivedSession.bundle)
|
||||||
|
}
|
||||||
|
is SessionBottomSheetFragment.SessionType.Current -> {
|
||||||
|
requireComponents.useCases.tabsUseCases.removeAllTabsOfType.invoke(false)
|
||||||
|
requireComponents.core.sessionStorage.current()?.apply {
|
||||||
|
requireComponents.core.sessionStorage.remove(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is SessionBottomSheetFragment.SessionType.Private -> {
|
||||||
|
requireComponents.useCases.tabsUseCases.removeAllTabsOfType.invoke(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requireComponents.core.sessionStorage.remove(archivedSession.bundle)
|
|
||||||
}
|
}
|
||||||
}.show(requireActivity().supportFragmentManager, SessionBottomSheetFragment.overflowFragmentTag)
|
}.show(requireActivity().supportFragmentManager, SessionBottomSheetFragment.overflowFragmentTag)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
package org.mozilla.fenix.home
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||||
|
import kotlinx.android.extensions.LayoutContainer
|
||||||
|
import kotlinx.android.synthetic.main.session_bottom_sheet.view.*
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.ext.requireComponents
|
||||||
|
import org.mozilla.fenix.home.sessions.ArchivedSession
|
||||||
|
|
||||||
|
class SessionBottomSheetFragment : BottomSheetDialogFragment(), LayoutContainer {
|
||||||
|
sealed class SessionType {
|
||||||
|
data class Current(val titles: List<String>) : SessionType()
|
||||||
|
data class Archived(val archivedSession: ArchivedSession) : SessionType()
|
||||||
|
data class Private(val titles: List<String>) : SessionType()
|
||||||
|
}
|
||||||
|
|
||||||
|
private var sessionType: SessionType? = null
|
||||||
|
var onDelete: ((SessionType) -> Unit)? = null
|
||||||
|
var onArchive: ((SessionType.Current) -> Unit)? = null
|
||||||
|
|
||||||
|
override val containerView: View?
|
||||||
|
get() = view
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CurrentSessionBottomSheetDialogTheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||||
|
val view = inflater.inflate(R.layout.session_bottom_sheet, container, false)
|
||||||
|
|
||||||
|
view.current_session_card_title.text = getCardTitle()
|
||||||
|
view.current_session_card_tab_list.text = getTabTitles()
|
||||||
|
view.archive_session_button.apply {
|
||||||
|
visibility = if (sessionType is SessionType.Current) View.VISIBLE else View.GONE
|
||||||
|
setOnClickListener {
|
||||||
|
sessionType?.also {
|
||||||
|
if (it is SessionType.Current) {
|
||||||
|
onArchive?.invoke(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
view.delete_session_button.setOnClickListener {
|
||||||
|
sessionType?.apply { onDelete?.invoke(this) }
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getCardTitle(): String? {
|
||||||
|
return sessionType?.let {
|
||||||
|
when (it) {
|
||||||
|
is SessionType.Archived -> it.archivedSession.formattedSavedAt
|
||||||
|
is SessionType.Current -> getString(R.string.tabs_header_title)
|
||||||
|
is SessionType.Private -> getString(R.string.tabs_header_private_title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTabTitles(): String? {
|
||||||
|
return sessionType?.let {
|
||||||
|
when (it) {
|
||||||
|
is SessionType.Current -> it.titles
|
||||||
|
is SessionType.Private -> it.titles
|
||||||
|
is SessionType.Archived ->
|
||||||
|
it.archivedSession.bundle.restoreSnapshot(requireComponents.core.engine)?.let { snapshot ->
|
||||||
|
snapshot.sessions.map { item -> item.session.title }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}?.joinToString(", ") {
|
||||||
|
if (it.length > maxTitleLength) it.substring(0,
|
||||||
|
maxTitleLength
|
||||||
|
) + "..." else it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val maxTitleLength = 20
|
||||||
|
const val overflowFragmentTag = "sessionOverflow"
|
||||||
|
|
||||||
|
fun create(sessionType: SessionType): SessionBottomSheetFragment {
|
||||||
|
val fragment = SessionBottomSheetFragment()
|
||||||
|
fragment.sessionType = sessionType
|
||||||
|
return fragment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,70 +0,0 @@
|
||||||
/* 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/. */
|
|
||||||
|
|
||||||
package org.mozilla.fenix.home.sessions
|
|
||||||
|
|
||||||
import android.os.Bundle
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
|
||||||
import kotlinx.android.extensions.LayoutContainer
|
|
||||||
import kotlinx.android.synthetic.main.session_bottom_sheet.view.*
|
|
||||||
import org.mozilla.fenix.R
|
|
||||||
import org.mozilla.fenix.ext.requireComponents
|
|
||||||
import java.lang.IllegalStateException
|
|
||||||
|
|
||||||
class SessionBottomSheetFragment : BottomSheetDialogFragment(), LayoutContainer {
|
|
||||||
var archivedSession: ArchivedSession? = null
|
|
||||||
var isCurrentSession: Boolean = false
|
|
||||||
private lateinit var tabTitles: String
|
|
||||||
var onDelete: ((ArchivedSession) -> Unit)? = null
|
|
||||||
var onArchive: (() -> Unit)? = null
|
|
||||||
|
|
||||||
override val containerView: View?
|
|
||||||
get() = view
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CurrentSessionBottomSheetDialogTheme)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
||||||
val view = inflater.inflate(R.layout.session_bottom_sheet, container, false)
|
|
||||||
val snapshot = archivedSession?.bundle?.restoreSnapshot(requireComponents.core.engine)
|
|
||||||
?: throw IllegalStateException()
|
|
||||||
|
|
||||||
tabTitles = snapshot.sessions
|
|
||||||
.map { it.session.title }
|
|
||||||
.joinToString(", ") {
|
|
||||||
if (it.length > maxTitleLength) it.substring(0, maxTitleLength) + "..." else it
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isCurrentSession) {
|
|
||||||
view.current_session_card_title.text = archivedSession?.formattedSavedAt
|
|
||||||
}
|
|
||||||
|
|
||||||
view.current_session_card_tab_list.text = tabTitles
|
|
||||||
|
|
||||||
view.archive_session_button.apply {
|
|
||||||
visibility = if (isCurrentSession) View.VISIBLE else View.GONE
|
|
||||||
setOnClickListener {
|
|
||||||
onArchive?.invoke()
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
view.delete_session_button.setOnClickListener {
|
|
||||||
onDelete?.invoke(archivedSession!!)
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
return view
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val maxTitleLength = 20
|
|
||||||
const val overflowFragmentTag = "sessionOverflow"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -46,6 +46,9 @@ class TabsUIView(
|
||||||
val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null)
|
val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null)
|
||||||
Navigation.findNavController(it).navigate(directions)
|
Navigation.findNavController(it).navigate(directions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val headerTextResourceId = if (isPrivate) R.string.tabs_header_private_title else R.string.tabs_header_title
|
||||||
|
header_text.text = context.getString(headerTextResourceId)
|
||||||
tabs_overflow_button.increaseTapArea(HomeFragment.overflowButtonIncreaseDps)
|
tabs_overflow_button.increaseTapArea(HomeFragment.overflowButtonIncreaseDps)
|
||||||
tabs_overflow_button.setOnClickListener {
|
tabs_overflow_button.setOnClickListener {
|
||||||
actionEmitter.onNext(TabsAction.MenuTapped)
|
actionEmitter.onNext(TabsAction.MenuTapped)
|
||||||
|
|
|
@ -159,6 +159,8 @@
|
||||||
<!-- Sessions -->
|
<!-- Sessions -->
|
||||||
<!-- Title for the list of tabs in the current session -->
|
<!-- Title for the list of tabs in the current session -->
|
||||||
<string name="tabs_header_title">Current Session</string>
|
<string name="tabs_header_title">Current Session</string>
|
||||||
|
<!-- Title for the list of tabs in the current private session -->
|
||||||
|
<string name="tabs_header_private_title">Private Session</string>
|
||||||
<!-- Content description (not visible, for screen readers etc.): Add tab button. Adds a news tab when pressed -->
|
<!-- Content description (not visible, for screen readers etc.): Add tab button. Adds a news tab when pressed -->
|
||||||
<string name="add_tab">Add Tab</string>
|
<string name="add_tab">Add Tab</string>
|
||||||
<!-- Content description (not visible, for screen readers etc.): Close tab button. Closes the current session when pressed -->
|
<!-- Content description (not visible, for screen readers etc.): Close tab button. Closes the current session when pressed -->
|
||||||
|
|
Loading…
Reference in New Issue