Skip to content

Commit

Permalink
fix(android): tabgroup crashes when using AppCompat theme
Browse files Browse the repository at this point in the history
Fixes TIMOB-28189
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Oct 21, 2020
1 parent c3783d3 commit 6403da2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
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

0 comments on commit 6403da2

Please sign in to comment.