@akylas/nativescript-app-utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.0 (2024-10-03)
7
+
8
+ **Note:** Version bump only for package @akylas/nativescript-app-utils
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Burke Holland
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # NativeScript Application Utils
2
+
3
+ A NativeScript plugin with utils for app dev
4
+
5
+ ## Usage
6
+
7
+ ````
8
+ npm install @akylas/nativescript-app-utils --save
9
+ ````
10
+
11
+
12
+ ## Development workflow
13
+
14
+ If you would like to contribute to this plugin in order to enabled the repositories code for development follow this steps:
15
+
16
+ - Fork the repository locally
17
+ - Open the repository in your favorite terminal
18
+ - Navigate to the src code that contains the plugin's code `cd /src`
19
+ - Execute the npm script `nmp run build.wrappers` or `npm run build.wrappers.watch`
20
+ - When running the Vanila NativeScript demo app execute: `npm run demo.android` or `npm run demo.ios`
21
+ - When running the Angular NativeScript demo app execute: `npm run demo.angular.android` or `npm run demo.angular.ios`
File without changes
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=index-common.js.map
@@ -0,0 +1,14 @@
1
+ export declare namespace AppUtilsAndroid {
2
+ function listenForWindowInsets(onWindowInsetsChange: (result: [number, number, number, number, number]) => void): void;
3
+ function prepareWindow(window: android.view.Window): void;
4
+ function getDimensionFromInt(context: android.content.Context, intToGet: number): number;
5
+ function getColorFromInt(context: android.content.Context, intToGet: number): number;
6
+ function getColorFromName(context: android.content.Context, name: string): number;
7
+ function restartApp(): any;
8
+ function prepareActivity(activity: androidx.appcompat.app.AppCompatActivity): any;
9
+ function applyDayNight(activity: androidx.appcompat.app.AppCompatActivity, applyDynamicColors: boolean): any;
10
+ function applyDynamicColors(activity: androidx.appcompat.app.AppCompatActivity): any;
11
+ }
12
+ export declare function setWorkerContextValue(key: any, value: any): void;
13
+ export declare function getWorkerContextValue(key: any): any;
14
+ export declare function getISO3Language(lang: any): string;
@@ -0,0 +1,59 @@
1
+ import { Application, Utils } from '@nativescript/core';
2
+ const NWorkerContext = com.nativescript.apputils.WorkersContext.Companion;
3
+ export var AppUtilsAndroid;
4
+ (function (AppUtilsAndroid) {
5
+ const NUtils = com.nativescript.apputils.Utils;
6
+ const UtilsCompanion = NUtils.Companion;
7
+ function listenForWindowInsets(onWindowInsetsChange) {
8
+ const rootView = Application.getRootView();
9
+ if (rootView) {
10
+ UtilsCompanion.listenForWindowInsetsChange(rootView.nativeViewProtected, new NUtils.WindowInsetsCallback({
11
+ onWindowInsetsChange
12
+ }));
13
+ }
14
+ }
15
+ AppUtilsAndroid.listenForWindowInsets = listenForWindowInsets;
16
+ function prepareWindow(window) {
17
+ UtilsCompanion.prepareWindow(window);
18
+ }
19
+ AppUtilsAndroid.prepareWindow = prepareWindow;
20
+ function getDimensionFromInt(context, intToGet) {
21
+ return UtilsCompanion.getDimensionFromInt(context, intToGet);
22
+ }
23
+ AppUtilsAndroid.getDimensionFromInt = getDimensionFromInt;
24
+ function getColorFromInt(context, intToGet) {
25
+ return UtilsCompanion.getColorFromInt(context, intToGet);
26
+ }
27
+ AppUtilsAndroid.getColorFromInt = getColorFromInt;
28
+ function getColorFromName(context, name) {
29
+ return UtilsCompanion.getColorFromName(context, name);
30
+ }
31
+ AppUtilsAndroid.getColorFromName = getColorFromName;
32
+ function restartApp() {
33
+ return UtilsCompanion.restartApp(Utils.android.getApplicationContext(), Application.android.startActivity);
34
+ }
35
+ AppUtilsAndroid.restartApp = restartApp;
36
+ function prepareActivity(activity) {
37
+ return UtilsCompanion.prepareActivity(activity);
38
+ }
39
+ AppUtilsAndroid.prepareActivity = prepareActivity;
40
+ function applyDayNight(activity, applyDynamicColors) {
41
+ return UtilsCompanion.applyDayNight(activity, applyDynamicColors);
42
+ }
43
+ AppUtilsAndroid.applyDayNight = applyDayNight;
44
+ function applyDynamicColors(activity) {
45
+ return UtilsCompanion.applyDynamicColors(activity);
46
+ }
47
+ AppUtilsAndroid.applyDynamicColors = applyDynamicColors;
48
+ })(AppUtilsAndroid || (AppUtilsAndroid = {}));
49
+ export function setWorkerContextValue(key, value) {
50
+ NWorkerContext.setValue(key, value);
51
+ }
52
+ export function getWorkerContextValue(key) {
53
+ return NWorkerContext.getValue(key);
54
+ }
55
+ export function getISO3Language(lang) {
56
+ const locale = java.util.Locale.forLanguageTag(lang);
57
+ return locale.getISO3Language();
58
+ }
59
+ //# sourceMappingURL=index.android.js.map
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './index.android';
package/index.ios.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ export declare namespace AppUtilsAndroid {
2
+ function listenForWindowInsets(onWindowInsetsChange: (result: [number, number, number, number, number]) => void): void;
3
+ function prepareWindow(window: any): void;
4
+ function getDimensionFromInt(context: any, intToGet: number): void;
5
+ function getColorFromInt(context: any, intToGet: number): void;
6
+ function getColorFromName(context: any, intToGet: number): void;
7
+ function restartApp(): void;
8
+ function prepareActivity(activity: any): void;
9
+ function applyDayNight(activity: any, applyDynamicColors: boolean): void;
10
+ function applyDynamicColors(activity: any): void;
11
+ }
12
+ export declare function setWorkerContextValue(key: any, value: any): void;
13
+ export declare function getWorkerContextValue(key: any): any;
14
+ export declare function getISO3Language(lang: any): any;
package/index.ios.js ADDED
@@ -0,0 +1,31 @@
1
+ export var AppUtilsAndroid;
2
+ (function (AppUtilsAndroid) {
3
+ function listenForWindowInsets(onWindowInsetsChange) { }
4
+ AppUtilsAndroid.listenForWindowInsets = listenForWindowInsets;
5
+ function prepareWindow(window) { }
6
+ AppUtilsAndroid.prepareWindow = prepareWindow;
7
+ function getDimensionFromInt(context, intToGet) { }
8
+ AppUtilsAndroid.getDimensionFromInt = getDimensionFromInt;
9
+ function getColorFromInt(context, intToGet) { }
10
+ AppUtilsAndroid.getColorFromInt = getColorFromInt;
11
+ function getColorFromName(context, intToGet) { }
12
+ AppUtilsAndroid.getColorFromName = getColorFromName;
13
+ function restartApp() { }
14
+ AppUtilsAndroid.restartApp = restartApp;
15
+ function prepareActivity(activity) { }
16
+ AppUtilsAndroid.prepareActivity = prepareActivity;
17
+ function applyDayNight(activity, applyDynamicColors) { }
18
+ AppUtilsAndroid.applyDayNight = applyDayNight;
19
+ function applyDynamicColors(activity) { }
20
+ AppUtilsAndroid.applyDynamicColors = applyDynamicColors;
21
+ })(AppUtilsAndroid || (AppUtilsAndroid = {}));
22
+ export function setWorkerContextValue(key, value) {
23
+ NWorkerContext.setValue(key, value);
24
+ }
25
+ export function getWorkerContextValue(key) {
26
+ return NWorkerContext.getValue(key);
27
+ }
28
+ export function getISO3Language(lang) {
29
+ return NSLocale.alloc().initWithLocaleIdentifier(lang).ISO639_2LanguageCode();
30
+ }
31
+ //# sourceMappingURL=index.ios.js.map
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@akylas/nativescript-app-utils",
3
+ "version": "1.0.0",
4
+ "description": "Provides API for changing the styles of SystemUI (StatusBar, NavigationBar...) on iOS.",
5
+ "main": "index",
6
+ "sideEffects": false,
7
+ "typings": "index.d.ts",
8
+ "scripts": {
9
+ "tsc": "cpy ../../src/app-utils/index.d.ts ./ && tsc -d",
10
+ "build": "npm run tsc",
11
+ "build.watch": "npm run tsc -- -w",
12
+ "build.win": "npm run tsc-win",
13
+ "build.all": "npm run build",
14
+ "build.angular": "ng-packagr -p ../../src/app-utils/angular/ng-package.json -c ../../src/app-utils/angular/tsconfig.json && rm angular/.npmignore",
15
+ "clean": "rimraf ./*.d.ts ./*.js ./*.js.map"
16
+ },
17
+ "nativescript": {
18
+ "platforms": {
19
+ "android": "5.0.0",
20
+ "ios": "5.0.0"
21
+ }
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/akylas/nativescript-app-utils"
29
+ },
30
+ "keywords": [
31
+ "nativescript",
32
+ "native",
33
+ "typescript",
34
+ "iOS",
35
+ "statusbar",
36
+ "status bar",
37
+ "Vue"
38
+ ],
39
+ "contributors": [
40
+ {
41
+ "name": "Burke Holland",
42
+ "email": "burkeholland@gmail.com",
43
+ "url": "https://github.com/burkeholland"
44
+ },
45
+ {
46
+ "name": "Brad Martin",
47
+ "email": "bradwaynemartin@gmail.com",
48
+ "url": "https://github.com/bradmartin"
49
+ },
50
+ {
51
+ "name": "Vladimir Amiorkov",
52
+ "email": "v.amiorkov@gmail.com",
53
+ "url": "https://github.com/VladimirAmiorkov"
54
+ },
55
+ {
56
+ "name": "Martin Guillon",
57
+ "email": "martin.guillon@akylas.fr",
58
+ "url": "https://github.com/farfromrefug"
59
+ }
60
+ ],
61
+ "license": "MIT",
62
+ "author": "Burke Holland <burkeholland@gmail.com>",
63
+ "bugs": {
64
+ "url": "https://github.com/akylas/nativescript-app-utils/issues"
65
+ },
66
+ "gitHead": "209db3a7039c188b6cf65b97ab6384f70b6f9515"
67
+ }
Binary file
@@ -0,0 +1,12 @@
1
+ dependencies {
2
+ def androidXSplashscreenVersion = project.hasProperty("androidXSplashscreenVersion") ? project.androidXSplashscreenVersion : "1.1.0-alpha02"
3
+ def androidxVersion = project.hasProperty("androidxVersion") ? project.androidxVersion : "1.2.0"
4
+ def androidXAppCompat = project.hasProperty("androidXAppCompat") ? project.androidXAppCompat : "1.1.0"
5
+ def androidXMaterial = project.hasProperty("androidXMaterial") ? project.androidXMaterial : "1.8.0"
6
+
7
+ implementation "com.google.android.material:material:$androidXMaterial"
8
+ implementation "androidx.appcompat:appcompat:$androidXAppCompat"
9
+ implementation "androidx.core:core-splashscreen:$androidXSplashscreenVersion"
10
+ implementation "androidx.core:core:$androidxVersion"
11
+
12
+ }
@@ -0,0 +1,111 @@
1
+ package com.nativescript.apputils
2
+
3
+ import android.os.Build
4
+ import android.graphics.Rect
5
+ import android.util.TypedValue
6
+ import android.content.Context
7
+ import android.content.Intent
8
+ import android.content.res.Resources
9
+ import android.view.Window
10
+ import android.view.View
11
+ import androidx.core.view.ViewCompat
12
+ import android.view.WindowInsets
13
+ import androidx.core.view.WindowInsetsCompat
14
+ import androidx.appcompat.app.AppCompatActivity
15
+ import androidx.core.os.ConfigurationCompat
16
+ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
17
+ import androidx.core.view.WindowCompat
18
+ import com.google.android.material.color.DynamicColors
19
+ import java.util.Locale
20
+ import android.util.Log
21
+
22
+ class Utils {
23
+ interface WindowInsetsCallback {
24
+ fun onWindowInsetsChange(result: IntArray)
25
+ }
26
+ companion object {
27
+ fun getColorFromName(context: Context, name: String?): Int {
28
+ val resID = context.resources.getIdentifier(name, "attr", context.packageName)
29
+ val array = context.theme.obtainStyledAttributes(intArrayOf(resID))
30
+ return array.getColor(0, 0xFF00FF)
31
+ }
32
+
33
+ fun getColorFromInt(context: Context, resId: Int): Int {
34
+ val array = context.theme.obtainStyledAttributes(intArrayOf(resId))
35
+ return array.getColor(0, 0xFF00FF)
36
+ }
37
+
38
+ fun getDimensionFromInt(context: Context, resId: Int): Float {
39
+ val array = context.theme.obtainStyledAttributes(intArrayOf(resId))
40
+ return array.getDimension(0, 0f)
41
+ }
42
+
43
+ fun applyDayNight(activity: AppCompatActivity, applyDynamicColors: Boolean) {
44
+
45
+ // we need to applyDayNight to update theme thus colors as we dont restart activity (configChanges:uiMode)
46
+ // but then dynamic colors are lost so let s call DynamicColors.applyIfAvailable
47
+ activity.getDelegate().applyDayNight()
48
+ if (applyDynamicColors) {
49
+ DynamicColors.applyIfAvailable(activity)
50
+ }
51
+ }
52
+
53
+ fun applyDynamicColors(activity: AppCompatActivity?) {
54
+ DynamicColors.applyIfAvailable(activity!!)
55
+ }
56
+
57
+ fun restartApp(ctx: Context, activity: AppCompatActivity?) {
58
+ val pm = ctx.packageManager
59
+ val intent = pm.getLaunchIntentForPackage(ctx.packageName)
60
+ val mainIntent = Intent.makeRestartActivityTask(intent!!.component)
61
+ ctx.startActivity(mainIntent)
62
+ Runtime.getRuntime().exit(0)
63
+ }
64
+
65
+ val systemLocale: Locale?
66
+ get() = ConfigurationCompat.getLocales(Resources.getSystem().configuration)[0]
67
+
68
+ fun prepareActivity(activity: AppCompatActivity) {
69
+ DynamicColors.applyToActivityIfAvailable(activity)
70
+ activity.installSplashScreen()
71
+ prepareWindow(activity.window)
72
+ }
73
+
74
+ fun prepareWindow(window: Window?) {
75
+ WindowCompat.setDecorFitsSystemWindows(window!!, false)
76
+ }
77
+ fun getRootWindowInsets(view: View): IntArray {
78
+ val windowInsets = ViewCompat.getRootWindowInsets(view) ?: return intArrayOf(0, 0,0, 0, 0)
79
+ val inset = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
80
+ val imeVisible = windowInsets.isVisible(WindowInsetsCompat.Type.ime())
81
+ val imeHeight = if (imeVisible) windowInsets.getInsets(WindowInsetsCompat.Type.ime()).bottom else 0
82
+ return intArrayOf(inset.top, inset.bottom, inset.left, inset.right, imeHeight)
83
+ }
84
+
85
+ fun listenForWindowInsetsChange(rootView: View, callback: WindowInsetsCallback) {
86
+ rootView.setOnApplyWindowInsetsListener{ view, insets ->
87
+ val inset = getRootWindowInsets(view)
88
+ callback.onWindowInsetsChange(inset)
89
+ return@setOnApplyWindowInsetsListener insets
90
+ }
91
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
92
+ var alreadyOpen = false
93
+ val defaultKeyboardHeightDP = 100
94
+ val estimatedKeyboardDP = defaultKeyboardHeightDP + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 48 else 0
95
+ val rect = Rect()
96
+ rootView.viewTreeObserver.addOnGlobalLayoutListener {
97
+ val estimatedKeyboardHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, estimatedKeyboardDP.toFloat(), rootView.resources.displayMetrics).toInt()
98
+ rootView.getWindowVisibleDisplayFrame(rect)
99
+ val heightDiff = rootView.rootView.height - (rect.bottom - rect.top)
100
+ val isShown = heightDiff >= estimatedKeyboardHeight
101
+
102
+ if (isShown !== alreadyOpen) {
103
+ alreadyOpen = isShown
104
+ rootView.requestApplyInsets()
105
+ }
106
+ }
107
+
108
+ }
109
+ }
110
+ }
111
+ }
@@ -0,0 +1,20 @@
1
+ package com.nativescript.apputils
2
+
3
+ import java.util.concurrent.ConcurrentHashMap
4
+
5
+ class WorkersContext {
6
+ companion object {
7
+ private val container: MutableMap<String, Any> = ConcurrentHashMap()
8
+ fun getValue(key: String): Any? {
9
+ return container[key]
10
+ }
11
+
12
+ fun setValue(key: String, value: Any?) {
13
+ if (value != null) {
14
+ container[key] = value
15
+ } else {
16
+ container.remove(key)
17
+ }
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "uses": [
3
+ "com.nativescript.apputils:WorkersContext",
4
+ "com.nativescript.apputils:WorkersContext.Companion",
5
+ "com.nativescript.apputils:Utils",
6
+ "com.nativescript.apputils:Utils.WindowInsetsCallback",
7
+ "com.nativescript.apputils:Utils.Companion"
8
+ ]
9
+ }