From edc19ac605cdfad8fe021a72fcc40edc6e8e19a5 Mon Sep 17 00:00:00 2001 From: Alexey Krasilnikov Date: Tue, 14 Jan 2025 23:05:39 +0700 Subject: [PATCH] Replace Listeners usages with ObservableList from Chromium Signed-off-by: Alexey Krasilnikov --- .../java/app/organicmaps/PanelAnimator.java | 38 ++++---- .../app/organicmaps/search/SearchEngine.java | 32 +++---- .../java/app/organicmaps/util/Listeners.java | 94 ------------------- 3 files changed, 34 insertions(+), 130 deletions(-) delete mode 100644 android/app/src/main/java/app/organicmaps/util/Listeners.java diff --git a/android/app/src/main/java/app/organicmaps/PanelAnimator.java b/android/app/src/main/java/app/organicmaps/PanelAnimator.java index f668f4e983..48fb8a37d9 100644 --- a/android/app/src/main/java/app/organicmaps/PanelAnimator.java +++ b/android/app/src/main/java/app/organicmaps/PanelAnimator.java @@ -11,13 +11,15 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import app.organicmaps.util.Listeners; +import org.chromium.base.ObserverList; + import app.organicmaps.util.UiUtils; class PanelAnimator { private final MwmActivity mActivity; - private final Listeners mAnimationTrackListeners = new Listeners<>(); + private final ObserverList mAnimationTrackListeners = new ObserverList<>(); + private final ObserverList.RewindableIterator mAnimationTrackIterator = mAnimationTrackListeners.rewindableIterator(); private final View mPanel; private final int mWidth; @IntegerRes @@ -33,7 +35,7 @@ class PanelAnimator void registerListener(@NonNull MwmActivity.LeftAnimationTrackListener animationTrackListener) { - mAnimationTrackListeners.register(animationTrackListener); + mAnimationTrackListeners.addObserver(animationTrackListener); } private void track(ValueAnimator animation) @@ -42,9 +44,9 @@ class PanelAnimator mPanel.setTranslationX(offset); mPanel.setAlpha(offset / mWidth + 1.0f); - for (MwmActivity.LeftAnimationTrackListener listener: mAnimationTrackListeners) - listener.onTrackLeftAnimation(offset + mWidth); - mAnimationTrackListeners.finishIterate(); + mAnimationTrackIterator.rewind(); + while (mAnimationTrackIterator.hasNext()) + mAnimationTrackIterator.next().onTrackLeftAnimation(offset + mWidth); } /** @param completionListener will be called before the fragment becomes actually visible */ @@ -70,9 +72,9 @@ class PanelAnimator UiUtils.show(mPanel); - for (MwmActivity.LeftAnimationTrackListener listener: mAnimationTrackListeners) - listener.onTrackStarted(false); - mAnimationTrackListeners.finishIterate(); + mAnimationTrackIterator.rewind(); + while (mAnimationTrackIterator.hasNext()) + mAnimationTrackIterator.next().onTrackStarted(false); ValueAnimator animator = ValueAnimator.ofFloat(-mWidth, 0.0f); animator.addUpdateListener(this::track); @@ -81,9 +83,9 @@ class PanelAnimator @Override public void onAnimationEnd(Animator animation) { - for (MwmActivity.LeftAnimationTrackListener listener: mAnimationTrackListeners) - listener.onTrackStarted(true); - mAnimationTrackListeners.finishIterate(); + mAnimationTrackIterator.rewind(); + while (mAnimationTrackIterator.hasNext()) + mAnimationTrackIterator.next().onTrackStarted(true); } }); @@ -101,9 +103,9 @@ class PanelAnimator return; } - for (MwmActivity.LeftAnimationTrackListener listener: mAnimationTrackListeners) - listener.onTrackStarted(true); - mAnimationTrackListeners.finishIterate(); + mAnimationTrackIterator.rewind(); + while (mAnimationTrackIterator.hasNext()) + mAnimationTrackIterator.next().onTrackStarted(true); ValueAnimator animator = ValueAnimator.ofFloat(0.0f, -mWidth); animator.addUpdateListener(this::track); @@ -114,9 +116,9 @@ class PanelAnimator { UiUtils.hide(mPanel); - for (MwmActivity.LeftAnimationTrackListener listener: mAnimationTrackListeners) - listener.onTrackStarted(false); - mAnimationTrackListeners.finishIterate(); + mAnimationTrackIterator.rewind(); + while (mAnimationTrackIterator.hasNext()) + mAnimationTrackIterator.next().onTrackStarted(false); if (completionListener != null) completionListener.run(); diff --git a/android/app/src/main/java/app/organicmaps/search/SearchEngine.java b/android/app/src/main/java/app/organicmaps/search/SearchEngine.java index ddae8e7a88..dd899e627b 100644 --- a/android/app/src/main/java/app/organicmaps/search/SearchEngine.java +++ b/android/app/src/main/java/app/organicmaps/search/SearchEngine.java @@ -5,9 +5,11 @@ import android.content.Context; import androidx.annotation.MainThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; + +import org.chromium.base.ObserverList; + import app.organicmaps.Framework; import app.organicmaps.util.Language; -import app.organicmaps.util.Listeners; import app.organicmaps.util.concurrency.UiThread; import java.nio.charset.StandardCharsets; @@ -30,7 +32,6 @@ public enum SearchEngine implements NativeSearchListener, { for (NativeSearchListener listener : mListeners) listener.onResultsUpdate(results, timestamp); - mListeners.finishIterate(); }); } @@ -42,7 +43,6 @@ public enum SearchEngine implements NativeSearchListener, { for (NativeSearchListener listener : mListeners) listener.onResultsEnd(timestamp); - mListeners.finishIterate(); }); } @@ -54,7 +54,6 @@ public enum SearchEngine implements NativeSearchListener, { for (NativeMapSearchListener listener : mMapListeners) listener.onMapSearchResults(results, timestamp, isLast); - mMapListeners.finishIterate(); }); } @@ -63,7 +62,6 @@ public enum SearchEngine implements NativeSearchListener, { for (NativeBookmarkSearchListener listener : mBookmarkListeners) listener.onBookmarkSearchResultsUpdate(bookmarkIds, timestamp); - mBookmarkListeners.finishIterate(); } @Override @@ -71,44 +69,42 @@ public enum SearchEngine implements NativeSearchListener, { for (NativeBookmarkSearchListener listener : mBookmarkListeners) listener.onBookmarkSearchResultsEnd(bookmarkIds, timestamp); - mBookmarkListeners.finishIterate(); } - @NonNull - private final Listeners mListeners = new Listeners<>(); - @NonNull - private final Listeners mMapListeners = new Listeners<>(); - @NonNull - private final Listeners mBookmarkListeners = new Listeners<>(); + private final ObserverList mListeners = new ObserverList<>(); + + private final ObserverList mMapListeners = new ObserverList<>(); + + private final ObserverList mBookmarkListeners = new ObserverList<>(); public void addListener(NativeSearchListener listener) { - mListeners.register(listener); + mListeners.addObserver(listener); } public void removeListener(NativeSearchListener listener) { - mListeners.unregister(listener); + mListeners.removeObserver(listener); } public void addMapListener(NativeMapSearchListener listener) { - mMapListeners.register(listener); + mMapListeners.addObserver(listener); } public void removeMapListener(NativeMapSearchListener listener) { - mMapListeners.unregister(listener); + mMapListeners.removeObserver(listener); } public void addBookmarkListener(NativeBookmarkSearchListener listener) { - mBookmarkListeners.register(listener); + mBookmarkListeners.addObserver(listener); } public void removeBookmarkListener(NativeBookmarkSearchListener listener) { - mBookmarkListeners.unregister(listener); + mBookmarkListeners.removeObserver(listener); } private native void nativeInit(); diff --git a/android/app/src/main/java/app/organicmaps/util/Listeners.java b/android/app/src/main/java/app/organicmaps/util/Listeners.java deleted file mode 100644 index aefdc1a266..0000000000 --- a/android/app/src/main/java/app/organicmaps/util/Listeners.java +++ /dev/null @@ -1,94 +0,0 @@ -package app.organicmaps.util; - -import androidx.annotation.NonNull; - -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.Set; - -/** - * `Registrator` pattern implementation which allows to maintain the list of listeners, - * offers safe adding/removing of listeners during iteration. - *
{@link #finishIterate()} must be called after iteration is complete. - */ -public final class Listeners implements Iterable -{ - private final Set mListeners = new LinkedHashSet<>(); - private final Set mListenersToAdd = new LinkedHashSet<>(); - private final Set mListenersToRemove = new LinkedHashSet<>(); - - private boolean mIterating; - - @Override - public @NonNull Iterator iterator() - { - if (mIterating) - throw new RuntimeException("finishIterate() must be called before new iteration"); - - mIterating = true; - return mListeners.iterator(); - } - - /** - * Completes listeners iteration. Must be called after iteration is done. - */ - public void finishIterate() - { - if (!mListenersToRemove.isEmpty()) - mListeners.removeAll(mListenersToRemove); - - if (!mListenersToAdd.isEmpty()) - mListeners.addAll(mListenersToAdd); - - mListenersToAdd.clear(); - mListenersToRemove.clear(); - mIterating = false; - } - - /** - * Safely registers new listener. If registered during iteration, new listener will NOT be called before current iteration is complete. - */ - public void register(T listener) - { - if (mIterating) - { - mListenersToRemove.remove(listener); - if (!mListeners.contains(listener)) - mListenersToAdd.add(listener); - } - else - mListeners.add(listener); - } - - /** - * Safely unregisters listener. If unregistered during iteration, old listener WILL be called in the current iteration. - */ - public void unregister(T listener) - { - if (mIterating) - { - mListenersToAdd.remove(listener); - if (mListeners.contains(listener)) - mListenersToRemove.add(listener); - } - else - mListeners.remove(listener); - } - - public int getSize() - { - int res = mListeners.size(); - if (mIterating) - { - res += mListenersToAdd.size(); - res -= mListenersToRemove.size(); - } - - return res; - } - - public boolean isEmpty() - { - return (getSize() <= 0); - } -}