Fixes #870 - Fixes logic for the session bottom sheet fragment for private mode
Also fixes #871nightly-build-test
parent
3a558f6f78
commit
79395631d7
@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue