gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-wallet-webex] branch master updated: android helpers


From: gnunet
Subject: [GNUnet-SVN] [taler-wallet-webex] branch master updated: android helpers
Date: Mon, 19 Aug 2019 20:24:34 +0200

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository wallet-webex.

The following commit(s) were added to refs/heads/master by this push:
     new 5fa70b1e android helpers
5fa70b1e is described below

commit 5fa70b1eb39d4fa309ee7109074513ea685a33b6
Author: Florian Dold <address@hidden>
AuthorDate: Mon Aug 19 20:24:29 2019 +0200

    android helpers
---
 src/android/index.ts    | 56 ++++++++++++++++++++++++++++++++++---------------
 src/headless/helpers.ts | 20 +++++++++++-------
 2 files changed, 52 insertions(+), 24 deletions(-)

diff --git a/src/android/index.ts b/src/android/index.ts
index abf065a8..b8958b41 100644
--- a/src/android/index.ts
+++ b/src/android/index.ts
@@ -18,28 +18,26 @@
  * Imports.
  */
 import { Wallet } from "../wallet";
-import { getDefaultNodeWallet } from "../headless/helpers";
-
-class AndroidWalletHelper {
-  walletPromise: Promise<Wallet> | undefined;
-  constructor() {}
-
-  async init() {
-    this.walletPromise = getDefaultNodeWallet();
-  }
-}
+import { getDefaultNodeWallet, withdrawTestBalance } from 
"../headless/helpers";
+import { openPromise } from "../promiseUtils";
 
 export function installAndroidWalletListener() {
   // @ts-ignore
-  const sendMessage: (m: any) => void = global.__akono_sendMessage;
+  const sendMessage: (m: string) => void = global.__akono_sendMessage;
   if (typeof sendMessage !== "function") {
     const errMsg =
       "FATAL: cannot install android wallet listener: akono functions missing";
     console.error(errMsg);
     throw new Error(errMsg);
   }
-  const walletHelper = new AndroidWalletHelper();
-  const onMessage = (msg: any) => {
+  let maybeWallet: Wallet | undefined;
+  const wp = openPromise<Wallet>();
+  const onMessage = async (msgStr: any) => {
+    if (typeof msgStr !== "string") {
+      console.error("expected string as message");
+      return;
+    }
+    const msg = JSON.parse(msgStr);
     const operation = msg.operation;
     if (typeof operation !== "string") {
       console.error(
@@ -51,21 +49,45 @@ export function installAndroidWalletListener() {
     let result;
     switch (operation) {
       case "init":
-        result = walletHelper.init();
+        {
+          maybeWallet = await getDefaultNodeWallet({
+            notifyHandler: async () => {
+              sendMessage(JSON.stringify({ type: "notification" }));
+            },
+          });
+          wp.resolve(maybeWallet);
+          result = true;
+        }
         break;
       case "getBalances":
+        {
+          const wallet = await wp.promise;
+          result = await wallet.getBalances();
+        }
         break;
-      case "withdraw-testkudos":
+      case "withdrawTestkudos":
+        {
+          const wallet = await wp.promise;
+          result = await withdrawTestBalance(wallet);
+        }
+        break;
+      case "downloadProposal":
+        {
+          const wallet = await wp.promise;
+          result = wallet.downloadProposal(msg.args.url);
+        }
         break;
       default:
         console.error(`operation "${operation}" not understood`);
         return;
     }
 
-    const respMsg = { result, id };
+    const respMsg = { result, id, operation, type: "response" };
     console.log("sending message back", respMsg);
-    sendMessage(respMsg);
+    sendMessage(JSON.stringify(respMsg));
   };
   // @ts-ignore
   globalThis.__akono_onMessage = onMessage;
+
+  console.log("android wallet listener installed");
 }
diff --git a/src/headless/helpers.ts b/src/headless/helpers.ts
index 975c45e5..5636b392 100644
--- a/src/headless/helpers.ts
+++ b/src/headless/helpers.ts
@@ -36,12 +36,6 @@ import fs = require("fs");
 
 const enableTracing = false;
 
-class ConsoleNotifier implements Notifier {
-  notify(): void {
-    // nothing to do.
-  }
-}
-
 class ConsoleBadge implements Badge {
   startBusy(): void {
     enableTracing && console.log("NOTIFICATION: busy");
@@ -120,6 +114,12 @@ interface DefaultNodeWalletArgs {
    * the wallet database is stored only in memory.
    */
   persistentStoragePath?: string;
+
+
+  /**
+   * Handler for asynchronous notifications from the wallet.
+   */
+  notifyHandler?: (reason: string) => void;
 }
 
 /**
@@ -128,7 +128,13 @@ interface DefaultNodeWalletArgs {
 export async function getDefaultNodeWallet(
   args: DefaultNodeWalletArgs = {},
 ): Promise<Wallet> {
-  const myNotifier = new ConsoleNotifier();
+  const myNotifier: Notifier = {
+    notify() {
+      if (args.notifyHandler) {
+        args.notifyHandler("");
+      }
+    }
+  }
 
   const myBadge = new ConsoleBadge();
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]