From f20bf293e24daff3f51ed2c08192cd9b7e30b7b6 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 20 Mar 2020 20:31:43 +0100 Subject: [PATCH] feat(App): Add getState method to check current app state (#2611) --- .../src/main/java/com/getcapacitor/plugin/App.java | 9 +++++++++ core/src/core-plugin-definitions.ts | 5 +++++ core/src/web/app.ts | 6 +++++- electron/src/electron/app.ts | 6 +++++- ios/Capacitor/Capacitor/Plugins/App.swift | 12 ++++++++++-- ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m | 1 + 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java index 5135d16d65..1fe2be2837 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java @@ -19,11 +19,13 @@ public class App extends Plugin { private static final String EVENT_URL_OPEN = "appUrlOpen"; private static final String EVENT_STATE_CHANGE = "appStateChange"; private static final String EVENT_RESTORED_RESULT = "appRestoredResult"; + private boolean isActive = false; public void fireChange(boolean isActive) { Log.d(getLogTag(), "Firing change: " + isActive); JSObject data = new JSObject(); data.put("isActive", isActive); + this.isActive = isActive; notifyListeners(EVENT_STATE_CHANGE, data, false); } @@ -60,6 +62,13 @@ public void getLaunchUrl(PluginCall call) { } } + @PluginMethod() + public void getState(PluginCall call) { + JSObject data = new JSObject(); + data.put("isActive", this.isActive); + call.success(data); + } + @PluginMethod() public void canOpenUrl(PluginCall call) { String url = call.getString("url"); diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 1972f51c2b..689584c702 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -107,6 +107,11 @@ export interface AppPlugin extends Plugin { */ openUrl(options: { url: string }): Promise<{completed: boolean}>; + /** + * Gets the current app state + */ + getState(): Promise; + /** * Get the URL the app was launched with, if any */ diff --git a/core/src/web/app.ts b/core/src/web/app.ts index 4ada3d4797..42e05514c3 100644 --- a/core/src/web/app.ts +++ b/core/src/web/app.ts @@ -1,6 +1,6 @@ import { WebPlugin } from './index'; -import { AppPlugin, AppLaunchUrl } from '../core-plugin-definitions'; +import { AppPlugin, AppLaunchUrl, AppState } from '../core-plugin-definitions'; export class AppPluginWeb extends WebPlugin implements AppPlugin { constructor() { @@ -30,6 +30,10 @@ export class AppPluginWeb extends WebPlugin implements AppPlugin { return Promise.resolve({ url: '' }); } + getState(): Promise { + return Promise.resolve({ isActive: document.hidden !== true }); + } + handleVisibilityChange(): void { const data = { isActive: document.hidden !== true diff --git a/electron/src/electron/app.ts b/electron/src/electron/app.ts index b2fb245222..57ce891610 100644 --- a/electron/src/electron/app.ts +++ b/electron/src/electron/app.ts @@ -1,6 +1,7 @@ -import { WebPlugin, AppPlugin, AppLaunchUrl } from '@capacitor/core'; +import { WebPlugin, AppPlugin, AppPluginWeb, AppLaunchUrl, AppState } from '@capacitor/core'; const { remote, shell } = require('electron'); +const webApp = new AppPluginWeb(); export class AppPluginElectron extends WebPlugin implements AppPlugin { constructor() { @@ -25,6 +26,9 @@ export class AppPluginElectron extends WebPlugin implements AppPlugin { getLaunchUrl(): Promise { throw new Error('Method not implemented.'); } + getState(): Promise { + return webApp.getState(); + } } const App = new AppPluginElectron(); diff --git a/ios/Capacitor/Capacitor/Plugins/App.swift b/ios/Capacitor/Capacitor/Plugins/App.swift index bcfc8427b5..fca086c181 100644 --- a/ios/Capacitor/Capacitor/Plugins/App.swift +++ b/ios/Capacitor/Capacitor/Plugins/App.swift @@ -53,7 +53,7 @@ public class CAPAppPlugin : CAPPlugin { @objc func exitApp(_ call: CAPPluginCall) { call.unimplemented() } - + @objc func getLaunchUrl(_ call: CAPPluginCall) { if let lastUrl = CAPBridge.getLastUrl() { let urlValue = lastUrl.absoluteString @@ -63,7 +63,15 @@ public class CAPAppPlugin : CAPPlugin { } call.resolve() } - + + @objc func getState(_ call: CAPPluginCall) { + DispatchQueue.main.async { + call.resolve([ + "isActive": UIApplication.shared.applicationState == UIApplication.State.active + ]) + } + } + @objc func canOpenUrl(_ call: CAPPluginCall) { guard let urlString = call.getString("url") else { call.error("Must supply a URL") diff --git a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m index 6881f69026..64be5e1961 100644 --- a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m +++ b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m @@ -11,6 +11,7 @@ CAP_PLUGIN(CAPAppPlugin, "App", CAP_PLUGIN_METHOD(exitApp, CAPPluginReturnNone); CAP_PLUGIN_METHOD(getLaunchUrl, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(getState, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(canOpenUrl, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(openUrl, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);