diff --git a/app/src/main/java/org/mozilla/fenix/components/SectionedAdapter.kt b/app/src/main/java/org/mozilla/fenix/components/SectionedAdapter.kt index e41a15be1..f3e654e1c 100644 --- a/app/src/main/java/org/mozilla/fenix/components/SectionedAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/components/SectionedAdapter.kt @@ -8,6 +8,7 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import java.lang.IllegalStateException +@SuppressWarnings("TooManyFunctions") abstract class SectionedAdapter : RecyclerView.Adapter() { sealed class SectionType { data class Header(val index: Int) : SectionType() @@ -33,9 +34,8 @@ abstract class SectionedAdapter : RecyclerView.Adapter( abstract fun onCreateItemViewHolder(parent: ViewGroup): RecyclerView.ViewHolder abstract fun onBindItemViewHolder(holder: RecyclerView.ViewHolder, row: SectionType.Row) - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { - return when (viewType) { + return when (viewType) { SectionType.HeaderViewType -> onCreateHeaderViewHolder(parent) SectionType.RowViewType -> onCreateItemViewHolder(parent) else -> throw IllegalStateException("ViewType: $viewType is invalid ") @@ -69,14 +69,14 @@ abstract class SectionedAdapter : RecyclerView.Adapter( for (sectionIndex in 0 until numberOfSections()) { if (position == currentPosition) { return SectionType.Header(sectionIndex) } - currentPosition +=1 + currentPosition += 1 for (rowIndex in 0 until numberOfRowsInSection(sectionIndex)) { if (currentPosition == position) { return SectionType.Row(sectionIndex, rowIndex) } currentPosition += 1 - } + } } - throw IllegalStateException("hello world!") + throw IllegalStateException("Position $position is out of bounds!") } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt index 5a290cd2c..1726722b2 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.library.history import android.content.Context +import android.text.format.DateUtils import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,13 +19,15 @@ import kotlinx.android.synthetic.main.history_header.view.* import kotlinx.android.synthetic.main.history_list_item.view.* import mozilla.components.browser.menu.BrowserMenu import org.mozilla.fenix.components.SectionedAdapter -import java.util.* +import java.lang.IllegalStateException +import java.util.Date +import java.util.Calendar -class HistoryList(val history: List) { +private class HistoryList(val history: List) { enum class Range { Today, ThisWeek, ThisMonth, Older; - - fun humanReadable(context: Context) : String = when (this) { + + fun humanReadable(context: Context): String = when (this) { Today -> context.getString(R.string.history_today) ThisWeek -> context.getString(R.string.history_this_week) ThisMonth -> context.getString(R.string.history_this_month) @@ -39,19 +42,21 @@ class HistoryList(val history: List) { return grouped[range] ?: listOf() } + fun item(range: Range, index: Int): HistoryItem? = grouped[range]?.let { it[index] } + private val grouped: Map> init { - val oneDayAgo = getDaysAgo(1).time - val sevenDaysAgo = getDaysAgo(7).time - val thirtyDaysAgo = getDaysAgo(30).time + val oneDayAgo = getDaysAgo(zero_days).time + val sevenDaysAgo = getDaysAgo(seven_days).time + val thirtyDaysAgo = getDaysAgo(thirty_days).time val lastWeek = LongRange(sevenDaysAgo, oneDayAgo) val lastMonth = LongRange(thirtyDaysAgo, sevenDaysAgo) grouped = history.groupBy { item -> when { - item.visitedAt > oneDayAgo -> Range.Today + DateUtils.isToday(item.visitedAt) -> Range.Today lastWeek.contains(item.visitedAt) -> Range.ThisWeek lastMonth.contains(item.visitedAt) -> Range.ThisMonth else -> Range.Older @@ -65,6 +70,12 @@ class HistoryList(val history: List) { return calendar.time } + + companion object { + private const val zero_days = 0 + private const val seven_days = 7 + private const val thirty_days = 30 + } } class HistoryAdapter( @@ -81,19 +92,24 @@ class HistoryAdapter( override fun onBindHeaderViewHolder(holder: RecyclerView.ViewHolder, header: SectionType.Header) { val sectionTitle = historyList.ranges[header.index].humanReadable(holder.itemView.context) - + when (holder) { is HistoryHeaderViewHolder -> holder.bind(sectionTitle) } } override fun onCreateItemViewHolder(parent: ViewGroup): RecyclerView.ViewHolder { - val view = LayoutInflater.from(parent.context).inflate(HistoryListItemViewHolder.LAYOUT_ID, parent, false) + val view = LayoutInflater + .from(parent.context) + .inflate(HistoryListItemViewHolder.LAYOUT_ID, parent, false) return HistoryListItemViewHolder(view, actionEmitter) } override fun onBindItemViewHolder(holder: RecyclerView.ViewHolder, row: SectionType.Row) { - (holder as? HistoryListItemViewHolder)?.bind(historyList.itemsInRange(historyList.ranges[row.section])[row.row], mode) + val item = historyList.ranges[row.section] + .let { historyList.item(it, row.row) } ?: throw IllegalStateException("No item for row: $row") + + (holder as? HistoryListItemViewHolder)?.bind(item, mode) } class HistoryListItemViewHolder( diff --git a/app/src/main/res/layout/history_header.xml b/app/src/main/res/layout/history_header.xml index 1de36e27d..08eb4081f 100644 --- a/app/src/main/res/layout/history_header.xml +++ b/app/src/main/res/layout/history_header.xml @@ -7,8 +7,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="20dp" - android:layout_marginTop="24dp" - android:layout_marginBottom="24dp"> + android:layout_marginTop="16dp" + android:layout_marginBottom="8dp">