@capacitor/core#Capacitor TypeScript Examples
The following examples show how to use
@capacitor/core#Capacitor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: capture-receipt.component.ts From fyle-mobile-app with MIT License | 6 votes |
toggleFlashMode() {
if (Capacitor.getPlatform() !== 'web') {
let nextActiveFlashMode = 'on';
if (this.flashMode === 'on') {
nextActiveFlashMode = 'off';
}
CameraPreview.setFlashMode({ flashMode: nextActiveFlashMode });
this.flashMode = nextActiveFlashMode;
this.trackingService.flashModeSet({
FlashMode: this.flashMode,
});
}
}
Example #2
Source File: capture-receipt.component.ts From fyle-mobile-app with MIT License | 6 votes |
async getFlashModes() {
if (Capacitor.getPlatform() !== 'web') {
CameraPreview.getSupportedFlashModes().then((flashModes) => {
if (flashModes.result && flashModes.result.includes('on') && flashModes.result.includes('off')) {
this.flashMode = this.flashMode || 'off';
CameraPreview.setFlashMode({ flashMode: this.flashMode });
}
});
}
}
Example #3
Source File: home.page.ts From angular-sqlite-app-starter with MIT License | 6 votes |
constructor(private _sqlite: SQLiteService,
private _detailService: DetailService,
private _ref: ChangeDetectorRef) {
this.platform = Capacitor.getPlatform();
const mConfig = config.plugins && config.plugins.CapacitorSQLite ? config.plugins.CapacitorSQLite : null;
if(this.platform === "android" && mConfig != null) {
this.isBiometric = mConfig.androidBiometric && mConfig.androidBiometric.biometricAuth
? mConfig.androidBiometric.biometricAuth : false;
}
if(this.platform === "ios" && mConfig != null) {
this.isBiometric = mConfig.iosBiometric && mConfig.iosBiometric.biometricAuth
? mConfig.iosBiometric.biometricAuth : false;
}
console.log(`>>>>> in Home constructor ${this.isBiometric }`)
}
Example #4
Source File: sqlite.service.ts From angular-sqlite-app-starter with MIT License | 6 votes |
/**
* Plugin Initialization
*/
initializePlugin(): Promise<boolean> {
return new Promise (resolve => {
this.platform = Capacitor.getPlatform();
if(this.platform === 'ios' || this.platform === 'android') this.native = true;
this.sqlitePlugin = CapacitorSQLite;
this.sqlite = new SQLiteConnection(this.sqlitePlugin);
this.isService = true;
resolve(true);
});
}
Example #5
Source File: custom-camera.service.ts From capture-lite with GNU General Public License v3.0 | 6 votes |
private mediaItemFromFilePath(
filePath: string,
type: CustomCameraMediaType
): CustomCameraMediaItem {
const src = Capacitor.convertFileSrc(filePath);
const safeUrl = this.sanitizer.bypassSecurityTrustUrl(src);
const mimeType: CustomCameraMimeType =
type === 'image' ? 'image/jpeg' : 'video/mp4';
const newItem = { filePath, src, safeUrl, type, mimeType };
return newItem;
}
Example #6
Source File: media-store.service.ts From capture-lite with GNU General Public License v3.0 | 6 votes |
private async _write(index: string, base64: string, mimeType: MimeType) {
await this.initialize();
return this.mutex.runExclusive(async () => {
const mediaExtension = await this.setMediaExtension(index, mimeType);
if (Capacitor.isNative) {
const blob = await base64ToBlob(base64, mimeType);
await write_blob({
directory: this.directory,
path: `${this.rootDir}/${index}.${mediaExtension.extension}`,
blob: blob,
recursive: true,
});
} else {
await this.filesystemPlugin.writeFile({
directory: this.directory,
path: `${this.rootDir}/${index}.${mediaExtension.extension}`,
data: base64,
recursive: true,
});
}
return index;
});
}
Example #7
Source File: media-store.service.ts From capture-lite with GNU General Public License v3.0 | 6 votes |
/**
* Use this method when loading large image. Read data as base64 string
* directly when dealing with small image for better performance.
*/
async getUrl(index: string, mimeType: MimeType) {
if (Capacitor.isNative) {
// Workaround to fix urls (thumbnails) saved as incorrect mimeType.
await this.fixIncorrectExtension(index, mimeType);
return Capacitor.convertFileSrc(await this.getUri(index));
}
return URL.createObjectURL(
await base64ToBlob(await this.readWithFileSystem(index), mimeType)
);
}
Example #8
Source File: push-notification.service.ts From capture-lite with GNU General Public License v3.0 | 6 votes |
async register() {
if (!Capacitor.isPluginAvailable('PushNotifications')) {
return;
}
const result = await this.pushNotificationsPlugin.requestPermissions();
return new Promise<string>((resolve, reject) => {
if (result.receive !== 'granted') {
reject(new Error('Push notification permission denied.'));
}
this.pushNotificationsPlugin.addListener('registration', token => {
this.token$.next(token.value);
resolve(token.value);
});
this.pushNotificationsPlugin.addListener('registrationError', reject);
this.pushNotificationsPlugin.register();
});
}
Example #9
Source File: push-notification.service.ts From capture-lite with GNU General Public License v3.0 | 6 votes |
constructor(
@Inject(PUSH_NOTIFICATIONS_PLUGIN)
private readonly pushNotificationsPlugin: PushNotificationsPlugin
) {
if (Capacitor.isPluginAvailable('PushNotifications')) {
this.pushNotificationsPlugin.addListener(
'pushNotificationReceived',
notification => {
this.pushData$.next(notification.data);
}
);
this.pushNotificationsPlugin.addListener(
'pushNotificationActionPerformed',
notification => {
this.pushData$.next(notification.notification.data);
}
);
}
}
Example #10
Source File: device.service.ts From fyle-mobile-app with MIT License | 5 votes |
//App plugin does not have a web implementation
getAppInfo(): Promise<AppInfo> | Observable<{ version: string }> {
return Capacitor.getPlatform() === 'web' ? of({ version: '1.2.3' }) : App.getInfo();
}
Example #11
Source File: push-notification.service.ts From fyle-mobile-app with MIT License | 5 votes |
initPush() {
if (Capacitor.getPlatform() !== 'web') {
this.registerPush();
}
}
Example #12
Source File: index.tsx From react-sqlite-app-starter with MIT License | 5 votes |
window.addEventListener('DOMContentLoaded', async () => {
const platform = Capacitor.getPlatform();
const sqlite: SQLiteConnection = new SQLiteConnection(CapacitorSQLite)
try {
if(platform === "web") {
const jeepEl = document.createElement("jeep-sqlite");
document.body.appendChild(jeepEl);
await customElements.whenDefined('jeep-sqlite');
await sqlite.initWebStore();
}
const ret = await sqlite.checkConnectionsConsistency();
const isConn = (await sqlite.isConnection("db_issue9")).result;
var db: SQLiteDBConnection
if (ret.result && isConn) {
db = await sqlite.retrieveConnection("db_issue9");
} else {
db = await sqlite.createConnection("db_issue9", false, "no-encryption", 1);
}
await db.open();
let query = `
CREATE TABLE IF NOT EXISTS test (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL
);
`
const res: any = await db.execute(query);
console.log(`res: ${JSON.stringify(res)}`);
await db.close();
await sqlite.closeConnection("db_issue9");
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorkerRegistration.unregister();
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
} catch (err) {
console.log(`Error: ${err}`);
throw new Error(`Error: ${err}`)
}
});
Example #13
Source File: go-pro-media.service.ts From capture-lite with GNU General Public License v3.0 | 5 votes |
async uploadToCaptureFromGoProCamera(
mediaFile: GoProFile | undefined
): Promise<{
isDownloaded: boolean;
isCaptured: boolean;
}> {
if (!mediaFile) return { isDownloaded: false, isCaptured: false };
const fileName = extractFileNameFromGoProUrl(mediaFile.url);
let isDownloaded = false;
let isCaptured = false;
try {
await Http.downloadFile({
url: mediaFile.url,
filePath: fileName,
fileDirectory: this.directory,
method: 'GET',
});
const readResult = await this.filesystemPlugin.getUri({
directory: this.directory,
path: fileName,
});
const url = Capacitor.convertFileSrc(readResult.uri);
const blob = await this.httpClient
.get(url, { responseType: 'blob' })
.toPromise();
const base64 = await blobToBase64(blob);
const mimeType = urlIsImage(mediaFile.url) ? 'image/jpeg' : 'video/mp4';
isDownloaded = true;
await this.captureService.capture({ base64, mimeType });
isCaptured = true;
// delete temp downloaded file
await this.filesystemPlugin.deleteFile({
directory: this.directory,
path: fileName,
});
} catch (error: any) {
const printIndentation = 2;
// eslint-disable-next-line no-console
console.warn(`'? ${JSON.stringify(error, null, printIndentation)}`);
}
return { isDownloaded, isCaptured };
}
Example #14
Source File: dia-backend-notification.service.ts From capture-lite with GNU General Public License v3.0 | 5 votes |
private async needLocalNotification() {
if (Capacitor.platform !== 'android') {
return false;
}
return (await this.appPlugin.getState()).isActive;
}