Skip to content

Commit

Permalink
Support selectedIcon options on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
guyca committed Oct 29, 2019
1 parent 291f161 commit 45e8389
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.android.material:material:1.1.0-alpha08'

implementation 'com.github.wix-playground:ahbottomnavigation:3.0.2'
implementation 'com.github.wix-playground:ahbottomnavigation:3.0.5'
implementation 'com.github.wix-playground:reflow-animator:1.0.4'
implementation 'com.github.clans:fab:1.6.4'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static BottomTabOptions parse(TypefaceLoader typefaceManager, JSONObject
options.textColor = ColorParser.parse(json, "textColor");
options.selectedTextColor = ColorParser.parse(json, "selectedTextColor");
if (json.has("icon")) options.icon = TextParser.parse(json.optJSONObject("icon"), "uri");
if (json.has("selectedIcon")) options.selectedIcon = TextParser.parse(json.optJSONObject("selectedIcon"), "uri");
options.iconColor = ColorParser.parse(json, "iconColor");
options.selectedIconColor = ColorParser.parse(json, "selectedIconColor");
options.badge = TextParser.parse(json, "badge");
Expand All @@ -47,6 +48,7 @@ public static BottomTabOptions parse(TypefaceLoader typefaceManager, JSONObject
public Colour textColor = new NullColor();
public Colour selectedTextColor = new NullColor();
public Text icon = new NullText();
public Text selectedIcon = new NullText();
public Colour iconColor = new NullColor();
public Colour selectedIconColor = new NullColor();
public Text testId = new NullText();
Expand All @@ -64,6 +66,7 @@ void mergeWith(final BottomTabOptions other) {
if (other.textColor.hasValue()) textColor = other.textColor;
if (other.selectedTextColor.hasValue()) selectedTextColor = other.selectedTextColor;
if (other.icon.hasValue()) icon = other.icon;
if (other.selectedIcon.hasValue()) selectedIcon = other.selectedIcon;
if (other.iconColor.hasValue()) iconColor = other.iconColor;
if (other.selectedIconColor.hasValue()) selectedIconColor = other.selectedIconColor;
if (other.badge.hasValue()) badge = other.badge;
Expand All @@ -81,6 +84,7 @@ void mergeWithDefault(final BottomTabOptions defaultOptions) {
if (!textColor.hasValue()) textColor = defaultOptions.textColor;
if (!selectedTextColor.hasValue()) selectedTextColor = defaultOptions.selectedTextColor;
if (!icon.hasValue()) icon = defaultOptions.icon;
if (!selectedIcon.hasValue()) selectedIcon = defaultOptions.selectedIcon;
if (!iconColor.hasValue()) iconColor = defaultOptions.iconColor;
if (!selectedIconColor.hasValue()) selectedIconColor = defaultOptions.selectedIconColor;
if (!badge.hasValue()) badge = defaultOptions.badge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public void onComplete(@NonNull Drawable drawable) {
bottomTabs.setIcon(index, drawable);
}
});
if (tab.selectedIcon.hasValue()) imageLoader.loadIcon(context, tab.selectedIcon.get(), new ImageLoadingListenerAdapter() {
@Override
public void onComplete(@NonNull Drawable drawable) {
bottomTabs.setSelectedIcon(index, drawable);
}
});
if (tab.testId.hasValue()) bottomTabs.setTag(index, tab.testId.get());
if (shouldApplyDot(tab)) mergeDotIndicator(index, tab.dotIndicator); else mergeBadge(index, tab);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.StrictMode;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
import com.reactnativenavigation.NavigationApplication;
Expand All @@ -20,6 +18,9 @@
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ImageLoader {

public interface ImagesLoadingListener {
Expand All @@ -33,7 +34,8 @@ public interface ImagesLoadingListener {
private static final String FILE_SCHEME = "file";

@Nullable
public Drawable loadIcon(Context context, String uri) {
public Drawable loadIcon(Context context, @Nullable String uri) {
if (uri == null) return null;
try {
return getDrawable(context, uri);
} catch (IOException e) {
Expand Down Expand Up @@ -64,7 +66,7 @@ public void loadIcons(final Context context, List<String> uris, ImagesLoadingLis
}

@NonNull
private Drawable getDrawable(Context context, String source) throws IOException {
private Drawable getDrawable(Context context, @NonNull String source) throws IOException {
Drawable drawable;
if (isLocalFile(Uri.parse(source))) {
drawable = loadFile(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ private List<AHBottomNavigationItem> createTabs() {
return new AHBottomNavigationItem(
options.text.get(""),
imageLoader.loadIcon(getActivity(), options.icon.get()),
imageLoader.loadIcon(getActivity(), options.selectedIcon.get(null)),
options.testId.get("")
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ public void setText(int index, String text) {
public void setIcon(int index, Drawable icon) {
AHBottomNavigationItem item = getItem(index);
if (!item.getDrawable(getContext()).equals(icon)) {
item.setDrawable(icon);
item.setIcon(icon);
refresh();
}
}

public void setSelectedIcon(int index, Drawable icon) {
AHBottomNavigationItem item = getItem(index);
if (!item.getDrawable(getContext()).equals(icon)) {
item.setSelectedIcon(icon);
refresh();
}
}
Expand Down
Binary file modified playground/img/layouts@2x.android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified playground/img/layouts@2x.ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added playground/img/layouts_selected@2x.android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added playground/img/layouts_selected@2x.ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified playground/img/options@2x.android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified playground/img/options@2x.ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added playground/img/options_selected@2x.android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added playground/img/options_selected@2x.ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion playground/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function start() {
bottomTab: {
text: 'Layouts',
icon: require('../img/layouts.png'),
fontSize: 10,
selectedIcon: require('../img/layouts_selected.png'),
testID: testIDs.LAYOUTS_TAB
}
}
Expand All @@ -73,6 +73,7 @@ function start() {
bottomTab: {
text: 'Options',
icon: require('../img/options.png'),
selectedIcon: require('../img/options_selected.png'),
testID: testIDs.OPTIONS_TAB
}
}
Expand Down

0 comments on commit 45e8389

Please sign in to comment.