Skip to content

Commit eb1ed3e

Browse files
vchimevADjenkov
authored andcommitted
refactor(bottom-navigation): strip item creation (#7477)
* feat(ios-tabs): tab bar item appearance Implements #7436 and NativeScript/nativescript-angular#1884. * refactor(ios-tabs): move _hasImage and _hasTitle properties * feat(ios-bottom-navigation): create tab bar from image nad label Implements NativeScript/nativescript-angular#1893 for iOS. * feat(android-bottom-navigation): create tab item spec from image and label Implements NativeScript/nativescript-angular#1893 for Android. * chore: remove console.logs
1 parent c34a48a commit eb1ed3e

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

tns-core-modules/ui/bottom-navigation/bottom-navigation.android.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { CSSType, Color } from "../core/view";
1212
import { Frame, View } from "../frame";
1313
import { RESOURCE_PREFIX, ad, layout } from "../../utils/utils";
1414
import { fromFileOrResource } from "../../image-source";
15+
import * as application from "../../application";
16+
1517
// TODO: Impl trace
1618
// import { isEnabled as traceEnabled, write as traceWrite } from "../../../trace";
1719

@@ -159,34 +161,35 @@ function initializeNativeClasses() {
159161
}
160162

161163
function createTabItemSpec(tabStripItem: TabStripItem): org.nativescript.widgets.TabItemSpec {
162-
const result = new org.nativescript.widgets.TabItemSpec();
163-
result.title = tabStripItem.title;
164-
165-
if (tabStripItem.backgroundColor instanceof Color) {
166-
result.backgroundColor = tabStripItem.backgroundColor.android;
167-
}
168-
169-
if (tabStripItem.iconSource) {
170-
if (tabStripItem.iconSource.indexOf(RESOURCE_PREFIX) === 0) {
171-
result.iconId = ad.resources.getDrawableId(tabStripItem.iconSource.substr(RESOURCE_PREFIX.length));
172-
if (result.iconId === 0) {
164+
let iconSource;
165+
const tabItemSpec = new org.nativescript.widgets.TabItemSpec();
166+
167+
// Image and Label children of TabStripItem
168+
// take priority over its `iconSource` and `title` properties
169+
iconSource = tabStripItem.image ? tabStripItem.image.src : tabStripItem.iconSource;
170+
tabItemSpec.title = tabStripItem.label ? tabStripItem.label.text : tabStripItem.title;
171+
172+
if (iconSource) {
173+
if (iconSource.indexOf(RESOURCE_PREFIX) === 0) {
174+
tabItemSpec.iconId = ad.resources.getDrawableId(iconSource.substr(RESOURCE_PREFIX.length));
175+
if (tabItemSpec.iconId === 0) {
173176
// TODO:
174-
// traceMissingIcon(tabStripItem.iconSource);
177+
// traceMissingIcon(iconSource);
175178
}
176179
} else {
177180
const is = fromFileOrResource(tabStripItem.iconSource);
178181
if (is) {
179182
// TODO: Make this native call that accepts string so that we don't load Bitmap in JS.
180183
// tslint:disable-next-line:deprecation
181-
result.iconDrawable = new android.graphics.drawable.BitmapDrawable(is.android);
184+
tabItemSpec.iconDrawable = new android.graphics.drawable.BitmapDrawable(application.android.context.getResources(), is.android);
182185
} else {
183186
// TODO:
184-
// traceMissingIcon(tabStripItem.iconSource);
187+
// traceMissingIcon(iconSource);
185188
}
186189
}
187190
}
188191

189-
return result;
192+
return tabItemSpec;
190193
}
191194

192195
function setElevation(grid: org.nativescript.widgets.GridLayout, bottomNavigationBar: org.nativescript.widgets.BottomNavigationBar) {
@@ -471,7 +474,7 @@ export class BottomNavigation extends TabNavigationBase {
471474
}
472475

473476
this._currentFragment = fragment;
474-
477+
475478
const tabItems = this.items;
476479
const tabItem = tabItems ? tabItems[position] : null;
477480
if (tabItem) {

tns-core-modules/ui/bottom-navigation/bottom-navigation.ios.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Types
1+
// Types
22
import { TabContentItem } from "../tab-navigation-base/tab-content-item";
33
import { TabStripItem } from "../tab-navigation-base/tab-strip-item";
44
import { TextTransform } from "../text-base";
@@ -500,15 +500,9 @@ export class BottomNavigation extends TabNavigationBase {
500500
items.forEach((item, i) => {
501501
const controller = this.getViewController(item);
502502

503-
let icon = null;
504-
let title = "";
505-
506503
if (this.tabStrip && this.tabStrip.items && this.tabStrip.items[i]) {
507504
const tabStripItem = <TabStripItem>this.tabStrip.items[i];
508-
icon = this._getIcon(tabStripItem.iconSource);
509-
title = tabStripItem.title;
510-
511-
const tabBarItem = UITabBarItem.alloc().initWithTitleImageTag((title || ""), icon, i);
505+
const tabBarItem = this.createTabBarItem(tabStripItem, i);
512506
updateTitleAndIconPositions(tabStripItem, tabBarItem, controller);
513507

514508
applyStatesToItem(tabBarItem, states);
@@ -528,6 +522,20 @@ export class BottomNavigation extends TabNavigationBase {
528522
this._ios.moreNavigationController.delegate = this._moreNavigationControllerDelegate;
529523
}
530524

525+
private createTabBarItem(item: TabStripItem, index: number): UITabBarItem {
526+
let image: UIImage;
527+
let title: string;
528+
529+
// Image and Label children of TabStripItem
530+
// take priority over its `iconSource` and `title` properties
531+
image = item.image ? this._getIcon(item.image.src) : this._getIcon(item.iconSource);
532+
title = item.label ? item.label.text : item.title;
533+
534+
const tabBarItem = UITabBarItem.alloc().initWithTitleImageTag(title, image, index);
535+
536+
return tabBarItem;
537+
}
538+
531539
private _getIconRenderingMode(): UIImageRenderingMode {
532540
return UIImageRenderingMode.AlwaysOriginal;
533541
}
@@ -632,4 +640,4 @@ function applyStatesToItem(item: UITabBarItem, states: TabStates) {
632640

633641
item.setTitleTextAttributesForState(states.normalState, UIControlState.Normal);
634642
item.setTitleTextAttributesForState(states.selectedState, UIControlState.Selected);
635-
}
643+
}

tns-core-modules/ui/tabs/tabs.ios.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,6 @@ class UIPageViewControllerDelegateImpl extends NSObject implements UIPageViewCon
345345
// } else {
346346
// owner.selectedIndex++;
347347
// }
348-
console.log("test");
349-
//
350348
}
351349

352350
public pageViewControllerDidFinishAnimatingPreviousViewControllersTransitionCompleted(pageViewController: UIPageViewController, didFinishAnimating: boolean, previousViewControllers: NSArray<UIViewController>, transitionCompleted: boolean): void {
@@ -364,9 +362,6 @@ class UIPageViewControllerDelegateImpl extends NSObject implements UIPageViewCon
364362
owner.selectedIndex = nextViewControllerIndex;
365363
owner._canSelectItem = true;
366364
}
367-
368-
console.log("test");
369-
//
370365
}
371366
}
372367

0 commit comments

Comments
 (0)