diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-01 07:34:00 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-01 14:57:25 +0200 |
commit | 32d669a661edec69f88f5d2646071de761b87630 (patch) | |
tree | c671e1583f9962373233e2c84c1409a91ea687dd /app/src/main/java/com/wireguard/android/util | |
parent | 918076a6703268307e7856dcd5f25c4f3dc870ab (diff) |
theme: add dark theme with toggle
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/util')
-rw-r--r-- | app/src/main/java/com/wireguard/android/util/Topic.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/util/Topic.java b/app/src/main/java/com/wireguard/android/util/Topic.java new file mode 100644 index 00000000..eed13c9e --- /dev/null +++ b/app/src/main/java/com/wireguard/android/util/Topic.java @@ -0,0 +1,97 @@ +/* + * Copyright © 2017 John Wu <topjohnwu@gmail.com> + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +package com.wireguard.android.util; + +import java.lang.ref.WeakReference; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +public class Topic { + + private static final int NON_INIT = 0; + private static final int PENDING = 1; + private static final int PUBLISHED = 2; + + private int state = NON_INIT; + private List<WeakReference<Subscriber>> subscribers; + private Object[] results; + + public void subscribe(Subscriber sub) { + if (subscribers == null) { + subscribers = new LinkedList<>(); + } + subscribers.add(new WeakReference<>(sub)); + } + + public void unsubscribe() { + subscribers = null; + } + + public void unsubscribe(Subscriber sub) { + for (Iterator<WeakReference<Subscriber>> i = subscribers.iterator(); i.hasNext();) { + WeakReference<Subscriber> subscriber = i.next(); + if (subscriber.get() == null || subscriber.get() == sub) { + i.remove(); + } + } + } + + public void reset() { + state = NON_INIT; + results = null; + } + + public boolean isPublished() { + return state == PUBLISHED; + } + + public void publish() { + publish(true); + } + + public void publish(boolean record, Object... results) { + if (record) + state = PUBLISHED; + this.results = results; + if (subscribers != null) { + for (WeakReference<Subscriber> subscriber : subscribers) { + if (subscriber.get() != null) + subscriber.get().onTopicPublished(this); + } + } + } + + public Object[] getResults() { + return results; + } + + public boolean isPending() { + return state == PENDING; + } + + public void setPending() { + state = PENDING; + } + + public interface Subscriber { + default void subscribeTopics() { + for (Topic topic : getSubscription()) { + if (topic.isPublished()) { + onTopicPublished(topic); + } + topic.subscribe(this); + } + } + default void unsubscribeTopics() { + for (Topic event : getSubscription()) { + event.unsubscribe(this); + } + } + void onTopicPublished(Topic topic); + Topic[] getSubscription(); + } +} |