mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Fix ios placeholder implementation (#4067)
* Fix https://github.com/NativeScript/NativeScript/issues/4043 * Merge ios & android implementation
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import * as TKUnit from "../../TKUnit";
|
import * as TKUnit from "../../TKUnit";
|
||||||
// >> article-creating-view
|
// >> article-creating-view
|
||||||
import * as platform from "tns-core-modules/platform";
|
import { isIOS, isAndroid } from "tns-core-modules/platform";
|
||||||
//var utils = require("utils/utils");
|
|
||||||
import * as utils from "tns-core-modules/utils/utils";
|
import * as utils from "tns-core-modules/utils/utils";
|
||||||
import * as helper from "../helper";
|
import * as helper from "../helper";
|
||||||
import * as viewModule from "tns-core-modules/ui/core/view";
|
import * as viewModule from "tns-core-modules/ui/core/view";
|
||||||
@@ -11,56 +10,57 @@ import * as placeholderModule from "tns-core-modules/ui/placeholder";
|
|||||||
// << article-require-placeholder-module
|
// << article-require-placeholder-module
|
||||||
|
|
||||||
function creatingView(args) {
|
function creatingView(args) {
|
||||||
var nativeView;
|
let nativeView;
|
||||||
if (platform.device.os === platform.platformNames.ios) {
|
if (isIOS) {
|
||||||
nativeView = UITextView.new();
|
nativeView = UITextView.new();
|
||||||
nativeView.text = "Native";
|
nativeView.text = "Native";
|
||||||
} else if (platform.device.os === platform.platformNames.android) {
|
} else if (isAndroid) {
|
||||||
nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
|
nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
|
||||||
nativeView.setText("Native");
|
nativeView.setText("Native");
|
||||||
}
|
}
|
||||||
|
|
||||||
args.view = nativeView;
|
args.view = nativeView;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.creatingView = creatingView;
|
exports.creatingView = creatingView;
|
||||||
// << article-creating-view
|
// << article-creating-view
|
||||||
|
|
||||||
export function test_placeholder_creatingView() {
|
export function test_placeholder_creatingView() {
|
||||||
var nativeView;
|
const p = new placeholderModule.Placeholder();
|
||||||
|
|
||||||
var p = new placeholderModule.Placeholder();
|
|
||||||
p.id = "test";
|
|
||||||
p.on(placeholderModule.Placeholder.creatingViewEvent, (args: placeholderModule.CreateViewEventData) => {
|
p.on(placeholderModule.Placeholder.creatingViewEvent, (args: placeholderModule.CreateViewEventData) => {
|
||||||
if (platform.device.os === platform.platformNames.ios) {
|
let nativeView;
|
||||||
nativeView = UITextView.new();
|
if (isIOS) {
|
||||||
|
nativeView = UITextView.new();
|
||||||
nativeView.text = "Native";
|
nativeView.text = "Native";
|
||||||
} else if (platform.device.os === platform.platformNames.android) {
|
} else if (isAndroid) {
|
||||||
nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
|
nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
|
||||||
nativeView.setText("Native");
|
nativeView.setText("Native");
|
||||||
}
|
}
|
||||||
|
|
||||||
args.view = nativeView;
|
args.view = nativeView;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (platform.device.os === platform.platformNames.ios) {
|
|
||||||
TKUnit.assert(p.ios instanceof UITextView, "ios property should be UITextView. Current value: " + p.ios);
|
|
||||||
} else if (platform.device.os === platform.platformNames.android) {
|
|
||||||
p._emit("creatingView");
|
|
||||||
TKUnit.assert(nativeView instanceof android.widget.TextView, "Native view should be android.widget.TextView. Current value: " + nativeView);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function test_placeholder_will_not_crash_wihout_creatingView() {
|
|
||||||
var p = new placeholderModule.Placeholder();
|
|
||||||
|
|
||||||
function testAction(views: Array<viewModule.View>) {
|
function testAction(views: Array<viewModule.View>) {
|
||||||
if (platform.device.os === platform.platformNames.ios) {
|
if (isIOS) {
|
||||||
TKUnit.assert(p.ios === undefined, "ios property should be undefined. Current value: " + p.ios);
|
TKUnit.assert(p.nativeView instanceof UITextView, "nativeView property should be UITextView. Current value: " + p.nativeView);
|
||||||
} else if (platform.device.os === platform.platformNames.android) {
|
} else if (isAndroid) {
|
||||||
TKUnit.assert(p.android === undefined, "android view should be undefined. Current value: " + p.android);
|
TKUnit.assert(p.nativeView instanceof android.widget.TextView, "Native view should be android.widget.TextView. Current value: " + p.nativeView);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
helper.buildUIAndRunTest(p, testAction);
|
helper.buildUIAndRunTest(p, testAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function test_placeholder_will_not_crash_wihout_creatingView() {
|
||||||
|
const p = new placeholderModule.Placeholder();
|
||||||
|
|
||||||
|
function testAction(views: Array<viewModule.View>) {
|
||||||
|
if (isIOS) {
|
||||||
|
TKUnit.assert(p.ios === undefined, "ios property should be undefined. Current value: " + p.ios);
|
||||||
|
} else if (isAndroid) {
|
||||||
|
TKUnit.assert(p.android === undefined, "android view should be undefined. Current value: " + p.android);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(p, testAction);
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ export class Placeholder extends View implements PlaceholderDefinition {
|
|||||||
public static creatingViewEvent = "creatingView";
|
public static creatingViewEvent = "creatingView";
|
||||||
|
|
||||||
public createNativeView() {
|
public createNativeView() {
|
||||||
let args = <CreateViewEventData>{ eventName: Placeholder.creatingViewEvent, object: this, view: undefined, context: this._context };
|
const args = <CreateViewEventData>{ eventName: Placeholder.creatingViewEvent, object: this, view: undefined, context: this._context };
|
||||||
this.notify(args);
|
this.notify(args);
|
||||||
return <android.view.View>args.view;
|
return <android.view.View>args.view;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
import { Placeholder as PlaceholderDefinition, CreateViewEventData } from "."
|
|
||||||
import { View } from "../core/view"
|
|
||||||
|
|
||||||
export class Placeholder extends View implements PlaceholderDefinition {
|
|
||||||
public static creatingViewEvent = "creatingView";
|
|
||||||
|
|
||||||
private _ios: UIView;
|
|
||||||
|
|
||||||
get ios(): UIView {
|
|
||||||
if (!this._ios) {
|
|
||||||
var args = <CreateViewEventData>{ eventName: Placeholder.creatingViewEvent, object: this, view: undefined, context: undefined };
|
|
||||||
super.notify(args);
|
|
||||||
this.nativeView = this._ios = args.view;
|
|
||||||
}
|
|
||||||
return this._ios;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
12
tns-core-modules/ui/placeholder/placeholder.ts
Normal file
12
tns-core-modules/ui/placeholder/placeholder.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { Placeholder as PlaceholderDefinition, CreateViewEventData } from "."
|
||||||
|
import { View } from "../core/view"
|
||||||
|
|
||||||
|
export class Placeholder extends View implements PlaceholderDefinition {
|
||||||
|
public static creatingViewEvent = "creatingView";
|
||||||
|
|
||||||
|
public createNativeView() {
|
||||||
|
const args = <CreateViewEventData>{ eventName: Placeholder.creatingViewEvent, object: this, view: undefined, context: this._context };
|
||||||
|
this.notify(args);
|
||||||
|
return args.view;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user