@akylas/nativescript-app-utils 2.1.6 → 2.2.1

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 CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.2.1](https://github.com/akylas/nativescript-app-utils/compare/v2.2.0...v2.2.1) (2024-12-18)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **ios:** handle urls starting with file:// ([cf78de2](https://github.com/akylas/nativescript-app-utils/commit/cf78de281977d11d2f16e59bfccfdd87bc81c543))
11
+
12
+ ## [2.2.0](https://github.com/akylas/nativescript-app-utils/compare/v2.1.6...v2.2.0) (2024-11-14)
13
+
14
+ ### Features
15
+
16
+ * added share ([2e27a03](https://github.com/akylas/nativescript-app-utils/commit/2e27a03665b32ebb54b86e86beed2288f0bafe7a))
17
+
6
18
  ## [2.1.6](https://github.com/akylas/nativescript-app-utils/compare/v2.1.5...v2.1.6) (2024-11-08)
7
19
 
8
20
  ### Bug Fixes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akylas/nativescript-app-utils",
3
- "version": "2.1.6",
3
+ "version": "2.2.1",
4
4
  "description": "Provides API for changing the styles of SystemUI (StatusBar, NavigationBar...) on iOS.",
5
5
  "main": "index",
6
6
  "sideEffects": false,
@@ -63,5 +63,8 @@
63
63
  "bugs": {
64
64
  "url": "https://github.com/akylas/nativescript-app-utils/issues"
65
65
  },
66
- "gitHead": "f9d8fa107bd8ef7a0bba2b7cd091e8b53402368d"
66
+ "dependencies": {
67
+ "@nativescript-community/ui-share-file": "1.3.3"
68
+ },
69
+ "gitHead": "83d8799e538f6dae600c486a122b6bbafefd7e4a"
67
70
  }
@@ -110,5 +110,13 @@ class Utils {
110
110
 
111
111
  }
112
112
  }
113
+
114
+ fun guessMimeType(context: Context, uri: android.net.Uri): String? {
115
+ val type = android.webkit.MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri));
116
+ if (type != null) {
117
+ return android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(type);
118
+ }
119
+ return null
120
+ }
113
121
  }
114
122
  }
@@ -152,7 +152,7 @@ class ImageUtils : NSObject {
152
152
  }
153
153
 
154
154
  static func getImageSize(_ src: String) -> Dictionary<String, Any>? {
155
- let url = NSURL.fileURL(withPath: src)
155
+ let url = NSURL.fileURL(withPath: src.replacingOccurrences(of: "file://", with: ""))
156
156
  let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil);
