Released version 1.33 of nativescript-toolbox

A new version of my NativeScript plugin toolbox is available.

 

What is nativescript-toolbox?

nativescript-toolbox is a collection of useful classes, (shorthand) functions and external sub modules.

What platforms are supported?

  • Android
  • iOS

How can I use it?

Call

tns plugin add nativescript-toolbox

inside your app project to install the plugin.

To use the global functions in your app, insert the following (TypeScript) code:

import Toolbox = require('nativescript-toolbox');

What’s new?

Since the last main release, the following things have been added:

Functions

getClipboard()

Returns an object that handles the clipboard of the device. The code is based on nativescript-clipboard by Eddy Verbruggen.

import Toolbox = require('nativescript-toolbox');
 
var clipboard = Toolbox.getClipboard();
 
clipboard.getText((result) => {
    switch (result.code) {
        case 0:
            console.log('Got text from clipboard: ' + result.value);
            break;
 
        default:
             console.log('Could not GET text (' + result.code + '): ' + result.error);
             break;
    }
});
 
clipboard.setText('Marcel Kloubert', (result) => {
    switch (result.code) {
        case 0:
            console.log('Stored text in clipboard: ' + result.value);
            break;
 
        default:
             console.log('Could not SET text (' + result.code + '): ' + result.error);
             break;
    }
});

getOrientation()

Returns the current screen orientation of the device. The code is based on nativescript-orientation by Nathanael Anderson.

Android example:

import Application = require("application");
import Toolbox = require('nativescript-toolbox');
 
Application.android.onActivityCreated = () => {
    var orientation = Toolbox.getOrientation();
 
    switch (orientation) {
        case Toolbox.DeviceOrientation.Landscape:
            console.log('Device is in landscape mode.');
            break;
 
        case Toolbox.DeviceOrientation.Portrait:
            console.log('Device is in portrait mode.');
            break;
 
        default:
           console.log('Device is in UNKNOWN mode.');
           break;
    }
};

getValue()

Tries to return a value that is stored as JSON string in the application settings.

import Toolbox = require('nativescript-toolbox');
 
interface IUser {
    email: string;
    name: string;
}
 
var lastUser: any = Toolbox.getValue<any>('lastUser', false);
if (false !== lastUser) {
    console.log('Last user: ' + lastUser.name + ' (' + lastUser.email + ')');
}
else {
    console.log('No user found.');
}

hash()

A generic function to hash a value as hex string:

import Toolbox = require('nativescript-toolbox');
 
var md5_Hash = Toolbox.hash('TM', 'md5');
var sha1_Hash = Toolbox.hash('TM', 'sha1');
var sha3_Hash = Toolbox.hash('TM', 'sha3');
var sha256_Hash = Toolbox.hash('TM', 'sha256');
var sha384_Hash = Toolbox.hash('TM', 'sha384');
var sha512_Hash = Toolbox.hash('TM', 'sha512');

hasValue()

Checks if a value is stored in the application settings.

import Toolbox = require('nativescript-toolbox');
 
if (Toolbox.hasValue('lastUser')) {
    console.log('Last user found.');
}
else {
    console.log('NO user found.');
}

invokeForOrientation()

Invokes callbacks for the actual screen orientation of the device.

Android example:

import Application = require("application");
import Toolbox = require('nativescript-toolbox');
 
Application.android.onActivityCreated = () => {
    Toolbox.invokeForOrientation({
        portrait: () => {
            console.log('Device is in portrait mode.');
        },
 
        landscape: () => {
            console.log('Device is in landscape mode.');
        },
 
        unknown: () => {
            console.log('Device is in UNKNOWN mode.');
        },
    });
};

removeValue()

Removes a JSON serialized value from the application settings.

import Toolbox = require('nativescript-toolbox');
 
if (Toolbox.removeValue('lastUser')) {
    console.log('Last user has been removed.');
}
else {
    console.log('NO user was stored in the app settings.');
}

setStatusBarVisibility()

Sets the visibility of the status bar of the device. The code is based on nativescript-status-bar by Peter Staev.

import Toolbox = require('nativescript-toolbox');
 
// hide status bar
Toolbox.setStatusBarVisibility(false);
 
// show status bar
Toolbox.setStatusBarVisibility(true);

setValue()

Saves a value / object as JSON string in the application settings.

import Toolbox = require('nativescript-toolbox');
 
var user = {
    email: 'marcel.kloubert@gmx.net',
    name: 'Marcel Kloubert',
};
 
if (Toolbox.setValue('lastUser', user)) {
    console.log('User has been saved.');
}
else {
    console.log('User could not be saved!');
}

uuid() / guid()

Generates a new UUID / GUID. The code is based on the StackOverflow question Create GUID / UUID in JavaScript?.

GREAT! Is there more?

Of, course! 🙂

Have a look at the README of the project page at GitHub to get an overview about all stuff that is included in the plugin.

Missing things?

If you are missing functions or modules that should be added here, you can open an issue at GitHub (or if you found a bug, of course).

Leave a Reply

Your email address will not be published. Required fields are marked *