Skip to content

Commit

Permalink
Fix Android's toolbar config update (#244)
Browse files Browse the repository at this point in the history
Before this change, updates made to toolbar's subviwews weren't properly reflected. We'd only add new views to toolbar w/o removing stale ones.
  • Loading branch information
kmagiera authored Dec 2, 2019
1 parent 84d684b commit 744b37f
Showing 1 changed file with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

import com.facebook.react.views.text.ReactFontManager;

import java.util.ArrayList;

public class ScreenStackHeaderConfig extends ViewGroup {

private final ScreenStackHeaderSubview mConfigSubviews[] = new ScreenStackHeaderSubview[3];
private int mSubviewsCount = 0;
private final ArrayList<ScreenStackHeaderSubview> mConfigSubviews = new ArrayList<>(3);
private String mTitle;
private int mTitleColor;
private String mTitleFontFamily;
Expand Down Expand Up @@ -174,8 +175,13 @@ public void onUpdate() {
}

// subviews
for (int i = 0; i < mSubviewsCount; i++) {
ScreenStackHeaderSubview view = mConfigSubviews[i];
for (int i = mToolbar.getChildCount() - 1; i >= 0; i--) {
if (mToolbar.getChildAt(i) instanceof ScreenStackHeaderSubview) {
mToolbar.removeViewAt(i);
}
}
for (int i = 0, size = mConfigSubviews.size(); i < size; i++) {
ScreenStackHeaderSubview view = mConfigSubviews.get(i);
ScreenStackHeaderSubview.Type type = view.getType();

Toolbar.LayoutParams params =
Expand All @@ -201,9 +207,7 @@ public void onUpdate() {
}

view.setLayoutParams(params);
if (view.getParent() == null) {
mToolbar.addView(view);
}
mToolbar.addView(view);
}
}

Expand All @@ -214,26 +218,20 @@ private void maybeUpdate() {
}

public ScreenStackHeaderSubview getConfigSubview(int index) {
return mConfigSubviews[index];
return mConfigSubviews.get(index);
}

public int getConfigSubviewsCount() {
return mSubviewsCount;
return mConfigSubviews.size();
}

public void removeConfigSubview(int index) {
if (mConfigSubviews[index] != null) {
mSubviewsCount--;
}
mConfigSubviews[index] = null;
mConfigSubviews.remove(index);
maybeUpdate();
}

public void addConfigSubview(ScreenStackHeaderSubview child, int index) {
if (mConfigSubviews[index] == null) {
mSubviewsCount++;
}
mConfigSubviews[index] = child;
mConfigSubviews.add(index, child);
maybeUpdate();
}

Expand Down

0 comments on commit 744b37f

Please sign in to comment.