Skip to content

Commit

Permalink
Merge branch 'backButtonTestID' of github.com:wix/react-native-naviga…
Browse files Browse the repository at this point in the history
…tion into backButtonTestID
  • Loading branch information
yogevbd committed Mar 3, 2020
2 parents 10ad7a7 + f32276b commit 6f3497e
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 32 deletions.
34 changes: 34 additions & 0 deletions autolink/postlink/gradleLinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var fs = require('fs');
var { warnn, errorn, logn, infon, debugn } = require('./log');
var { insertString } = require('./stringUtils');
var DEFAULT_KOTLIN_VERSION = '1.3.61';
// This should be the minSdkVersion required for RNN.
var DEFAULT_MIN_SDK_VERSION = 19

class GradleLinker {
constructor() {
Expand All @@ -16,6 +18,7 @@ class GradleLinker {
var contents = fs.readFileSync(this.gradlePath, 'utf8');
contents = this._setKotlinVersion(contents);
contents = this._setKotlinPluginDependency(contents);
contents = this._setMinSdkVersion(contents);
fs.writeFileSync(this.gradlePath, contents);
infon('Root build.gradle linked successfully!\n');
} else {
Expand Down Expand Up @@ -54,6 +57,22 @@ class GradleLinker {
return contents;
}

/**
* Check the current minSdkVersion specified and if it's lower than
* the required version, set it to the required version otherwise leave as it is.
*/
_setMinSdkVersion(contents) {
var minSdkVersion = this._getMinSdkVersion(contents)
// If user entered minSdkVersion is lower than the default, set it to default.
if (minSdkVersion < DEFAULT_MIN_SDK_VERSION) {
debugn(` Updating minSdkVersion to ${DEFAULT_MIN_SDK_VERSION}`)
return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${DEFAULT_MIN_SDK_VERSION}`)
}

debugn(` Already specified minSdkVersion ${minSdkVersion}`)
return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${minSdkVersion}`)
}

/**
* @param { string } contents
*/
Expand All @@ -69,6 +88,21 @@ class GradleLinker {
return `"${DEFAULT_KOTLIN_VERSION}"`;
}

/**
* Get the minSdkVersion value.
* @param { string } contents
*/
_getMinSdkVersion(contents) {
var minSdkVersion = contents.match(/minSdkVersion\s{0,}=\s{0,}(\d*)/)

if (minSdkVersion && minSdkVersion[1]) {
// It'd be something like 16 for a fresh React Native project.
return +minSdkVersion[1]
}

return DEFAULT_MIN_SDK_VERSION
}

/**
* @param {string} contents
*/
Expand Down
3 changes: 2 additions & 1 deletion autolink/postlink/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ var mainApplicationJava = glob.sync("**/MainApplication.java", ignoreFolders)[0]
exports.mainApplicationJava = mainApplicationJava;
exports.rootGradle = mainApplicationJava.replace(/android\/app\/.*\.java/, "android/build.gradle");;

exports.appDelegate = glob.sync("**/AppDelegate.m", ignoreFolders)[0];
exports.appDelegate = glob.sync("**/AppDelegate.m", ignoreFolders)[0];
exports.podFile = glob.sync("**/Podfile", ignoreFolders)[0]
39 changes: 39 additions & 0 deletions autolink/postlink/podfileLinker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// @ts-check
var path = require('./path');
var fs = require("fs");
var {logn, debugn, infon} = require("./log");

class PodfileLinker {
constructor() {
this.podfilePath = path.podFile;
}

link() {
if (this.podfilePath) {
logn("Updating Podfile...")
var podfileContent = fs.readFileSync(this.podfilePath, "utf8");

podfileContent = this._removeRNNPodLink(podfileContent);

fs.writeFileSync(this.podfilePath, podfileContent);
infon("Podfile updated successfully!\n")
}
}

/**
* Removes the RNN pod added by react-native link script.
*/
_removeRNNPodLink(contents) {
const rnnPodLink = contents.match(/\s+.*pod 'ReactNativeNavigation'.+react-native-navigation'/)

if (!rnnPodLink) {
debugn(" RNN Pod has not been added to Podfile")
return contents
}

debugn(" Removing RNN Pod from Podfile")
return contents.replace(rnnPodLink, "")
}
}

module.exports = PodfileLinker
2 changes: 2 additions & 0 deletions autolink/postlink/postLinkIOS.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-check
var AppDelegateLinker = require("./appDelegateLinker");
var PodfileLinker = require('./podfileLinker');

module.exports = () => {
console.log("Running iOS postlink script\n");
new AppDelegateLinker().link();
new PodfileLinker().link();
}
2 changes: 1 addition & 1 deletion lib/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'

implementation 'com.github.wix-playground:ahbottomnavigation:3.1.2'
implementation 'com.github.wix-playground:ahbottomnavigation:3.2.0'
implementation 'com.github.wix-playground:reflow-animator:1.0.6'
implementation 'com.github.clans:fab:1.6.4'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.reactnativenavigation.viewcontrollers.bottomtabs.TabSelector;
import com.reactnativenavigation.views.BottomTabs;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.annotation.IntRange;
Expand Down Expand Up @@ -118,7 +120,7 @@ private void applyBottomTabsOptions(Options options) {

bottomTabs.setLayoutDirection(options.layout.direction);
bottomTabs.setPreferLargeIcons(options.bottomTabsOptions.preferLargeIcons.get(false));
bottomTabs.setTitleState(bottomTabsOptions.titleDisplayMode.get(TitleState.SHOW_WHEN_ACTIVE));
bottomTabs.setTitleState(bottomTabsOptions.titleDisplayMode.get(getDefaultTitleState()));
bottomTabs.setBackgroundColor(bottomTabsOptions.backgroundColor.get(Color.WHITE));
if (bottomTabsOptions.currentTabIndex.hasValue()) {
int tabIndex = bottomTabsOptions.currentTabIndex.get();
Expand Down Expand Up @@ -148,6 +150,14 @@ private void applyBottomTabsOptions(Options options) {
}
}

@NotNull
private TitleState getDefaultTitleState() {
for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
if (bottomTabs.getItem(i).hasIcon()) return TitleState.SHOW_WHEN_ACTIVE;
}
return TitleState.ALWAYS_SHOW;
}

public void applyBottomInset(int bottomInset) {
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) bottomTabs.getLayoutParams();
lp.bottomMargin = bottomInset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private List<AHBottomNavigationItem> createTabs() {
BottomTabOptions options = tab.resolveCurrentOptions().bottomTabOptions;
return new AHBottomNavigationItem(
options.text.get(""),
imageLoader.loadIcon(getActivity(), options.icon.get()),
imageLoader.loadIcon(getActivity(), options.icon.get(null)),
imageLoader.loadIcon(getActivity(), options.selectedIcon.get(null)),
options.testId.get("")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.TestUtils;
import com.reactnativenavigation.mocks.ImageLoaderMock;
import com.reactnativenavigation.mocks.SimpleViewController;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.parse.params.Bool;
import com.reactnativenavigation.parse.params.Colour;
import com.reactnativenavigation.parse.params.NullText;
import com.reactnativenavigation.parse.params.Number;
import com.reactnativenavigation.parse.params.Text;
import com.reactnativenavigation.presentation.BottomTabPresenter;
Expand Down Expand Up @@ -43,6 +45,7 @@
import edu.emory.mathcs.backport.java.util.Collections;

import static com.reactnativenavigation.TestUtils.hideBackButton;
import static com.reactnativenavigation.utils.ObjectUtils.perform;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -74,32 +77,9 @@ public class BottomTabsControllerTest extends BaseTest {
@Override
public void beforeEach() {
activity = newActivity();
bottomTabs = spy(new BottomTabs(activity) {
@Override
public void superCreateItems() {

}
});
childRegistry = new ChildControllersRegistry();
eventEmitter = Mockito.mock(EventEmitter.class);

child1 = spy(new SimpleViewController(activity, childRegistry, "child1", tabOptions));
child2 = spy(new SimpleViewController(activity, childRegistry, "child2", tabOptions));
child3 = spy(new SimpleViewController(activity, childRegistry, "child3", tabOptions));
child4 = spy(createStack());
child5 = spy(new SimpleViewController(activity, childRegistry, "child5", tabOptions));
when(child5.handleBack(any())).thenReturn(true);
tabs = createTabs();
presenter = spy(new BottomTabsPresenter(tabs, new Options()));
bottomTabPresenter = spy(new BottomTabPresenter(activity, tabs, ImageLoaderMock.mock(), new Options()));
tabsAttacher = spy(new BottomTabsAttacher(tabs, presenter));
uut = createBottomTabs();

uut.setParentController(Mockito.mock(ParentController.class));
CoordinatorLayout parent = new CoordinatorLayout(activity);
parent.addView(uut.getView());
activity.setContentView(parent);

prepareViewsForTests();
StatusBarUtils.saveStatusBarHeight(63);
}

Expand All @@ -110,6 +90,22 @@ public void createView_checkProperStructure() {
assertThat(((CoordinatorLayout.LayoutParams) uut.getBottomTabs().getLayoutParams()).gravity).isEqualTo(Gravity.BOTTOM);
}

@Test
public void createView_tabsWithoutIconsAreAccepted() {
tabOptions.bottomTabOptions.icon = new NullText();
prepareViewsForTests();
assertThat(uut.getBottomTabs().getItemsCount()).isEqualTo(tabs.size());
}

@Test
public void createView_showTitlesWhenAllTabsDontHaveIcons() {
tabOptions.bottomTabOptions.icon = new NullText();
assertThat(tabOptions.bottomTabsOptions.titleDisplayMode.hasValue()).isFalse();
prepareViewsForTests();
presenter.applyOptions(Options.EMPTY);
assertThat(bottomTabs.getTitleState()).isEqualTo(AHBottomNavigation.TitleState.ALWAYS_SHOW);
}

@Test(expected = RuntimeException.class)
public void setTabs_ThrowWhenMoreThan5() {
tabs.add(new SimpleViewController(activity, childRegistry, "6", tabOptions));
Expand Down Expand Up @@ -393,6 +389,36 @@ public void destroy() {
verify(tabsAttacher).destroy();
}

private void prepareViewsForTests() {
perform(uut, ViewController::destroy);
bottomTabs = spy(new BottomTabs(activity) {
@Override
public void superCreateItems() {

}
});
createChildren();
tabs = createTabs();
presenter = spy(new BottomTabsPresenter(tabs, new Options()));
bottomTabPresenter = spy(new BottomTabPresenter(activity, tabs, ImageLoaderMock.mock(), new Options()));
tabsAttacher = spy(new BottomTabsAttacher(tabs, presenter));
uut = createBottomTabs();

uut.setParentController(Mockito.mock(ParentController.class));
CoordinatorLayout parent = new CoordinatorLayout(activity);
parent.addView(uut.getView());
activity.setContentView(parent);
}

private void createChildren() {
child1 = spy(new SimpleViewController(activity, childRegistry, "child1", tabOptions));
child2 = spy(new SimpleViewController(activity, childRegistry, "child2", tabOptions));
child3 = spy(new SimpleViewController(activity, childRegistry, "child3", tabOptions));
child4 = spy(createStack());
child5 = spy(new SimpleViewController(activity, childRegistry, "child5", tabOptions));
when(child5.handleBack(any())).thenReturn(true);
}

@NonNull
private List<ViewController> createTabs() {
return Arrays.asList(child1, child2, child3, child4, child5);
Expand Down
4 changes: 0 additions & 4 deletions lib/ios/RNNComponentViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
}

- (BOOL)prefersStatusBarHidden {
return [_presenter isStatusBarVisibility:self.navigationController resolvedOptions:self.resolveOptions];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
return [_presenter getStatusBarStyle:[self resolveOptions]];
}
Expand Down
4 changes: 4 additions & 0 deletions lib/ios/UIViewController+LayoutProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return interfaceOrientationMask;
}

- (BOOL)prefersStatusBarHidden {
return [self.presenter isStatusBarVisibility:self.navigationController resolvedOptions:self.resolveOptions];
}

- (UINavigationController *)stack {
if ([self isKindOfClass:UINavigationController.class]) {
return (UINavigationController *)self;
Expand Down

0 comments on commit 6f3497e

Please sign in to comment.