Merge pull request #3863 from NoelDeMartin/MOBILE-3947

MOBILE-3947 native: Fix chooser plugin
main
Dani Palou 2023-11-29 09:07:10 +01:00 committed by GitHub
commit fbc19eed43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 109 additions and 20 deletions

13
package-lock.json generated
View File

@ -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",
@ -808,18 +807,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",

View File

@ -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",

View File

@ -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.

View File

@ -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,

View File

@ -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<IChooserResult | undefined> {
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<IChooserResult | undefined> {
return cordova.chooser.getFileMetadata(accept);
}
}

View File

@ -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);

View File

@ -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<Service extends object = object>(
// 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);

View File

@ -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<IChooserResult | undefined>;
getFileMetadata(accept?: string): Promise<IChooserResult | undefined>;
};
}
declare module 'cordova-plugin-chooser' {
export type ChooserResult = IChooserResult;
}