Close #25954: add a new delete time range confirmation dialog for history screen
parent
bfd0eb7306
commit
7982c6b79f
@ -0,0 +1,44 @@
|
||||
/* 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.library.history
|
||||
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
|
||||
/**
|
||||
* A helper class that provides starting and ending timestamps for a set time frame. Is used by
|
||||
* [HistoryFragment] to provide timestamps for options inside the delete history dialog.
|
||||
*/
|
||||
enum class RemoveTimeFrame {
|
||||
LastHour,
|
||||
TodayAndYesterday;
|
||||
|
||||
/**
|
||||
* Provides starting and ending timestamps for a set time frame. Each call is calculated at the
|
||||
* moment of execution, which is different from [HistoryItemTimeGroup] implementation.
|
||||
*/
|
||||
fun toLongRange(): LongRange {
|
||||
return when (this) {
|
||||
LastHour -> LongRange(getHourAgo(hoursAgo = 1).time, Long.MAX_VALUE)
|
||||
TodayAndYesterday -> LongRange(getDaysAgo(daysAgo = 1).time, Long.MAX_VALUE)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getHourAgo(hoursAgo: Int): Date {
|
||||
return Calendar.getInstance().apply {
|
||||
add(Calendar.HOUR_OF_DAY, -hoursAgo)
|
||||
}.time
|
||||
}
|
||||
|
||||
private fun getDaysAgo(daysAgo: Int): Date {
|
||||
return Calendar.getInstance().apply {
|
||||
set(Calendar.HOUR_OF_DAY, 0)
|
||||
set(Calendar.MINUTE, 0)
|
||||
set(Calendar.SECOND, 0)
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
add(Calendar.DAY_OF_YEAR, -daysAgo)
|
||||
}.time
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:ellipsize="end"
|
||||
android:text="@string/delete_history_prompt_title"
|
||||
android:textColor="?attr/textPrimary"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/body"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:text="@string/delete_history_prompt_body"
|
||||
android:textColor="?attr/textPrimary"
|
||||
android:textSize="14sp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/title" />
|
||||
|
||||
<RadioGroup
|
||||
android:id="@+id/radio_group"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/body">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/last_hour_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="48dp"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:ellipsize="end"
|
||||
android:buttonTint="?attr/textSecondary"
|
||||
android:layout_marginStart="18dp"
|
||||
android:paddingStart="32dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:text="@string/delete_history_prompt_button_last_hour"
|
||||
android:textColor="?attr/textPrimary"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/today_and_yesterday_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="48dp"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:ellipsize="end"
|
||||
android:buttonTint="?attr/textSecondary"
|
||||
android:layout_marginStart="18dp"
|
||||
android:paddingStart="32dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:text="@string/delete_history_prompt_button_today_and_yesterday"
|
||||
android:textColor="?attr/textPrimary"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/everything_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="48dp"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:ellipsize="end"
|
||||
android:buttonTint="?attr/textSecondary"
|
||||
android:layout_marginStart="18dp"
|
||||
android:paddingStart="32dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:text="@string/delete_history_prompt_button_everything"
|
||||
android:textColor="?attr/textPrimary"
|
||||
android:textSize="16sp" />
|
||||
</RadioGroup>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -0,0 +1,58 @@
|
||||
/* 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.library.history
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import java.util.Calendar
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class RemoveTimeFrameTest {
|
||||
|
||||
@Test
|
||||
fun `WHEN LastHour is calculated THEN first timeStamp is one hour ago`() {
|
||||
val lastHourRange = RemoveTimeFrame.LastHour.toLongRange()
|
||||
val nowMillis = Calendar.getInstance().timeInMillis
|
||||
val millisDif = nowMillis - lastHourRange.first
|
||||
val hourDif = TimeUnit.HOURS.convert(millisDif, TimeUnit.MILLISECONDS)
|
||||
Assert.assertEquals(1, hourDif)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN LastHour is calculated THEN second timeStamp is equal or greater than now`() {
|
||||
val lastHourRange = RemoveTimeFrame.LastHour.toLongRange()
|
||||
val nowMillis = Calendar.getInstance().timeInMillis
|
||||
Assert.assertTrue(nowMillis <= lastHourRange.last)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN TodayAndYesterday is calculated THEN first timeStamp is one day ago`() {
|
||||
val lastHourRange = RemoveTimeFrame.TodayAndYesterday.toLongRange()
|
||||
val nowMillis = Calendar.getInstance().timeInMillis
|
||||
val millisDif = nowMillis - lastHourRange.first
|
||||
val daysDif = TimeUnit.DAYS.convert(millisDif, TimeUnit.MILLISECONDS)
|
||||
Assert.assertEquals(1, daysDif)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN TodayAndYesterday is calculated THEN first timeStamp is the start of the previous day`() {
|
||||
val todayAndYesterdayRange = RemoveTimeFrame.TodayAndYesterday.toLongRange()
|
||||
val yesterdayStartMillis = Calendar.getInstance().apply {
|
||||
add(Calendar.DAY_OF_YEAR, -1)
|
||||
set(Calendar.HOUR_OF_DAY, 0)
|
||||
set(Calendar.MINUTE, 0)
|
||||
set(Calendar.SECOND, 0)
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
}.timeInMillis
|
||||
Assert.assertEquals(yesterdayStartMillis, todayAndYesterdayRange.first)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN TodayAndYesterday is calculated THEN second timeStamp is equal or greater than now`() {
|
||||
val lastHourRange = RemoveTimeFrame.TodayAndYesterday.toLongRange()
|
||||
val nowMillis = Calendar.getInstance().timeInMillis
|
||||
Assert.assertTrue(nowMillis <= lastHourRange.last)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue