refactor: improve build configuration and code organization
- Add build scripts for Android and iOS platforms - Remove duplicate web implementation (src/web.ts) - Add proper TypeScript configuration - Add module documentation to index.ts - Clean up package.json scripts This commit improves the project structure and build process by: 1. Adding dedicated build scripts for native platforms 2. Removing redundant web implementation 3. Adding proper TypeScript configuration with strict mode 4. Improving code documentation 5. Organizing package.json scripts The changes maintain backward compatibility while improving the development experience and code quality.
This commit is contained in:
117
src/web/index.ts
Normal file
117
src/web/index.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Web implementation of the Daily Notification plugin
|
||||
* @module DailyNotificationWeb
|
||||
*/
|
||||
|
||||
import { WebPlugin } from '@capacitor/core';
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import type { DailyNotificationPlugin, NotificationOptions, NotificationSettings, NotificationResponse, NotificationStatus, PermissionStatus, PermissionState } from '../definitions';
|
||||
|
||||
export class DailyNotificationWeb extends WebPlugin implements DailyNotificationPlugin {
|
||||
private lastNotification: NotificationResponse | null = null;
|
||||
private nextNotificationTime: string | undefined;
|
||||
private isScheduled: boolean = false;
|
||||
private lastNotificationTime: string | undefined;
|
||||
|
||||
/**
|
||||
* Initialize the daily notification system for web
|
||||
* @param options Configuration options for the notification system
|
||||
*/
|
||||
async initialize(options: NotificationOptions): Promise<void> {
|
||||
if (Capacitor.getPlatform() !== 'web') {
|
||||
throw new Error('This implementation is for web only');
|
||||
}
|
||||
// TODO: Implement web-specific initialization
|
||||
}
|
||||
|
||||
async checkPermissions(): Promise<PermissionStatus> {
|
||||
if (!('Notification' in window)) {
|
||||
return {
|
||||
notifications: 'denied' as PermissionState,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
notifications: this.mapWebPermission(Notification.permission),
|
||||
};
|
||||
}
|
||||
|
||||
async requestPermissions(): Promise<PermissionStatus> {
|
||||
if (!('Notification' in window)) {
|
||||
return {
|
||||
notifications: 'denied' as PermissionState,
|
||||
};
|
||||
}
|
||||
|
||||
const permission = await Notification.requestPermission();
|
||||
return {
|
||||
notifications: this.mapWebPermission(permission),
|
||||
};
|
||||
}
|
||||
|
||||
private mapWebPermission(permission: NotificationPermission): PermissionState {
|
||||
switch (permission) {
|
||||
case 'granted':
|
||||
return 'granted';
|
||||
case 'denied':
|
||||
return 'denied';
|
||||
default:
|
||||
return 'prompt';
|
||||
}
|
||||
}
|
||||
|
||||
async scheduleDailyNotification(options: NotificationOptions): Promise<void> {
|
||||
if (!('Notification' in window)) {
|
||||
throw new Error('Notifications not supported in this browser');
|
||||
}
|
||||
|
||||
const permission = await Notification.requestPermission();
|
||||
if (permission !== 'granted') {
|
||||
throw new Error('Notification permission denied');
|
||||
}
|
||||
|
||||
// Schedule notification using the browser's notification API
|
||||
const notification = new Notification(options.title || 'Daily Update', {
|
||||
body: options.body || 'Your daily update is ready',
|
||||
});
|
||||
|
||||
// Store notification data
|
||||
this.lastNotification = {
|
||||
title: options.title || 'Daily Update',
|
||||
body: options.body || 'Your daily update is ready',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Update status
|
||||
this.nextNotificationTime = options.time;
|
||||
this.isScheduled = true;
|
||||
}
|
||||
|
||||
async getLastNotification(): Promise<NotificationResponse | null> {
|
||||
return this.lastNotification;
|
||||
}
|
||||
|
||||
async cancelAllNotifications(): Promise<void> {
|
||||
// No direct way to cancel notifications in web, but we can clear our stored data
|
||||
this.lastNotification = null;
|
||||
this.nextNotificationTime = undefined;
|
||||
this.isScheduled = false;
|
||||
this.lastNotificationTime = undefined;
|
||||
}
|
||||
|
||||
async getNotificationStatus(): Promise<NotificationStatus> {
|
||||
return {
|
||||
isScheduled: this.isScheduled,
|
||||
nextNotificationTime: this.nextNotificationTime,
|
||||
lastNotificationTime: this.lastNotificationTime,
|
||||
};
|
||||
}
|
||||
|
||||
async updateSettings(settings: NotificationSettings): Promise<void> {
|
||||
// Web implementation might not need to do anything with settings
|
||||
// but we'll keep track of them
|
||||
if (settings.time) {
|
||||
this.nextNotificationTime = settings.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user