Move profile management to Profiles

This commit is contained in:
huangyuhui
2018-08-31 22:28:06 +08:00
parent b3d84c4f99
commit efa280b839
9 changed files with 140 additions and 114 deletions

View File

@@ -17,6 +17,21 @@
*/
package org.jackhuang.hmcl.setting;
import javafx.beans.value.ObservableValue;
import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.event.EventBus;
import org.jackhuang.hmcl.event.ProfileChangedEvent;
import org.jackhuang.hmcl.event.ProfileLoadingEvent;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.StringUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public final class Profiles {
@@ -37,4 +52,105 @@ public final class Profiles {
return profile.getName();
}
}
/****************************************
* PROFILES *
****************************************/
public static Profile getSelectedProfile() {
checkProfileMap();
if (!hasProfile(config().getSelectedProfile())) {
getProfileMap().keySet().stream().findFirst().ifPresent(selectedProfile -> {
config().setSelectedProfile(selectedProfile);
});
Schedulers.computation().schedule(Profiles::onProfileChanged);
}
return getProfile(config().getSelectedProfile());
}
public static void setSelectedProfile(Profile selectedProfile) {
if (hasProfile(selectedProfile.getName()) && !Objects.equals(selectedProfile.getName(), config().getSelectedProfile())) {
config().setSelectedProfile(selectedProfile.getName());
Schedulers.computation().schedule(Profiles::onProfileChanged);
}
}
public static Profile getProfile(String name) {
checkProfileMap();
Optional<Profile> p = name == null ? getProfileMap().values().stream().findFirst() : Optional.ofNullable(getProfileMap().get(name));
return p.orElse(null);
}
public static boolean hasProfile(String name) {
return getProfileMap().containsKey(name);
}
public static Map<String, Profile> getProfileMap() {
return config().getConfigurations();
}
public static Collection<Profile> getProfiles() {
return getProfileMap().values().stream().filter(t -> StringUtils.isNotBlank(t.getName())).collect(Collectors.toList());
}
public static void putProfile(Profile ver) {
if (StringUtils.isBlank(ver.getName()))
throw new IllegalArgumentException("Profile's name is empty");
getProfileMap().put(ver.getName(), ver);
Schedulers.computation().schedule(Profiles::onProfileLoading);
ver.nameProperty().setChangedListener(Profiles::profileNameChanged);
}
public static void deleteProfile(Profile profile) {
deleteProfile(profile.getName());
}
public static void deleteProfile(String profileName) {
getProfileMap().remove(profileName);
checkProfileMap();
Schedulers.computation().schedule(Profiles::onProfileLoading);
}
private static void checkProfileMap() {
if (getProfileMap().isEmpty()) {
Profile current = new Profile(Profiles.DEFAULT_PROFILE);
current.setUseRelativePath(true);
getProfileMap().put(Profiles.DEFAULT_PROFILE, current);
Profile home = new Profile(Profiles.HOME_PROFILE, Launcher.MINECRAFT_DIRECTORY);
getProfileMap().put(Profiles.HOME_PROFILE, home);
}
}
private static void onProfileChanged() {
EventBus.EVENT_BUS.fireEvent(new ProfileChangedEvent(new Object(), getSelectedProfile()));
getSelectedProfile().getRepository().refreshVersionsAsync().start();
}
private static void profileNameChanged(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
getProfileMap().put(newValue, getProfileMap().remove(oldValue));
}
/**
* Start profiles loading process.
* Invoked by loading GUI phase.
*/
public static void onProfileLoading() {
EventBus.EVENT_BUS.fireEvent(new ProfileLoadingEvent(new Object(), getProfiles()));
onProfileChanged();
}
static void init() {
checkProfileMap();
for (Map.Entry<String, Profile> profileEntry : getProfileMap().entrySet()) {
profileEntry.getValue().setName(profileEntry.getKey());
profileEntry.getValue().nameProperty().setChangedListener(Profiles::profileNameChanged);
profileEntry.getValue().addPropertyChangedListener(e -> ConfigHolder.markConfigDirty());
}
}
}

View File

