Apply getter check to determine if API is exposed as property or as method for iOS backward compatibility

This commit is contained in:
Panayot Cankov
2016-09-12 12:54:38 +03:00
parent e9c7c5a749
commit b81c034f67
29 changed files with 174 additions and 113 deletions

View File

@@ -154,6 +154,13 @@
* Module with ios specific utilities.
*/
module ios {
/**
* Checks if the property is a function and if it is, calls it on this.
* Designed to support backward compatibility for methods that became properties.
* Will not work on delegates since it checks if the propertyValue is a function, and delegates are marshalled as functions.
* Example: getter(NSRunLoop, NSRunLoop.currentRunLoop).runUntilDate(NSDate.dateWithTimeIntervalSinceNow(waitTime));
*/
export function getter<T>(_this: any, propertyValue: T | {(): T}): T;
export function getTransformedText(view, source: string, transform: string): string;
export function setWhiteSpace(view, value: string, parentView?: any);
export function setTextAlignment(view, value: string);

View File

@@ -56,6 +56,14 @@ export module ios {
}
}
export function getter<T>(_this: any, property: T | {(): T}): T {
if (typeof property === "function") {
return (<{(): T}>property).call(_this);
} else {
return <T>property;
}
}
export function getTransformedText(view, source: string, transform: string): string {
let result = source;
@@ -131,13 +139,13 @@ export module ios {
}
export function isLandscape(): boolean {
var device = UIDevice.currentDevice();
var statusBarOrientation = UIApplication.sharedApplication().statusBarOrientation;
var device = getter(UIDevice, UIDevice.currentDevice);
var statusBarOrientation = getter(UIApplication, UIApplication.sharedApplication).statusBarOrientation;
var isStatusBarOrientationLandscape = isOrientationLandscape(statusBarOrientation);
return isOrientationLandscape(device.orientation) || isStatusBarOrientationLandscape;
}
export var MajorVersion = NSString.stringWithString(UIDevice.currentDevice().systemVersion).intValue;
export var MajorVersion = NSString.stringWithString(getter(UIDevice, UIDevice.currentDevice).systemVersion).intValue;
export function openFile(filePath: string): boolean {
try {
@@ -163,8 +171,8 @@ export function GC() {
export function openUrl(location: string): boolean {
try {
var url = NSURL.URLWithString(location.trim());
if (UIApplication.sharedApplication().canOpenURL(url)) {
return UIApplication.sharedApplication().openURL(url);
if (ios.getter(UIApplication, UIApplication.sharedApplication).canOpenURL(url)) {
return ios.getter(UIApplication, UIApplication.sharedApplication).openURL(url);
}
}
catch (e) {