Skip to content

Commit f6937dd

Browse files
committed
feat: battery shared for angular and typescript flavors
1 parent 1206c84 commit f6937dd

18 files changed

Lines changed: 194 additions & 1294 deletions

File tree

‎apps/battery/angular/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"devDependencies": {
2222
"@angular-devkit/build-angular": "~14.1.0",
2323
"@angular/compiler-cli": "~14.1.0",
24+
"@nativescript/ios": "8.3.3",
2425
"@nativescript/preview-cli": "1.0.0-beta.18",
2526
"@nativescript/stackblitz": "0.0.5",
2627
"@nativescript/types-minimal": "~8.3.0",

‎apps/battery/angular/src/app/home.component.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<Label text="The Battery Level is:" class="h1 text-center"></Label>
55
<Label [text]="level ? level + '%' : '---'" class="h2 text-center"></Label>
66
<Button [text]="isListening ? 'Stop Listening to Battery Changes' : 'Listen for battery changes'" class="action-btn" (tap)="toggleListenForChanges()"></Button>
7+
<Image src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Angular_full_color_logo.svg/500px-Angular_full_color_logo.svg.png" width="200" class="m-t-20"></Image>
78
</StackLayout>

‎apps/battery/angular/tsconfig.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
"~/*": ["src/*"],
1515
"@/*": ["src/*"],
1616
"@stackblitz/nativescript-battery": [
17-
"../../../libs/nativescript-battery/src/index.d.ts"
17+
"../../../libs/nativescript-battery/src/index.ts"
1818
]
1919
}
2020
},
21-
"include": ["src/tests/**/*.ts", "src/**/*.ios.ts", "src/**/*.android.ts"],
21+
"include": ["src/tests/**/*.ts", "src/**/*.ios.ts", "src/**/*.android.ts", "../../../libs/nativescript-battery/**/*.ios.ts", "../../../libs/nativescript-battery/**/*.android.ts"],
2222
"files": ["./src/main.ts", "./references.d.ts", "./src/polyfills.ts"],
2323
"exclude": ["node_modules", "platforms", "e2e"]
2424
}

‎apps/battery/react/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@nativescript/webpack": "~5.0.0",
1818
"@types/react": "16.9.34",
1919
"patch-package": "~6.4.7",
20-
"typescript": "~4.5.5"
20+
"typescript": "~4.7.0"
2121
},
2222
"scripts": {
2323
"postinstall": "patch-package",

‎apps/battery/svelte/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"svelte-loader": "^3.1.2",
1717
"svelte-native-preprocessor": "^1.0.0",
1818
"svelte-preprocess": "^4.7.0",
19-
"typescript": "~4.5.5"
19+
"typescript": "~4.7.0"
2020
}
2121
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
@import '@nativescript/theme/css/core.css';
22
@import '@nativescript/theme/css/default.css';
33

