Replace Listeners usages with ObservableList from Chromium

Signed-off-by: Alexey Krasilnikov <alexey@krasilnikov.me>
This commit is contained in:
Alexey Krasilnikov 2025-01-14 23:05:39 +07:00 committed by Viktor Havaka
parent bc3e637ab6
commit edc19ac605
3 changed files with 34 additions and 130 deletions

View file

@ -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<MwmActivity.LeftAnimationTrackListener> mAnimationTrackListeners = new Listeners<>();
private final ObserverList<MwmActivity.LeftAnimationTrackListener> mAnimationTrackListeners = new ObserverList<>();
private final ObserverList.RewindableIterator<MwmActivity.LeftAnimationTrackListener> 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();

View file

@ -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<NativeSearchListener> mListeners = new Listeners<>();
@NonNull
private final Listeners<NativeMapSearchListener> mMapListeners = new Listeners<>();
@NonNull
private final Listeners<NativeBookmarkSearchListener> mBookmarkListeners = new Listeners<>();
private final ObserverList<NativeSearchListener> mListeners = new ObserverList<>();
private final ObserverList<NativeMapSearchListener> mMapListeners = new ObserverList<>();
private final ObserverList<NativeBookmarkSearchListener> 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();

View file

@ -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.
* <br/>{@link #finishIterate()} must be called after iteration is complete.
*/
public final class Listeners<T> implements Iterable<T>
{
private final Set<T> mListeners = new LinkedHashSet<>();
private final Set<T> mListenersToAdd = new LinkedHashSet<>();
private final Set<T> mListenersToRemove = new LinkedHashSet<>();
private boolean mIterating;
@Override
public @NonNull Iterator<T> 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);
}
}