Add json config support
This commit is contained in:
parent
fcc8136d14
commit
7596a2c627
5 changed files with 89 additions and 17 deletions
|
@ -24,6 +24,7 @@ dependencies {
|
|||
implementation 'org.xerial:sqlite-jdbc:3.32.3'
|
||||
implementation 'org.kordamp.ikonli:ikonli-javafx:12.0.0'
|
||||
implementation 'org.kordamp.ikonli:ikonli-fontawesome5-pack:12.0.0'
|
||||
implementation 'org.json:json:20201115'
|
||||
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
|
||||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
|
||||
compile 'com.jfoenix:jfoenix:9.0.10'
|
||||
|
|
3
config.json
Normal file
3
config.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"serveur" :"test"
|
||||
}
|
|
@ -9,6 +9,7 @@ import fr.insa.clavardator.ui.dialogs.SnackbarController;
|
|||
import fr.insa.clavardator.ui.users.UserListController;
|
||||
import fr.insa.clavardator.users.CurrentUser;
|
||||
import fr.insa.clavardator.users.UserList;
|
||||
import fr.insa.clavardator.util.ConfigLoader;
|
||||
import fr.insa.clavardator.util.Log;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXML;
|
||||
|
@ -67,15 +68,15 @@ public class MainController implements Initializable {
|
|||
* @param newState The new user state
|
||||
*/
|
||||
private void onCurrentUserStateChange(CurrentUser.State newState) {
|
||||
if (newState == CurrentUser.State.VALID)
|
||||
startChat();
|
||||
else if (newState != CurrentUser.State.UNINITIALIZED) {
|
||||
final boolean isError = newState == CurrentUser.State.INVALID;
|
||||
if (isError) {
|
||||
endChat();
|
||||
}
|
||||
Platform.runLater(() -> showLogin(isError));
|
||||
if (newState == CurrentUser.State.VALID)
|
||||
startChat();
|
||||
else if (newState != CurrentUser.State.UNINITIALIZED) {
|
||||
final boolean isError = newState == CurrentUser.State.INVALID;
|
||||
if (isError) {
|
||||
endChat();
|
||||
}
|
||||
Platform.runLater(() -> showLogin(isError));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -252,14 +253,21 @@ public class MainController implements Initializable {
|
|||
/**
|
||||
* Creates database if needed, then init current user, and finally load user list
|
||||
*/
|
||||
private void initDb() {
|
||||
final DatabaseController db = new DatabaseController();
|
||||
db.initTables(() -> {
|
||||
userList.retrievedPreviousUsers(
|
||||
() -> currentUser.init(this::onInitError),
|
||||
this::onInitError
|
||||
);
|
||||
}, this::onInitError);
|
||||
private void initBackend() {
|
||||
ConfigLoader.load((ConfigLoader.Config config) -> {
|
||||
final String serverUri = config.getServerUri();
|
||||
if (serverUri != null) {
|
||||
Log.v("INIT", "Found server URI: " + serverUri);
|
||||
} else {
|
||||
Log.v("INIT", "Server URI not found. Proceeding without");
|
||||
}
|
||||
final DatabaseController db = new DatabaseController();
|
||||
db.initTables(
|
||||
() -> userList.retrievedPreviousUsers(
|
||||
() -> currentUser.init(this::onInitError),
|
||||
this::onInitError
|
||||
), this::onInitError);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -273,6 +281,6 @@ public class MainController implements Initializable {
|
|||
listController.setUserList(userList);
|
||||
listController.setRefreshUserListener(this::discoverActiveUsers);
|
||||
editUserDialogController.setUserList(userList);
|
||||
initDb();
|
||||
initBackend();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import javafx.fxml.Initializable;
|
|||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.VBox;
|
||||
import netscape.javascript.JSObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
|
|
59
src/main/java/fr/insa/clavardator/util/ConfigLoader.java
Normal file
59
src/main/java/fr/insa/clavardator/util/ConfigLoader.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
package fr.insa.clavardator.util;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ConfigLoader {
|
||||
|
||||
public static final String PATH = "config.json";
|
||||
|
||||
public static void load(ConfigLoadedCallback callback) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
InputStream is = new FileInputStream(PATH);
|
||||
JSONTokener tokener = new JSONTokener(is);
|
||||
JSONObject obj = new JSONObject(tokener);
|
||||
callback.onLoaded(new Config(obj));
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.v("CONFIG", "No config file found");
|
||||
callback.onLoaded(new Config());
|
||||
} catch (JSONException e) {
|
||||
Log.v("CONFIG", "Could not read config file. Please make sure it is a valid JSON.");
|
||||
callback.onLoaded(new Config());
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public interface ConfigLoadedCallback {
|
||||
void onLoaded(Config config);
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
@Nullable
|
||||
private String serverUri;
|
||||
private final String SERVER_URI_KEY = "serveur";
|
||||
|
||||
public Config() {
|
||||
serverUri = null;
|
||||
}
|
||||
|
||||
public Config(JSONObject obj) {
|
||||
this();
|
||||
System.out.println(obj);
|
||||
if (obj.has(SERVER_URI_KEY)) {
|
||||
serverUri = obj.getString(SERVER_URI_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable String getServerUri() {
|
||||
return serverUri;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue