Skip to content

Commit

Permalink
Notes auto delete
Browse files Browse the repository at this point in the history
  • Loading branch information
dd-dreams authored and maltaisn committed Dec 28, 2023
1 parent 9b4a13b commit 8a17fac
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/kotlin/com/maltaisn/notes/model/PrefsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ 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,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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> {
DAY("24h"),
DAYS_7("7d"),
MONTH("30d"),
YEAR("1y");

companion object {
fun fromValue(value: String): DeletedNotesTimeoutField = findValueEnum(value)
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@
<item>none</item>
</string-array>

<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>
</string-array>
<string-array name="pref_deleted_notes_timeout_values">
<item>24h</item>
<item>7d</item>
<item>30d</item>
<item>1y</item>
</string-array>

</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@
<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_preview_lines">Maximum lines in previews</string>
<string name="pref_preview_lines_summary">Configure how many lines and items are shown on notes,
for each layout mode and note type</string>
Expand All @@ -210,6 +216,7 @@
<string name="pref_behavior_swipe_action_left">Swipe left action</string>
<string name="pref_behavior_swipe_action_right">Swipe right action</string>
<string name="pref_behavior_move_checked">Move checked items to the bottom</string>
<string name="pref_deleted_notes_timeout">Deleted notes timeout</string>

<string name="pref_group_data">Data</string>
<string name="pref_data_export">Export data</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@
app:useSimpleSummaryProvider="true"
/>

<DropDownPreference
app:entries="@array/pref_deleted_notes_timeout_entries"
app:entryValues="@array/pref_deleted_notes_timeout_values"
app:key="deleted_timeout"
app:title="@string/pref_deleted_notes_timeout_title"
app:useSimpleSummaryProvider="true"
/>

<SwitchPreferenceCompat
app:defaultValue="false"
app:key="move_checked_to_bottom"
Expand Down

0 comments on commit 8a17fac

Please sign in to comment.