Skip to content

Commit

Permalink
fix: fixes for bottom-navigation and tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Jan 18, 2021
1 parent ea44b7e commit 89fb903
Show file tree
Hide file tree
Showing 20 changed files with 406 additions and 260 deletions.
14 changes: 14 additions & 0 deletions src/bottom-navigation/angular/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Directive, NgModule } from '@angular/core';
import { registerElement } from '@nativescript/angular';
import { BottomNavigation } from '@nativescript-community/ui-material-bottomnavigation';

@Directive({ selector: 'MDBottomNavigation' })
export class MaterialBottomNavigationDirective {}

@NgModule({
declarations: [MaterialBottomNavigationDirective],
exports: [MaterialBottomNavigationDirective]
})
export class NativeScriptMaterialBottomNavigationModule {}

registerElement('MDBottomNavigation', () => BottomNavigation);
18 changes: 18 additions & 0 deletions src/bottom-navigation/angular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@nativescript-community/ui-material-bottomnavigation-angular",
"main": "index.js",
"ngPackage": {
"dest":"../../../packages/bottomnavigation/angular",
"lib": {
"entryFile": "index.ts",
"umdModuleIds": {
"@nativescript/core": "ns-core",
"@nativescript/angular": "ns-angular",
"@nativescript-community/ui-material-bottomnavigation": "ns-material-bottomnavigation"
}
},
"whitelistedNonPeerDependencies": [
"."
]
}
}
18 changes: 18 additions & 0 deletions src/bottom-navigation/angular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"rootDir": "./",
"paths": {
"tns-core-modules": ["./node_modules/@nativescript/core"],
"tns-core-modules/*": ["./node_modules/@nativescript/core/*"],
"@nativescript-community/ui-material-core": ["packages/core/index"],
"@nativescript-community/ui-material-core/*": ["packages/core/*"],
"@nativescript-community/ui-material-bottomnavigation": ["packages/bottomnavigation/index"],
"@nativescript-community/ui-material-bottomnavigation/*": ["packages/bottomnavigation/*"]
}
},
"include": ["./**/*.ts", "../../../references.d.ts", "../../references.d.ts"],
"angularCompilerOptions": {
"enableIvy": true
}
}
104 changes: 84 additions & 20 deletions src/bottom-navigation/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TabStripItem } from '@nativescript-community/ui-material-core/tab-navig
// Requires
import { Application, CSSType, Color, Font, Frame, ImageSource, Utils, View, getIconSpecSize } from '@nativescript/core';
import { TextTransform, getTransformedText } from '@nativescript/core/ui/text-base';
export { TabContentItem, TabStrip, TabStripItem };

// TODO: Impl trace
// import { Trace } from "../../../trace";
Expand Down Expand Up @@ -344,19 +345,21 @@ export class BottomNavigation extends TabNavigationBase {

iterateIndexRange(newIndex, offsideItems, lastIndex, (i) => toLoad.push(i));

items.forEach((item, i) => {
const indexOfI = toLoad.indexOf(i);
if (indexOfI < 0) {
toUnload.push(i);
}
});
if (this.unloadOnTabChange) {
items.forEach((item, i) => {
const indexOfI = toLoad.indexOf(i);
if (indexOfI < 0) {
toUnload.push(i);
}
});

toUnload.forEach((index) => {
const item = items[index];
if (items[index]) {
item.unloadView(item.content);
}
});
toUnload.forEach((index) => {
const item = items[index];
if (items[index]) {
item.unloadView(item.content);
}
});
}

const newItem = items[newIndex];
const selectedView = newItem && newItem.content;
Expand Down Expand Up @@ -417,11 +420,10 @@ export class BottomNavigation extends TabNavigationBase {
this.setTabStripItems(null);
}

const fragmentToDetach = this._currentFragment;
if (fragmentToDetach) {
this.destroyItem((fragmentToDetach as any).index, fragmentToDetach);
this.removeFragment(fragmentToDetach);
}
this.items.forEach((item, i) => {
item.unloadView(item.content);
});
this.disposeTabFragments();
}

