From 8a17facbe2d62700b09c4ac73a97414b89e55c70 Mon Sep 17 00:00:00 2001 From: dd-dreams <80887265+dd-dreams@users.noreply.github.com> Date: Sun, 1 Oct 2023 21:50:39 +0300 Subject: [PATCH] Notes auto delete --- .../notes/model/DefaultNotesRepository.kt | 9 ++++- .../com/maltaisn/notes/model/PrefsManager.kt | 8 ++--- .../maltaisn/notes/ui/home/HomeViewModel.kt | 9 ++++- .../notes/ui/note/DeletedNotesTimeoutField.kt | 36 +++++++++++++++++++ app/src/main/res/values/arrays.xml | 13 +++++++ app/src/main/res/values/strings.xml | 7 ++++ app/src/main/res/xml/prefs.xml | 8 +++++ 7 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 app/src/main/kotlin/com/maltaisn/notes/ui/note/DeletedNotesTimeoutField.kt diff --git a/app/src/main/kotlin/com/maltaisn/notes/model/DefaultNotesRepository.kt b/app/src/main/kotlin/com/maltaisn/notes/model/DefaultNotesRepository.kt index d7c2eda4..e706a3b0 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/model/DefaultNotesRepository.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/model/DefaultNotesRepository.kt @@ -19,8 +19,10 @@ package com.maltaisn.notes.model 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, @@ -72,7 +74,12 @@ class DefaultNotesRepository @Inject constructor( } override suspend fun deleteOldNotesInTrash() { - val delay = PrefsManager.TRASH_AUTO_DELETE_DELAY.inWholeMilliseconds + val delay = when (prefs.deletedNotesTimeoutField) { + DeletedNotesTimeoutField.DAY -> 1.days + DeletedNotesTimeoutField.DAYS_7 -> 7.days + DeletedNotesTimeoutField.MONTH -> 30.days + DeletedNotesTimeoutField.YEAR -> 365.days + }.inWholeMilliseconds val minDate = System.currentTimeMillis() - delay notesDao.deleteNotesByStatusAndDate(NoteStatus.DELETED, minDate) } diff --git a/app/src/main/kotlin/com/maltaisn/notes/model/PrefsManager.kt b/app/src/main/kotlin/com/maltaisn/notes/model/PrefsManager.kt index a2fe751c..a8ba8c28 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/model/PrefsManager.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/model/PrefsManager.kt @@ -24,6 +24,7 @@ import androidx.preference.PreferenceManager import com.maltaisn.notes.R import com.maltaisn.notes.model.entity.NoteType import com.maltaisn.notes.ui.AppTheme +import com.maltaisn.notes.ui.note.DeletedNotesTimeoutField import com.maltaisn.notes.ui.note.ShownDateField import com.maltaisn.notes.ui.note.SwipeAction import com.maltaisn.notes.ui.note.adapter.NoteListLayoutMode @@ -51,6 +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) var sortField: SortField by enumPreference(SORT_FIELD, SortField.MODIFIED_DATE) var sortDirection: SortDirection by enumPreference(SORT_DIRECTION, SortDirection.DESCENDING) @@ -188,6 +190,7 @@ class PrefsManager @Inject constructor( const val CLEAR_DATA = "clear_data" const val VIEW_LICENSES = "view_licenses" const val VERSION = "version" + const val DELETED_TIMEOUT = "deleted_timeout" // Other keys private const val AUTO_EXPORT_URI = "auto_export_uri" @@ -209,11 +212,6 @@ class PrefsManager @Inject constructor( R.xml.prefs_preview_lines, ) - /** - * Delay after which notes in trash are automatically deleted forever. - */ - val TRASH_AUTO_DELETE_DELAY = 7.days - /** * Required delay before showing the trash reminder delay after user dismisses it. */ diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/home/HomeViewModel.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/home/HomeViewModel.kt index 26b9dd79..0bb33236 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/ui/home/HomeViewModel.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/home/HomeViewModel.kt @@ -43,6 +43,7 @@ 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 @@ -50,6 +51,7 @@ 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, @@ -288,7 +290,12 @@ class HomeViewModel @AssistedInject constructor( ) { this += MessageItem(TRASH_REMINDER_ITEM_ID, R.string.trash_reminder_message, - listOf(PrefsManager.TRASH_AUTO_DELETE_DELAY.inWholeDays)) + listOf(when (prefs.deletedNotesTimeoutField) { + DeletedNotesTimeoutField.DAY -> 1.days + DeletedNotesTimeoutField.DAYS_7 -> 7.days + DeletedNotesTimeoutField.MONTH -> 30.days + DeletedNotesTimeoutField.YEAR -> 365.days + }.inWholeDays)) } for (note in notes) { diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/note/DeletedNotesTimeoutField.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/note/DeletedNotesTimeoutField.kt new file mode 100644 index 00000000..0c4a5d6e --- /dev/null +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/note/DeletedNotesTimeoutField.kt @@ -0,0 +1,36 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.maltaisn.notes.ui.note + +import com.maltaisn.notes.R +import com.maltaisn.notes.model.ValueEnum +import com.maltaisn.notes.model.findValueEnum + +/** + * Enum for deleted notes timeout. + * [value] is from [R.array.pref_shown_date_values]. + */ +enum class DeletedNotesTimeoutField(override val value: String) : ValueEnum { + DAY("24h"), + DAYS_7("7d"), + MONTH("30d"), + YEAR("1y"); + + companion object { + fun fromValue(value: String): DeletedNotesTimeoutField = findValueEnum(value) + } +} \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index b7c8e37e..7388a30a 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -52,4 +52,17 @@ none + + @string/pref_deleted_notes_timeout_day + @string/pref_deleted_notes_timeout_7_days + @string/pref_deleted_notes_timeout_1_month + @string/pref_deleted_notes_timeout_1_year + + + 24h + 7d + 30d + 1y + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d34da0f5..fe465e95 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -198,6 +198,12 @@ Last modified date No date shown + Deleted notes timeout + 24 Hours + 7 Days + 30 Days + 1 Year + Maximum lines in previews Configure how many lines and items are shown on notes, for each layout mode and note type @@ -210,6 +216,7 @@ Swipe left action Swipe right action Move checked items to the bottom + Deleted notes timeout Data Export data diff --git a/app/src/main/res/xml/prefs.xml b/app/src/main/res/xml/prefs.xml index 00d1d1a9..1aa59516 100644 --- a/app/src/main/res/xml/prefs.xml +++ b/app/src/main/res/xml/prefs.xml @@ -90,6 +90,14 @@ app:useSimpleSummaryProvider="true" /> + +