1
0
Fork 0

For #673 - Modifies Session Bottom Sheet to work for current and archived sessions

master
Jeff Boek 2019-02-28 16:16:45 -08:00 committed by Colin Lee
parent 22c915b5a4
commit e05f0894e8
4 changed files with 106 additions and 65 deletions

View File

@ -31,7 +31,7 @@ import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.home.sessions.ArchivedSession
import org.mozilla.fenix.home.sessions.CurrentSessionBottomSheetFragment
import org.mozilla.fenix.home.sessions.SessionBottomSheetFragment
import org.mozilla.fenix.home.sessions.SessionsAction
import org.mozilla.fenix.home.sessions.SessionsChange
import org.mozilla.fenix.home.sessions.SessionsComponent
@ -181,7 +181,11 @@ class HomeFragment : Fragment() {
is TabsAction.Archive -> {
requireComponents.core.sessionStorage.archive(requireComponents.core.sessionManager)
}
is TabsAction.MenuTapped -> openSessionMenu()
is TabsAction.MenuTapped -> {
requireComponents.core.sessionStorage.current()
?.let { ArchivedSession(it.id!!, it, it.lastSavedAt, it.urls) }
?.also { openSessionMenu(it) }
}
is TabsAction.Select -> {
val session = requireComponents.core.sessionManager.findSessionById(it.sessionId)
requireComponents.core.sessionManager.select(session!!)
@ -211,7 +215,7 @@ class HomeFragment : Fragment() {
is SessionsAction.Delete -> {
requireComponents.core.sessionStorage.remove(it.archivedSession.bundle)
}
is SessionsAction.MenuTapped -> openSessionMenu()
is SessionsAction.MenuTapped -> openSessionMenu(it.archivedSession)
}
}
}
@ -319,12 +323,26 @@ class HomeFragment : Fragment() {
)
}
private fun openSessionMenu() {
CurrentSessionBottomSheetFragment().show(
requireActivity().supportFragmentManager,
CurrentSessionBottomSheetFragment.overflowFragmentTag
private fun openSessionMenu(archivedSession: ArchivedSession) {
val isCurrentSession = archivedSession.bundle.id == requireComponents.core.sessionStorage.current()?.id
SessionBottomSheetFragment().also {
it.archivedSession = archivedSession
it.isCurrentSession = isCurrentSession
it.onArchive = {
if (isCurrentSession) {
requireComponents.core.sessionStorage.archive(requireComponents.core.sessionManager)
}
}
it.onDelete = {
if (isCurrentSession) {
requireComponents.useCases.tabsUseCases.removeAllTabsOfType.invoke(false)
}
)
requireComponents.core.sessionStorage.remove(archivedSession.bundle)
true
}
}.show(requireActivity().supportFragmentManager, SessionBottomSheetFragment.overflowFragmentTag)
}
companion object {

View File

@ -1,51 +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.current_session_bottom_sheet.view.*
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.requireComponents
class CurrentSessionBottomSheetFragment : BottomSheetDialogFragment(), LayoutContainer {
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.current_session_bottom_sheet, container, false)
val sessions = requireComponents.core.sessionManager.sessions.filter {
(activity as HomeActivity).browsingModeManager.isPrivate == it.private
}
view.current_session_card_tab_list.text = sessions.joinToString(", ") {
if (it.title.length > maxTitleLength) it.title.substring(0, maxTitleLength) + "..." else it.title
}
view.delete_session_button.setOnClickListener {
sessions.forEach {
requireComponents.core.sessionManager.remove(it)
}
dismiss()
}
return view
}
companion object {
const val maxTitleLength = 20
const val overflowFragmentTag = "sessionOverflow"
}
}

View File

@ -0,0 +1,70 @@
/* 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) -> Boolean)? = 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()
}
}
view.delete_session_button.setOnClickListener {
if (onDelete?.invoke(archivedSession!!) == true) {
dismiss()
}
}
return view
}
companion object {
const val maxTitleLength = 20
const val overflowFragmentTag = "sessionOverflow"
}
}

View File

@ -1,10 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 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/. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/current_session_card"