clear nativeView field. (#3418)

moved some native setters from TabView to TabViewItem.
shorthand properties converters now accounts for unsetValue.
better formatting for test duration.
This commit is contained in:
Hristo Hristov
2017-01-05 18:08:42 +02:00
committed by GitHub
parent ea92b12419
commit bfab188ba0
8 changed files with 226 additions and 209 deletions

View File

@ -49,7 +49,7 @@ export var write = function write(message: string, type?: number) {
var runTest = function (testInfo: TestInfoEntry) { var runTest = function (testInfo: TestInfoEntry) {
let start = time(); let start = time();
let duration; let duration: number;
try { try {
if (testInfo.instance) { if (testInfo.instance) {
testInfo.testFunc.apply(testInfo.instance); testInfo.testFunc.apply(testInfo.instance);
@ -59,7 +59,7 @@ var runTest = function (testInfo: TestInfoEntry) {
} }
if (testInfo.isTest) { if (testInfo.isTest) {
duration = time() - start; duration = (time() - start).toFixed(2);
testInfo.duration = duration; testInfo.duration = duration;
write(`--- [${testInfo.testName}] OK, duration: ${duration}`, trace.messageType.info); write(`--- [${testInfo.testName}] OK, duration: ${duration}`, trace.messageType.info);
testInfo.isPassed = true; testInfo.isPassed = true;
@ -67,7 +67,7 @@ var runTest = function (testInfo: TestInfoEntry) {
} }
catch (e) { catch (e) {
if (testInfo.isTest) { if (testInfo.isTest) {
duration = time() - start; duration = (time() - start).toFixed(2);
testInfo.duration = duration; testInfo.duration = duration;
write(`--- [${testInfo.testName}] FAILED: ${e.message}, Stack: ${e.stack}, duration: ${duration}`, trace.messageType.error); write(`--- [${testInfo.testName}] FAILED: ${e.message}, Stack: ${e.stack}, duration: ${duration}`, trace.messageType.error);
testInfo.isPassed = false; testInfo.isPassed = false;

View File

@ -210,7 +210,7 @@ function startLog(): void {
function log(): void { function log(): void {
let testsName: string = this.name; let testsName: string = this.name;
let duration = TKUnit.time() - this.start; let duration = TKUnit.time() - this.start;
TKUnit.write(testsName + " COMPLETED for " + duration + " BACKSTACK DEPTH: " + topmost().backStack.length, messageType.info); TKUnit.write(testsName + " COMPLETED for " + duration.toFixed(2) + " BACKSTACK DEPTH: " + topmost().backStack.length, messageType.info);
} }
let testsSelector: string let testsSelector: string
@ -243,7 +243,7 @@ export var runAll = function (testSelector?: string) {
} }
} }
console.log("TESTS: " + singleModuleName + " " + singleTestName); console.log("TESTS: " + singleModuleName ? singleModuleName : "" + " " + singleTestName ? singleTestName : "");
var totalSuccess = 0; var totalSuccess = 0;
var totalFailed: Array<TKUnit.TestFailure> = []; var totalFailed: Array<TKUnit.TestFailure> = [];

View File

@ -492,7 +492,9 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
this.parent._addViewToNativeVisualTree(this, atIndex); this.parent._addViewToNativeVisualTree(this, atIndex);
} }
if (this.nativeView) {
applyNativeSetters(this); applyNativeSetters(this);
}
this.eachChild((child) => { this.eachChild((child) => {
child._setupUI(context); child._setupUI(context);
@ -510,8 +512,10 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
return true; return true;
}); });
if (this.nativeView) {
// TODO: rename and implement this as resetNativeSetters // TODO: rename and implement this as resetNativeSetters
resetStyleProperties(this.style); resetStyleProperties(this.style);
}
if (this.parent) { if (this.parent) {
this.parent._removeViewFromNativeVisualTree(this); this.parent._removeViewFromNativeVisualTree(this);

View File

@ -4,7 +4,7 @@ import { Animation, AnimationPromise } from "ui/animation";
import { Source } from "utils/debug"; import { Source } from "utils/debug";
import { Background } from "ui/styling/background"; import { Background } from "ui/styling/background";
import { import {
ViewBase, getEventOrGestureName, EventData, Style, ViewBase, getEventOrGestureName, EventData, Style, unsetValue,
Property, CssProperty, ShorthandProperty, InheritedCssProperty, Property, CssProperty, ShorthandProperty, InheritedCssProperty,
gestureFromString, isIOS, traceEnabled, traceWrite, traceCategories, printUnregisteredProperties, makeParser, makeValidator gestureFromString, isIOS, traceEnabled, traceWrite, traceCategories, printUnregisteredProperties, makeParser, makeValidator
} from "./view-base"; } from "./view-base";
@ -1406,10 +1406,10 @@ function transformConverter(value: string): Object {
} }
function convertToTransform(value: string): [CssProperty<any, any>, any][] { function convertToTransform(value: string): [CssProperty<any, any>, any][] {
let newTransform = transformConverter(value); let newTransform = value == unsetValue ? { "none": "none" } : transformConverter(value);
let array = []; let array = [];
let values: Array<string>; let values: Array<string>;
for (var transform in newTransform) { for (let transform in newTransform) {
switch (transform) { switch (transform) {
case "scaleX": case "scaleX":
array.push([scaleXProperty, parseFloat(newTransform[transform])]); array.push([scaleXProperty, parseFloat(newTransform[transform])]);
@ -1950,6 +1950,14 @@ const fontProperty = new ShorthandProperty<Style, string>({
return `${this.fontStyle} ${this.fontWeight} ${this.fontSize} ${this.fontFamily}`; return `${this.fontStyle} ${this.fontWeight} ${this.fontSize} ${this.fontFamily}`;
}, },
converter: function (value) { converter: function (value) {
if (value == unsetValue) {
return [
[fontStyleProperty, unsetValue],
[fontWeightProperty, unsetValue],
[fontSizeProperty, unsetValue],
[fontFamilyProperty, unsetValue]
];
} else {
let font = parseFont(value); let font = parseFont(value);
let fontSize = fontSizeConverter(font.fontSize); let fontSize = fontSizeConverter(font.fontSize);
@ -1958,7 +1966,8 @@ const fontProperty = new ShorthandProperty<Style, string>({
[fontWeightProperty, font.fontWeight], [fontWeightProperty, font.fontWeight],
[fontSizeProperty, fontSize], [fontSizeProperty, fontSize],
[fontFamilyProperty, font.fontFamily] [fontFamilyProperty, font.fontFamily]
] ];
}
} }
}) })
fontProperty.register(Style); fontProperty.register(Style);

View File

@ -120,6 +120,7 @@ export class View extends ViewCommon {
} }
this[ANDROID] = undefined; this[ANDROID] = undefined;
this.nativeView = undefined;
} }
get _nativeView(): android.view.View { get _nativeView(): android.view.View {

View File

@ -1,4 +1,4 @@
import { LayoutBase, View, Style, CssProperty, isIOS, ShorthandProperty, makeValidator, makeParser} from "ui/layouts/layout-base"; import { LayoutBase, View, Style, CssProperty, isIOS, ShorthandProperty, makeValidator, makeParser, unsetValue } from "ui/layouts/layout-base";
export * from "ui/layouts/layout-base"; export * from "ui/layouts/layout-base";
@ -316,6 +316,10 @@ const flexFlowProperty = new ShorthandProperty<Style, string>({
}, },
converter: function (value: string) { converter: function (value: string) {
const properties: [CssProperty<any, any>, any][] = []; const properties: [CssProperty<any, any>, any][] = [];
if (value == unsetValue) {
properties.push([flexDirectionProperty, value]);
properties.push([flexWrapProperty, value]);
} else {
const trimmed = value && value.trim(); const trimmed = value && value.trim();
if (trimmed) { if (trimmed) {
const values = trimmed.split(/\s+/); const values = trimmed.split(/\s+/);
@ -326,7 +330,8 @@ const flexFlowProperty = new ShorthandProperty<Style, string>({
properties.push([flexWrapProperty, FlexWrap.parse(values[1])]); properties.push([flexWrapProperty, FlexWrap.parse(values[1])]);
} }
} }
return properties;; }
return properties;
} }
}) })
flexFlowProperty.register(Style); flexFlowProperty.register(Style);
@ -339,6 +344,10 @@ const flexProperty = new ShorthandProperty<Style, string>({
}, },
converter: function (value: string) { converter: function (value: string) {
const properties: [CssProperty<any, any>, any][] = []; const properties: [CssProperty<any, any>, any][] = [];
if (value == unsetValue) {
properties.push([flexGrowProperty, value]);
properties.push([flexShrinkProperty, value]);
} else {
const trimmed = value && value.trim(); const trimmed = value && value.trim();
if (trimmed) { if (trimmed) {
const values = trimmed.split(/\s+/); const values = trimmed.split(/\s+/);
@ -377,6 +386,7 @@ const flexProperty = new ShorthandProperty<Style, string>({
// properties.push({ property: flexBasisProperty, value: FlexBasis.parse(values[2])}) // properties.push({ property: flexBasisProperty, value: FlexBasis.parse(values[2])})
// } // }
} }
}
return properties; return properties;
} }

View File

@ -50,11 +50,13 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
public setNativeView(textView: android.widget.TextView): void { public setNativeView(textView: android.widget.TextView): void {
this._textView = textView; this._textView = textView;
if (textView) {
applyNativeSetters(this); applyNativeSetters(this);
if (this.titleDirty) { if (this.titleDirty) {
this._update(); this._update();
} }
} }
}
private titleDirty: boolean; private titleDirty: boolean;
public _update(): void { public _update(): void {
@ -67,7 +69,7 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
// titleTextView.setText(this.title || ""); // titleTextView.setText(this.title || "");
// } // }
let tv = this._textView; const tv = this._textView;
if (tv) { if (tv) {
let title = this.title; let title = this.title;
title = (title === null || title === undefined) ? "" : title; title = (title === null || title === undefined) ? "" : title;

View File

@ -2,7 +2,8 @@ import {
TabViewBase, TabViewItemBase, itemsProperty, selectedIndexProperty, TabViewBase, TabViewItemBase, itemsProperty, selectedIndexProperty,
tabTextColorProperty, tabBackgroundColorProperty, selectedTabTextColorProperty, tabTextColorProperty, tabBackgroundColorProperty, selectedTabTextColorProperty,
androidSelectedTabHighlightColorProperty, androidOffscreenTabLimitProperty, androidSelectedTabHighlightColorProperty, androidOffscreenTabLimitProperty,
fontInternalProperty, traceCategory, View, layout, Color, Font, traceEnabled, traceWrite fontInternalProperty, traceCategory, View, layout, Color, Font, traceEnabled, traceWrite,
applyNativeSetters
} from "./tab-view-common" } from "./tab-view-common"
import { textTransformProperty, TextTransform, getTransformedText } from "ui/text-base"; import { textTransformProperty, TextTransform, getTransformedText } from "ui/text-base";
import { fromFileOrResource } from "image-source"; import { fromFileOrResource } from "image-source";
@ -16,11 +17,74 @@ const PRIMARY_COLOR = "colorPrimary";
const DEFAULT_ELEVATION = 4; const DEFAULT_ELEVATION = 4;
export class TabViewItem extends TabViewItemBase { export class TabViewItem extends TabViewItemBase {
public nativeView: android.widget.TextView;
public tabItemSpec: org.nativescript.widgets.TabItemSpec;
public index: number;
public _update() { public setNativeView(textView: android.widget.TextView): void {
const parent = <TabView>this.parent; this.nativeView = textView;
if (parent) { if (textView) {
parent._updateTabForItem(this); applyNativeSetters(this);
}
}
public _update(): void {
const tv = this.nativeView;
if (tv) {
const tabLayout = <org.nativescript.widgets.TabLayout>tv.getParent();
tabLayout.updateItemAt(this.index, this.tabItemSpec);
}
}
get [fontInternalProperty.native](): { typeface: android.graphics.Typeface, fontSize: number } {
const tv = this.nativeView;
return {
typeface: tv.getTypeface(),
fontSize: tv.getTextSize()
};
}
set [fontInternalProperty.native](value: Font | { typeface: android.graphics.Typeface, fontSize: number }) {
let typeface: android.graphics.Typeface;
let isFont: boolean;
const fontSize = value.fontSize;
if (value instanceof Font) {
isFont = true;
typeface = value.getAndroidTypeface();
}
else {
typeface = value.typeface;
}
const tv = this.nativeView;
tv.setTypeface(typeface);
if (isFont) {
if (fontSize !== undefined) {
tv.setTextSize(fontSize);
}
}
else {
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, fontSize);
}
}
get [textTransformProperty.native](): TextTransform {
return "none";
}
set [textTransformProperty.native](value: TextTransform) {
const tv = this.nativeView;
const result = getTransformedText(this.title, value);
tv.setText(result);
}
get [tabTextColorProperty.native](): android.content.res.ColorStateList {
return this.nativeView.getTextColors();
}
set [tabTextColorProperty.native](value: android.content.res.ColorStateList | Color) {
if (value instanceof Color) {
this.nativeView.setTextColor(value.android);
} else {
this.nativeView.setTextColor(value);
} }
} }
} }
@ -171,6 +235,25 @@ function ensurePageChangedListenerClass() {
PageChangedListenerClass = PageChangedListener; PageChangedListenerClass = PageChangedListener;
} }
function createTabItemSpec(item: TabViewItem): org.nativescript.widgets.TabItemSpec {
let result = new org.nativescript.widgets.TabItemSpec();
result.title = item.title;
if (item.iconSource) {
if (item.iconSource.indexOf(RESOURCE_PREFIX) === 0) {
result.iconId = ad.resources.getDrawableId(item.iconSource.substr(RESOURCE_PREFIX.length));
} else {
let is = fromFileOrResource(item.iconSource);
if (is) {
// TODO: Make this native call that accepts string so that we don't load Bitmap in JS.
result.iconDrawable = new android.graphics.drawable.BitmapDrawable(is.android);
}
}
}
return result;
}
export class TabView extends TabViewBase { export class TabView extends TabViewBase {
private _grid: org.nativescript.widgets.GridLayout; private _grid: org.nativescript.widgets.GridLayout;
private _tabLayout: org.nativescript.widgets.TabLayout; private _tabLayout: org.nativescript.widgets.TabLayout;
@ -184,6 +267,18 @@ export class TabView extends TabViewBase {
return this._grid; return this._grid;
} }
public onItemsChanged(oldItems: TabViewItem[], newItems: TabViewItem[]): void {
super.onItemsChanged(oldItems, newItems);
if (oldItems) {
oldItems.forEach((item: TabViewItem, i, arr) => {
item.index = 0;
item.tabItemSpec = null;
item.setNativeView(null);
});
}
}
public _createNativeView() { public _createNativeView() {
if (traceEnabled) { if (traceEnabled) {
traceWrite("TabView._createUI(" + this + ");", traceCategory); traceWrite("TabView._createUI(" + this + ");", traceCategory);
@ -245,8 +340,11 @@ export class TabView extends TabViewBase {
} }
const tabItems = new Array<org.nativescript.widgets.TabItemSpec>(); const tabItems = new Array<org.nativescript.widgets.TabItemSpec>();
items.forEach((item, idx, arr) => { items.forEach((item, i, arr) => {
tabItems.push(this.createTabItem(item)); const tabItemSpec = createTabItemSpec(item);
item.index = i;
item.tabItemSpec = tabItemSpec;
tabItems.push(tabItemSpec);
}); });
ensurePagerAdapterClass(); ensurePagerAdapterClass();
@ -254,49 +352,17 @@ export class TabView extends TabViewBase {
this._pagerAdapter = new PagerAdapterClass(this, items); this._pagerAdapter = new PagerAdapterClass(this, items);
this._viewPager.setAdapter(this._pagerAdapter); this._viewPager.setAdapter(this._pagerAdapter);
this._tabLayout.setItems(tabItems, this._viewPager); const tabLayout = this._tabLayout;
tabLayout.setItems(tabItems, this._viewPager);
items.forEach((item, i, arr) => {
const tv = tabLayout.getTextViewForItemAt(i);
item.setNativeView(tv);
});
let selectedIndex = this.selectedIndex; let selectedIndex = this.selectedIndex;
if (selectedIndex < 0) { if (selectedIndex < 0) {
this.selectedIndex = this._viewPager.getCurrentItem(); this.selectedIndex = this._viewPager.getCurrentItem();
} }
//<<<<<<< HEAD
//=======
// this._updateSelectedIndexOnItemsPropertyChanged(data.newValue);
// Style properties such as fonts need to re-applied on the newwly created native TextViews
// this.style._syncNativeProperties();
//>>>>>>> ae68368... Fixed: Setting `setTypeface()` to null object
}
public _updateTabForItem(item: TabViewItem) {
let items = this.items;
if (items && items.length > 0) {
let index = this.items.indexOf(item);
if (index >= 0) {
this._tabLayout.updateItemAt(index, this.createTabItem(item));
}
}
}
private createTabItem(item: TabViewItem): org.nativescript.widgets.TabItemSpec {
let result = new org.nativescript.widgets.TabItemSpec();
result.title = item.title;
if (item.iconSource) {
if (item.iconSource.indexOf(RESOURCE_PREFIX) === 0) {
result.iconId = ad.resources.getDrawableId(item.iconSource.substr(RESOURCE_PREFIX.length));
} else {
let is = fromFileOrResource(item.iconSource);
if (is) {
// TODO: Make this native call that accepts string so that we don't load Bitmap in JS.
result.iconDrawable = new android.graphics.drawable.BitmapDrawable(is.android);
}
}
}
return result;
} }
get [androidOffscreenTabLimitProperty.native](): number { get [androidOffscreenTabLimitProperty.native](): number {
@ -316,21 +382,13 @@ export class TabView extends TabViewBase {
this._viewPager.setCurrentItem(value, true); this._viewPager.setCurrentItem(value, true);
} }
get [itemsProperty.native](): TabViewItemBase[] { get [itemsProperty.native](): TabViewItem[] {
return null; return null;
} }
set [itemsProperty.native](value: TabViewItemBase[]) { set [itemsProperty.native](value: TabViewItem[]) {
this.setAdapter(value); this.setAdapter(value);
} }
get [tabTextColorProperty.native](): number {
return this._tabLayout.getTabTextColor();
}
set [tabTextColorProperty.native](value: number | Color) {
let color = value instanceof Color ? value.android : value;
this._tabLayout.setTabTextColor(color);
}
get [tabBackgroundColorProperty.native](): android.graphics.drawable.Drawable { get [tabBackgroundColorProperty.native](): android.graphics.drawable.Drawable {
return this._tabLayout.getBackground(); return this._tabLayout.getBackground();
} }
@ -359,71 +417,4 @@ export class TabView extends TabViewBase {
let color = value instanceof Color ? value.android : value; let color = value instanceof Color ? value.android : value;
tabLayout.setSelectedIndicatorColors([color]); tabLayout.setSelectedIndicatorColors([color]);
} }
// TODO: Move this to TabViewItem
get [fontInternalProperty.native](): { typeface: android.graphics.Typeface, fontSize: number } {
let items = this.items;
if (items && items.length > 0) {
let tabLayout = this._tabLayout;
let tv = tabLayout.getTextViewForItemAt(0);
return {
typeface: tv.getTypeface(),
fontSize: tv.getTextSize()
};
}
return {
typeface: undefined,
fontSize: undefined
};
}
set [fontInternalProperty.native](value: Font | { typeface: android.graphics.Typeface, fontSize: number }) {
let typeface: android.graphics.Typeface;
let fontSize = value.fontSize;
let isFont: boolean;
if (value instanceof Font) {
isFont = true;
typeface = value.getAndroidTypeface();
}
else {
typeface = value.typeface;
}
let items = this.items;
if (items && items.length > 0) {
let tabLayout = this._tabLayout;
for (let i = 0, length = items.length; i < length; i++) {
let tv = tabLayout.getTextViewForItemAt(i);
tv.setTypeface(typeface);
if (isFont) {
if (fontSize !== undefined){
tv.setTextSize(fontSize);
}
}
else {
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, fontSize);
}
}
}
}
// TODO: Move this to TabViewItem
get [textTransformProperty.native](): TextTransform {
return "none";
}
set [textTransformProperty.native](value: TextTransform) {
let tabLayout = this._tabLayout;
let items = this.items;
if (!items) {
return;
}
for (let i = 0, count = items.length; i < count; i++) {
const textView = tabLayout.getTextViewForItemAt(i);
const result = getTransformedText(items[i].title, value);
textView.setText(result);
}
}
} }