107 lines
3.6 KiB
Java
107 lines
3.6 KiB
Java
package io.gitlab.jfronny.inceptum.util.account;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
import io.gitlab.jfronny.inceptum.Inceptum;
|
|
import io.gitlab.jfronny.inceptum.util.Utils;
|
|
import io.gitlab.jfronny.inceptum.windows.AlertWindow;
|
|
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Type;
|
|
import java.nio.file.Files;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class AccountManager {
|
|
private static final Type abstractAccountListType = new TypeToken<List<MicrosoftAccount>>() {}.getType();
|
|
private static MicrosoftAccount SELECTED_ACCOUNT;
|
|
private static final List<MicrosoftAccount> ACCOUNTS = new ArrayList<>();
|
|
|
|
public static AuthInfo getSelectedAccount() {
|
|
if (SELECTED_ACCOUNT == null) return new AuthInfo("Joe", "2536abce90e8476a871679918164abc5", "99abe417230342cb8e9e2168ab46297a", "legacy");
|
|
return new AuthInfo(SELECTED_ACCOUNT);
|
|
}
|
|
|
|
public static List<MicrosoftAccount> getAccounts() {
|
|
return List.copyOf(ACCOUNTS);
|
|
}
|
|
|
|
public static void saveAccounts() {
|
|
try {
|
|
Utils.writeObject(Inceptum.ACCOUNTS_PATH, ACCOUNTS);
|
|
} catch (IOException e) {
|
|
Inceptum.LOGGER.error("Could not save accounts", e);
|
|
}
|
|
}
|
|
|
|
public static void loadAccounts() {
|
|
Inceptum.LOGGER.info("Loading accounts");
|
|
if (Files.exists(Inceptum.ACCOUNTS_PATH)) {
|
|
try {
|
|
ACCOUNTS.addAll(Utils.loadObject(Inceptum.ACCOUNTS_PATH, abstractAccountListType));
|
|
} catch (IOException e) {
|
|
Inceptum.LOGGER.error("Could not load accounts", e);
|
|
}
|
|
}
|
|
for (MicrosoftAccount account : ACCOUNTS) {
|
|
if (account.accountId.equalsIgnoreCase(Inceptum.CONFIG.lastAccount)) {
|
|
SELECTED_ACCOUNT = account;
|
|
}
|
|
}
|
|
if (SELECTED_ACCOUNT == null && ACCOUNTS.size() >= 1) {
|
|
SELECTED_ACCOUNT = ACCOUNTS.get(0);
|
|
}
|
|
Inceptum.LOGGER.info("Finished loading accounts");
|
|
}
|
|
|
|
public static void addAccount(MicrosoftAccount account) {
|
|
ACCOUNTS.add(account);
|
|
if (ACCOUNTS.size() > 1) {
|
|
Inceptum.open(new AlertWindow("Account added successfully. Switch to it now?",
|
|
() -> switchAccount(account),
|
|
() -> {}));
|
|
}
|
|
else switchAccount(account);
|
|
saveAccounts();
|
|
}
|
|
|
|
public static void removeAccount(MicrosoftAccount account) {
|
|
if (SELECTED_ACCOUNT == account) {
|
|
if (ACCOUNTS.size() == 1)
|
|
switchAccount(null);
|
|
else
|
|
switchAccount(ACCOUNTS.get(0));
|
|
}
|
|
ACCOUNTS.remove(account);
|
|
saveAccounts();
|
|
}
|
|
|
|
public static void switchAccount(MicrosoftAccount account) {
|
|
if (account == null) {
|
|
Inceptum.LOGGER.info("Logging out");
|
|
SELECTED_ACCOUNT = null;
|
|
Inceptum.CONFIG.lastAccount = null;
|
|
} else {
|
|
Inceptum.LOGGER.info("Changed account to " + account);
|
|
SELECTED_ACCOUNT = account;
|
|
Inceptum.CONFIG.lastAccount = account.accountId;
|
|
}
|
|
Inceptum.saveConfig();
|
|
}
|
|
|
|
public static Object getAccountByName(String username) {
|
|
for (MicrosoftAccount account : ACCOUNTS) {
|
|
if (account.accountId.equalsIgnoreCase(username))
|
|
return account;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static boolean isAccountByName(String username) {
|
|
for (MicrosoftAccount account : ACCOUNTS) {
|
|
if (account.accountId.equalsIgnoreCase(username))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|