Use flag instead of accountId to store selected account
Mentioned in #488
This commit is contained in:
@@ -97,10 +97,6 @@ public final class Accounts {
|
|||||||
throw new IllegalArgumentException("Failed to determine account type: " + account);
|
throw new IllegalArgumentException("Failed to determine account type: " + account);
|
||||||
}
|
}
|
||||||
|
|
||||||
static String accountId(Account account) {
|
|
||||||
return account.getUsername() + ":" + account.getCharacter();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ObservableList<Account> accounts = observableArrayList(account -> new Observable[] { account });
|
private static ObservableList<Account> accounts = observableArrayList(account -> new Observable[] { account });
|
||||||
private static ReadOnlyListWrapper<Account> accountsWrapper = new ReadOnlyListWrapper<>(Accounts.class, "accounts", accounts);
|
private static ReadOnlyListWrapper<Account> accountsWrapper = new ReadOnlyListWrapper<>(Accounts.class, "accounts", accounts);
|
||||||
|
|
||||||
@@ -135,7 +131,7 @@ public final class Accounts {
|
|||||||
// selection is valid, store it
|
// selection is valid, store it
|
||||||
if (!initialized)
|
if (!initialized)
|
||||||
return;
|
return;
|
||||||
config().setSelectedAccount(selected == null ? "" : accountId(selected));
|
updateAccountStorages();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -148,9 +144,12 @@ public final class Accounts {
|
|||||||
accounts.addListener(onInvalidating(Accounts::updateAccountStorages));
|
accounts.addListener(onInvalidating(Accounts::updateAccountStorages));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Map<Object, Object> getAccountStorage(Account account) {
|
private static Map<Object, Object> getAccountStorage(Account account) {
|
||||||
Map<Object, Object> storage = account.toStorage();
|
Map<Object, Object> storage = account.toStorage();
|
||||||
storage.put("type", getLoginType(getAccountFactory(account)));
|
storage.put("type", getLoginType(getAccountFactory(account)));
|
||||||
|
if (account == selectedAccount.get()) {
|
||||||
|
storage.put("selected", true);
|
||||||
|
}
|
||||||
return storage;
|
return storage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,27 +184,26 @@ public final class Accounts {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
accounts.add(account);
|
accounts.add(account);
|
||||||
|
|
||||||
|
if (Boolean.TRUE.equals(storage.get("selected"))) {
|
||||||
|
selectedAccount.set(account);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
|
||||||
config().getAuthlibInjectorServers().addListener(onInvalidating(Accounts::removeDanglingAuthlibInjectorAccounts));
|
config().getAuthlibInjectorServers().addListener(onInvalidating(Accounts::removeDanglingAuthlibInjectorAccounts));
|
||||||
|
|
||||||
// load selected account
|
Account selected = selectedAccount.get();
|
||||||
Account selected = accounts.stream()
|
if (selected != null) {
|
||||||
.filter(it -> accountId(it).equals(config().getSelectedAccount()))
|
Schedulers.io().schedule(() -> {
|
||||||
.findFirst()
|
|
||||||
.orElse(null);
|
|
||||||
selectedAccount.set(selected);
|
|
||||||
|
|
||||||
Schedulers.io().schedule(() -> {
|
|
||||||
if (selected != null)
|
|
||||||
try {
|
try {
|
||||||
selected.logIn();
|
selected.logIn();
|
||||||
} catch (AuthenticationException e) {
|
} catch (AuthenticationException e) {
|
||||||
LOG.log(Level.WARNING, "Failed to log " + selected + " in", e);
|
LOG.log(Level.WARNING, "Failed to log " + selected + " in", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ObservableList<Account> getAccounts() {
|
public static ObservableList<Account> getAccounts() {
|
||||||
|
|||||||
@@ -120,9 +120,6 @@ public final class Config implements Cloneable, Observable {
|
|||||||
@SerializedName("accounts")
|
@SerializedName("accounts")
|
||||||
private ObservableList<Map<Object, Object>> accountStorages = FXCollections.observableArrayList();
|
private ObservableList<Map<Object, Object>> accountStorages = FXCollections.observableArrayList();
|
||||||
|
|
||||||
@SerializedName("selectedAccount")
|
|
||||||
private StringProperty selectedAccount = new SimpleStringProperty("");
|
|
||||||
|
|
||||||
@SerializedName("fontFamily")
|
@SerializedName("fontFamily")
|
||||||
private StringProperty fontFamily = new SimpleStringProperty("Consolas");
|
private StringProperty fontFamily = new SimpleStringProperty("Consolas");
|
||||||
|
|
||||||
@@ -371,18 +368,6 @@ public final class Config implements Cloneable, Observable {
|
|||||||
return accountStorages;
|
return accountStorages;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSelectedAccount() {
|
|
||||||
return selectedAccount.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSelectedAccount(String selectedAccount) {
|
|
||||||
this.selectedAccount.set(selectedAccount);
|
|
||||||
}
|
|
||||||
|
|
||||||
public StringProperty selectedAccountProperty() {
|
|
||||||
return selectedAccount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFontFamily() {
|
public String getFontFamily() {
|
||||||
return fontFamily.get();
|
return fontFamily.get();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.setting;
|
package org.jackhuang.hmcl.setting;
|
||||||
|
|
||||||
import org.jackhuang.hmcl.auth.offline.OfflineAccount;
|
|
||||||
import org.jackhuang.hmcl.auth.offline.OfflineAccountFactory;
|
|
||||||
import org.jackhuang.hmcl.util.StringUtils;
|
import org.jackhuang.hmcl.util.StringUtils;
|
||||||
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.Lang.tryCast;
|
import static org.jackhuang.hmcl.util.Lang.tryCast;
|
||||||
@@ -67,20 +62,21 @@ final class ConfigUpgrader {
|
|||||||
// Convert OfflineAccounts whose stored uuid is important.
|
// Convert OfflineAccounts whose stored uuid is important.
|
||||||
tryCast(rawJson.get("auth"), Map.class).ifPresent(auth -> {
|
tryCast(rawJson.get("auth"), Map.class).ifPresent(auth -> {
|
||||||
tryCast(auth.get("offline"), Map.class).ifPresent(offline -> {
|
tryCast(auth.get("offline"), Map.class).ifPresent(offline -> {
|
||||||
List<OfflineAccount> accounts = new LinkedList<>();
|
String selected = rawJson.containsKey("selectedAccount") ? null
|
||||||
|
: tryCast(offline.get("IAuthenticator_UserName"), String.class).orElse(null);
|
||||||
|
|
||||||
tryCast(offline.get("uuidMap"), Map.class).ifPresent(uuidMap -> {
|
tryCast(offline.get("uuidMap"), Map.class).ifPresent(uuidMap -> {
|
||||||
((Map<?, ?>) uuidMap).forEach((key, value) -> {
|
((Map<?, ?>) uuidMap).forEach((key, value) -> {
|
||||||
OfflineAccount account = OfflineAccountFactory.INSTANCE.create((String) key, UUIDTypeAdapter.fromString((String) value));
|
Map<Object, Object> storage = new HashMap<>();
|
||||||
accounts.add(account);
|
storage.put("type", "offline");
|
||||||
deserialized.getAccountStorages().add(Accounts.getAccountStorage(account));
|
storage.put("username", key);
|
||||||
|
storage.put("uuid", value);
|
||||||
|
if (key.equals(selected)) {
|
||||||
|
storage.put("selected", true);
|
||||||
|
}
|
||||||
|
deserialized.getAccountStorages().add(storage);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tryCast(offline.get("IAuthenticator_UserName"), String.class).ifPresent(selected -> {
|
|
||||||
if (!rawJson.containsKey("selectedAccount"))
|
|
||||||
accounts.stream().filter(account -> selected.equals(account.getUsername())).findAny().ifPresent(account ->
|
|
||||||
deserialized.setSelectedAccount(Accounts.accountId(account)));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -112,5 +108,22 @@ final class ConfigUpgrader {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tryCast(rawJson.get("selectedAccount"), String.class)
|
||||||
|
.ifPresent(selected -> {
|
||||||
|
deserialized.getAccountStorages().stream()
|
||||||
|
.filter(storage -> {
|
||||||
|
Object type = storage.get("type");
|
||||||
|
if ("offline".equals(type)) {
|
||||||
|
return selected.equals(storage.get("username") + ":" + storage.get("username"));
|
||||||
|
} else if ("yggdrasil".equals(type) || "authlibInjector".equals(type)) {
|
||||||
|
return selected.equals(storage.get("username") + ":" + storage.get("displayName"));
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.findFirst()
|
||||||
|
.ifPresent(storage -> storage.put("selected", true));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user