Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to remove old events in the settings (issue #2248) #2281

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.simplemobiletools.calendar.pro.activities

import android.annotation.SuppressLint
import android.app.Activity
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.ActivityNotFoundException
import android.content.Intent
import android.icu.util.Calendar
import android.media.AudioManager
import android.media.RingtoneManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.RelativeLayout
import android.widget.Button
import android.widget.DatePicker
import android.widget.ImageButton
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import com.simplemobiletools.calendar.pro.R
Expand All @@ -27,7 +37,6 @@ import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import kotlin.system.exitProcess

Expand All @@ -36,13 +45,12 @@ class SettingsActivity : SimpleActivity() {
private val PICK_SETTINGS_IMPORT_SOURCE_INTENT = 2
private val PICK_EVENTS_IMPORT_SOURCE_INTENT = 3
private val PICK_EVENTS_EXPORT_FILE_INTENT = 4

private var mStoredPrimaryColor = 0

private var eventTypesToExport = listOf<Long>()

private val binding by viewBinding(ActivitySettingsBinding::inflate)

@SuppressLint("WrongViewCast")
@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
isMaterialActivity = true
super.onCreate(savedInstanceState)
Expand All @@ -51,14 +59,42 @@ class SettingsActivity : SimpleActivity() {

updateMaterialActivityViews(binding.settingsCoordinator, binding.settingsHolder, useTransparentNavigation = true, useTopSearchMenu = false)
setupMaterialScrollListener(binding.settingsNestedScrollview, binding.settingsToolbar)

val settingsDeleteEventsBeforeDateHolder: RelativeLayout = findViewById(R.id.settings_delete_events_before_date_holder)
settingsDeleteEventsBeforeDateHolder.setOnClickListener {
showDatePickerDialog()
}
}

@RequiresApi(Build.VERSION_CODES.N)
private fun showDatePickerDialog() {
val currentDate = Calendar.getInstance()
val year = currentDate.get(Calendar.YEAR)
val month = currentDate.get(Calendar.MONTH)
val day = currentDate.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(
this,
{ _, year, month, day ->
val selectedDate = Calendar.getInstance()
selectedDate.set(year, month, day)
val chosenDateTimestamp = selectedDate.timeInMillis
setupDeleteEventsBeforeDate(chosenDateTimestamp)
},
year,
month,
day
)
datePickerDialog.show()
}

@RequiresApi(Build.VERSION_CODES.N)
override fun onResume() {
super.onResume()
setupToolbar(binding.settingsToolbar, NavigationIcon.Arrow)
setupSettingItems()
}

@RequiresApi(Build.VERSION_CODES.N)
private fun setupSettingItems() {
setupCustomizeColors()
setupCustomizeNotifications()
Expand Down Expand Up @@ -112,6 +148,7 @@ class SettingsActivity : SimpleActivity() {
setupExportSettings()
setupImportSettings()


arrayOf(
binding.settingsColorCustomizationSectionLabel,
binding.settingsGeneralSettingsLabel,
Expand Down Expand Up @@ -144,6 +181,7 @@ class SettingsActivity : SimpleActivity() {
config.defaultReminder3 = reminders.getOrElse(2) { REMINDER_OFF }
}

@RequiresApi(Build.VERSION_CODES.N)
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == GET_RINGTONE_URI && resultCode == RESULT_OK && resultData != null) {
Expand Down Expand Up @@ -402,10 +440,16 @@ class SettingsActivity : SimpleActivity() {
settingsDeleteAllEventsHolder.setOnClickListener {
ConfirmationDialog(this@SettingsActivity, messageId = R.string.delete_all_events_confirmation) {
eventsHelper.deleteAllEvents()

}
}
}

@RequiresApi(Build.VERSION_CODES.N)
private fun setupDeleteEventsBeforeDate(chosenDateTimestamp: Long) = binding.apply {
eventsHelper.deleteEventsBeforeDate(chosenDateTimestamp)
}

private fun setupDisplayDescription() = binding.apply {
settingsDisplayDescription.isChecked = config.displayDescription
settingsReplaceDescriptionHolder.beVisibleIf(config.displayDescription)
Expand All @@ -424,6 +468,7 @@ class SettingsActivity : SimpleActivity() {
}
}

@RequiresApi(Build.VERSION_CODES.N)
private fun setupWeeklyStart() = binding.apply {
settingsStartWeeklyAt.text = getHoursString(config.startWeeklyAt)
settingsStartWeeklyAtHolder.setOnClickListener {
Expand Down Expand Up @@ -620,6 +665,7 @@ class SettingsActivity : SimpleActivity() {
}
}

@RequiresApi(Build.VERSION_CODES.N)
private fun getHoursString(hours: Int): String {
return if (config.use24HourFormat) {
String.format("%02d:00", hours)
Expand Down Expand Up @@ -972,6 +1018,7 @@ class SettingsActivity : SimpleActivity() {
}
}

@RequiresApi(Build.VERSION_CODES.N)
private fun setupImportSettings() {
binding.settingsImportHolder.setOnClickListener {
if (isQPlus()) {
Expand Down Expand Up @@ -1001,6 +1048,7 @@ class SettingsActivity : SimpleActivity() {
}
}

@RequiresApi(Build.VERSION_CODES.N)
private fun parseFile(inputStream: InputStream?) {
if (inputStream == null) {
toast(com.simplemobiletools.commons.R.string.unknown_error_occurred)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.pro.helpers

import android.app.Activity
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.collection.LongSparseArray
Expand All @@ -13,6 +14,7 @@ import com.simplemobiletools.commons.extensions.getProperPrimaryColor
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.CHOPPED_LIST_DEFAULT_SIZE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import java.util.Calendar

class EventsHelper(val context: Context) {
private val config = context.config
Expand Down Expand Up @@ -257,6 +259,13 @@ class EventsHelper(val context: Context) {
}
}

fun deleteEventsBeforeDate(chosenDateTimestamp: Long) {
ensureBackgroundThread {
val eventIds = eventsDB.getEventIdsBeforeDate(chosenDateTimestamp).toMutableList()
deleteEvents(eventIds, true)
}
}

fun deleteEvent(id: Long, deleteFromCalDAV: Boolean) = deleteEvents(arrayListOf(id), deleteFromCalDAV)

fun deleteEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ interface EventsDao {

@Query("DELETE FROM events WHERE source = :source AND import_id = :importId")
fun deleteBirthdayAnniversary(source: String, importId: String): Int
@Query("SELECT id FROM events WHERE start_ts*1000 < :chosenDateTimestamp")
fun getEventIdsBeforeDate(chosenDateTimestamp: Long): List<Long>
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,22 @@

</RelativeLayout>

<RelativeLayout
android:id="@+id/settings_delete_events_before_date_holder"
style="@style/SettingsHolderTextViewOneLinerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">

<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_before_events_from_date"
style="@style/SettingsTextLabelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete events and tasks before a specific date" />

</RelativeLayout>

<include
android:id="@+id/settings_events_divider"
layout="@layout/divider" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
<string name="add_holidays">Ajouter des jours fériés</string>
<string name="national_holidays">Jours fériés nationaux</string>
<string name="religious_holidays">Jours fériés religieux</string>
<string name="holidays_imported_successfully">Les jours fériés ont été correctement importés dans le type d'évènement \'Jours fériés\'</string>
<string name="holidays_imported_successfully">Les jours fériés ont été correctement importés dans le type d\'évènement \'Jours fériés\'</string>
<string name="importing_some_holidays_failed">L\'importation de certains évènements a échoué</string>
<string name="importing_holidays_failed">L\'importation des jours fériés a échoué</string>
<!-- Settings -->
Expand Down