diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 7c5469df9..ffbc45c69 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -27,7 +27,7 @@ import { NewActionPage } from '../pages/publications/new-action/new-action.page' import { PublicationsPage } from '../pages/publications/publications.page'; import { fetchData } from 'plugins/Echo'; import { Encoding, Filesystem, FilesystemDirectory } from '@capacitor/filesystem'; - +import { sendIntent } from 'src/app/services/shareIntent' const { App } = Plugins; @@ -140,15 +140,7 @@ export class HomePage implements OnInit { } - - /* navigator.serviceWorker.ready.then((registration) => { - console.log('yes please') - registration.active.postMessage( - "Test message sent immediately after creation", - ); - }); */ - - /* this.checkSendIntentReceived(''); */ + sendIntent.setRouteService(this.router) } @@ -172,7 +164,7 @@ export class HomePage implements OnInit { if (result.url) { let resultUrl = decodeURIComponent(result.url); Filesystem.readFile({path: resultUrl}) - + .then(async (content) => { const modal = await this.modalController.create({ @@ -184,7 +176,7 @@ export class HomePage implements OnInit { cssClass: 'new-action modal modal-desktop', backdropDismiss: false }); - + modal.onDidDismiss().then(() => { SendIntent.finish(); }); diff --git a/src/app/services/callbackScheduler.ts b/src/app/services/callbackScheduler.ts new file mode 100644 index 000000000..5e24fded2 --- /dev/null +++ b/src/app/services/callbackScheduler.ts @@ -0,0 +1,125 @@ +/** + * Manages a queue of callbacks and controls their execution with start, pause, and resume functionality. + */ +export class CallbackScheduler { + private callbackQueue: (() => void)[] = []; // A queue to store the callbacks. + private running: boolean = false; // Tracks whether the scheduler is running. + + /** + * Enqueues a callback function to be executed in the queue. + * @param callback - The callback function to be executed. + * @returns A function to rerun the callback + */ + enqueueCallback(callback: () => void): Function { + const add = () => { this.callbackQueue.push(callback); }; + add(); + return add; + } + + /** + * Adds a callback function to the queue or run immediately depending on `this.running` state + * @param callback - The callback function to be added to the queue. + * @returns A function to remove the callback from the queue. + */ + function(callback: (...args: any) => any): (...args: any) => Promise { + return async (...args: any): Promise => { + return new Promise(async (resolve) => { + if (this.running) { + console.log("running") + resolve(await callback(...args)); + } else { + console.log("callbackQueue") + this.callbackQueue.push(async () => { + resolve(await callback(...args)); + }); + } + }); + }; + } + + /** + * Starts the execution of callbacks in the queue. + */ + start(): void { + if (!this.running) { + this.running = true; + this.executeNextCallback(); + } + } + + /** + * Pauses the execution of callbacks. + */ + pause(): void { + this.running = false; + } + + /** + * Resumes the execution of callbacks. + */ + resume(): void { + if (!this.running) { + this.running = true; + this.executeNextCallback(); + } + } + + /** + * Executes the next callback in the queue, removing it after execution. + */ + executeNextCallback(): void { + if (this.running && this.callbackQueue.length > 0) { + const callback = this.callbackQueue.shift(); + callback(); + this.executeNextCallback(); // Execute the next callback + } + } +} + + +// // Example usage: +// const manager = new CallbackScheduler(); + +// manager.enqueueCallback(() => { +// console.log("Callback 1 executed."); +// }); + +// manager.enqueueCallback(() => { +// console.log("Callback 2 executed."); +// }); + +// manager.enqueueCallback(() => { +// console.log("Callback 3 executed."); +// }); + +// manager.start(); + +// setTimeout(() => { +// manager.pause(); +// console.log("Manager paused."); +// }, 3000); + +// setTimeout(() => { +// manager.resume(); +// console.log("Manager resumed."); +// }, 6000); + + +// const timingManager = new CallbackScheduler(); + +// class someResponsibility{ + + +// constructor() { + +// setTimeout(()=> { +// timingManager.start() +// }, 1000) + +// this.execute() +// } + +// execute = timingManager.triggerToEnqueueCallback(()=> { + +// }) +// } diff --git a/src/app/services/shareIntent.ts b/src/app/services/shareIntent.ts new file mode 100644 index 000000000..0f2ecce12 --- /dev/null +++ b/src/app/services/shareIntent.ts @@ -0,0 +1,50 @@ +import { Filesystem } from '@capacitor/filesystem'; +import { SendIntent } from "send-intent"; +import { Router } from '@angular/router'; +import { CallbackScheduler } from './callbackScheduler'; + +class SendIntent { + + Router!: Router + + callbackScheduler = new CallbackScheduler() + + constructor() { + + SendIntent.checkSendIntentReceived().then((result: any) => { + // logger + if (result) { + console.log('SendIntent received'); + console.log(JSON.stringify(result)); + } + // event handler + if (result.url) { + this.onReceive(result) + } + }).catch(err => console.error(err)); + } + + private initialize() { + this.callbackScheduler.start() + } + + setRouteService(Router: Router) { + this.Router = Router + this.initialize() + } + + + private onReceive = this.callbackScheduler.function((result)=> { + let resultUrl = decodeURIComponent(result.url); + Filesystem.readFile({path: resultUrl}).then(async (content) => { + + this.Router.navigateByUrl("/home/publication"); + + alert('shared') + console.log(content.data); + }) + }) + +} + +export const sendIntent = new SendIntent() diff --git a/src/main.ts b/src/main.ts index 390700473..0cbebee5f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,7 @@ import { defineCustomElements } from '@ionic/pwa-elements/loader'; import "hammerjs"; // HAMMER TIME import { SendIntent } from "send-intent"; import { Filesystem } from '@capacitor/filesystem'; - +import 'src/app/services/shareIntent' if (environment.production) { enableProdMode(); @@ -48,46 +48,4 @@ platformBrowserDynamic().bootstrapModule(AppModule) // Call the element loader after the platform has been bootstrapped - SendIntent.checkSendIntentReceived().then((result: any) => { - - if (result) { - console.log('SendIntent received'); - console.log(JSON.stringify(result)); - } - if (result.url) { - let resultUrl = decodeURIComponent(result.url); - Filesystem.readFile({path: resultUrl}) - - .then(async (content) => { - - /* const modal = await this.modalController.create({ - component: PublicationsPage, - componentProps: { - item: "item", - intent: content.data - }, - cssClass: 'new-action modal modal-desktop', - backdropDismiss: false - }); - - modal.onDidDismiss().then(() => { - SendIntent.finish(); - }); - await modal.present(); - */ - - window["sharedintend"] = content.data - - window["this.router"].navigateByUrl("/home/publication"); - - - - alert('shared') - console.log(content.data); - - }) - .catch((err) => console.error(err)); - } -}).catch(err => console.error(err)); - defineCustomElements(window);