Skip to content

Commit

Permalink
MOBILE-3947 native: Fix chooser plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Nov 29, 2023
1 parent d2fa4e6 commit c2b86a1
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions src/core/features/native/native.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -68,7 +68,6 @@ export const CORE_NATIVE_SERVICES = [
providers: [
Badge,
Camera,
Chooser,
Clipboard,
Device,
Diagnostic,
Expand Down
47 changes: 47 additions & 0 deletions src/core/features/native/plugins/chooser.ts
Original file line number Diff line number Diff line change
@@ -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);
}

}
18 changes: 18 additions & 0 deletions src/core/features/native/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 0 additions & 2 deletions src/core/singletons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions src/types/cordova-plugin-chooser.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit c2b86a1

Please sign in to comment.