Skip to content

Commit

Permalink
Fix topBar title with subtitle view (#6509)
Browse files Browse the repository at this point in the history
Move topBar title with subtitle view initialisation to when the viewController view is already initialised. Fix's cases where the titleView initialised too soon and therefor not visible because `layoutSubviews` never gets called.

Co-authored-by: Guy Carmeli <[email protected]>
  • Loading branch information
yogevbd and guyca authored Sep 1, 2020
1 parent fcf23d2 commit 1878393
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
6 changes: 6 additions & 0 deletions e2e/Stack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('Stack', () => {
await expect(elementByLabel('back button clicked')).toBeVisible();
});

it('push title with subtitle', async () => {
await elementById(TestIDs.PUSH_TITLE_WITH_SUBTITLE).tap();
await expect(elementByLabel('Title')).toBeVisible();
await expect(elementByLabel('Subtitle')).toBeVisible();
});

it('screen lifecycle', async () => {
await elementById(TestIDs.PUSH_LIFECYCLE_BTN).tap();
await expect(elementByLabel('didAppear')).toBeVisible();
Expand Down
4 changes: 3 additions & 1 deletion lib/ios/RNNComponentPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ - (void)applyOptions:(RNNNavigationOptions *)options {
}
[viewController setSearchBarWithPlaceholder:[withDefault.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
}

[_topBarTitlePresenter applyOptions:withDefault.topBar];
}

- (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
Expand All @@ -70,7 +72,7 @@ - (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
RNNComponentViewController* viewController = self.boundViewController;
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];

[_topBarTitlePresenter applyOptionsOnInit:withDefault.topBar];
[_topBarTitlePresenter applyOptionsOnInit:withDefault.topBar];

[viewController setTopBarPrefersLargeTitle:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
[viewController setDrawBehindTopBar:[withDefault.topBar shouldDrawBehind]];
Expand Down
2 changes: 2 additions & 0 deletions lib/ios/TopBarTitlePresenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- (void)applyOptionsOnInit:(RNNTopBarOptions *)options;

- (void)applyOptions:(RNNTopBarOptions *)options;

- (void)mergeOptions:(RNNTopBarOptions *)options resolvedOptions:(RNNTopBarOptions *)resolvedOptions;

- (void)setCustomNavigationTitleView:(RNNTopBarOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock;
Expand Down
36 changes: 18 additions & 18 deletions lib/ios/TopBarTitlePresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ @implementation TopBarTitlePresenter {
}

- (void)applyOptionsOnInit:(RNNTopBarOptions *)initialOptions {
[self updateTitleWithOptions:initialOptions];
if (initialOptions.title.component.hasValue) {
[self setCustomNavigationTitleView:initialOptions perform:nil];
} else if (initialOptions.title.text.hasValue) {
[self removeTitleComponents];
self.boundViewController.navigationItem.title = initialOptions.title.text.get;
}
}

- (void)updateTitleWithOptions:(RNNTopBarOptions *)options {
if (options.title.component.hasValue) {
[self setCustomNavigationTitleView:options perform:nil];
} else if (options.subtitle.text.hasValue) {
- (void)applyOptions:(RNNTopBarOptions *)options {
if (options.subtitle.text.hasValue) {
[self setTitleViewWithSubtitle:options];
} else if (options.title.text.hasValue) {
[self removeTitleComponents];
self.boundViewController.navigationItem.title = options.title.text.get;
}
}

Expand All @@ -35,18 +35,18 @@ - (void)mergeOptions:(RNNTopBarOptions *)options resolvedOptions:(RNNTopBarOptio
}

- (void)setTitleViewWithSubtitle:(RNNTopBarOptions *)options {
if (!_customTitleView && ![options.largeTitle.visible getWithDefaultValue:NO]) {
_titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.title subTitleOptions:options.subtitle viewController:self.boundViewController];
if (!_customTitleView && ![options.largeTitle.visible getWithDefaultValue:NO]) {
_titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.title subTitleOptions:options.subtitle viewController:self.boundViewController];

if (options.title.text.hasValue) {
[_titleViewHelper setTitleOptions:options.title];
}
if (options.subtitle.text.hasValue) {
[_titleViewHelper setSubtitleOptions:options.subtitle];
}
if (options.title.text.hasValue) {
[_titleViewHelper setTitleOptions:options.title];
}
if (options.subtitle.text.hasValue) {
[_titleViewHelper setSubtitleOptions:options.subtitle];
}

[_titleViewHelper setup];
}
[_titleViewHelper setup];
}
}

- (void)renderComponents:(RNNTopBarOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
Expand Down
23 changes: 23 additions & 0 deletions playground/src/screens/StackScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
PUSH_LIFECYCLE_BTN,
POP_NONE_EXISTENT_SCREEN_BTN,
PUSH_CUSTOM_BACK_BTN,
PUSH_TITLE_WITH_SUBTITLE,
PUSH_LAZY_BTN,
CUSTOM_BACK_BTN,
SEARCH_BTN,
Expand Down Expand Up @@ -57,6 +58,11 @@ export default class StackScreen extends React.Component<NavigationComponentProp
testID={PUSH_CUSTOM_BACK_BTN}
onPress={this.pushCustomBackButton}
/>
<Button
label="Push Title With Subtitle"
testID={PUSH_TITLE_WITH_SUBTITLE}
onPress={this.pushTitleWithSubtitle}
/>
<Button label="Set Stack Root" testID={SET_STACK_ROOT_BTN} onPress={this.setStackRoot} />
<Button
label="Set Stack Root With ID"
Expand Down Expand Up @@ -99,6 +105,23 @@ export default class StackScreen extends React.Component<NavigationComponentProp
},
});

pushTitleWithSubtitle = () =>
Navigation.push(this, {
component: {
name: Screens.Pushed,
options: {
topBar: {
title: {
text: 'Title',
},
subtitle: {
text: 'Subtitle',
},
},
},
},
});

search = () => Navigation.push(this, Screens.Search);

setStackRoot = () =>
Expand Down
1 change: 1 addition & 0 deletions playground/src/testIDs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const testIDs = {
TOP_BAR_BTN: 'TOP_BAR_BUTTON',
CUSTOM_BACK_BTN: 'CUSTOM_BACK_BUTTON',
PUSH_CUSTOM_BACK_BTN: 'PUSH_CUSTOM_BACK_BTN',
PUSH_TITLE_WITH_SUBTITLE: 'PUSH_TITLE_WITH_SUBTITLE',
BACK_BUTTON: 'BACK_BUTTON',
SWITCH_TAB_BY_INDEX_BTN: 'SWITCH_TAB_BY_INDEX_BTN',
SWITCH_TAB_BY_COMPONENT_ID_BTN: 'SWITCH_TAB_BY_COMPONENT_ID_BTN',
Expand Down

0 comments on commit 1878393

Please sign in to comment.