Skip to content

Commit

Permalink
Apply TopBar buttons only if they are different than current buttons (#…
Browse files Browse the repository at this point in the history
…5314)

Like all other options, buttons are applied when a screen becomes visible. This means that in a BottomTabs layout,
options are applied when a tab is selected. Since tab change happens involves only changing view visibility, and button creation take a bit long,
the buttons appear to flicker some times.
  • Loading branch information
guyca authored Jul 24, 2019
1 parent 9804c62 commit f15e9b3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ public void mergeWithDefault(Component defaultOptions) {
public boolean hasValue() {
return name.hasValue();
}

public boolean equals(Component other) {
return name.equals(other.name) &&
componentId.equals(other.componentId) &&
alignment.equals(other.alignment) &&
waitForRender.equals(other.waitForRender);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Objects;

public class Button {
public String instanceId = "btn" + CompatUtils.generateViewId();
Expand All @@ -34,6 +35,22 @@ public class Button {
public Text testId = new NullText();
public Component component = new Component();

public boolean equals(Button other) {
return Objects.equals(id, other.id) &&
text.equals(other.text) &&
enabled.equals(other.enabled) &&
disableIconTint.equals(other.disableIconTint) &&
showAsAction.equals(other.showAsAction) &&
color.equals(other.color) &&
disabledColor.equals(other.disabledColor) &&
fontSize.equals(other.fontSize) &&
fontWeight.equals(other.fontWeight) &&
Objects.equals(fontFamily, other.fontFamily) &&
icon.equals(other.icon) &&
testId.equals(other.testId) &&
component.equals(other.component);
}

private static Button parseJson(JSONObject json, TypefaceLoader typefaceManager) {
Button button = new Button();
button.id = json.optString("id");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.reactnativenavigation.parse.params;

import static com.reactnativenavigation.utils.ObjectUtils.equalsNotNull;

public abstract class Param<T> {
protected T value;

Expand All @@ -21,4 +23,8 @@ public T get(T defaultValue) {
public boolean hasValue() {
return value != null;
}

public boolean equals(Param other) {
return value == other.value || equalsNotNull(value, other.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.Pair;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class CollectionUtils {
public interface Apply<T> {
Expand Down Expand Up @@ -136,4 +139,23 @@ public static <T> Boolean reduce(@Nullable Collection<T> items, boolean initialV
return t == null ? Collections.EMPTY_LIST : t.values();
}

public static <T> boolean equals(@Nullable Collection<T> a, @Nullable Collection<T> b) {
if (size(a) != size(b)) return false;
return reduce(zip(a, b), true, (p, currentValue) -> currentValue && Objects.equals(p.first, p.second));
}

public static int size(@Nullable Collection items) {
return items == null ? 0 : items.size();
}

public static <T> Collection<Pair<T, T>> zip(@Nullable Collection<T> a, @Nullable Collection<T> b) {
if (a == null || b == null) return new ArrayList<>();
Iterator iter1 = a.iterator();
Iterator iter2 = b.iterator();
ArrayList<Pair<T,T>> result = new ArrayList<>();
while (iter1.hasNext() && iter2.hasNext()) {
result.add(new Pair(iter1.next(), iter2.next()));
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class ViewUtils {
@Nullable
public static <T> T findChildByClass(ViewGroup root, Class clazz) {
public static <T extends View> T findChildByClass(ViewGroup root, Class<T> clazz) {
for (int i = 0; i < root.getChildCount(); i++) {
View view = root.getChildAt(i);
if (clazz.isAssignableFrom(view.getClass())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.reactnativenavigation.BuildConfig;
import com.reactnativenavigation.R;
import com.reactnativenavigation.anim.TopBarAnimator;
import com.reactnativenavigation.anim.TopBarCollapseBehavior;
Expand All @@ -29,13 +28,15 @@
import com.reactnativenavigation.parse.AnimationOptions;
import com.reactnativenavigation.parse.params.Colour;
import com.reactnativenavigation.parse.params.Number;
import com.reactnativenavigation.utils.CollectionUtils;
import com.reactnativenavigation.utils.CompatUtils;
import com.reactnativenavigation.utils.UiUtils;
import com.reactnativenavigation.viewcontrollers.TitleBarButtonController;
import com.reactnativenavigation.views.StackLayout;
import com.reactnativenavigation.views.titlebar.TitleBar;
import com.reactnativenavigation.views.toptabs.TopTabs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand All @@ -52,6 +53,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
private View border;
private View component;
private float elevation = -1;
private List<TitleBarButtonController> rightButtons = new ArrayList<>();

public TopBar(final Context context, StackLayout parentView) {
super(context);
Expand Down Expand Up @@ -218,6 +220,8 @@ public void clearLeftButtons() {
}

public void setRightButtons(List<TitleBarButtonController> rightButtons) {
if (CollectionUtils.equals(this.rightButtons, rightButtons)) return;
this.rightButtons = rightButtons;
titleBar.setRightButtons(rightButtons);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class TopBarTest extends BaseTest {
private TopBarAnimator animator;
private Activity activity;

@SuppressWarnings("Convert2Lambda")
@Override
public void beforeEach() {
activity = newActivity();
Expand Down

0 comments on commit f15e9b3

Please sign in to comment.