2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-02 03:40:16 +00:00

For #635 - Restores a session

This commit is contained in:
Jeff Boek 2019-02-25 15:43:30 -08:00
parent 3ecf9d338e
commit 2ac2f9a9da
5 changed files with 41 additions and 18 deletions

View File

@ -26,6 +26,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.SessionsAction
import org.mozilla.fenix.home.sessions.SessionsChange
import org.mozilla.fenix.home.sessions.SessionsComponent
import org.mozilla.fenix.home.tabs.TabsAction
@ -33,7 +34,6 @@ import org.mozilla.fenix.home.tabs.TabsChange
import org.mozilla.fenix.home.tabs.TabsComponent
import org.mozilla.fenix.home.tabs.TabsState
import org.mozilla.fenix.home.tabs.toSessionViewState
import org.mozilla.fenix.isPrivate
import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.getAutoDisposeObservable
import org.mozilla.fenix.mvi.getManagedEmitter
@ -75,7 +75,7 @@ class HomeFragment : Fragment() {
bundles.observe(this, Observer {sessionBundles ->
val archivedSessions = sessionBundles.mapNotNull { sessionBundle ->
sessionBundle.id?.let { ArchivedSession(it, sessionBundle.lastSavedAt, sessionBundle.urls) }
sessionBundle.id?.let { ArchivedSession(it, sessionBundle, sessionBundle.lastSavedAt, sessionBundle.urls) }
}
getManagedEmitter<SessionsChange>().onNext(SessionsChange.Changed(archivedSessions))
@ -162,6 +162,17 @@ class HomeFragment : Fragment() {
}
}
}
getAutoDisposeObservable<SessionsAction>()
.subscribe {
when (it) {
is SessionsAction.Select -> {
it.archivedSession.bundle.restoreSnapshot(requireComponents.core.engine)?.apply {
requireComponents.core.sessionManager.restore(this)
}
}
}
}
}
sessionObserver = subscribeToSessions()

View File

@ -14,13 +14,14 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.session_item.view.*
import io.reactivex.Observer
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.session_item.*
import org.mozilla.fenix.R
class SessionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class SessionsAdapter(private val actionEmitter: Observer<SessionsAction>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var items = listOf<ArchivedSession>()
fun reloadDatat(items: List<ArchivedSession>) {
this.items = items
notifyDataSetChanged()
@ -32,7 +33,7 @@ class SessionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
return when (viewType) {
HeaderViewHolder.LAYOUT_ID -> HeaderViewHolder(view)
EmptyListViewHolder.LAYOUT_ID -> EmptyListViewHolder(view)
SessionItemViewHolder.LAYOUT_ID -> SessionItemViewHolder(view)
SessionItemViewHolder.LAYOUT_ID -> SessionItemViewHolder(view, actionEmitter)
PrivateEmptyListViewHolder.LAYOUT_ID -> PrivateEmptyListViewHolder(view)
else -> EmptyListViewHolder(view)
}
@ -84,23 +85,28 @@ class SessionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
}
}
private class SessionItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val thumbnail = view.session_card_thumbnail
private val timestampLabel = view.session_card_timestamp
private val titlesLabel = view.session_card_titles
private val extrasLabel = view.session_card_extras
private class SessionItemViewHolder(
view: View,
private val actionEmitter: Observer<SessionsAction>,
override val containerView: View? = view
) :RecyclerView.ViewHolder(view), LayoutContainer {
private var session: ArchivedSession? = null
init {
session_item.setOnClickListener {
session?.apply { actionEmitter.onNext(SessionsAction.Select(this)) }
}
}
fun bind(session: ArchivedSession) {
this.session = session
thumbnail.setColorFilter(
session_card_thumbnail.setColorFilter(
ContextCompat.getColor(itemView.context, AVAILABLE_COLOR_IDS.random()),
PorterDuff.Mode.MULTIPLY)
timestampLabel.text = session.formattedSavedAt
titlesLabel.text = session.titles
extrasLabel.text = if (session.extrasLabel > 0) {
session_card_timestamp.text = session.formattedSavedAt
session_card_titles.text = session.titles
session_card_extras.text = if (session.extrasLabel > 0) {
"+${session.extrasLabel} sites..."
} else { "" }
}

View File

@ -6,6 +6,7 @@ package org.mozilla.fenix.home.sessions
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import mozilla.components.feature.session.bundling.SessionBundle
import org.mozilla.fenix.mvi.Action
import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.Change
@ -15,7 +16,11 @@ import java.net.URL
import java.text.SimpleDateFormat
import java.util.*
data class ArchivedSession(val id: Long, private val savedAt: Long, private val _urls: List<String>) {
data class ArchivedSession(
val id: Long,
val bundle: SessionBundle,
private val savedAt: Long,
private val _urls: List<String>) {
val formattedSavedAt by lazy {
val isSameDay: (Calendar, Calendar) -> Boolean = { a, b ->
a.get(Calendar.ERA) == b.get(Calendar.ERA) &&

View File

@ -25,7 +25,7 @@ class SessionsUIView(
.inflate(R.layout.component_sessions, container, true)
.findViewById(R.id.session_list)
private val sessionsAdapter = SessionsAdapter()
private val sessionsAdapter = SessionsAdapter(actionEmitter)
init {
view.apply {

View File

@ -12,6 +12,7 @@
android:clipChildren="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/session_item"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"