157
157
  if (imageSource == nil) {
158
158
  // Error loading image
@@ -191,7 +191,7 @@ class ImageUtils : NSObject {
191
191
 
192
192
 
193
193
  static func readImageFromFileSync(_ src: String, options: NSDictionary?) -> UIImage? {
194
- let image = UIImage(contentsOfFile: src)
194
+ let image = UIImage(contentsOfFile: src.replacingOccurrences(of: "file://", with: ""))
195
195
  if let image {
196
196
  let size = image.size
197
197
  let imageOrientation = image.imageOrientation
@@ -0,0 +1 @@
1
+ /// <reference path="../../node_modules/@nativescript/types-ios/lib/ios/objc-x86_64/objc!LinkPresentation.d.ts" />
@@ -0,0 +1,3 @@
1
+ import { Content, Options } from '.';
2
+ export * from './index.common';
3
+ export declare function share(content: Content, options?: Options): Promise<boolean>;
@@ -0,0 +1,101 @@
1
+ import { Application, Device, Utils, knownFolders, path } from '@nativescript/core';
2
+ export * from './index.common';
3
+ let numberOfImagesCreated = 0;
4
+ const sdkVersionInt = parseInt(Device.sdkVersion, 10);
5
+ const EXTRA_STREAM = 'android.intent.extra.STREAM';
6
+ const ACTION_SEND = 'android.intent.action.SEND';
7
+ const ACTION_SEND_MULTIPLE = 'android.intent.action.SEND_MULTIPLE';
8
+ export async function share(content, options = {}) {
9
+ if (content == null) {
10
+ throw new Error('missing_content');
11
+ }
12
+ const Intent = android.content.Intent;
13
+ let mimetype = options.mimetype;
14
+ const intent = new Intent(ACTION_SEND);
15
+ const currentActivity = Application.android.foregroundActivity || Application.android.startActivity;
16
+ const context = Utils.android.getApplicationContext();
17
+ const uris = [];
18
+ async function addImage(image) {
19
+ if (!mimetype) {
20
+ mimetype = 'image/jpeg';
21
+ }
22
+ const imageFileName = 'share_image_' + numberOfImagesCreated++ + '.jpg';
23
+ const filePath = path.join(knownFolders.temp().path, imageFileName);
24
+ await image.saveToFileAsync(filePath, 'jpg');
25
+ const newFile = new java.io.File(filePath);
26
+ let shareableFileUri;
27
+ if (sdkVersionInt >= 21) {
28
+ shareableFileUri = androidx.core.content.FileProvider.getUriForFile(currentActivity, Application.android.nativeApp.getPackageName() + '.provider', newFile);
29
+ }
30
+ else {
31
+ shareableFileUri = android.net.Uri.fromFile(newFile);
32
+ }
33
+ uris.push(shareableFileUri);
34
+ }
35
+ function addFile(file) {
36
+ const newFile = new java.io.File(file);
37
+ let shareableFileUri;
38
+ if (sdkVersionInt >= 21) {
39
+ shareableFileUri = androidx.core.content.FileProvider.getUriForFile(currentActivity, Application.android.nativeApp.getPackageName() + '.provider', newFile);
40
+ }
41
+ else {
42
+ shareableFileUri = android.net.Uri.fromFile(newFile);
43
+ }
44
+ if (!mimetype) {
45
+ // try to guess the file mimetype
46
+ const guessedMimetype = com.nativescript.apputils.Utils.Companion.guessMimeType(context, shareableFileUri);
47
+ if (guessedMimetype) {
48
+ mimetype = guessedMimetype;
49
+ }
50
+ }
51
+ uris.push(shareableFileUri);
52
+ }
53
+ if (content.image) {
54
+ await addImage(content.image);
55
+ }
56
+ if (content.images) {
57
+ for (let index = 0; index < content.images.length; index++) {
58
+ await addImage(content.images[index]);
59
+ }
60
+ }
61
+ if (content.file) {
62
+ addFile(content.file);
63
+ }
64
+ if (content.files) {
65
+ for (let index = 0; index < content.files.length; index++) {
66
+ addFile(content.files[index]);
67
+ }
68
+ }
69
+ if (content.title) {
70
+ if (!mimetype) {
71
+ mimetype = 'text/plain';
72
+ }
73
+ intent.putExtra(Intent.EXTRA_SUBJECT, content.title);
74
+ }
75
+ if (content.message) {
76
+ if (!mimetype) {
77
+ mimetype = 'text/plain';
78
+ }
79
+ intent.putExtra(Intent.EXTRA_TEXT, content.message);
80
+ }
81
+ if (uris.length === 1) {
82
+ intent.putExtra(EXTRA_STREAM, uris[0]);
83
+ }
84
+ else if (uris.length > 1) {
85
+ const arrayList = new java.util.ArrayList(uris.length);
86
+ uris.forEach((uri) => arrayList.add(uri));
87
+ intent.setAction(ACTION_SEND_MULTIPLE);
88
+ intent.putExtra(EXTRA_STREAM, arrayList);
89
+ }
90
+ intent.setTypeAndNormalize(mimetype || '*/*');
91
+ const chooser = Intent.createChooser(intent, options.dialogTitle);
92
+ chooser.addCategory(Intent.CATEGORY_DEFAULT);
93
+ if (currentActivity !== null) {
94
+ currentActivity.startActivity(chooser);
95
+ }
96
+ else {
97
+ Utils.android.getApplicationContext().startActivity(chooser);
98
+ }
99
+ return true;
100
+ }
101
+ //# sourceMappingURL=index.android.js.map
@@ -0,0 +1,2 @@
1
+ import { ShareOptions } from '@nativescript-community/ui-share-file';
2
+ export declare function shareFile(content: string, fileName: string, shareOptions?: Partial<ShareOptions>): Promise<boolean>;
@@ -0,0 +1,18 @@
1
+ import { knownFolders } from '@nativescript/core';
2
+ import { ShareFile } from '@nativescript-community/ui-share-file';
3
+ const shareFileObject = new ShareFile();
4
+ export async function shareFile(content, fileName, shareOptions = {}) {
5
+ const file = knownFolders.temp().getFile(fileName);
6
+ // iOS: using writeText was not adding the file. Surely because it was too soon or something
7
+ // doing it sync works better but still needs a timeout
8
+ // showLoading('loading');
9
+ await file.writeText(content);
10
+ return shareFileObject.open({
11
+ path: file.path,
12
+ title: fileName,
13
+ options: true, // optional iOS
14
+ animated: true, // optional iOS,
15
+ ...shareOptions
16
+ });
17
+ }
18
+ //# sourceMappingURL=index.common.js.map
@@ -0,0 +1,22 @@
1
+ import { Color } from '@nativescript/core';
2
+ export * from './index.common';
3
+
4
+ export interface Content {
5
+ title?: string;
6
+ message?: string;
7
+ image?: ImageSource;
8
+ images?: ImageSource[];
9
+ file?: string;
10
+ files?: string[];
11
+ url?: string;
12
+ }
13
+ export interface Options {
14
+ mimetype?: string;
15
+ dialogTitle?: string;
16
+ excludedActivityTypes?: string[];
17
+ tintColor?: string | Color;
18
+ subject?: string;
19
+ anchor?: View; //ios only
20
+ appearance?: 'light' | 'dark';
21
+ }
22
+ export function share(content: Content, options?: Options): Promise<boolean>;
@@ -0,0 +1,3 @@
1
+ import { Content, Options } from '.';
2
+ export * from './index.common';
3
+ export declare function share(content: Content, options?: Options): Promise<unknown>;
@@ -0,0 +1,134 @@
1
+ import { Application, Color } from '@nativescript/core';
2
+ export * from './index.common';
3
+ var ItemSource = /** @class */ (function (_super) {
4
+ __extends(ItemSource, _super);
5
+ function ItemSource() {
6
+ return _super !== null && _super.apply(this, arguments) || this;
7
+ }
8
+ ItemSource.initWithData = function (data, placeholder, subject, metadata) {
9
+ var delegate = ItemSource.new();
10
+ delegate.data = data;
11
+ delegate.placeholder = placeholder;
12
+ delegate.subject = subject;
13
+ delegate.metadata = metadata;
14
+ return delegate;
15
+ };
16
+ // activityViewControllerDataTypeIdentifierForActivityType?(activityViewController: UIActivityViewController, activityType: string): string {
17
+ // throw new Error('Method not implemented.');
18
+ // }
19
+ ItemSource.prototype.activityViewControllerItemForActivityType = function (activityViewController, activityType) {
20
+ return this.data;
21
+ };
22
+ ItemSource.prototype.activityViewControllerLinkMetadata = function (activityViewController) {
23
+ if (this.metadata) {
24
+ var metadata = LPLinkMetadata.new();
25
+ metadata.title = this.metadata.title;
26
+ if (this.metadata.image) {
27
+ metadata.imageProvider = NSItemProvider.alloc().initWithObject(this.metadata.image);
28
+ metadata.iconProvider = metadata.imageProvider;
29
+ }
30
+ if (this.metadata.icon) {
31
+ metadata.iconProvider = NSItemProvider.alloc().initWithObject(this.metadata.icon);
32
+ }
33
+ if (this.metadata.originalURL) {
34
+ metadata.originalURL = NSURL.alloc().initWithString(this.metadata.originalURL);
35
+ }
36
+ return metadata;
37
+ }
38
+ return null;
39
+ };
40
+ ItemSource.prototype.activityViewControllerPlaceholderItem = function (activityViewController) {
41
+ return this.placeholder;
42
+ };
43
+ ItemSource.prototype.activityViewControllerSubjectForActivityType = function (activityViewController, activityType) {
44
+ return this.subject;
45
+ };
46
+ ItemSource.ObjCProtocols = [UIActivityItemSource];
47
+ return ItemSource;
48
+ }(NSObject));
49
+ export async function share(content, options = {}) {
50
+ if (content == null) {
51
+ throw new Error('missing_content');
52
+ }
53
+ return new Promise((resolve, reject) => {
54
+ let metadata;
55
+ if (content.title) {
56
+ metadata = { title: content.title };
57
+ }
58
+ const items = [];
59
+ if (content.url) {
60
+ const url = NSURL.URLWithString(content.url);
61
+ if (url.scheme.toLowerCase() === 'data') {
62
+ const data = NSData.dataWithContentsOfURLOptionsError(url, 0);
63
+ if (!data) {
64
+ throw new Error('cant_share_url');
65
+ }
66
+ items.push(data);
67
+ }
68
+ else {
69
+ items.push(url);
70
+ }
71
+ }
72
+ if (content.message) {
73
+ items.push(content.message);
74
+ }
75
+ if (content.image) {
76
+ items.push(content.image.ios);
77
+ }
78
+ if (content.images) {
79
+ content.images.forEach((image) => items.push(image.ios));
80
+ }
81
+ if (content.file) {
82
+ items.push(NSURL.fileURLWithPath(content.file));
83
+ }
84
+ if (content.files) {
85
+ content.files.forEach((file) => items.push(NSURL.fileURLWithPath(file)));
86
+ }
87
+ const shareController = UIActivityViewController.alloc().initWithActivityItemsApplicationActivities(items, null);
88
+ // should we use UIActivityItemSource?
89
+ // const shareController = UIActivityViewController.alloc().initWithActivityItemsApplicationActivities(
90
+ // items.map((i) => ItemSource.initWithData(i, i, options.subject, metadata)),
91
+ // null
92
+ // );
93
+ if (options.subject) {
94
+ shareController.setValueForKey(options.subject, 'subject');
95
+ }
96
+ if (options.excludedActivityTypes) {
97
+ shareController.excludedActivityTypes = NSArray.arrayWithArray(options.excludedActivityTypes);
98
+ }
99
+ const presentingController = Application.ios.rootController;
100
+ shareController.completionWithItemsHandler = (activityType, completed, error) => {
101
+ if (error) {
102
+ reject(error);
103
+ }
104
+ else if (completed || activityType == null) {
105
+ resolve(true);
106
+ }
107
+ };
108
+ shareController.modalPresentationStyle = 7 /* UIModalPresentationStyle.Popover */;
109
+ const appearance = options.appearance || Application.ios.systemAppearance;
110
+ if (appearance === 'dark') {
111
+ shareController.overrideUserInterfaceStyle = 2 /* UIUserInterfaceStyle.Dark */;
112
+ }
113
+ else if (appearance === 'light') {
114
+ shareController.overrideUserInterfaceStyle = 1 /* UIUserInterfaceStyle.Light */;
115
+ }
116
+ let sourceView = presentingController.view;
117
+ if (options.anchor) {
118
+ sourceView = options.anchor.nativeViewProtected;
119
+ }
120
+ else if (shareController.popoverPresentationController) {
121
+ shareController.popoverPresentationController.permittedArrowDirections = 0;
122
+ }
123
+ if (shareController.popoverPresentationController) {
124
+ shareController.popoverPresentationController.sourceView = sourceView;
125
+ shareController.popoverPresentationController.sourceRect = sourceView.bounds;
126
+ }
127
+ if (options.tintColor) {
128
+ const color = options.tintColor instanceof Color ? options.tintColor : new Color(options.tintColor);
129
+ shareController.view.tintColor = color.ios;
130
+ }
131
+ presentingController.presentViewControllerAnimatedCompletion(shareController, true, null);
132
+ });
133
+ }
134
+ //# sourceMappingURL=index.ios.js.map
@@ -30,6 +30,7 @@ declare namespace com {
30
30
  static getSystemLocale(): java.util.Locale;
31
31
  static getRootWindowInsets(view: android.view.View): number[];
32
32
  static listenForWindowInsetsChange(view: android.view.View, callback: WindowInsetsCallback);
33
+ static guessMimeType(context: android.content.Context, uri: android.net.Uri);
33
34
  }
34
35
  }
35
36
  export class ImageUtils extends java.lang.Object {