Skip to content

Commit 3e4cb1d

Browse files
Veselina RadevaVeselina Radeva
authored andcommitted
chore: Move permission request for Android 6+ into the plugin
1 parent 5c443b8 commit 3e4cb1d

File tree

10 files changed

+52
-89
lines changed

10 files changed

+52
-89
lines changed

README.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,22 @@ var context = imagepicker.create({ mode: "single" }); // use "multiple" for mult
5858
### Request permissions, show the images list and process the selection
5959
6060
```
61-
requestPermissions()
62-
.then(function() {
63-
context
64-
.authorize()
65-
.then(function() {
66-
return context.present();
67-
})
68-
.then(function(selection) {
69-
selection.forEach(function(selected) {
70-
// process the selected image
71-
});
72-
list.items = selection;
73-
}).catch(function (e) {
74-
// process error
61+
context
62+
.authorize()
63+
.then(function() {
64+
return context.present();
65+
})
66+
.then(function(selection) {
67+
selection.forEach(function(selected) {
68+
// process the selected image
7569
});
76-
}).catch(function (e) {
77-
// process error
78-
});
70+
list.items = selection;
71+
}).catch(function (e) {
72+
// process error
73+
});
7974
```
8075
81-
`requestPermissions` is used to request Android specific permissions which is required for Android 6+ (API 23+). We use [nativescript-permissions](https://www.npmjs.com/package/nativescript-permissions) plugin to do so. Review the demo projects for detailed implementation.
76+
> **NOTE**: To request permissions for Android 6+ (API 23+) we use [nativescript-permissions](https://www.npmjs.com/package/nativescript-permissions).
8277
8378
## API
8479

demo-angular/app/app.component.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, ChangeDetectorRef } from "@angular/core";
22
import { ListView } from "tns-core-modules/ui/list-view";
33
import { isAndroid } from "tns-core-modules/platform";
4-
import * as permissions from "nativescript-permissions";
54
import * as imagepicker from "nativescript-imagepicker";
65

76
@Component({
@@ -31,34 +30,24 @@ export class AppComponent {
3130

3231
startSelection(context) {
3332
let _that = this;
34-
this.requestPermissions()
35-
.then(function(){
36-
context
37-
.authorize()
38-
.then(() => {
39-
_that.items = [];
40-
return context.present();
41-
})
42-
.then((selection) => {
43-
console.log("Selection done:");
44-
selection.forEach(function (selected) {
45-
console.log("----------------");
46-
console.log("uri: " + selected.uri);
47-
console.log("fileUri: " + selected.fileUri);
48-
});
49-
_that.items = selection;
50-
_that._changeDetectionRef.detectChanges();
51-
}).catch(function (e) {
52-
console.log(e);
33+
34+
context
35+
.authorize()
36+
.then(() => {
37+
_that.items = [];
38+
return context.present();
39+
})
40+
.then((selection) => {
41+
console.log("Selection done:");
42+
selection.forEach(function (selected) {
43+
console.log("----------------");
44+
console.log("uri: " + selected.uri);
45+
console.log("fileUri: " + selected.fileUri);
5346
});
47+
_that.items = selection;
48+
_that._changeDetectionRef.detectChanges();
49+
}).catch(function (e) {
50+
console.log(e);
5451
});
5552
}
56-
57-
requestPermissions() {
58-
if (isAndroid && (<any>android).os.Build.VERSION.SDK_INT >= 23) {
59-
return permissions.requestPermission([(<any>android).Manifest.permission.READ_EXTERNAL_STORAGE]);
60-
} else {
61-
return Promise.resolve();
62-
}
63-
}
6453
}

demo-angular/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@angular/router": "~4.1.0",
2020
"nativescript-angular": "~3.1.0",
2121
"nativescript-imagepicker": "../src",
22-
"nativescript-permissions": "~1.2.3",
2322
"nativescript-theme-core": "^1.0.4",
2423
"nativescript-unit-test-runner": "^0.3.4",
2524
"reflect-metadata": "~0.1.8",

demo-angular/references.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

demo/app/main-page.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { EventData } from 'tns-core-modules/data/observable';
22
import { Page } from 'tns-core-modules/ui/page';
33
import { isAndroid } from "tns-core-modules/platform";
44
import * as imagepicker from "nativescript-imagepicker";
5-
import * as permissions from "nativescript-permissions";
65

76
let list;
87

@@ -22,33 +21,20 @@ export function onSelectSingleTap(args) {
2221
}
2322

2423
function startSelection(context) {
25-
requestPermissions()
24+
context
25+
.authorize()
2626
.then(function() {
27-
context
28-
.authorize()
29-
.then(function() {
30-
list.items = [];
31-
return context.present();
32-
})
33-
.then(function(selection) {
34-
console.log("Selection done:");
35-
selection.forEach(function(selected) {
36-
console.log("----------------");
37-
console.log("uri: " + selected.uri);
38-
});
39-
list.items = selection;
40-
}).catch(function (e) {
41-
console.log(e);
42-
});
27+
list.items = [];
28+
return context.present();
29+
})
30+
.then(function(selection) {
31+
console.log("Selection done:");
32+
selection.forEach(function(selected) {
33+
console.log("----------------");
34+
console.log("uri: " + selected.uri);
35+
});
36+
list.items = selection;
4337
}).catch(function (e) {
4438
console.log(e);
4539
});
46-
}
47-
48-
function requestPermissions() {
49-
if (isAndroid && (<any>android).os.Build.VERSION.SDK_INT >= 23) {
50-
return permissions.requestPermission([(<any>android).Manifest.permission.READ_EXTERNAL_STORAGE]);
51-
} else {
52-
return Promise.resolve();
53-
}
54-
}
40+
}

demo/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
},
1111
"dependencies": {
1212
"nativescript-imagepicker": "../src",
13-
"nativescript-permissions": "~1.2.3",
1413
"nativescript-theme-core": "^1.0.4",
1514
"nativescript-unit-test-runner": "^0.3.4",
1615
"tns-core-modules": "^3.1.0"
@@ -53,4 +52,4 @@
5352
"build-android-bundle": "npm run ns-bundle --android --build-app",
5453
"build-ios-bundle": "npm run ns-bundle --ios --build-app"
5554
}
56-
}
55+
}

demo/references.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/imagepicker.android.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as imagesource from "tns-core-modules/image-source";
33
import * as application from "tns-core-modules/application";
44
import * as platform from "tns-core-modules/platform";
55
import * as imageAssetModule from "tns-core-modules/image-asset";
6+
import * as permissions from "nativescript-permissions";
67

78
interface ArrayBufferStatic extends ArrayBufferConstructor {
89
from(buffer: java.nio.ByteBuffer): ArrayBuffer;
@@ -317,7 +318,11 @@ export class ImagePicker {
317318
}
318319

319320
authorize(): Promise<void> {
320-
return Promise.resolve();
321+
if ((<any>android).os.Build.VERSION.SDK_INT >= 23) {
322+
return permissions.requestPermission([(<any>android).Manifest.permission.READ_EXTERNAL_STORAGE]);
323+
} else {
324+
return Promise.resolve();
325+
}
321326
}
322327

323328
present(): Promise<SelectedAsset[]> {

src/index.d.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,6 @@ interface Options {
9393
* Set the text for the albums button in iOS
9494
*/
9595
albumsText?: string;
96-
97-
android?: {
98-
/**
99-
* Provide a reason for permission request to access external storage on api levels above 23.
100-
*/
101-
read_external_storage?: string;
102-
};
10396
}
10497

10598
export function create(options?: Options): ImagePicker;

src/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"tslint": "~5.4.3"
5151
},
5252
"dependencies": {
53-
"nativescript-telerik-ui": "^3.0.0"
53+
"nativescript-telerik-ui": "^3.0.0",
54+
"nativescript-permissions": "~1.2.3"
5455
},
5556
"bootstrapper": "nativescript-plugin-seed"
5657
}

0 commit comments

Comments
 (0)