npm package: @i4mi/ionic-storage-service
Description: IONIC service for native and secure storage access
Author: Stefan Iseli @ Institute for Medical Informatics
Latest version: 0.0.3

# Ionic-Storage-Service
# USE AT OWN RISK - WIP - 

Service lib to easily implement a storage service for your ionic project. Just install the lib and requeired plugins, import it and you are ready to go :)

# Usage
## 1. Install package
```shell
npm i @i4mi/ionic-storage-service
```

## 2. Install dependencies
```shell
ionic cordova plugin add secure-storage-echo
ionic cordova plugin add nativestorage
ionic cordova plugin add cordova-open-native-settings
```

## 3. Add as provider
Add to your `app.module.ts`:
```ts
import { IonicStorageService } from '@i4mi/ionic-storage-service';

@NgModule({
  ...
  providers: [
    ...
    IonicStorageService,
    ...
  ],
  ...
})
```

## 4. Call init on app startup

Normally just call the init:

```ts
import { IonicStorageService } from '@i4mi/ionic-storage-service';
@Component({
    templateUrl: 'sample.html',
    styleUrls: ['sample.scss']
})
export class SamplePage {

    constructor(private _storageService: IonicStorageService) {}

    // Using lifecycle event to be sure page is loaded before the magic happens :)
    ionViewDidEnter() {
        this._storageService.init(
            'NAME',
            ['SECURE_KEY1', 'SECURE_KEY2'],
            ['NATIVE_KEY1', 'NATIVE_KEY2'], )
            .then(() => { 
                console.log('yeyy :)');
            }).catch((error) => {
                console.log(error);
            });
    }
}
```

If you want to open the native settings when a user has no secured device, call the open screen lock settings function

```ts
    ionViewDidEnter() {
        this._storageService.init(
            'NAME',
            ['SECURE_KEY1', 'SECURE_KEY2'],
            ['NATIVE_KEY1', 'NATIVE_KEY2'], )
            .then(() => { 
                console.log('yeyy :)');
            }).catch((error) => {
                // NOW CALLING OPEN SCREEN LOCK
                this._storageService.openScreenLockSettings(() => {
                    // HERE YOU CAN USE A HANDLER FUNCTION
                    // THIS DOES ONLY WORK ON ANDROÌD
                }
                console.log(error);
            });
    }
```

## 5. Using the functions

### save
You can save an object or a value with the save method:

```ts
    awesomeSave() {
        const emptyObjectToSave = {};
        // SAVING TO SECURE STORAGE
        // 1: When key is was in init array
        this._storageService.save('SECURE_KEY1', emptyObjectToSave).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
        // 2: When key was NOT in init array
        this._storageService.save('SOME_OTHER_SECURE_KEY', emptyObjectToSave, true).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });

        // SAVING TO NATIVE STORAGE
        // 1. When key was in init array
        this._storageService.save('NATIVE_KEY1', emptyObjectToSave).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
        // 2. When key was NOT in init, DO THE SAME AS ABOVE OR...
        this._storageService.save('SOME_OTHER_NATIVE_KEY', emptyObjectToSave, false).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
    }
```

### get
Get the object or value stored in storage:

```ts
    awesomeGet() {
        // GET FROM SECURE STORAGE
        // 1: When key is was in init array
        this._storageService.get('SECURE_KEY1').then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
        // 2: When key was NOT in init array
        this._storageService.get('SOME_OTHER_SECURE_KEY', true).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });

        // SAVING TO NATIVE STORAGE
        // 1. When key was in init array
        this._storageService.get('NATIVE_KEY1').then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
        // 2. When key was NOT in init, DO THE SAME AS ABOVE OR...
        this._storageService.get('SOME_OTHER_NATIVE_KEY', false).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
    }
```

### remove
Remove an object or value with given key

```ts
    awesomeRemove() {
        // GET FROM SECURE STORAGE
        // 1: When key is was in init array
        this._storageService.remove('SECURE_KEY1').then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
        // 2: When key was NOT in init array
        this._storageService.remove('SOME_OTHER_SECURE_KEY', true).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });

        // SAVING TO NATIVE STORAGE
        // 1. When key was in init array
        this._storageService.remove('NATIVE_KEY1').then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
        // 2. When key was NOT in init, DO THE SAME AS ABOVE OR...
        this._storageService.remove('SOME_OTHER_NATIVE_KEY', false).then((obj) => {
            console.log(obj);
        }).catch((error) => {
            console.log(error);
        });
    }
```

### clear
Cleans both storages:

```ts
    awesomeClear() {
       this._storageService.clear().then(() => {
           // do your stuff
        }).catch((error) => {
            console.log(error);
        }); 
    }
```

### keys
Returns the keys of all saved objects or values of both storages:

```ts
    awesomeKeys() {
       this._storageService.keys().then((keys: Array<string>) => {
            console.log(keys);
        }).catch((error) => {
            console.log(error);
        }); 
    }
```