Let Account implement Observable
This commit is contained in:
@@ -17,8 +17,13 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.auth;
|
||||
|
||||
import org.jackhuang.hmcl.util.ObservableHelper;
|
||||
import org.jackhuang.hmcl.util.ToStringBuilder;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.Observable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
@@ -27,7 +32,7 @@ import java.util.UUID;
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public abstract class Account {
|
||||
public abstract class Account implements Observable {
|
||||
|
||||
/**
|
||||
* @return the name of the account who owns the character
|
||||
@@ -66,6 +71,26 @@ public abstract class Account {
|
||||
|
||||
public abstract void clearCache();
|
||||
|
||||
private ObservableHelper helper = new ObservableHelper(this);
|
||||
|
||||
@Override
|
||||
public void addListener(InvalidationListener listener) {
|
||||
helper.addListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(InvalidationListener listener) {
|
||||
helper.removeListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the account has changed.
|
||||
* This method can be called from any thread.
|
||||
*/
|
||||
protected void invalidate() {
|
||||
Platform.runLater(helper::invalidate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this)
|
||||
|
||||
@@ -114,6 +114,7 @@ public class YggdrasilAccount extends Account {
|
||||
}
|
||||
|
||||
this.characterUUID = this.session.getSelectedProfile().getId();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hmcl.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.Observable;
|
||||
|
||||
/**
|
||||
* Helper class for implementing {@link Observable}.
|
||||
*
|
||||
* @author yushijinhun
|
||||
*/
|
||||
public class ObservableHelper implements Observable, InvalidationListener {
|
||||
|
||||
private List<InvalidationListener> listeners = new CopyOnWriteArrayList<>();
|
||||
private Observable source;
|
||||
|
||||
public ObservableHelper(Observable source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method can be called from any thread.
|
||||
*/
|
||||
@Override
|
||||
public void addListener(InvalidationListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method can be called from any thread.
|
||||
*/
|
||||
@Override
|
||||
public void removeListener(InvalidationListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
public void invalidate() {
|
||||
listeners.forEach(it -> it.invalidated(source));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidated(Observable observable) {
|
||||
this.invalidate();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user