Avoid logboog to grow too much
This commit is contained in:
parent
c6b5ec1a4d
commit
653c57e6e0
@ -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())
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -1,5 +1,19 @@
|
||||
package it.danieleverducci.lunatracker.entities
|
||||
|
||||
class Logbook {
|
||||
companion object {
|
||||
val MAX_SAFE_LOGBOOK_SIZE = 30000
|
||||
}
|
||||
val logs = ArrayList<LunaEvent>()
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
||||
<string name="event_unknown_desc"></string>
|
||||
|
||||
<string name="toast_event_added">Evento aggiunto</string>
|
||||
<string name="toast_logbook_saved">Diario salvato</string>
|
||||
<string name="toast_event_add_error">Impossibile aggiungere l\'evento</string>
|
||||
<string name="toast_integer_error">Valore non valido. Inserire un numero intero.</string>
|
||||
|
||||
@ -60,4 +61,11 @@
|
||||
<string name="settings_webdav_creation_ok">Connessione al server WebDAV avvenuta con successo</string>
|
||||
<string name="settings_json_error">Sul server esiste un salvataggio, ma è corrotto o illeggibile. Cancellare il file </string>
|
||||
<string name="settings_generic_error">Si è verificato un errore: </string>
|
||||
|
||||
<string name="trim_logbook_dialog_title">Il tuo diario è bello grande!</string>
|
||||
<string name="trim_logbook_dialog_message_local">Il file del tuo diario sta crescendo molto. Ti suggeriamo di cancellare gli eventi più vecchi per evitare problemi di memoria.</string>
|
||||
<string name="trim_logbook_dialog_message_dav">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.</string>
|
||||
<string name="trim_logbook_dialog_button_ok">Cancella i più vecchi</string>
|
||||
<string name="trim_logbook_dialog_button_cancel">Ricordamelo dopo</string>
|
||||
|
||||
</resources>
|
@ -29,6 +29,7 @@
|
||||
<string name="event_unknown_desc"></string>
|
||||
|
||||
<string name="toast_event_added">Event logged</string>
|
||||
<string name="toast_logbook_saved">Logbook saved</string>
|
||||
<string name="toast_event_add_error">Unable to log the event</string>
|
||||
<string name="toast_integer_error">Invalid value. Insert an integer.</string>
|
||||
|
||||
@ -60,4 +61,11 @@
|
||||
<string name="settings_webdav_creation_ok">Successfully connected with WebDAV server</string>
|
||||
<string name="settings_json_error">There\'s a save file on the server, but is corrupted or unreadable. Please delete it </string>
|
||||
<string name="settings_generic_error">Error: </string>
|
||||
|
||||
<string name="trim_logbook_dialog_title">Your logbook is pretty big!</string>
|
||||
<string name="trim_logbook_dialog_message_local">Your logbook file is growing a lot. We suggest trimming the oldest events to avoid crashes.</string>
|
||||
<string name="trim_logbook_dialog_message_dav">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.</string>
|
||||
<string name="trim_logbook_dialog_button_ok">Trim it now</string>
|
||||
<string name="trim_logbook_dialog_button_cancel">Remind me later</string>
|
||||
|
||||
</resources>
|
Loading…
Reference in New Issue
Block a user