Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(App): Add getState method to check current app state #2611

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export interface AppPlugin extends Plugin {
*/
openUrl(options: { url: string }): Promise<{completed: boolean}>;

/**
* Gets the current app state
*/
getState(): Promise<AppState>;

/**
* Get the URL the app was launched with, if any
*/
Expand Down
6 changes: 5 additions & 1 deletion core/src/web/app.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -30,6 +30,10 @@ export class AppPluginWeb extends WebPlugin implements AppPlugin {
return Promise.resolve({ url: '' });
}

getState(): Promise<AppState> {
return Promise.resolve({ isActive: document.hidden !== true });
}

handleVisibilityChange(): void {
const data = {
isActive: document.hidden !== true
Expand Down
6 changes: 5 additions & 1 deletion electron/src/electron/app.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -25,6 +26,9 @@ export class AppPluginElectron extends WebPlugin implements AppPlugin {
getLaunchUrl(): Promise<AppLaunchUrl> {
throw new Error('Method not implemented.');
}
getState(): Promise<AppState> {
return webApp.getState();
}
}

const App = new AppPluginElectron();
Expand Down
12 changes: 10 additions & 2 deletions ios/Capacitor/Capacitor/Plugins/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down