Skip to content

Commit

Permalink
Minor changes for configurable trash delete timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaisn committed Dec 28, 2023
1 parent 8a17fac commit 3049bb0
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v1.5.4 (2023-12-28)
- Ask for notification permission on Android 13 when importing data with reminders.
- Added option to change trash auto-delete delay (#118, @dd-dreams).
- Fixed link URL not saved across process death in EditFragment.
- Fixed alarm permission causing crash on Android 14 (#125, @GitGitro)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Nicolas Maltais
* Copyright 2023 Nicolas Maltais
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,12 @@

package com.maltaisn.notes.model

import android.text.format.DateUtils
import com.maltaisn.notes.model.entity.Note
import com.maltaisn.notes.model.entity.NoteStatus
import kotlinx.coroutines.NonCancellable
import com.maltaisn.notes.ui.note.DeletedNotesTimeoutField
import kotlinx.coroutines.withContext
import javax.inject.Inject
import kotlin.time.Duration.Companion.days

class DefaultNotesRepository @Inject constructor(
private val notesDao: NotesDao,
Expand Down Expand Up @@ -74,12 +73,7 @@ class DefaultNotesRepository @Inject constructor(
}

override suspend fun deleteOldNotesInTrash() {
val delay = when (prefs.deletedNotesTimeoutField) {
DeletedNotesTimeoutField.DAY -> 1.days
DeletedNotesTimeoutField.DAYS_7 -> 7.days
DeletedNotesTimeoutField.MONTH -> 30.days
DeletedNotesTimeoutField.YEAR -> 365.days
}.inWholeMilliseconds
val delay = prefs.deletedNotesTimeout.value.toInt() * DateUtils.DAY_IN_MILLIS
val minDate = System.currentTimeMillis() - delay
notesDao.deleteNotesByStatusAndDate(NoteStatus.DELETED, minDate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PrefsManager @Inject constructor(
val swipeActionRight: SwipeAction by enumPreference(SWIPE_ACTION_RIGHT, SwipeAction.ARCHIVE)
val shownDateField: ShownDateField by enumPreference(SHOWN_DATE, ShownDateField.NONE)
val maximumPreviewLabels: Int by preference(PREVIEW_LABELS, 0)
val deletedNotesTimeoutField: DeletedNotesTimeoutField by enumPreference(DELETED_TIMEOUT, DeletedNotesTimeoutField.DAYS_7)
val deletedNotesTimeout: DeletedNotesTimeoutField by enumPreference(DELETED_TIMEOUT, DeletedNotesTimeoutField.WEEK)

var sortField: SortField by enumPreference(SORT_FIELD, SortField.MODIFIED_DATE)
var sortDirection: SortDirection by enumPreference(SORT_DIRECTION, SortDirection.DESCENDING)
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/kotlin/com/maltaisn/notes/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ import com.maltaisn.notes.ui.note.adapter.MessageItem
import com.maltaisn.notes.ui.note.adapter.NoteAdapter
import com.maltaisn.notes.ui.note.adapter.NoteItem
import com.maltaisn.notes.ui.note.adapter.NoteListItem
import com.maltaisn.notes.ui.note.DeletedNotesTimeoutField
import com.maltaisn.notes.ui.send
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import debugCheck
import kotlinx.coroutines.launch
import java.util.Calendar
import kotlin.time.Duration.Companion.days

class HomeViewModel @AssistedInject constructor(
@Assisted savedStateHandle: SavedStateHandle,
Expand Down Expand Up @@ -288,14 +286,8 @@ class HomeViewModel @AssistedInject constructor(
System.currentTimeMillis() - prefs.lastTrashReminderTime >
PrefsManager.TRASH_REMINDER_DELAY.inWholeMilliseconds
) {
this += MessageItem(TRASH_REMINDER_ITEM_ID,
R.string.trash_reminder_message,
listOf(when (prefs.deletedNotesTimeoutField) {
DeletedNotesTimeoutField.DAY -> 1.days
DeletedNotesTimeoutField.DAYS_7 -> 7.days
DeletedNotesTimeoutField.MONTH -> 30.days
DeletedNotesTimeoutField.YEAR -> 365.days
}.inWholeDays))
this += MessageItem(TRASH_REMINDER_ITEM_ID, R.plurals.trash_reminder_message,
listOf(prefs.deletedNotesTimeout.value.toInt()))
}

for (note in notes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import com.maltaisn.notes.model.findValueEnum

/**
* Enum for deleted notes timeout.
* [value] is from [R.array.pref_shown_date_values].
* [value] is from [R.array.pref_deleted_notes_timeout_values].
*/
enum class DeletedNotesTimeoutField(override val value: String) : ValueEnum<String> {
DAY("24h"),
DAYS_7("7d"),
MONTH("30d"),
YEAR("1y");
DAY("1"),
WEEK("7"),
MONTH("30"),
YEAR("365");

companion object {
fun fromValue(value: String): DeletedNotesTimeoutField = findValueEnum(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,14 @@ class MessageViewHolder(private val binding: ItemMessageBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(item: MessageItem, adapter: NoteAdapter) {
binding.messageTxv.text = adapter.context.getString(item.message, *item.args.toTypedArray())
val firstArg = item.args.getOrNull(0)
binding.messageTxv.text = if (firstArg is Int) {
// Likely a plural
adapter.context.resources.getQuantityString(item.message, firstArg, *item.args.toTypedArray())
} else {
// A string, possibly with format arguments
adapter.context.getString(item.message, *item.args.toTypedArray())
}
binding.closeImv.setOnClickListener {
adapter.callback.onMessageItemDismissed(item, bindingAdapterPosition)
adapter.notifyItemRemoved(bindingAdapterPosition)
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2021 Nicolas Maltais
Copyright 2023 Nicolas Maltais
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,15 +54,15 @@

<string-array name="pref_deleted_notes_timeout_entries">
<item>@string/pref_deleted_notes_timeout_day</item>
<item>@string/pref_deleted_notes_timeout_7_days</item>
<item>@string/pref_deleted_notes_timeout_1_month</item>
<item>@string/pref_deleted_notes_timeout_1_year</item>
<item>@string/pref_deleted_notes_timeout_week</item>
<item>@string/pref_deleted_notes_timeout_month</item>
<item>@string/pref_deleted_notes_timeout_year</item>
</string-array>
<string-array name="pref_deleted_notes_timeout_values">
<item>24h</item>
<item>7d</item>
<item>30d</item>
<item>1y</item>
<item>1</item>
<item>7</item>
<item>30</item>
<item>365</item>
</string-array>

</resources>
15 changes: 9 additions & 6 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@
<item quantity="other">+ %d checked items</item>
</plurals>

<string name="trash_reminder_message">Notes in recycle bin are automatically deleted after %d days.</string>
<plurals name="trash_reminder_message">
<item quantity="one">Notes in recycle bin are automatically deleted after %d day.</item>
<item quantity="other">Notes in recycle bin are automatically deleted after %d days.</item>
</plurals>
<string name="trash_delete_message">This note will be permanently deleted.</string>
<string name="trash_delete_selected_message">Selected notes will be permanently deleted.</string>
<string name="trash_empty_message">All notes in recycle bin will be permanently deleted.</string>
Expand Down Expand Up @@ -198,11 +201,11 @@
<string name="pref_shown_date_modified">Last modified date</string>
<string name="pref_shown_date_none">No date shown</string>

<string name="pref_deleted_notes_timeout_title">Deleted notes timeout</string>
<string name="pref_deleted_notes_timeout_day">24 Hours</string>
<string name="pref_deleted_notes_timeout_7_days">7 Days</string>
<string name="pref_deleted_notes_timeout_1_month">30 Days</string>
<string name="pref_deleted_notes_timeout_1_year">1 Year</string>
<string name="pref_deleted_notes_timeout_title">Trash auto-delete delay</string>
<string name="pref_deleted_notes_timeout_day">24 hours</string>
<string name="pref_deleted_notes_timeout_week">7 days</string>
<string name="pref_deleted_notes_timeout_month">30 days</string>
<string name="pref_deleted_notes_timeout_year">1 year</string>

<string name="pref_preview_lines">Maximum lines in previews</string>
<string name="pref_preview_lines_summary">Configure how many lines and items are shown on notes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ class HomeViewModelTest {
assertFalse(viewModel.fabShown.getOrAwaitValue())

assertEquals(listOf(
MessageItem(-1, R.string.trash_reminder_message,
listOf(PrefsManager.TRASH_AUTO_DELETE_DELAY.inWholeDays)),
MessageItem(-1, R.plurals.trash_reminder_message, listOf(7)),
noteItem(note)
), viewModel.noteItems.getOrAwaitValue())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.maltaisn.notesshared.model

import com.maltaisn.notes.model.NotesRepository
import com.maltaisn.notes.model.PrefsManager
import com.maltaisn.notes.model.SortDirection
import com.maltaisn.notes.model.SortField
import com.maltaisn.notes.model.entity.LabelRef
Expand Down Expand Up @@ -218,8 +217,7 @@ class MockNotesRepository(private val labelsRepository: MockLabelsRepository) :
override suspend fun deleteOldNotesInTrash() {
notes.entries.removeIf { (_, note) ->
note.status == NoteStatus.DELETED &&
(System.currentTimeMillis() - note.lastModifiedDate.time) >
PrefsManager.TRASH_AUTO_DELETE_DELAY.inWholeMilliseconds
(System.currentTimeMillis() - note.lastModifiedDate.time) > 7 // Actually this is now a setting...
}
changeFlow.emit(Unit)
}
Expand Down

0 comments on commit 3049bb0

Please sign in to comment.