Skip to content

Commit

Permalink
On tab press event (#5880)
Browse files Browse the repository at this point in the history
* Open modal from tab iOS

* Open modal from tab Android

* Remove playground changes

Co-authored-by: Yogev Ben David <[email protected]>
  • Loading branch information
pontusab and yogevbd committed Jan 28, 2020
1 parent 8a1bfe7 commit b153142
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static BottomTabOptions parse(TypefaceLoader typefaceManager, JSONObject
options.fontSize = NumberParser.parse(json, "fontSize");
options.selectedFontSize = NumberParser.parse(json, "selectedFontSize");
options.dotIndicator = DotIndicatorOptions.parse(json.optJSONObject("dotIndicator"));
options.selectTabOnPress = BoolParser.parse(json, "selectTabOnPress");

return options;
}

Expand All @@ -59,6 +61,7 @@ public static BottomTabOptions parse(TypefaceLoader typefaceManager, JSONObject
public DotIndicatorOptions dotIndicator = new DotIndicatorOptions();
public Number fontSize = new NullNumber();
public Number selectedFontSize = new NullNumber();
public Bool selectTabOnPress = new NullBool();
@Nullable public Typeface fontFamily;


Expand All @@ -78,6 +81,7 @@ void mergeWith(final BottomTabOptions other) {
if (other.selectedFontSize.hasValue()) selectedFontSize = other.selectedFontSize;
if (other.fontFamily != null) fontFamily = other.fontFamily;
if (other.dotIndicator.hasValue()) dotIndicator = other.dotIndicator;
if (other.selectTabOnPress.hasValue()) selectTabOnPress = other.selectTabOnPress;
}

void mergeWithDefault(final BottomTabOptions defaultOptions) {
Expand All @@ -96,5 +100,6 @@ void mergeWithDefault(final BottomTabOptions defaultOptions) {
if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
if (!testId.hasValue()) testId = defaultOptions.testId;
if (!dotIndicator.hasValue()) dotIndicator = defaultOptions.dotIndicator;
if (!selectTabOnPress.hasValue()) selectTabOnPress = defaultOptions.selectTabOnPress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class EventEmitter {
private static final String AppLaunched = "RNN.AppLaunched";
private static final String CommandCompleted = "RNN.CommandCompleted";
private static final String BottomTabSelected = "RNN.BottomTabSelected";
private static final String BottomTabPressed = "RNN.BottomTabPressed";
private static final String ComponentDidAppear = "RNN.ComponentDidAppear";
private static final String ComponentDidDisappear = "RNN.ComponentDidDisappear";
private static final String NavigationButtonPressed = "RNN.NavigationButtonPressed";
Expand Down Expand Up @@ -61,6 +62,12 @@ public void emitBottomTabSelected(int unselectedTabIndex, int selectedTabIndex)
emit(BottomTabSelected, event);
}

public void emitBottomTabPressed(int tabIndex) {
WritableMap event = Arguments.createMap();
event.putInt("tabIndex", tabIndex);
emit(BottomTabPressed, event);
}

public void emitCommandCompleted(String commandName, String commandId, long completionTime) {
WritableMap event = Arguments.createMap();
event.putString("commandName", commandName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,18 @@ protected ViewController getCurrentChild() {

@Override
public boolean onTabSelected(int index, boolean wasSelected) {
eventEmitter.emitBottomTabSelected(bottomTabs.getCurrentItem(), index);
if (wasSelected) return false;
selectTab(index);
return false;
BottomTabOptions options = tabs.get(index).resolveCurrentOptions().bottomTabOptions;

eventEmitter.emitBottomTabPressed(index);

if (options.selectTabOnPress.get(true)){
eventEmitter.emitBottomTabSelected(bottomTabs.getCurrentItem(), index);
if (wasSelected) return false;
selectTab(index);
return false;
} else {
return false;
}
}

private List<AHBottomNavigationItem> createTabs() {
Expand Down
1 change: 1 addition & 0 deletions lib/ios/RNNBottomTabOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@property(nonatomic, strong) Color *textColor;
@property(nonatomic, strong) Number *fontSize;
@property(nonatomic, strong) Bool *visible;
@property(nonatomic, strong) Bool *selectTabOnPress;


@end
1 change: 1 addition & 0 deletions lib/ios/RNNBottomTabOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
self.textColor = [ColorParser parse:dict key:@"textColor"];
self.fontSize = [NumberParser parse:dict key:@"fontSize"];
self.visible = [BoolParser parse:dict key:@"visible"];
self.selectTabOnPress = [BoolParser parse:dict key:@"selectTabOnPress"];

return self;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/ios/RNNBottomTabsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ - (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
}
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSUInteger _index = [tabBarController.viewControllers indexOfObject:viewController];
[self.eventEmitter sendBottomTabPressed:@(_index)];

if([[viewController resolveOptions].bottomTab.selectTabOnPress getWithDefaultValue:YES]){
return YES;
}

return NO;
}

@end
1 change: 1 addition & 0 deletions lib/ios/RNNButtonOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@property (nonatomic, strong) Image* icon;
@property (nonatomic, strong) Bool* enabled;
@property (nonatomic, strong) RNNInsetsOptions* iconInsets;
@property(nonatomic, strong) Bool *selectTabOnPress;

@end
3 changes: 2 additions & 1 deletion lib/ios/RNNButtonOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
self.icon = [ImageParser parse:dict key:@"icon"];
self.iconInsets = [[RNNInsetsOptions alloc] initWithDict:dict[@"iconInsets"]];
self.enabled = [BoolParser parse:dict key:@"enabled"];

self.selectTabOnPress = [BoolParser parse:dict key:@"selectTabOnPress"];


return self;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/ios/RNNEventEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@

- (void)sendScreenPoppedEvent:(NSString *)componentId;

- (void)sendBottomTabPressed:(NSNumber *)tabIndex;


@end
8 changes: 8 additions & 0 deletions lib/ios/RNNEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ @implementation RNNEventEmitter {
static NSString* const SearchBarCancelPressed = @"RNN.SearchBarCancelPressed";
static NSString* const PreviewCompleted = @"RNN.PreviewCompleted";
static NSString* const ScreenPopped = @"RNN.ScreenPopped";
static NSString* const BottomTabPressed = @"RNN.BottomTabPressed";

-(NSArray<NSString *> *)supportedEvents {
return @[AppLaunched,
CommandCompleted,
BottomTabSelected,
BottomTabLongPressed,
BottomTabPressed,
ComponentDidAppear,
ComponentDidDisappear,
NavigationButtonPressed,
Expand Down Expand Up @@ -84,6 +86,12 @@ - (void)sendBottomTabLongPressed:(NSNumber *)selectedTabIndex {
}];
}

-(void)sendBottomTabPressed:(NSNumber *)tabIndex {
[self send:BottomTabPressed body:@{
@"tabIndex": tabIndex
}];
}

-(void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId params:(NSDictionary*)params {
[self send:CommandCompleted body:@{
@"commandId":commandId,
Expand Down
76 changes: 59 additions & 17 deletions lib/src/adapters/NativeEventsReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { NativeModules, NativeEventEmitter, EventEmitter, EmitterSubscription } from 'react-native';
import {
NativeModules,
NativeEventEmitter,
EventEmitter,
EmitterSubscription
} from 'react-native';
import {
ComponentDidAppearEvent,
ComponentDidDisappearEvent,
Expand All @@ -10,7 +15,12 @@ import {
ScreenPoppedEvent,
ModalAttemptedToDismissEvent
} from '../interfaces/ComponentEvents';
import { CommandCompletedEvent, BottomTabSelectedEvent, BottomTabLongPressedEvent } from '../interfaces/Events';
import {
CommandCompletedEvent,
BottomTabSelectedEvent,
BottomTabLongPressedEvent,
BottomTabPressedEvent
} from '../interfaces/Events';

export class NativeEventsReceiver {
private emitter: EventEmitter;
Expand All @@ -20,65 +30,97 @@ export class NativeEventsReceiver {
try {
this.emitter = new NativeEventEmitter(NativeModules.RNNEventEmitter);
} catch (e) {
this.emitter = {
this.emitter = ({
addListener: () => {
return {
remove: () => undefined
};
}
} as any as EventEmitter;
} as any) as EventEmitter;
}
}

public registerAppLaunchedListener(callback: () => void): EmitterSubscription {
public registerAppLaunchedListener(
callback: () => void
): EmitterSubscription {
return this.emitter.addListener('RNN.AppLaunched', callback);
}

public registerComponentDidAppearListener(callback: (event: ComponentDidAppearEvent) => void): EmitterSubscription {
public registerComponentDidAppearListener(
callback: (event: ComponentDidAppearEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.ComponentDidAppear', callback);
}

public registerComponentDidDisappearListener(callback: (event: ComponentDidDisappearEvent) => void): EmitterSubscription {
public registerComponentDidDisappearListener(
callback: (event: ComponentDidDisappearEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.ComponentDidDisappear', callback);
}

public registerNavigationButtonPressedListener(callback: (event: NavigationButtonPressedEvent) => void): EmitterSubscription {
public registerNavigationButtonPressedListener(
callback: (event: NavigationButtonPressedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.NavigationButtonPressed', callback);
}

public registerModalDismissedListener(callback: (event: ModalDismissedEvent) => void): EmitterSubscription {
public registerBottomTabPressedListener(
callback: (data: BottomTabPressedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.BottomTabPressed', callback);
}

public registerModalDismissedListener(
callback: (event: ModalDismissedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.ModalDismissed', callback);
}

public registerModalAttemptedToDismissListener(callback: (event: ModalAttemptedToDismissEvent) => void): EmitterSubscription {
public registerModalAttemptedToDismissListener(
callback: (event: ModalAttemptedToDismissEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.ModalAttemptedToDismiss', callback);
}

public registerSearchBarUpdatedListener(callback: (event: SearchBarUpdatedEvent) => void): EmitterSubscription {
public registerSearchBarUpdatedListener(
callback: (event: SearchBarUpdatedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.SearchBarUpdated', callback);
}

public registerSearchBarCancelPressedListener(callback: (event: SearchBarCancelPressedEvent) => void): EmitterSubscription {
public registerSearchBarCancelPressedListener(
callback: (event: SearchBarCancelPressedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.SearchBarCancelPressed', callback);
}

public registerPreviewCompletedListener(callback: (event: PreviewCompletedEvent) => void): EmitterSubscription {
public registerPreviewCompletedListener(
callback: (event: PreviewCompletedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.PreviewCompleted', callback);
}

public registerCommandCompletedListener(callback: (data: CommandCompletedEvent) => void): EmitterSubscription {
public registerCommandCompletedListener(
callback: (data: CommandCompletedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.CommandCompleted', callback);
}

public registerBottomTabSelectedListener(callback: (data: BottomTabSelectedEvent) => void): EmitterSubscription {
public registerBottomTabSelectedListener(
callback: (data: BottomTabSelectedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.BottomTabSelected', callback);
}

public registerBottomTabLongPressedListener(callback: (data: BottomTabLongPressedEvent) => void): EmitterSubscription {
public registerBottomTabLongPressedListener(
callback: (data: BottomTabLongPressedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.BottomTabLongPressed', callback);
}

public registerScreenPoppedListener(callback: (event: ScreenPoppedEvent) => void): EmitterSubscription {
public registerScreenPoppedListener(
callback: (event: ScreenPoppedEvent) => void
): EmitterSubscription {
return this.emitter.addListener('RNN.ScreenPopped', callback);
}
}
Loading

0 comments on commit b153142

Please sign in to comment.