public disposeNativeView() {
Expand Down Expand Up @@ -495,7 +497,12 @@ export class BottomNavigation extends TabNavigationBase {

const fragmentToDetach = this._currentFragment;
if (fragmentToDetach) {
this.destroyItem((fragmentToDetach as any).index, fragmentToDetach);
console.log('unloadOnTabChange', this.unloadOnTabChange);
if (this.unloadOnTabChange) {
this.destroyItem((fragmentToDetach as any).index, fragmentToDetach);
} else {
this.hideFragment(fragmentToDetach as any);
}
}

const fragment = this.instantiateItem(this._contentView, index);
Expand Down Expand Up @@ -532,6 +539,9 @@ export class BottomNavigation extends TabNavigationBase {
if (fragment != null) {
fragment.setMenuVisibility(true);
fragment.setUserVisibleHint(true);
if (!this.unloadOnTabChange) {
this.showFragment(fragment);
}
}

this._currentFragment = fragment;
Expand All @@ -545,19 +555,73 @@ export class BottomNavigation extends TabNavigationBase {
}
}
}

private destroyItem(position: number, fragment: androidx.fragment.app.Fragment): void {
if (fragment) {
this.removeFragment(fragment);
if (this._currentFragment === fragment) {
this._currentFragment = null;
}
}

if (this.items && this.items[position]) {
this.items[position].canBeLoaded = false;
}
}
private hideFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) {
if (!fragmentManager) {
fragmentManager = this._getFragmentManager();
}
if (fragment) {
if (!fragment.isAdded() || fragment.isRemoving()) {
// ignore
return;
} else {
const fragmentExitTransition = fragment.getExitTransition();
if (fragmentExitTransition && fragmentExitTransition instanceof org.nativescript.widgets.CustomTransition) {
fragmentExitTransition.setResetOnTransitionEnd(true);
}
if (fragment && fragment.isAdded() && !fragment.isRemoving()) {
const pfm = (fragment as any).getParentFragmentManager ? (fragment as any).getParentFragmentManager() : null;
if (pfm && !pfm.isDestroyed()) {
try {
if (pfm.isStateSaved()) {
pfm.beginTransaction().hide(fragment).commitNowAllowingStateLoss();
} else {
pfm.beginTransaction().hide(fragment).commitNow();
}
} catch (e) {}
}
}
}
}
}
private showFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) {
if (!fragmentManager) {
fragmentManager = this._getFragmentManager();
}
if (fragment) {
if (!fragment.isAdded() || fragment.isRemoving()) {
// ignore
return;
} else {
const fragmentExitTransition = fragment.getExitTransition();
if (fragmentExitTransition && fragmentExitTransition instanceof org.nativescript.widgets.CustomTransition) {
fragmentExitTransition.setResetOnTransitionEnd(true);
}
if (fragment && fragment.isAdded() && !fragment.isRemoving()) {
const pfm = (fragment as any).getParentFragmentManager ? (fragment as any).getParentFragmentManager() : null;
if (pfm && !pfm.isDestroyed()) {
try {
if (pfm.isStateSaved()) {
pfm.beginTransaction().show(fragment).commitNowAllowingStateLoss();
} else {
pfm.beginTransaction().show(fragment).commitNow();
}
} catch (e) {}
}
}
}
}
}
private removeFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) {
if (!fragmentManager) {
fragmentManager = this._getFragmentManager();
Expand Down
2 changes: 2 additions & 0 deletions src/bottom-navigation/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import { TabNavigationBase } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-navigation-base';
import { TabStrip } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-strip';
import { CoercibleProperty, EventData, Property } from '@nativescript/core';
import { TabStripItem } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-strip-item';

export { TabContentItem, TabStrip, TabStripItem };
/**
* Defines the data for the TabView.selectedIndexChanged event.
*/
Expand Down
38 changes: 24 additions & 14 deletions src/bottom-navigation/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { TabStrip } from '@nativescript-community/ui-material-core/tab-navigatio
import { TabStripItem } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-strip-item';
// Types
// Requires
import { Application, CSSType, Color, Device, Font, Frame, IOSHelper, ImageSource, Utils, View, getIconSpecSize } from '@nativescript/core';
import { CSSType, Color, Device, Font, Frame, IOSHelper, ImageSource, Utils, View, getIconSpecSize } from '@nativescript/core';
import { TextTransform, getTransformedText } from '@nativescript/core/ui/text-base';
import { iOSNativeHelper } from '@nativescript/core/utils';
export { TabContentItem, TabStrip, TabStripItem };

// TODO:
// import { profile } from "../../profiling";
Expand Down Expand Up @@ -79,10 +80,14 @@ class UITabBarControllerImpl extends UITabBarController {

if (majorVersion >= 13) {
const owner = this._owner.get();
if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) {
if (
owner &&
this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection &&
this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)
) {
owner.notify({
eventName: IOSHelper.traitCollectionColorAppearanceChangedEvent,
object: owner,
object: owner
});
}
}
Expand Down Expand Up @@ -125,7 +130,7 @@ class UITabBarControllerDelegateImpl extends NSObject implements UITabBarControl
tabStrip.notify({
eventName: TabStrip.itemTapEvent,
object: tabStrip,
index: position,
index: position
});
}
}
Expand Down Expand Up @@ -230,7 +235,7 @@ function updateTitleAndIconPositions(tabStripItem: TabStripItem, tabBarItem: UIT
if (isIconAboveTitle) {
tabBarItem.titlePositionAdjustment = {
horizontal: 0,
vertical: -20,
vertical: -20
};
} else {
tabBarItem.titlePositionAdjustment = { horizontal: 0, vertical: 0 };
Expand All @@ -243,14 +248,14 @@ function updateTitleAndIconPositions(tabStripItem: TabStripItem, tabBarItem: UIT
top: 6,
left: 0,
bottom: -6,
right: 0,
right: 0
});
} else {
tabBarItem.imageInsets = new UIEdgeInsets({
top: 0,
left: 0,
bottom: 0,
right: 0,
right: 0
});
}
}
Expand Down Expand Up @@ -303,14 +308,19 @@ export class BottomNavigation extends TabNavigationBase {
}

