mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-16 16:16:04 -04:00
ExtraCore can now deal all kinds of values
The extraListener is now an interface
This commit is contained in:
parent
2be5ae2584
commit
edd8859f2d
@ -118,15 +118,12 @@ public class PojavLauncherActivity extends BaseLauncherActivity
|
||||
initTabs(0);
|
||||
|
||||
//Setup listener to the backPreference system
|
||||
backPreferenceListener = new ExtraListener() {
|
||||
@Override
|
||||
public boolean onValueSet(String key, String value) {
|
||||
backPreferenceListener = (key, value) -> {
|
||||
if(value.equals("true")){
|
||||
onBackPressed();
|
||||
ExtraCore.setValue(key, "false");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
ExtraCore.addExtraListener("back_preference", backPreferenceListener);
|
||||
|
||||
|
@ -8,6 +8,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
/**
|
||||
* Class providing callback across all of a program
|
||||
* to allow easy thread safe implementations of UI update without context leak
|
||||
* It is also perfectly engineered to make it unpleasant to use.
|
||||
*
|
||||
* This class uses a singleton pattern to simplify access to it
|
||||
*/
|
||||
@ -16,7 +17,7 @@ public final class ExtraCore {
|
||||
private ExtraCore(){}
|
||||
|
||||
// Store the key-value pair
|
||||
private final Map<String, String> valueMap = new ConcurrentHashMap<>();
|
||||
private final Map<String, Object> valueMap = new ConcurrentHashMap<>();
|
||||
|
||||
// Store what each ExtraListener listen to
|
||||
private final Map<String, ConcurrentLinkedQueue<WeakReference<ExtraListener>>> listenerMap = new ConcurrentHashMap<>();
|
||||
@ -36,20 +37,25 @@ public final class ExtraCore {
|
||||
* @param key The key
|
||||
* @param value The value
|
||||
*/
|
||||
public static void setValue(String key, String value){
|
||||
public static void setValue(String key, Object value){
|
||||
getInstance().valueMap.put(key, value);
|
||||
ConcurrentLinkedQueue<WeakReference<ExtraListener>> extraListenerList = getInstance().listenerMap.get(key);
|
||||
if(extraListenerList == null) return; //No listeners
|
||||
for(WeakReference<ExtraListener> listener : extraListenerList){
|
||||
if(listener.get() == null){
|
||||
extraListenerList.remove(listener);
|
||||
continue;
|
||||
}
|
||||
listener.get().notifyDataChanged(key, value);
|
||||
|
||||
//Notify the listener about a state change and remove it if asked for
|
||||
if(listener.get().onValueSet(key, value)){
|
||||
ExtraCore.removeExtraListenerFromValue(key, listener.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @return The value behind the key */
|
||||
public static String getValue(String key){
|
||||
public static Object getValue(String key){
|
||||
return getInstance().valueMap.get(key);
|
||||
}
|
||||
|
||||
|
@ -1,20 +1,12 @@
|
||||
package net.kdt.pojavlaunch.extra;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Listener class for the ExtraCore
|
||||
* An ExtraListener can listen to a virtually unlimited amount of values
|
||||
*/
|
||||
public abstract class ExtraListener {
|
||||
|
||||
/**
|
||||
* Called by the ExtraCore after a value is set.
|
||||
* Technically, it can be triggered from outside but is seems pointless
|
||||
*/
|
||||
public final void notifyDataChanged(String key, String value){
|
||||
if(onValueSet(key, value)){
|
||||
ExtraCore.removeExtraListenerFromValue(key, this);
|
||||
}
|
||||
}
|
||||
public interface ExtraListener<T> {
|
||||
|
||||
/**
|
||||
* Called upon a new value being set
|
||||
@ -22,5 +14,6 @@ public abstract class ExtraListener {
|
||||
* @param value The new value as a string
|
||||
* @return Whether you consume the Listener (stop listening)
|
||||
*/
|
||||
public abstract boolean onValueSet(String key, String value);
|
||||
boolean onValueSet(String key, @Nullable T value);
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user