Merge pull request 'Add a LocalDB Auto Save Mechanism' (#41) from f/localdb-autosave into develop

Reviewed-on: https://git.kske.dev/zdm/envoy/pulls/41
Reviewed-by: delvh <leon@kske.dev>
This commit is contained in:
Kai S. K. Engelbart 2020-09-22 16:45:58 +02:00
commit 9419ba2ee8
4 changed files with 25 additions and 3 deletions

View File

@ -35,6 +35,7 @@ public final class ClientConfig extends Config {
put("server", "s", identity());
put("port", "p", Integer::parseInt);
put("localDB", "db", File::new);
put("localDBSaveInterval", "db-si", Integer::parseInt);
}
/**
@ -54,4 +55,10 @@ public final class ClientConfig extends Config {
* @since Envoy Client v0.1-alpha
*/
public File getLocalDB() { return (File) items.get("localDB").get(); }
/**
* @return the amount of minutes after which the local database should be saved
* @since Envoy Client v0.2-beta
*/
public Integer getLocalDBSaveInterval() { return (Integer) items.get("localDBSaveInterval").get(); }
}

View File

@ -160,6 +160,20 @@ public final class LocalDB implements EventListener {
.forEach(chats::add);
}
/**
* Initializes a timer that automatically saves this local database after a
* period of time specified in the settings.
*
* @since Envoy Client v0.2-beta
*/
public void initAutoSave() {
new Timer("LocalDB Autosave", true).schedule(new TimerTask() {
@Override
public void run() { save(); }
}, 2000, ClientConfig.getInstance().getLocalDBSaveInterval() * 60000);
}
/**
* Stores all users. If the client user is specified, their chats will be stored
* as well. The message id generator will also be saved if present.

View File

@ -226,5 +226,8 @@ public final class Startup extends Application {
else trayIcon.hide();
});
}
// Start auto save thread
localDB.initAutoSave();
}
}

View File

@ -1,8 +1,6 @@
server=localhost
port=8080
localDB=localDB
password=
user=
ignoreLocalDB=false
localDBSaveInterval=2
consoleLevelBarrier=FINER
fileLevelBarrier=OFF