Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): TabGroup without badge crashes when using AppCompat theme as of 9.3.0 #12175

Merged
merged 3 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,16 @@ public void updateBadge(int index)
return;
}

Object badgeValue = tabProxy.getProperty(TiC.PROPERTY_BADGE);
if ((badgeValue == null) && !TiUIHelper.isUsingMaterialTheme(this.mBottomNavigationView.getContext())) {
return;
}

int menuItemId = this.mBottomNavigationView.getMenu().getItem(index).getItemId();
BadgeDrawable badgeDrawable = this.mBottomNavigationView.getOrCreateBadge(menuItemId);
if (tabProxy.getProperty(TiC.PROPERTY_BADGE) != null) {
if (badgeValue != null) {
badgeDrawable.setVisible(true);
badgeDrawable.setNumber(TiConvert.toInt(tabProxy.getProperty(TiC.PROPERTY_BADGE), 0));
badgeDrawable.setNumber(TiConvert.toInt(badgeValue, 0));
} else {
badgeDrawable.setVisible(false);
}
Expand All @@ -314,8 +319,8 @@ public void updateBadgeColor(int index)
return;
}

int menuItemId = this.mBottomNavigationView.getMenu().getItem(index).getItemId();
if (tabProxy.getProperty(TiC.PROPERTY_BADGE_COLOR) != null) {
int menuItemId = this.mBottomNavigationView.getMenu().getItem(index).getItemId();
BadgeDrawable badgeDrawable = this.mBottomNavigationView.getOrCreateBadge(menuItemId);
badgeDrawable.setBackgroundColor(
TiConvert.toColor((String) tabProxy.getProperty(TiC.PROPERTY_BADGE_COLOR)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,15 @@ public void updateBadge(int index)
return;
}

Object badgeValue = tabProxy.getProperty(TiC.PROPERTY_BADGE);
if ((badgeValue == null) && !TiUIHelper.isUsingMaterialTheme(this.mTabLayout.getContext())) {
return;
}

BadgeDrawable badgeDrawable = this.mTabLayout.getTabAt(index).getOrCreateBadge();
if (tabProxy.getProperty(TiC.PROPERTY_BADGE) != null) {
if (badgeValue != null) {
badgeDrawable.setVisible(true);
badgeDrawable.setNumber(TiConvert.toInt(tabProxy.getProperty(TiC.PROPERTY_BADGE), 0));
badgeDrawable.setNumber(TiConvert.toInt(badgeValue, 0));
} else {
badgeDrawable.setVisible(false);
}
Expand All @@ -300,8 +305,8 @@ public void updateBadgeColor(int index)
return;
}

BadgeDrawable badgeDrawable = this.mTabLayout.getTabAt(index).getOrCreateBadge();
if (tabProxy.getProperty(TiC.PROPERTY_BADGE_COLOR) != null) {
BadgeDrawable badgeDrawable = this.mTabLayout.getTabAt(index).getOrCreateBadge();
badgeDrawable.setVisible(true);
badgeDrawable.setBackgroundColor(
TiConvert.toColor((String) tabProxy.getProperty(TiC.PROPERTY_BADGE_COLOR)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import android.content.DialogInterface.OnClickListener;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
Expand Down Expand Up @@ -1288,6 +1289,25 @@ public static String getBackgroundColorForState(TiBackgroundDrawable backgroundD
return null;
}

/**
* Determines if the given context has been assigned a "Theme.MaterialComponents" derived theme.
* @param context Reference to the context such as an Activity or Application object to inspect. Can be null.
* @return Returns true if assigned a material theme. Returns false if not or argument is null.
*/
public static boolean isUsingMaterialTheme(Context context)
{
if (context == null) {
return false;
}

TypedArray typedArray = context.obtainStyledAttributes(new int[] {
com.google.android.material.R.attr.colorPrimaryVariant
});
boolean isMaterial = typedArray.hasValue(0);
typedArray.recycle();
return isMaterial;
}

public static String hexStringFrom(int colorInt)
{
return String.format("#%08X", 0xFFFFFFFF & colorInt);
Expand Down