Skip to content

Commit b0026e9

Browse files
grigalamadmas
andauthored
feat: Migrate secure-storage plugin (#513)
Co-authored-by: Markus Schlichting <[email protected]>
1 parent 6997ac9 commit b0026e9

28 files changed

+944
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ npm start
3535
- [@nativescript/localize](packages/localize/README.md)
3636
- [@nativescript/pdf](packages/pdf/README.md)
3737
- [@nativescript/picker](packages/picker/README.md)
38+
- [@nativescript/secure-storage](packages/secure-storage/README.md)
3839
- [@nativescript/shared-notification-delegate](packages/shared-notification-delegate/README.md)
3940
- [@nativescript/social-share](packages/social-share/README.md)
4041
- [@nativescript/swift-ui](packages/swift-ui/README.md)

apps/demo-angular/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"@nativescript/swift-ui": "file:../../dist/packages/swift-ui",
3838
"@nativescript/theme-switcher": "file:../../dist/packages/theme-switcher",
3939
"@nativescript/twitter": "file:../../dist/packages/twitter",
40-
"@nativescript/zip": "file:../../dist/packages/zip"
40+
"@nativescript/zip": "file:../../dist/packages/zip",
41+
"@nativescript/secure-storage": "file:../../dist/packages/secure-storage"
4142
},
4243
"devDependencies": {
4344
"@nativescript/android": "~8.5.0",

apps/demo-angular/src/app-routing.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const routes: Routes = [
3737
{ path: 'localize', loadChildren: () => import('./plugin-demos/localize.module').then((m) => m.LocalizeModule) },
3838
{ path: 'pdf', loadChildren: () => import('./plugin-demos/pdf.module').then((m) => m.PdfModule) },
3939
{ path: 'picker', loadChildren: () => import('./plugin-demos/picker.module').then((m) => m.PickerModule) },
40+
{ path: 'secure-storage', loadChildren: () => import('./plugin-demos/secure-storage.module').then((m) => m.SecureStorageModule) },
4041
{ path: 'shared-notification-delegate', loadChildren: () => import('./plugin-demos/shared-notification-delegate.module').then((m) => m.SharedNotificationDelegateModule) },
4142
{ path: 'social-share', loadChildren: () => import('./plugin-demos/social-share.module').then((m) => m.SocialShareModule) },
4243
{ path: 'swift-ui', loadChildren: () => import('./plugin-demos/swift-ui.module').then((m) => m.SwiftUiModule) },

apps/demo-angular/src/home.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export class HomeComponent {
9696
{
9797
name: 'picker',
9898
},
99+
{
100+
name: 'secure-storage',
101+
},
99102
{
100103
name: 'shared-notification-delegate',
101104
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<ActionBar title="secure-storage" class="action-bar"> </ActionBar>
2+
<StackLayout class="p-20">
3+
<Page>
4+
<ScrollView>
5+
<StackLayout>
6+
<Button text="Set a value, sync" (tap)="demoShared.doSetSync()" class="btn btn-primary button-a button-group-first"></Button>
7+
<Button text="Set a value, async" (tap)="demoShared.doSet()" class="btn btn-primary button-a"></Button>
8+
9+
<Button text="Get a value, sync" (tap)="demoShared.doGetSync()" class="btn btn-primary button-b button-group-first"></Button>
10+
<Button text="Get a value, async" (tap)="demoShared.doGet()" class="btn btn-primary button-b"></Button>
11+
12+
<Label [text]="demoShared.lastRetrievedValue" class="message" textWrap="true"></Label>
13+
14+
<Button text="Remove a value, sync" (tap)="demoShared.doRemoveSync()" class="btn btn-primary button-c"></Button>
15+
<Button text="Remove a value, async" (tap)="demoShared.doRemove()" class="btn btn-primary button-c"></Button>
16+
17+
<Button text="Remove all values, sync" (tap)="demoShared.doRemoveAllSync()" class="btn btn-primary button-d button-group-first"></Button>
18+
<Button text="Remove all values, async" (tap)="demoShared.doRemoveAll()" class="btn btn-primary button-d"></Button>
19+
</StackLayout>
20+
</ScrollView>
21+
</Page>
22+
</StackLayout>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.message {
2+
color: #666;
3+
font-size: 16;
4+
padding: 20;
5+
}
6+
7+
button {
8+
color: #ffffff;
9+
background-color: #6494aa;
10+
padding: 6;
11+
margin: 8 24;
12+
font-size: 14;
13+
border-radius: 4;
14+
}
15+
16+
.button-group-first {
17+
margin-top: 24;
18+
}
19+
20+
.button-a {
21+
background-color: cornflowerblue;
22+
}
23+
24+
.button-b {
25+
background-color: forestgreen;
26+
}
27+
28+
.button-c {
29+
background-color: orange;
30+
}
31+
32+
.button-d {
33+
background-color: red;
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Component, NgZone } from '@angular/core';
2+
import { DemoSharedSecureStorage } from '@demo/shared';
3+
4+
@Component({
5+
selector: 'demo-secure-storage',
6+
templateUrl: 'secure-storage.component.html',
7+
styleUrls: ['secure-storage.component.scss'],
8+
})
9+
export class SecureStorageComponent {
10+
demoShared: DemoSharedSecureStorage;
11+
12+
constructor(private _ngZone: NgZone) {}
13+
14+
ngOnInit() {
15+
this.demoShared = new DemoSharedSecureStorage();
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
2+
import { NativeScriptCommonModule, NativeScriptRouterModule } from '@nativescript/angular';
3+
import { SecureStorageComponent } from './secure-storage.component';
4+
5+
@NgModule({
6+
imports: [NativeScriptCommonModule, NativeScriptRouterModule.forChild([{ path: '', component: SecureStorageComponent }])],
7+
declarations: [SecureStorageComponent],
8+
schemas: [NO_ERRORS_SCHEMA],
9+
})
10+
export class SecureStorageModule {}

apps/demo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"@nativescript/swift-ui": "file:../../packages/swift-ui",
4141
"@nativescript/theme-switcher": "file:../../packages/theme-switcher",
4242
"@nativescript/twitter": "file:../../packages/twitter",
43-
"@nativescript/zip": "file:../../packages/zip"
43+
"@nativescript/zip": "file:../../packages/zip",
44+
"@nativescript/secure-storage": "file:../../packages/secure-storage"
4445
},
4546
"devDependencies": {
4647
"@nativescript/android": "~8.5.0",

apps/demo/src/main-page.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Button text="theme-switcher" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
4242
<Button text="twitter" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
4343
<Button text="zip" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
44+
<Button text="secure-storage" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
4445
</StackLayout>
4546
</ScrollView>
4647
</StackLayout>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { EventData, Observable, Page } from '@nativescript/core';
2+
import { DemoSharedSecureStorage } from '@demo/shared';
3+
import { SecureStorage } from '@nativescript/secure-storage';
4+
5+
export function navigatingTo(args: EventData) {
6+
const page = <Page>args.object;
7+
page.bindingContext = new DemoModel();
8+
}
9+
10+
export class DemoModel extends DemoSharedSecureStorage {
11+
private secureStorage: SecureStorage;
12+
13+
constructor() {
14+
super();
15+
this.secureStorage = new SecureStorage();
16+
}
17+
18+
public doGet() {
19+
this.secureStorage
20+
.get({
21+
key: 'foo',
22+
})
23+
.then(
24+
(value) => {
25+
console.log('Value: ' + value);
26+
this.set('lastRetrievedValue', value === null ? '(no value set)' : value);
27+
},
28+
(err) => {
29+
console.log(err);
30+
}
31+
);
32+
}
33+
34+
public doGetSync() {
35+
console.log('go');
36+
const value = this.secureStorage.getSync({
37+
key: 'foo',
38+
});
39+
this.set('lastRetrievedValue', value === null ? '(no value set)' : value);
40+
}
41+
42+
public doSet() {
43+
this.secureStorage
44+
.set({
45+
key: 'foo',
46+
value: 'I was set at ' + new Date(),
47+
})
48+
.then(
49+
(success) => {
50+
console.log('Successfully set a value? ' + success);
51+
},
52+
(err) => {
53+
console.log(err);
54+
}
55+
);
56+
}
57+
58+
public doSetSync() {
59+
const success = this.secureStorage.setSync({
60+
key: 'foo',
61+
value: 'I was set at ' + new Date(),
62+
});
63+
console.log('Successfully set a value? ' + success);
64+
}
65+
66+
public doRemove() {
67+
this.secureStorage
68+
.remove({
69+
key: 'foo',
70+
})
71+
.then(
72+
(success) => {
73+
console.log('Successfully removed a value? ' + success);
74+
this.set('lastRetrievedValue', '');
75+
},
76+
(err) => {
77+
console.log(err);
78+
}
79+
);
80+
}
81+
82+
public doRemoveSync() {
83+
this.secureStorage.removeSync({
84+
key: 'foo',
85+
});
86+
this.set('lastRetrievedValue', '');
87+
}
88+
89+
public doRemoveAll() {
90+
this.secureStorage.removeAll().then(
91+
(success) => {
92+
console.log('Successfully removed all values? ' + success);
93+
this.set('lastRetrievedValue', '');
94+
},
95+
(err) => {
96+
console.log(err);
97+
}
98+
);
99+
}
100+
101+
public doRemoveAllSync() {
102+
this.secureStorage.removeAllSync();
103+
this.set('lastRetrievedValue', '');
104+
}
105+
106+
public doClearAllOnFirstRunSync() {
107+
const res: boolean = this.secureStorage.clearAllOnFirstRunSync();
108+
if (res) {
109+
console.log('Cleared');
110+
this.set('lastRetrievedValue', '');
111+
} else {
112+
alert('Is not the first run ! \n use `removeAllSync` or `removeAll` ;-)');
113+
}
114+
}
115+
116+
public doClearAllOnFirstRun() {
117+
this.secureStorage.clearAllOnFirstRun().then((res) => {
118+
if (res) {
119+
console.log('Cleared');
120+
this.set('lastRetrievedValue', '');
121+
} else {
122+
alert('Is not the first run ! \n use `removeAllSync` or `removeAll` ;-)');
123+
}
124+
});
125+
}
126+
127+
public doIsFirstRun() {
128+
this.secureStorage.isFirstRun().then((isFirst) => {
129+
this.set('isFirstRun', isFirst);
130+
});
131+
}
132+
133+
public doIsFirstRunSync() {
134+
this.set('isFirstRun', this.secureStorage.isFirstRunSync());
135+
}
136+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
2+
<Page.actionBar>
3+
<ActionBar title="secure-storage" icon="" class="action-bar">
4+
</ActionBar>
5+
</Page.actionBar>
6+
<StackLayout class="p-20">
7+
<ScrollView class="h-full">
8+
<StackLayout>
9+
<Image margin="0" src="~/res/securestorage.png" width="100%" />
10+
11+
<Button text="Set a value, sync" tap="{{ doSetSync }}" class="button-a button-group-first" />
12+
<Button text="Set a value, async" tap="{{ doSet }}" class="button-a" />
13+
14+
<Button text="Get a value, sync" tap="{{ doGetSync }}" class="button-b button-group-first" />
15+
<Button text="Get a value, async" tap="{{ doGet }}" class="button-b" />
16+
17+
<Label text="{{ lastRetrievedValue }}" class="message" textWrap="true" />
18+
19+
<Button text="Remove a value, sync" tap="{{ doRemoveSync }}" class="button-c" />
20+
<Button text="Remove a value, async" tap="{{ doRemove }}" class="button-c" />
21+
22+
<Button text="Remove all values, sync" tap="{{ doRemoveAllSync }}" class="button-d button-group-first" />
23+
<Button text="Remove all values, async" tap="{{ doRemoveAll }}" class="button-d" />
24+
25+
<Button text="Clear all on first run sync" tap="{{ doClearAllOnFirstRunSync }}" class="button-e button-group-first" />
26+
<Button text="Clear all on first run, async" tap="{{ doClearAllOnFirstRun }}" class="button-e" />
27+
28+
<Button text="{{ 'Is first run (sync)? ' + (isFirstRun === undefined ? '' : isFirstRun) }}" tap="{{ doIsFirstRunSync }}" class="button-f button-group-first" />
29+
<Button text="{{ 'Is first run (async)? ' + (isFirstRun === undefined ? '' : isFirstRun) }}" tap="{{ doIsFirstRun }}" class="button-f" />
30+
</StackLayout>
31+
</ScrollView>
32+
</StackLayout>
33+
</Page>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*", "node_modules/**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
}
17+
]
18+
}

0 commit comments

Comments
 (0)