diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 9de67ac57b..e72f2c16c8 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -7,12 +7,14 @@ package org.mozilla.fenix.home import android.content.res.Resources import android.graphics.drawable.BitmapDrawable import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.motion.widget.MotionLayout import androidx.fragment.app.Fragment +import androidx.lifecycle.Observer import androidx.navigation.Navigation import kotlinx.android.synthetic.main.fragment_home.* import kotlinx.android.synthetic.main.fragment_home.view.* @@ -24,6 +26,8 @@ import org.mozilla.fenix.DefaultThemeManager 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.SessionsChange import org.mozilla.fenix.home.sessions.SessionsComponent import org.mozilla.fenix.home.tabs.TabsAction import org.mozilla.fenix.home.tabs.TabsChange @@ -51,7 +55,7 @@ class HomeFragment : Fragment() { TabsComponent(view.homeLayout, bus, (activity as HomeActivity).browsingModeManager.isPrivate, TabsState(sessionManager.sessions.map { it.toSessionViewState(it == sessionManager.selectedSession) }) ) - SessionsComponent(view.homeLayout, bus, (activity as HomeActivity).browsingModeManager.isPrivate) + SessionsComponent(view.homeLayout, bus) layoutComponents(view) ActionBusFactory.get(this).logMergedObservables() val activity = activity as HomeActivity @@ -64,6 +68,16 @@ class HomeFragment : Fragment() { setupHomeMenu() + val bundles = requireComponents.core.sessionStorage.bundles(40) + + bundles.observe(this, Observer {sessionBundles -> + val archivedSessions = sessionBundles.mapNotNull { sessionBundle -> + sessionBundle.id?.let { ArchivedSession(it, sessionBundle.lastSavedAt, sessionBundle.urls) } + } + + getManagedEmitter().onNext(SessionsChange.Changed(archivedSessions)) + }) + val searchIcon = requireComponents.search.searchEngineManager.getDefaultSearchEngine(requireContext()).let { BitmapDrawable(resources, it.icon) } diff --git a/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsAdapter.kt b/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsAdapter.kt index 2acd56e88b..8981c2245c 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsAdapter.kt @@ -4,7 +4,6 @@ package org.mozilla.fenix.home.sessions -import android.content.Context import android.text.SpannableString import android.text.style.ClickableSpan import android.text.style.ForegroundColorSpan @@ -16,35 +15,34 @@ import androidx.recyclerview.widget.RecyclerView import org.mozilla.fenix.R class SessionsAdapter : RecyclerView.Adapter() { - var isPrivate = false - var context: Context? = null + private var isPrivate = false + private var items = listOf() + + + fun reloadDatat(items: List) { + this.items = items + notifyDataSetChanged() + } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false) return when (viewType) { HeaderViewHolder.LAYOUT_ID -> HeaderViewHolder(view) - EmptyListViewHolder.LAYOUT_ID -> EmptyListViewHolder(view) - PrivateEmptyListViewHolder.LAYOUT_ID -> PrivateEmptyListViewHolder(view) else -> EmptyListViewHolder(view) } } override fun getItemViewType(position: Int) = when (position) { 0 -> HeaderViewHolder.LAYOUT_ID - 1 -> if (isPrivate) PrivateEmptyListViewHolder.LAYOUT_ID else EmptyListViewHolder.LAYOUT_ID - else -> -1 + else -> EmptyListViewHolder.LAYOUT_ID } - override fun getItemCount(): Int = 2 + override fun getItemCount(): Int = items.size + 1 override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { when (holder) { - is HeaderViewHolder -> if (isPrivate) { - holder.headerText.text = "Private Session" - } else { - holder.headerText.text = "Today" - } + is HeaderViewHolder -> holder.headerText.text = "Today" is PrivateEmptyListViewHolder -> { // Format the description text to include a hyperlink val descriptionText = String @@ -66,21 +64,21 @@ class SessionsAdapter : RecyclerView.Adapter() { } } - private class HeaderViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { + private class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view) { val headerText = view.findViewById(R.id.header_text) companion object { const val LAYOUT_ID = R.layout.session_list_header } } - private class PrivateEmptyListViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { + private class PrivateEmptyListViewHolder(view: View) : RecyclerView.ViewHolder(view) { val description = view.findViewById(R.id.session_description) companion object { const val LAYOUT_ID = R.layout.session_list_empty_private } } - private class EmptyListViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { + private class EmptyListViewHolder(view: View) : RecyclerView.ViewHolder(view) { companion object { const val LAYOUT_ID = R.layout.session_list_empty } diff --git a/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsComponent.kt b/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsComponent.kt index 818ab0e711..3d79c7190f 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsComponent.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsComponent.kt @@ -12,10 +12,11 @@ import org.mozilla.fenix.mvi.Change import org.mozilla.fenix.mvi.UIComponent import org.mozilla.fenix.mvi.ViewState +data class ArchivedSession(val id: Long, private val savedAt: Long, val urls: List) + class SessionsComponent( private val container: ViewGroup, bus: ActionBusFactory, - private val isPrivate: Boolean, override var initialState: SessionsState = SessionsState(emptyList()) ) : UIComponent( @@ -25,23 +26,23 @@ class SessionsComponent( override val reducer: (SessionsState, SessionsChange) -> SessionsState = { state, change -> when (change) { - is SessionsChange.Changed -> state // copy state with changes here + is SessionsChange.Changed -> state.copy(archivedSessions = change.archivedSessions) // copy state with changes here } } - override fun initView() = SessionsUIView(container, actionEmitter, isPrivate, changesObservable) + override fun initView() = SessionsUIView(container, actionEmitter, changesObservable) init { render(reducer) } } -data class SessionsState(val sessions: List, val isPrivate: Boolean = false) : ViewState +data class SessionsState(val archivedSessions: List) : ViewState sealed class SessionsAction : Action { - object Select : SessionsAction() + data class Select(val archivedSession: ArchivedSession) : SessionsAction() } sealed class SessionsChange : Change { - data class Changed(val isPrivate: Boolean) : SessionsChange() + data class Changed(val archivedSessions: List) : SessionsChange() } diff --git a/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsUIView.kt b/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsUIView.kt index c3b36f2231..3158987ceb 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsUIView.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessions/SessionsUIView.kt @@ -17,7 +17,6 @@ import org.mozilla.fenix.mvi.UIView class SessionsUIView( container: ViewGroup, actionEmitter: Observer, - isPrivate: Boolean, changesObservable: Observable ) : UIView(container, actionEmitter, changesObservable) { @@ -31,12 +30,11 @@ class SessionsUIView( init { view.apply { layoutManager = LinearLayoutManager(container.context) - sessionsAdapter.isPrivate = isPrivate - sessionsAdapter.context = context adapter = sessionsAdapter } } override fun updateView() = Consumer { + sessionsAdapter.reloadDatat(it.archivedSessions) } }