diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index f4fe25bb84..086725565c 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -33,6 +33,12 @@ + + + + + + diff --git a/android/app/src/main/java/org/lichess/mobileapp/LiIntent.java b/android/app/src/main/java/org/lichess/mobileapp/LiIntent.java new file mode 100644 index 0000000000..2dbab38db4 --- /dev/null +++ b/android/app/src/main/java/org/lichess/mobileapp/LiIntent.java @@ -0,0 +1,22 @@ +package org.lichess.mobileapp; + +import android.content.Intent; + +import com.getcapacitor.JSObject; +import com.getcapacitor.NativePlugin; +import com.getcapacitor.Plugin; + +@NativePlugin() +public class LiIntent extends Plugin { + @Override + protected void handleOnNewIntent(Intent intent) { + String action = intent.getAction(); + String text = intent.getStringExtra(Intent.EXTRA_TEXT); + + if (Intent.ACTION_SEND.equals(action) && text != null) { + JSObject obj = new JSObject(); + obj.put("pgn", text); + notifyListeners("importPGN", obj, true); + } + } +} diff --git a/android/app/src/main/java/org/lichess/mobileapp/MainActivity.java b/android/app/src/main/java/org/lichess/mobileapp/MainActivity.java index 0cc6b115fd..fa346274f2 100644 --- a/android/app/src/main/java/org/lichess/mobileapp/MainActivity.java +++ b/android/app/src/main/java/org/lichess/mobileapp/MainActivity.java @@ -9,7 +9,6 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.PatternMatcher; import android.util.Log; import android.webkit.WebView; @@ -17,7 +16,6 @@ import com.getcapacitor.Dialogs; import com.getcapacitor.Plugin; import com.getcapacitor.community.keepawake.KeepAwake; -import com.getcapacitor.plugin.Storage; import org.lichess.plugin.SoundEffect; import org.lichess.mobileapp.stockfish.Stockfish; @@ -41,6 +39,7 @@ public void onCreate(Bundle savedInstanceState) { // Initializes the Bridge this.init(savedInstanceState, new ArrayList>() {{ // Additional plugins you've installed go here + add(LiIntent.class); add(LiToast.class); add(LiShare.class); add(LiBuildConfig.class); diff --git a/src/app.ts b/src/app.ts index cf0c4dbce7..8fa89f262c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -14,7 +14,9 @@ import push from './push' import router from './router' import socket from './socket' import sound from './sound' +import * as utils from './utils' import { isForeground, setForeground, setBackground } from './utils/appMode' +import { ImportEvent } from './ui/importer/importer' let firstConnection = true @@ -88,6 +90,13 @@ export default function appInit( Plugins.App.addListener('backButton', router.backbutton) + Plugins.LiIntent.addListener('importPGN', (e: ImportEvent) => { + const params: Record = { + pgn: e.pgn, + } + router.set(`/importer?${utils.serializeQueryParameters(params)}`) + }) + window.addEventListener('resize', debounce(onResize), false) } diff --git a/src/ui/importer/ImporterCtrl.ts b/src/ui/importer/ImporterCtrl.ts index de17326b03..dee5c1592f 100644 --- a/src/ui/importer/ImporterCtrl.ts +++ b/src/ui/importer/ImporterCtrl.ts @@ -9,12 +9,15 @@ import { OnlineGameData } from '../../lichess/interfaces/game' export interface IImporterCtrl { importGame(pgn: string): void importing: Prop + pgn: Prop } export default function ImporterCtrl(): IImporterCtrl { const importing = prop(false) + const pgn = prop(null) + function submitOnline(pgn: string, analyse: boolean): Promise { const data: {[i: string]: string } = { pgn } if (analyse) data.analyse = '1' @@ -45,6 +48,7 @@ export default function ImporterCtrl(): IImporterCtrl { handleXhrError(err) }) }, - importing + importing, + pgn } } diff --git a/src/ui/importer/importer.ts b/src/ui/importer/importer.ts index fadacf1e1c..13077d2fe6 100644 --- a/src/ui/importer/importer.ts +++ b/src/ui/importer/importer.ts @@ -13,10 +13,15 @@ interface State { ctrl: IImporterCtrl } -const ImporterScreen: Mithril.Component, State> = { - oninit() { +export interface ImportEvent { + pgn: string +} + +const ImporterScreen: Mithril.Component = { + oninit(vnode) { socket.createDefault() this.ctrl = ImporterCtrl() + this.ctrl.pgn(vnode.attrs.pgn) }, oncreate: helper.viewFadeIn, @@ -35,13 +40,17 @@ function renderBody(ctrl: IImporterCtrl) { h('form', { onsubmit: (e: Event) => { e.preventDefault() - const target = e.target as HTMLFormElement - const pgn: string = (target[0] as HTMLInputElement).value - if (pgn) ctrl.importGame(pgn) + if (ctrl.pgn()) ctrl.importGame(ctrl.pgn()!) } }, [ h('label', i18n('pasteThePgnStringHere') + ' :'), - h('textarea.pgnImport'), + h('textarea.pgnImport', { + value: ctrl.pgn(), + onchange(e: Event) { + const target = e.target as HTMLTextAreaElement + ctrl.pgn(target.value) + } + }), formWidgets.renderCheckbox(i18n('requestAComputerAnalysis'), 'analyse', settings.importer.analyse), h('button.fatButton', ctrl.importing() ? h('div.fa.fa-hourglass-half') : i18n('importGame'))