How to use tns-core-modules - 10 common examples

To help you get started, we’ve selected a few tns-core-modules examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NativeScript / NativeScript / tests / app / application / application-tests-common.ts View on Github external
// >> application-require
import * as app from "tns-core-modules/application";
import * as platform from "tns-core-modules/platform";
// << application-require

// >> application-app-check
if (app.android) {
    console.log("We are running on Android device!");
} else if (app.ios) {
    console.log("We are running on iOS device");
}
// << application-app-check

import * as TKUnit from "../TKUnit";

export var testInitialized = function () {
    if (platform.device.os === platform.platformNames.android) {
        // we have the android defined
        TKUnit.assert(app.android, "Application module not properly intialized");
    } else if (platform.device.os === platform.platformNames.ios) {
        TKUnit.assert(app.ios, "Application module not properly intialized");
    }
}
github NativeScript / NativeScript / apps / app / ui-tests-app / app.ts View on Github external
if (error.nativeError) {
        console.warn("native error: " + error.nativeError);
    }
});

application.on(application.launchEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android.content.Intent class.
        console.log("### Launched application with: " + args.android + ".");
    } else if (args.ios !== undefined) {
        // For iOS applications, args.ios is NSDictionary (launchOptions).
        console.log("### Launched application with: " + args.ios);
    }
});

application.on(application.suspendEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android activity class.
        console.log("#" + ++countSuspend + "# SuspendEvent Activity: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is UIApplication.
        console.log("#" + ++countSuspend + "# SuspendEvent UIApplication: " + args.ios);
    }
});

application.on(application.resumeEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android activity class.
        console.log("#" + ++countResume + "# ResumeEvent Activity: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is UIApplication.
        console.log("#" + ++countResume + "# ResumeEvent UIApplication: " + args.ios);
github NativeScript / NativeScript / apps / app / ui-tests-app / app.ts View on Github external
// For iOS applications, args.ios is UIApplication.
        console.log("#" + ++countSuspend + "# SuspendEvent UIApplication: " + args.ios);
    }
});

application.on(application.resumeEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android activity class.
        console.log("#" + ++countResume + "# ResumeEvent Activity: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is UIApplication.
        console.log("#" + ++countResume + "# ResumeEvent UIApplication: " + args.ios);
    }
});

application.on(application.exitEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android activity class.
        console.log("### ExitEvent Activity: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is UIApplication.
        console.log("### ExitEvent UIApplication: " + args.ios);
    }
});

application.on(application.lowMemoryEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android activity class.
        console.log("### LowMemoryEvent Activity: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is UIApplication.
        console.log("### LowMemoryEvent UIApplication: " + args.ios);
github NativeScript / NativeScript / tests / app / ui / image-cache / image-cache-tests.ts View on Github external
export const test_ImageCache_NothingAtProvidedUrl = function () {
    const cache = new imageCacheModule.Cache();
    cache.maxRequests = 5;

    let errorCaught = false;
    let errorMessage: string;

    let imgSource: imageSource.ImageSource;
    const url = "https://github.com/NativeScript-NoImage.png";
    // Try to read the image from the cache
    const image = cache.get(url);
    if (image) {
        // If present -- use it.
        imgSource = imageSource.fromNativeSource(image);
    }
    else {
        // If not present -- request its download.
        cache.push({
            key: url,
            url: url,
            completed: (image: any, key: string) => {
                if (url === key) {
                    imgSource = imageSource.fromNativeSource(image);
                }
            },
            error: (key: string) => {
                console.log("No image for key: ", key);
                errorMessage = `No image for key: ${key}`;
                errorCaught = true;
            }
github NativeScript / NativeScript / tests / app / application / application-tests.ios.ts View on Github external
//// Add custom application delegate
if (app.ios) {
    class MyDelegate extends UIResponder implements UIApplicationDelegate {
        public static ObjCProtocols = [UIApplicationDelegate];

        applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: NSDictionary): boolean {
            return true;
        }

        applicationDidBecomeActive(application: UIApplication): void {
            // Get reference to the application window.
            //console.log("keyWindow: " + application.keyWindow);
        }
    }

    app.ios.delegate = MyDelegate;
}

// << application-ios-delegate

export function testIOSApplicationInitialized() {
    TKUnit.assert(app.ios, "iOS application not initialized.");
    TKUnit.assert(app.ios.delegate, "iOS delegate not initialized.");
    TKUnit.assert(app.ios.nativeApp, "iOS nativeApp not initialized.");
    TKUnit.assert(app.ios.orientation, "iOS orientation not initialized.");
    TKUnit.assert(app.ios.window, "iOS window not initialized.");
    TKUnit.assert(app.ios.rootController, "iOS root controller not initialized.");
}
github NativeScript / NativeScript / tests / app / data / observable-array-tests.ts View on Github external
export const test_ObservableArray_sortShouldReturnNewSortedArray = function () {
    // >> observable-array-sort
    const array = new ObservableArray([3, 2, 1]);
    const result = array.sort();
    // << observable-array-sort
    TKUnit.assert(result[0] === 1 && result.length === 3, "ObservableArray sort() should return new sorted array!");
};
github NativeScript / NativeScript / tests / app / data / observable-array-tests.ts View on Github external
export const test_ObservableArray_sliceWithParamsShouldReturnSectionAsNewArray = function () {
    // >> observable-array-slice-args
    const array = new ObservableArray([1, 2, 3, 4, 5]);
    const result = array.slice(2, 4);
    // << observable-array-slice-args
    TKUnit.assert(result[1] === 4 && result.length === 2, "ObservableArray slice() should return section according to specified arguments!");
};
github NativeScript / NativeScript / tests / app / data / observable-array-tests.ts View on Github external
export const test_ObservableArray_indexOfShouldReturnCorrectIndex = function () {
    // >> observable-array-indexof
    const array = new ObservableArray(["one", "two", "three"]);
    const result = array.indexOf("two");
    // << observable-array-indexof
    TKUnit.assert(result === 1, "ObservableArray indexOf() should return correct index!");
};
github NativeScript / NativeScript / tests / app / data / observable-array-tests.ts View on Github external
export const test_ObservableArray_unshiftShouldInsertNewElementsFromTheStartAndRaiseChangeEventWithCorrectArgs = function () {
    let result: ChangedData;

    // >> observable-array-unshift-change
    const array = new ObservableArray([1, 2, 3]);
    array.on(ObservableArray.changeEvent, (args: ChangedData) => {
        //// Argument (args) is ChangedData.
        //// args.eventName is "change".
        //// args.action is "add".
        //// args.index is 0.
        //// args.removed.length is 0.
        //// args.addedCount is equal to the number of inserted items.

        // >> (hide)
        result = args;
        // << (hide)
    });

    array.unshift(4, 5);
    // << observable-array-unshift-change
github NativeScript / NativeScript / tests / app / data / observable-array-tests.ts View on Github external
export const test_ObservableArray_spliceShouldInsertNewItemsInPlaceOfRemovedItemsStartingFromSpecifiedIndex = function () {
    // >> observable-array-splice-args
    const array = new ObservableArray(["one", "two", "three"]);
    const result = array.splice(1, 2, "six", "seven");
    // << observable-array-splice-args
    TKUnit.assert(result.length === 2 && result[0] === "two" && array.length === 3 && array.getItem(2) === "seven",
        "ObservableArray splice() should insert new items in place of removed!");
};