4-
Button.-primary {
4+
Button.action-btn {
5+
border-radius: 25;
6+
background-color: rgb(0, 162, 255);
7+
color: white;
58
font-size: 18;
9+
padding: 5;
610
}

‎apps/battery/typescript/app/app.ts‎

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,68 @@
1-
import { Application } from '@nativescript/core';
1+
import { Application, CoreTypes, TouchManager, View } from '@nativescript/core';
2+
3+
// default Touch animations
4+
const originalTransform = Symbol('originalTransform');
5+
TouchManager.enableGlobalTapAnimations = true;
6+
TouchManager.animations = {
7+
down: (view: View) => {
8+
if (global.isIOS) {
9+
UIView.animateWithDurationDelayUsingSpringWithDampingInitialSpringVelocityOptionsAnimationsCompletion(
10+
0.3,
11+
0,
12+
0.5,
13+
3,
14+
UIViewAnimationOptions.CurveEaseInOut,
15+
() => {
16+
if (view?.ios) {
17+
view[originalTransform] =
18+
view[originalTransform] ?? view.ios.transform;
19+
20+
view.ios.transform = CGAffineTransformConcat(
21+
view[originalTransform],
22+
CGAffineTransformMakeScale(0.95, 0.95)
23+
);
24+
}
25+
},
26+
() => {}
27+
);
28+
} else {
29+
view
30+
?.animate({
31+
scale: { x: 0.95, y: 0.95 },
32+
duration: 120,
33+
curve: CoreTypes.AnimationCurve.easeInOut,
34+
})
35+
.then(() => {})
36+
.catch(() => {});
37+
}
38+
},
39+
up: (view: View) => {
40+
if (global.isIOS) {
41+
UIView.animateWithDurationDelayUsingSpringWithDampingInitialSpringVelocityOptionsAnimationsCompletion(
42+
0.3,
43+
0,
44+
0.5,
45+
3,
46+
UIViewAnimationOptions.CurveEaseInOut,
47+
() => {
48+
if (view?.ios) {
49+
view.ios.transform =
50+
view[originalTransform] ?? CGAffineTransformMakeScale(1, 1);
51+
}
52+
},
53+
() => {}
54+
);
55+
} else {
56+
view
57+
?.animate({
58+
scale: { x: 1, y: 1 },
59+
duration: 120,
60+
curve: CoreTypes.AnimationCurve.easeInOut,
61+
})
62+
.then(() => {})
63+
.catch(() => {});
64+
}
65+
},
66+
};
267

368
Application.run({ moduleName: 'app-root' });
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
2-
<ActionBar title="My App" icon="" />
2+
<ActionBar title="NativeScript is pretty cool!" icon="" />
33

44
<StackLayout class="p-20">
5-
<Label text="Tap the button" class="h1 text-center" />
6-
<Button text="TAP" tap="{{ onTap }}" class="-primary" />
7-
<Label text="{{ message }}" class="h2 text-center" textWrap="true" />
5+
<Label text="The Battery Level is:" class="h1 text-center" />
6+
<Label text="{{ level ? level + '%' : '---' }}" class="h2 text-center" />
7+
<Button text="{{ isListening ? 'Stop Listening to Battery Changes' : 'Listen for battery changes' }}" class="action-btn" tap="{{toggleListenForChanges}}" />
8+
<Image src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Typescript_logo_2020.svg/1024px-Typescript_logo_2020.svg.png" width="200" class="m-t-20"/>
9+
810
</StackLayout>
911
</Page>
Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
import { Observable } from '@nativescript/core';
2+
import { checkBatteryLevel, listenForBatteryChanges } from '@stackblitz/nativescript-battery';
3+
4+
const formatMessage = (level) => `The Battery Level is: ${level}%`;
25

36
export class HelloWorldModel extends Observable {
4-
private _counter: number;
5-
private _message: string;
7+
level: number;
8+
isListening = false;
69

710
constructor() {
811
super();
9-
10-
// Initialize default values.
11-
this._counter = 42;
12-
this.updateMessage();
12+
this.updateBatteryLevel();
1313
}
1414

15-
get message(): string {
16-
return this._message;
15+
async updateBatteryLevel() {
16+
const value = await checkBatteryLevel();
17+
this.updateLevel(value);
18+
alert(formatMessage(value));
1719
}
1820

19-
set message(value: string) {
20-
if (this._message !== value) {
21-
this._message = value;
22-
this.notifyPropertyChange('message', value);
21+
async toggleListenForChanges() {
22+
this.isListening = listenForBatteryChanges((value) => {
23+
this.updateLevel(value);
24+
});
25+
this.notifyPropertyChange('isListening', this.isListening);
26+
if (this.isListening) {
27+
// update view binding right away
28+
const value = await checkBatteryLevel();
29+
this.updateLevel(value);
2330
}
2431
}
2532

26-
onTap() {
27-
this._counter--;
28-
this.updateMessage();
29-
}
30-
31-
private updateMessage() {
32-
if (this._counter <= 0) {
33-
this.message =
34-
'Hoorraaay! You unlocked the NativeScript clicker achievement!';
35-
} else {
36-
this.message = `${this._counter} taps left`;
37-
}
38-
39-
// log the message to the console
40-
console.log(this.message);
33+
private updateLevel(value: number) {
34+
this.level = value;
35+
this.notifyPropertyChange('level', this.level);
4136
}
4237
}

‎apps/battery/typescript/package.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
"private": true,
66
"dependencies": {
77
"@nativescript/core": "~8.3.0",
8-
"@nativescript/theme": "~3.0.2"
8+
"@nativescript/theme": "~3.0.2",
9+
"@stackblitz/nativescript-battery": "file:../../../libs/nativescript-battery/src"
910
},
1011
"devDependencies": {
12+
"@nativescript/ios": "8.3.3",
1113
"@nativescript/preview-cli": "1.0.0-beta.18",
1214
"@nativescript/stackblitz": "0.0.5",
1315
"@nativescript/types-minimal": "~8.3.0",
1416
"@nativescript/webpack": "~5.0.0",
15-
"typescript": "~4.5.5"
17+
"typescript": "~4.7.0"
1618
}
1719
}

0 commit comments

Comments
 (0)