Refactor Accounts to support login type name

This commit is contained in:
yushijinhun 2018-10-28 01:08:36 +08:00
parent cbcc89b48f
commit d890b41380
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
3 changed files with 40 additions and 34 deletions

View File

@ -39,6 +39,7 @@ import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccountFactory;
import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Schedulers;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Level; import java.util.logging.Level;
@ -64,28 +65,37 @@ public final class Accounts {
new AuthlibInjectorDownloader(Metadata.HMCL_DIRECTORY, DownloadProviders::getDownloadProvider)::getArtifactInfo, new AuthlibInjectorDownloader(Metadata.HMCL_DIRECTORY, DownloadProviders::getDownloadProvider)::getArtifactInfo,
Accounts::getOrCreateAuthlibInjectorServer); Accounts::getOrCreateAuthlibInjectorServer);
private static final String TYPE_OFFLINE = "offline"; // ==== login type / account factory mapping ====
private static final String TYPE_YGGDRASIL_ACCOUNT = "yggdrasil"; private static final Map<String, AccountFactory<?>> type2factory = new HashMap<>();
private static final String TYPE_AUTHLIB_INJECTOR = "authlibInjector"; private static final Map<AccountFactory<?>, String> factory2type = new HashMap<>();
static {
type2factory.put("offline", FACTORY_OFFLINE);
type2factory.put("yggdrasil", FACTORY_YGGDRASIL);
type2factory.put("authlibInjector", FACTORY_AUTHLIB_INJECTOR);
private static Map<String, AccountFactory<?>> type2factory = mapOf( type2factory.forEach((type, factory) -> factory2type.put(factory, type));
pair(TYPE_OFFLINE, FACTORY_OFFLINE),
pair(TYPE_YGGDRASIL_ACCOUNT, FACTORY_YGGDRASIL),
pair(TYPE_AUTHLIB_INJECTOR, FACTORY_AUTHLIB_INJECTOR));
private static String accountType(Account account) {
if (account instanceof OfflineAccount)
return TYPE_OFFLINE;
else if (account instanceof AuthlibInjectorAccount)
return TYPE_AUTHLIB_INJECTOR;
else if (account instanceof YggdrasilAccount)
return TYPE_YGGDRASIL_ACCOUNT;
else
throw new IllegalArgumentException("Failed to determine account type: " + account);
} }
public static String getLoginType(AccountFactory<?> factory) {
return Optional.ofNullable(factory2type.get(factory))
.orElseThrow(() -> new IllegalArgumentException("Unrecognized account factory"));
}
public static AccountFactory<?> getAccountFactory(String loginType) {
return Optional.ofNullable(type2factory.get(loginType))
.orElseThrow(() -> new IllegalArgumentException("Unrecognized login type"));
}
// ====
public static AccountFactory<?> getAccountFactory(Account account) { public static AccountFactory<?> getAccountFactory(Account account) {
return type2factory.get(accountType(account)); if (account instanceof OfflineAccount)
return FACTORY_OFFLINE;
else if (account instanceof AuthlibInjectorAccount)
return FACTORY_AUTHLIB_INJECTOR;
else if (account instanceof YggdrasilAccount)
return FACTORY_YGGDRASIL;
else
throw new IllegalArgumentException("Failed to determine account type: " + account);
} }
static String accountId(Account account) { static String accountId(Account account) {
@ -141,7 +151,7 @@ public final class Accounts {
static Map<Object, Object> getAccountStorage(Account account) { static Map<Object, Object> getAccountStorage(Account account) {
Map<Object, Object> storage = account.toStorage(); Map<Object, Object> storage = account.toStorage();
storage.put("type", accountType(account)); storage.put("type", getLoginType(getAccountFactory(account)));
return storage; return storage;
} }
@ -255,18 +265,14 @@ public final class Accounts {
// ==== // ====
// ==== Login type name i18n === // ==== Login type name i18n ===
private static Map<AccountFactory<?>, String> loginType2name = mapOf( private static Map<AccountFactory<?>, String> unlocalizedLoginTypeNames = mapOf(
pair(Accounts.FACTORY_OFFLINE, i18n("account.methods.offline")), pair(Accounts.FACTORY_OFFLINE, "account.methods.offline"),
pair(Accounts.FACTORY_YGGDRASIL, i18n("account.methods.yggdrasil")), pair(Accounts.FACTORY_YGGDRASIL, "account.methods.yggdrasil"),
pair(Accounts.FACTORY_AUTHLIB_INJECTOR, i18n("account.methods.authlib_injector"))); pair(Accounts.FACTORY_AUTHLIB_INJECTOR, "account.methods.authlib_injector"));
public static String getAccountTypeName(AccountFactory<?> factory) { public static String getLocalizedLoginTypeName(AccountFactory<?> factory) {
return Optional.ofNullable(loginType2name.get(factory)) return i18n(Optional.ofNullable(unlocalizedLoginTypeNames.get(factory))
.orElseThrow(() -> new IllegalArgumentException("No corresponding login type name")); .orElseThrow(() -> new IllegalArgumentException("Unrecognized account factory")));
}
public static String getAccountTypeName(Account account) {
return getAccountTypeName(getAccountFactory(account));
} }
// ==== // ====
} }

View File

@ -44,7 +44,7 @@ public class AccountListItem extends RadioButton {
getStyleClass().clear(); getStyleClass().clear();
setUserData(account); setUserData(account);
StringBuilder subtitleString = new StringBuilder(Accounts.getAccountTypeName(account)); StringBuilder subtitleString = new StringBuilder(Accounts.getLocalizedLoginTypeName(Accounts.getAccountFactory(account)));
if (account instanceof AuthlibInjectorAccount) { if (account instanceof AuthlibInjectorAccount) {
AuthlibInjectorServer server = ((AuthlibInjectorAccount) account).getServer(); AuthlibInjectorServer server = ((AuthlibInjectorAccount) account).getServer();
subtitleString.append(", ").append(i18n("account.injector.server")).append(": ").append(server.getName()); subtitleString.append(", ").append(i18n("account.injector.server")).append(": ").append(server.getName());

View File

@ -80,11 +80,11 @@ public class AddAccountPane extends StackPane {
selectDefaultServer(); selectDefaultServer();
cboType.getItems().setAll(Accounts.FACTORY_OFFLINE, Accounts.FACTORY_YGGDRASIL, Accounts.FACTORY_AUTHLIB_INJECTOR); cboType.getItems().setAll(Accounts.FACTORY_OFFLINE, Accounts.FACTORY_YGGDRASIL, Accounts.FACTORY_AUTHLIB_INJECTOR);
cboType.setConverter(stringConverter(Accounts::getAccountTypeName)); cboType.setConverter(stringConverter(Accounts::getLocalizedLoginTypeName));
// try selecting the preferred login type // try selecting the preferred login type
cboType.getSelectionModel().select( cboType.getSelectionModel().select(
cboType.getItems().stream() cboType.getItems().stream()
.filter(type -> Accounts.getAccountTypeName(type).equals(config().getPreferredLoginType())) .filter(type -> Accounts.getLoginType(type).equals(config().getPreferredLoginType()))
.findFirst() .findFirst()
.orElse(Accounts.FACTORY_OFFLINE)); .orElse(Accounts.FACTORY_OFFLINE));
@ -97,7 +97,7 @@ public class AddAccountPane extends StackPane {
ReadOnlyObjectProperty<AccountFactory<?>> loginType = cboType.getSelectionModel().selectedItemProperty(); ReadOnlyObjectProperty<AccountFactory<?>> loginType = cboType.getSelectionModel().selectedItemProperty();
// remember the last used login type // remember the last used login type
loginType.addListener((observable, oldValue, newValue) -> config().setPreferredLoginType(Accounts.getAccountTypeName(newValue))); loginType.addListener((observable, oldValue, newValue) -> config().setPreferredLoginType(Accounts.getLoginType(newValue)));
txtPassword.visibleProperty().bind(loginType.isNotEqualTo(Accounts.FACTORY_OFFLINE)); txtPassword.visibleProperty().bind(loginType.isNotEqualTo(Accounts.FACTORY_OFFLINE));
lblPassword.visibleProperty().bind(txtPassword.visibleProperty()); lblPassword.visibleProperty().bind(txtPassword.visibleProperty());