this._ios.delegate = this._delegate;
this.onSelectedIndexChanged(selectedIndex, selectedIndex);
}

public onUnloaded() {
this._ios.delegate = null;
this._ios.moreNavigationController.delegate = null;
super.onUnloaded();
this.items.forEach((item, i) => {
item.unloadView(item.content);
});
}

// @ts-ignore
get ios(): UITabBarController {
return this._ios;
}
Expand All @@ -328,11 +338,12 @@ export class BottomNavigation extends TabNavigationBase {
if (!items) {
return;
}

const oldItem = items[oldIndex];
if (oldItem) {
oldItem.canBeLoaded = false;
oldItem.unloadView(oldItem.content);
if (this.unloadOnTabChange) {
const oldItem = items[oldIndex];
if (oldItem) {
oldItem.canBeLoaded = false;
oldItem.unloadView(oldItem.content);
}
}

const newItem = items[newIndex];
Expand Down Expand Up @@ -704,7 +715,7 @@ export class BottomNavigation extends TabNavigationBase {

const iconSpecSize = getIconSpecSize({
width: inWidth,
height: inHeight,
height: inHeight
});

const widthPts = iconSpecSize.width;
Expand Down Expand Up @@ -743,7 +754,6 @@ export class BottomNavigation extends TabNavigationBase {
// if (Trace.isEnabled()) {
// Trace.write("TabView._onSelectedIndexPropertyChangedSetNativeValue(" + value + ")", Trace.categories.Debug);
// }

if (value > -1) {
(this._ios as any)._willSelectViewController = this._ios.viewControllers[value];
this._ios.selectedIndex = value;
Expand Down
28 changes: 28 additions & 0 deletions src/bottom-navigation/vue/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default {
model: {
prop: 'selectedIndex',
event: 'selectedIndexChange'
},

render(h) {
return h(
'NativeMDBottomNavigation',
{
on: this.$listeners,
attrs: this.$attrs
},
this.$slots.default
);
},

methods: {
registerTabStrip(tabStrip) {
this.$el.setAttribute('tabStrip', tabStrip);
},
registerTabContentItem(tabContentItem) {
const items = this.$el.nativeView.items || [];

this.$el.setAttribute('items', items.concat([tabContentItem]));
}
}
};
21 changes: 21 additions & 0 deletions src/bottom-navigation/vue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BottomNavigation, TabContentItem, TabStrip, TabStripItem } from '../';

let installed = false;

export default {
install(Vue) {
if (!installed) {
installed = true;
Vue.registerElement('MDBottomNavigation', () => BottomNavigation, {
model: {
prop: 'selectedIndex',
event: 'selectedIndexChange'
},
component: require('./component').default
});
Vue.registerElement('MDTabContentItem', () => TabContentItem, {});
Vue.registerElement('MDTabStripItem', () => TabStripItem, {});
Vue.registerElement('MDTabStrip', () => TabStrip, {});
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TabNavigationBase } from '../tab-navigation-base';

export const traceCategory = 'TabView';

@CSSType('TabContentItem')
@CSSType('MDTabContentItem')
export abstract class TabContentItemBase extends ContentView implements TabContentItemDefinition, AddChildFromBuilder {
public eachChild(callback: (child: View) => boolean) {
if (this.content) {
Expand Down
Loading

0 comments on commit 89fb903

Please sign in to comment.