diff --git a/package-lock.json b/package-lock.json index 13db67f1a..db6841e8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,6 @@ "@angular/router": "^16.2.0", "@awesome-cordova-plugins/badge": "^6.3.0", "@awesome-cordova-plugins/camera": "^6.3.0", - "@awesome-cordova-plugins/chooser": "^6.3.0", "@awesome-cordova-plugins/clipboard": "^6.3.0", "@awesome-cordova-plugins/core": "^6.3.0", "@awesome-cordova-plugins/device": "^6.3.0", @@ -809,18 +808,6 @@ "rxjs": "^5.5.0 || ^6.5.0 || ^7.3.0" } }, - "node_modules/@awesome-cordova-plugins/chooser": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@awesome-cordova-plugins/chooser/-/chooser-6.4.0.tgz", - "integrity": "sha512-/0Tei8A9Ms2sE9VyWKtt0pVC9r+d9hURQxFnVmtZfKX3nM5fdYRRfzh5BBIrJzn4YMV+TIOv6g3EE1YAhvYTpA==", - "dependencies": { - "@types/cordova": "latest" - }, - "peerDependencies": { - "@awesome-cordova-plugins/core": "^6.0.1", - "rxjs": "^5.5.0 || ^6.5.0 || ^7.3.0" - } - }, "node_modules/@awesome-cordova-plugins/clipboard": { "version": "6.4.0", "resolved": "https://registry.npmjs.org/@awesome-cordova-plugins/clipboard/-/clipboard-6.4.0.tgz", diff --git a/package.json b/package.json index ed6a23c9b..d1cb0a4a2 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "@angular/router": "^16.2.0", "@awesome-cordova-plugins/badge": "^6.3.0", "@awesome-cordova-plugins/camera": "^6.3.0", - "@awesome-cordova-plugins/chooser": "^6.3.0", "@awesome-cordova-plugins/clipboard": "^6.3.0", "@awesome-cordova-plugins/core": "^6.3.0", "@awesome-cordova-plugins/device": "^6.3.0", diff --git a/src/core/features/fileuploader/services/fileuploader-helper.ts b/src/core/features/fileuploader/services/fileuploader-helper.ts index d1d2cf9c1..532eabb19 100644 --- a/src/core/features/fileuploader/services/fileuploader-helper.ts +++ b/src/core/features/fileuploader/services/fileuploader-helper.ts @@ -15,7 +15,7 @@ import { Injectable } from '@angular/core'; import { ActionSheetButton } from '@ionic/core'; import { CameraOptions } from '@awesome-cordova-plugins/camera/ngx'; -import { ChooserResult } from '@awesome-cordova-plugins/chooser/ngx'; +import { ChooserResult } from 'cordova-plugin-chooser'; import { FileEntry, IFile } from '@awesome-cordova-plugins/file/ngx'; import { MediaFile } from '@awesome-cordova-plugins/media-capture/ngx'; @@ -25,7 +25,7 @@ import { CoreDomUtils } from '@services/utils/dom'; import { CoreMimetypeUtils } from '@services/utils/mimetype'; import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; -import { makeSingleton, Translate, Camera, Chooser, ActionSheetController } from '@singletons'; +import { makeSingleton, Translate, Camera, ActionSheetController } from '@singletons'; import { CoreLogger } from '@singletons/logger'; import { CoreCanceledError } from '@classes/errors/cancelederror'; import { CoreError } from '@classes/errors/error'; @@ -43,6 +43,7 @@ import { CoreSites } from '@services/sites'; import { CorePath } from '@singletons/path'; import { CorePromisedValue } from '@classes/promised-value'; import { CorePlatform } from '@services/platform'; +import { Chooser } from '@features/native/plugins'; /** * Helper service to upload files. diff --git a/src/core/features/native/native.module.ts b/src/core/features/native/native.module.ts index 1e1404954..b1bd71648 100644 --- a/src/core/features/native/native.module.ts +++ b/src/core/features/native/native.module.ts @@ -16,7 +16,7 @@ import { NgModule } from '@angular/core'; import { Badge } from '@awesome-cordova-plugins/badge/ngx'; import { Camera } from '@awesome-cordova-plugins/camera/ngx'; -import { Chooser } from '@awesome-cordova-plugins/chooser/ngx'; +import { Chooser } from '@features/native/plugins/chooser'; import { Clipboard } from '@awesome-cordova-plugins/clipboard/ngx'; import { Device } from '@awesome-cordova-plugins/device/ngx'; import { Diagnostic } from '@awesome-cordova-plugins/diagnostic/ngx'; @@ -68,7 +68,6 @@ export const CORE_NATIVE_SERVICES = [ providers: [ Badge, Camera, - Chooser, Clipboard, Device, Diagnostic, diff --git a/src/core/features/native/plugins/chooser.ts b/src/core/features/native/plugins/chooser.ts new file mode 100644 index 000000000..6e13746d7 --- /dev/null +++ b/src/core/features/native/plugins/chooser.ts @@ -0,0 +1,47 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable } from '@angular/core'; + +/** + * Chooser plugin wrapper. + */ +@Injectable({ providedIn: 'root' }) +export class Chooser { + + /** + * Displays native prompt for user to select a file. + * + * @param accept Optional MIME type filter (e.g. 'image/gif,video/*'). + * @returns Selected file's raw binary data, base64-encoded data: URI, MIME type, display name, and original URI. + * If user cancels, promise will be resolved as undefined. + * If error occurs, promise will be rejected. + */ + getFile(accept?: string): Promise { + return cordova.chooser.getFile(accept); + } + + /** + * Displays native prompt for user to select a file. + * + * @param accept Optional MIME type filter (e.g. 'image/gif,video/*'). + * @returns Selected file's MIME type, display name, and original URI. + * If user cancels, promise will be resolved as undefined. + * If error occurs, promise will be rejected. + */ + getFileMetadata(accept?: string): Promise { + return cordova.chooser.getFileMetadata(accept); + } + +} diff --git a/src/core/features/native/plugins/index.ts b/src/core/features/native/plugins/index.ts new file mode 100644 index 000000000..8efe862c2 --- /dev/null +++ b/src/core/features/native/plugins/index.ts @@ -0,0 +1,18 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { makeSingleton } from '@singletons'; +import { Chooser as ChooserService } from './chooser'; + +export const Chooser = makeSingleton(ChooserService); diff --git a/src/core/singletons/index.ts b/src/core/singletons/index.ts index 51df7f5f8..3cc93fbb2 100644 --- a/src/core/singletons/index.ts +++ b/src/core/singletons/index.ts @@ -39,7 +39,6 @@ import { import { Badge as BadgeService } from '@awesome-cordova-plugins/badge/ngx'; import { Camera as CameraService } from '@awesome-cordova-plugins/camera/ngx'; -import { Chooser as ChooserService } from '@awesome-cordova-plugins/chooser/ngx'; import { Clipboard as ClipboardService } from '@awesome-cordova-plugins/clipboard/ngx'; import { Diagnostic as DiagnosticService } from '@awesome-cordova-plugins/diagnostic/ngx'; import { Device as DeviceService } from '@awesome-cordova-plugins/device/ngx'; @@ -175,7 +174,6 @@ export function makeSingleton( // Convert ionic-native services to singleton. export const Badge = makeSingleton(BadgeService); -export const Chooser = makeSingleton(ChooserService); export const Clipboard = makeSingleton(ClipboardService); export const Diagnostic = makeSingleton(DiagnosticService); export const File = makeSingleton(FileService); diff --git a/src/types/cordova-plugin-chooser.d.ts b/src/types/cordova-plugin-chooser.d.ts new file mode 100644 index 000000000..6afe573cf --- /dev/null +++ b/src/types/cordova-plugin-chooser.d.ts @@ -0,0 +1,40 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * Types for chooser cordova plugin. + * + * @see https://github.com/cyph/cordova-plugin-chooser + */ + +interface IChooserResult { + data?: Uint8Array; + dataURI?: string; + mediaType: string; + name: string; + uri: string; +} + +interface Cordova { + + chooser: { + getFile(accept?: string): Promise; + getFileMetadata(accept?: string): Promise; + }; + +} + +declare module 'cordova-plugin-chooser' { + export type ChooserResult = IChooserResult; +}