@@ -52,14 +52,7 @@ public class Settings {
private Settings() {
ProxyManager.init();
Accounts.init();
checkProfileMap();
for (Map.Entry<String, Profile> profileEntry : getProfileMap().entrySet()) {
profileEntry.getValue().setName(profileEntry.getKey());
profileEntry.getValue().nameProperty().setChangedListener(this::profileNameChanged);
profileEntry.getValue().addPropertyChangedListener(e -> ConfigHolder.markConfigDirty());
}
Profiles.init();
}
public Font getFont() {
@@ -114,95 +107,4 @@ public class Settings {
throw new IllegalArgumentException("Unknown download provider: " + downloadProvider);
config().setDownloadType(index);
}
/****************************************
* PROFILES *
****************************************/
public Profile getSelectedProfile() {
checkProfileMap();
if (!hasProfile(config().getSelectedProfile())) {
getProfileMap().keySet().stream().findFirst().ifPresent(selectedProfile -> {
config().setSelectedProfile(selectedProfile);
});
Schedulers.computation().schedule(this::onProfileChanged);
}
return getProfile(config().getSelectedProfile());
}
public void setSelectedProfile(Profile selectedProfile) {
if (hasProfile(selectedProfile.getName()) && !Objects.equals(selectedProfile.getName(), config().getSelectedProfile())) {
config().setSelectedProfile(selectedProfile.getName());
Schedulers.computation().schedule(this::onProfileChanged);
}
}
public Profile getProfile(String name) {
checkProfileMap();
Optional<Profile> p = name == null ? getProfileMap().values().stream().findFirst() : Optional.ofNullable(getProfileMap().get(name));
return p.orElse(null);
}
public boolean hasProfile(String name) {
return getProfileMap().containsKey(name);
}
public Map<String, Profile> getProfileMap() {
return config().getConfigurations();
}
public Collection<Profile> getProfiles() {
return getProfileMap().values().stream().filter(t -> StringUtils.isNotBlank(t.getName())).collect(Collectors.toList());
}
public void putProfile(Profile ver) {
if (StringUtils.isBlank(ver.getName()))
throw new IllegalArgumentException("Profile's name is empty");
getProfileMap().put(ver.getName(), ver);
Schedulers.computation().schedule(this::onProfileLoading);
ver.nameProperty().setChangedListener(this::profileNameChanged);
}
public void deleteProfile(Profile profile) {
deleteProfile(profile.getName());
}
public void deleteProfile(String profileName) {
getProfileMap().remove(profileName);
checkProfileMap();
Schedulers.computation().schedule(this::onProfileLoading);
}
private void checkProfileMap() {
if (getProfileMap().isEmpty()) {
Profile current = new Profile(Profiles.DEFAULT_PROFILE);
current.setUseRelativePath(true);
getProfileMap().put(Profiles.DEFAULT_PROFILE, current);
Profile home = new Profile(Profiles.HOME_PROFILE, Launcher.MINECRAFT_DIRECTORY);
getProfileMap().put(Profiles.HOME_PROFILE, home);
}
}
private void onProfileChanged() {
EventBus.EVENT_BUS.fireEvent(new ProfileChangedEvent(this, getSelectedProfile()));
getSelectedProfile().getRepository().refreshVersionsAsync().start();
}
private void profileNameChanged(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
getProfileMap().put(newValue, getProfileMap().remove(oldValue));
}
/**
* Start profiles loading process.
* Invoked by loading GUI phase.
*/
public void onProfileLoading() {
EventBus.EVENT_BUS.fireEvent(new ProfileLoadingEvent(this, getProfiles()));
onProfileChanged();
}
}

View File

@@ -24,6 +24,7 @@ import javafx.scene.layout.Region;
import javafx.stage.Stage;
import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskExecutor;
@@ -121,7 +122,7 @@ public final class Controllers {
decorator.showPage(null);
leftPaneController = new LeftPaneController(decorator.getLeftPane());
Settings.instance().onProfileLoading();
Profiles.onProfileLoading();
Task.of(JavaVersion::initialize).start();
decorator.setCustomMaximize(false);

View File

@@ -126,11 +126,11 @@ public final class LeftPaneController {
private void onProfilesLoading() {
LinkedList<RipplerContainer> list = new LinkedList<>();
for (Profile profile : Settings.instance().getProfiles()) {
for (Profile profile : Profiles.getProfiles()) {
AdvancedListItem item = new AdvancedListItem(Profiles.getProfileDisplayName(profile));
RipplerContainer ripplerContainer = new RipplerContainer(item);
item.setOnSettingsButtonClicked(e -> Controllers.getDecorator().showPage(new ProfilePage(profile)));
ripplerContainer.setOnMouseClicked(e -> Settings.instance().setSelectedProfile(profile));
ripplerContainer.setOnMouseClicked(e -> Profiles.setSelectedProfile(profile));
ripplerContainer.getProperties().put("profile", profile.getName());
ripplerContainer.maxWidthProperty().bind(leftPane.widthProperty());
list.add(ripplerContainer);

View File

@@ -35,6 +35,7 @@ import org.jackhuang.hmcl.event.RefreshedVersionsEvent;
import org.jackhuang.hmcl.event.RefreshingVersionsEvent;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
@@ -92,7 +93,7 @@ public final class MainPage extends StackPane implements DecoratorPage {
btnAdd.setOnMouseClicked(e -> Controllers.getDecorator().startWizard(new DownloadWizardProvider(0), i18n("install")));
FXUtils.installTooltip(btnAdd, i18n("install"));
btnRefresh.setOnMouseClicked(e -> Settings.instance().getSelectedProfile().getRepository().refreshVersionsAsync().start());
btnRefresh.setOnMouseClicked(e -> Profiles.getSelectedProfile().getRepository().refreshVersionsAsync().start());
FXUtils.installTooltip(btnRefresh, i18n("button.refresh"));
}

View File

@@ -80,7 +80,7 @@ public final class ProfilePage extends StackPane implements DecoratorPage {
@FXML
private void onDelete() {
if (profile != null) {
Settings.instance().deleteProfile(profile);
Profiles.deleteProfile(profile);
Controllers.navigate(null);
}
}
@@ -99,10 +99,10 @@ public final class ProfilePage extends StackPane implements DecoratorPage {
}
Profile newProfile = new Profile(txtProfileName.getText(), new File(getLocation()));
newProfile.setUseRelativePath(toggleUseRelativePath.isSelected());
Settings.instance().putProfile(newProfile);
Profiles.putProfile(newProfile);
}
Settings.instance().onProfileLoading();
Profiles.onProfileLoading();
Controllers.navigate(null);
}

View File

@@ -25,6 +25,7 @@ import org.jackhuang.hmcl.download.RemoteVersion;
import org.jackhuang.hmcl.game.ModpackHelper;
import org.jackhuang.hmcl.mod.Modpack;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.ui.wizard.WizardController;
@@ -45,7 +46,7 @@ public final class DownloadWizardProvider implements WizardProvider {
@Override
public void start(Map<String, Object> settings) {
profile = Settings.instance().getSelectedProfile();
profile = Profiles.getSelectedProfile();
settings.put(PROFILE, profile);
}

View File

@@ -28,6 +28,7 @@ import org.jackhuang.hmcl.event.EventBus;
import org.jackhuang.hmcl.event.ProfileChangedEvent;
import org.jackhuang.hmcl.event.RefreshedVersionsEvent;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.ui.AdvancedListItem2;
import org.jackhuang.hmcl.ui.WeakListenerHelper;
@@ -50,7 +51,7 @@ public class GameAdvancedListItem extends AdvancedListItem2 {
loadVersion();
});
}));
loadProfile(Settings.instance().getSelectedProfile());
loadProfile(Profiles.getSelectedProfile());
}
private void loadProfile(Profile newProfile) {
@@ -66,11 +67,14 @@ public class GameAdvancedListItem extends AdvancedListItem2 {
if (profile == null || !profile.getRepository().isLoaded()) return;
String version = profile.getSelectedVersion();
File iconFile = profile.getRepository().getVersionIcon(version);
if (iconFile.exists())
imageProperty().set(new Image("file:" + iconFile.getAbsolutePath()));
else
imageProperty().set(new Image("/assets/img/grass.png"));
JFXUtilities.runInFX(() -> {
if (iconFile.exists())
imageProperty().set(new Image("file:" + iconFile.getAbsolutePath()));
else
imageProperty().set(new Image("/assets/img/grass.png"));
titleProperty().set(version);
titleProperty().set(version);
});
}
}

View File

@@ -29,6 +29,7 @@ import org.jackhuang.hmcl.event.RefreshedVersionsEvent;
import org.jackhuang.hmcl.event.RefreshingVersionsEvent;
import org.jackhuang.hmcl.game.HMCLGameRepository;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.download.DownloadWizardProvider;
@@ -62,7 +63,7 @@ public class GameList extends Control implements DecoratorPage {
this.profile = event.getProfile();
});
profile = Settings.instance().getSelectedProfile();
profile = Profiles.getSelectedProfile();
if (profile.getRepository().isLoaded())
loadVersions(profile.getRepository());
else