Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 581659ba authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

fix:3241: Display weekly report on Sundays

parent fb82df9a
Loading
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -64,4 +64,8 @@ class WeeklyReportLocalRepository(
            }.getOrNull()
        } ?: emptyList()
    }

    suspend fun clearAllReports() {
        return store.removeKey(weeklyReportsKey)
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import foundation.e.advancedprivacy.domain.entities.weeklyreport.WeeklyReportSco
import foundation.e.advancedprivacy.externalinterfaces.workers.WeeklyReportWorkerScheduler
import foundation.e.advancedprivacy.trackers.data.StatsDatabase
import foundation.e.advancedprivacy.trackers.data.TrackersRepository
import java.time.DayOfWeek
import java.time.DayOfWeek.SUNDAY
import java.time.Instant
import java.time.LocalDate
import java.time.temporal.ChronoUnit
@@ -47,7 +49,7 @@ class WeeklyReportUseCase(
    private val scope: CoroutineScope
) {
    companion object {
        const val REPORT_DAY_OF_WEEK: Long = 7L
        val REPORT_DAY_OF_WEEK: DayOfWeek = SUNDAY
        const val REPORT_HOUR: Long = 11L
    }
    private val _currentReport = MutableStateFlow<DisplayableReport?>(null)
+8 −6
Original line number Diff line number Diff line
@@ -25,10 +25,11 @@ import androidx.work.WorkManager
import androidx.work.WorkerParameters
import foundation.e.advancedprivacy.domain.entities.weeklyreport.WeeklyReport
import foundation.e.advancedprivacy.domain.usecases.WeeklyReportUseCase
import java.time.LocalDate
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit
import java.time.temporal.WeekFields
import java.time.temporal.TemporalAdjusters.nextOrSame
import java.util.concurrent.TimeUnit
import org.koin.java.KoinJavaComponent.get
import timber.log.Timber
@@ -57,16 +58,17 @@ class WeeklyReportDismissWorker(appContext: Context, workerParams: WorkerParamet
class WeeklyReportWorkerScheduler(private val appContext: Context) {
    fun scheduleNext(lastReport: WeeklyReport?) {
        val now = ZonedDateTime.now()
        var next = now
        next = next.with(WeekFields.of(appContext.resources.configuration.locales[0]).dayOfWeek(), WeeklyReportUseCase.REPORT_DAY_OF_WEEK)
        next = next.truncatedTo(ChronoUnit.DAYS)
        var nextDate: LocalDate = now.toLocalDate()

        nextDate.with(nextOrSame(WeeklyReportUseCase.REPORT_DAY_OF_WEEK))

        // Report has already been created today,
        // next report will be scheduled for next week
        if (next.toLocalDate() == lastReport?.timestamp && now.hour >= WeeklyReportUseCase.REPORT_HOUR) {
            next = next.plus(7, ChronoUnit.DAYS)
        if (nextDate == lastReport?.getDate() && now.hour >= WeeklyReportUseCase.REPORT_HOUR) {
            nextDate = nextDate.plus(7, ChronoUnit.DAYS)
        }

        var next = nextDate.atStartOfDay(ZoneId.systemDefault())
        next = next.plus(WeeklyReportUseCase.REPORT_HOUR, ChronoUnit.HOURS)

        val delay = next.toEpochSecond() - now.toEpochSecond()
+6 −0
Original line number Diff line number Diff line
@@ -147,6 +147,12 @@ class DebugWeeklyReportFragment : Fragment(R.layout.debug_weekly_report_fragment
            }
        }

        binding.debugClearAllReport.setOnClickListener {
            lifecycleScope.launch(Dispatchers.IO) {
                weeklyReportRepository.clearAllReports()
            }
        }

        binding.debugGenerateOldReport.setOnClickListener {
            lifecycleScope.launch {
                val count = binding.debugWeeksAgo.text.toString().toInt()
+21 −7
Original line number Diff line number Diff line
@@ -25,6 +25,12 @@
        android:layout_width="match_parent"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingHorizontal="8dp"
            >
            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/debugUpdateReport"
                android:layout_width="wrap_content"
@@ -32,6 +38,14 @@
                android:layout_margin="4dp"
                android:text="@string/debug_weekly_report_update_report"
                />
            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/debugClearAllReport"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_margin="4dp"
                android:text="@string/debug_weekly_report_clear_all_reports"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
Loading