diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index f68197f..3b3a93c 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -192,6 +192,26 @@ class MainActivity : AppCompatActivity() { alertDialog.show() } + fun askToTrimLogbook() { + val d = AlertDialog.Builder(this) + d.setTitle(R.string.trim_logbook_dialog_title) + d.setMessage( + when (LocalSettingsRepository(this).loadDataRepository()) { + LocalSettingsRepository.DATA_REPO.WEBDAV -> R.string.trim_logbook_dialog_message_dav + else -> R.string.trim_logbook_dialog_message_local + } + ) + d.setPositiveButton(R.string.trim_logbook_dialog_button_ok) { dialogInterface, i -> + logbook.trim() + saveLogbook() + } + d.setNegativeButton(R.string.trim_logbook_dialog_button_cancel) { dialogInterface, i -> + dialogInterface.dismiss() + } + val alertDialog = d.create() + alertDialog.show() + } + fun loadLogbook() { if (savingEvent) return @@ -261,13 +281,33 @@ class MainActivity : AppCompatActivity() { setLoading(true) logbook.logs.add(0, event) + saveLogbook(event) + + // Check logbook size to avoid OOM errors + if (logbook.isTooBig()) { + askToTrimLogbook() + } + } + + /** + * Saves the logbook. If saving while adding an event, please specify the event so in case + * of error can be removed from the list. + */ + fun saveLogbook(lastEventAdded: LunaEvent? = null) { logbookRepo?.saveLogbook(this, logbook, object: LogbookSavedListener{ override fun onLogbookSaved() { Log.d(TAG, "Logbook saved") runOnUiThread({ setLoading(false) - Toast.makeText(this@MainActivity, R.string.toast_event_added, Toast.LENGTH_SHORT).show() + Toast.makeText( + this@MainActivity, + if (lastEventAdded != null) + R.string.toast_event_added + else + R.string.toast_logbook_saved, + Toast.LENGTH_SHORT + ).show() savingEvent(false) }) } @@ -276,7 +316,8 @@ class MainActivity : AppCompatActivity() { runOnUiThread({ setLoading(false) onRepoError(getString(R.string.settings_network_error) + error.toString()) - onAddError(event, error.toString()) + if (lastEventAdded != null) + onAddError(lastEventAdded, error.toString()) }) } @@ -290,7 +331,8 @@ class MainActivity : AppCompatActivity() { getString(R.string.settings_webdav_error_generic) + error.toString() } ) - onAddError(event, error.toString()) + if (lastEventAdded != null) + onAddError(lastEventAdded, error.toString()) }) } @@ -298,7 +340,8 @@ class MainActivity : AppCompatActivity() { runOnUiThread({ setLoading(false) onRepoError(getString(R.string.settings_json_error) + error.toString()) - onAddError(event, error.toString()) + if (lastEventAdded != null) + onAddError(lastEventAdded, error.toString()) }) } @@ -306,7 +349,8 @@ class MainActivity : AppCompatActivity() { runOnUiThread({ setLoading(false) onRepoError(getString(R.string.settings_generic_error) + error.toString()) - onAddError(event, error.toString()) + if (lastEventAdded != null) + onAddError(lastEventAdded, error.toString()) }) } }) diff --git a/app/src/main/java/it/danieleverducci/lunatracker/entities/Logbook.kt b/app/src/main/java/it/danieleverducci/lunatracker/entities/Logbook.kt index 216b1b6..8017383 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/entities/Logbook.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/entities/Logbook.kt @@ -1,5 +1,19 @@ package it.danieleverducci.lunatracker.entities class Logbook { + companion object { + val MAX_SAFE_LOGBOOK_SIZE = 30000 + } val logs = ArrayList() + + fun isTooBig(): Boolean { + return logs.size > MAX_SAFE_LOGBOOK_SIZE + } + + /** + * Halves the logbook to avoid the file being too big + */ + fun trim() { + logs.subList(MAX_SAFE_LOGBOOK_SIZE/2, logs.size).clear() + } } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt index 5d2aa9a..3475ce0 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt @@ -63,6 +63,7 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p val evt = LunaEvent.fromJson(jo) logbook.logs.add(evt) } + Log.d(TAG, "Loaded ${logbook.logs.size} events into logbook") return logbook } diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 8122f76..995827a 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -29,6 +29,7 @@ Evento aggiunto + Diario salvato Impossibile aggiungere l\'evento Valore non valido. Inserire un numero intero. @@ -60,4 +61,11 @@ Connessione al server WebDAV avvenuta con successo Sul server esiste un salvataggio, ma è corrotto o illeggibile. Cancellare il file Si è verificato un errore: + + Il tuo diario è bello grande! + Il file del tuo diario sta crescendo molto. Ti suggeriamo di cancellare gli eventi più vecchi per evitare problemi di memoria. + Il file del tuo diario sta crescendo molto. Ti suggeriamo di cancellare gli eventi più vecchi per evitare problemi di memoria. Se vuoi conservare tutto lo storico, fai un backup del file "lunatracker_logbook.json" sul tuo server WebDAV o rinominalo per mantenerlo così e cominciare un nuovo diario. + Cancella i più vecchi + Ricordamelo dopo + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 89dd814..e55477f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -29,6 +29,7 @@ Event logged + Logbook saved Unable to log the event Invalid value. Insert an integer. @@ -60,4 +61,11 @@ Successfully connected with WebDAV server There\'s a save file on the server, but is corrupted or unreadable. Please delete it Error: + + Your logbook is pretty big! + Your logbook file is growing a lot. We suggest trimming the oldest events to avoid crashes. + Your logbook file is growing a lot. We suggest trimming the oldest events to avoid crashes. If you want to preserve all the events, please backup the "lunatracker_logbook.json" file on the WebDAV server or rename it to start a new logbook keeping the old one. + Trim it now + Remind me later + \ No newline at end of file