From 514073241ea92fc17b0fe37223ddfe025510a1e3 Mon Sep 17 00:00:00 2001 From: jwinterm Date: Sat, 23 Jan 2016 11:36:41 -0800 Subject: [PATCH] Tx history and wallet save now working. --- .../jw/lightwallet/screens/DaemonView.java | 2 +- .../jw/lightwallet/screens/HistoryView.java | 2 +- .../lightwallet/screens/ImportKeysScreen.java | 30 +++++----- .../jw/lightwallet/screens/MainScreen.java | 55 +++++++++++++++--- .../jw/lightwallet/screens/WalletView.java | 56 +++++++++++++------ .../lightwallet/utils/WalletSaveValues.java | 1 + .../jw/lightwallet/wallet/WalletSaveRPC.java | 18 ++++-- 7 files changed, 116 insertions(+), 48 deletions(-) diff --git a/core/src/com/jw/lightwallet/screens/DaemonView.java b/core/src/com/jw/lightwallet/screens/DaemonView.java index 8f7df9c..5d7b952 100644 --- a/core/src/com/jw/lightwallet/screens/DaemonView.java +++ b/core/src/com/jw/lightwallet/screens/DaemonView.java @@ -74,7 +74,7 @@ public class DaemonView { daemonlayout.add(rewardlabel).pad(padvalue); daemonlayout.add(rewardvalue).pad(padvalue).row(); - daemonlayout.add(new Label("", uiSkin)).fill().expand(); + daemonlayout.add(new Label("", uiSkin)); } public void Update(DaemonValues daemonvalues) { diff --git a/core/src/com/jw/lightwallet/screens/HistoryView.java b/core/src/com/jw/lightwallet/screens/HistoryView.java index 607d02b..c548a8c 100644 --- a/core/src/com/jw/lightwallet/screens/HistoryView.java +++ b/core/src/com/jw/lightwallet/screens/HistoryView.java @@ -29,7 +29,7 @@ public class HistoryView { historylayout.add(new Label("Transaction history (click to copy txid)", uiSkin)).row(); historylayout.add(new Label("Type | Amount | Transaction ID", uiSkin)).row(); - historylayout.add(scroller).fill().expand(); + historylayout.add(scroller).fill(); } diff --git a/core/src/com/jw/lightwallet/screens/ImportKeysScreen.java b/core/src/com/jw/lightwallet/screens/ImportKeysScreen.java index cea27b8..f795365 100644 --- a/core/src/com/jw/lightwallet/screens/ImportKeysScreen.java +++ b/core/src/com/jw/lightwallet/screens/ImportKeysScreen.java @@ -30,7 +30,6 @@ public class ImportKeysScreen extends AbstractScreen { Stage stage; Table screenlayout; - Skin uiSkin; int padvalue; Image logo; @@ -58,7 +57,6 @@ public class ImportKeysScreen extends AbstractScreen { public ImportKeysScreen(final LightWallet game) { super(game); - uiSkin = new Skin(Gdx.files.internal("skin/uiskin.json")); padvalue = 20; stage = new Stage(); @@ -75,23 +73,23 @@ public class ImportKeysScreen extends AbstractScreen { + "The wallet keys file will be copied to the local directory and a new binary file will be created. " + "The default bitmonerod node is set for moneroclub, if you want to run a local daemon change the node field to http://localhost:18081."; - instructionlabel= new Label(instruction, uiSkin); + instructionlabel= new Label(instruction, game.uiSkin); instructionlabel.setWrap(true); textcontainer = new Table(); textcontainer.add(instructionlabel).width(600); inputtable = new Table(); - pathlabel = new Label("Wallet path: ", uiSkin); - pathtext = new TextField("", uiSkin); + pathlabel = new Label("Wallet path: ", game.uiSkin); + pathtext = new TextField("", game.uiSkin); - pwlabel = new Label("Password: ", uiSkin); - pwtext = new TextField("", uiSkin); + pwlabel = new Label("Password: ", game.uiSkin); + pwtext = new TextField("", game.uiSkin); pwtext.setPasswordMode(true); pwtext.setPasswordCharacter((char) 42); - nodelabel = new Label("Node address: ", uiSkin); - nodetext = new TextField(Constants.moneroclubdmn, uiSkin); + nodelabel = new Label("Node address: ", game.uiSkin); + nodetext = new TextField(Constants.moneroclubdmn, game.uiSkin); inputtable.add(pathlabel).width(200); inputtable.add(pathtext).width(400).row(); @@ -102,7 +100,7 @@ public class ImportKeysScreen extends AbstractScreen { buttontable = new Table(); - importbutton = new TextButton("Import!", uiSkin); + importbutton = new TextButton("Import!", game.uiSkin); importbutton.addListener(new ClickListener() { @Override public void clicked (InputEvent event, float x, float y) { @@ -113,7 +111,7 @@ public class ImportKeysScreen extends AbstractScreen { } }); - gobackbutton = new TextButton("Cancel", uiSkin); + gobackbutton = new TextButton("Cancel", game.uiSkin); gobackbutton.addListener(new ClickListener() { @Override public void clicked (InputEvent event, float x, float y) { @@ -165,37 +163,35 @@ public class ImportKeysScreen extends AbstractScreen { // Create success and failure dialogs to show based on outcome - Dialog successdialog = new Dialog("Success!", uiSkin, "dialog") { + Dialog successdialog = new Dialog("Success!", game.uiSkin, "dialog") { public void result(Object obj) { System.out.println("result "+obj); } }; - Label successlabel = new Label("", uiSkin); + Label successlabel = new Label("", game.uiSkin); successlabel.setWrap(true); successdialog.add(successlabel).width(400).row(); successdialog.button("Continue", true).addListener(new ClickListener() { @Override public void clicked (InputEvent event, float x, float y) { game.setScreen(new MainScreen(game)); - uiSkin.dispose(); stage.dispose(); } }); successdialog.key(Keys.ENTER, true); //sends "true" when the ENTER key is pressed - Dialog failuredialog = new Dialog("Utter failure!", uiSkin, "dialog") { + Dialog failuredialog = new Dialog("Utter failure!", game.uiSkin, "dialog") { public void result(Object obj) { System.out.println("result "+obj); } }; - Label failurelabel = new Label("Wallet import failed. Click to start over.", uiSkin); + Label failurelabel = new Label("Wallet import failed. Click to start over.", game.uiSkin); failurelabel.setWrap(true); failuredialog.add(failurelabel).width(400).row(); failuredialog.button("Continue", true).addListener(new ClickListener() { @Override public void clicked (InputEvent event, float x, float y) { game.setScreen(new WizardScreen(game)); - uiSkin.dispose(); stage.dispose(); } }); diff --git a/core/src/com/jw/lightwallet/screens/MainScreen.java b/core/src/com/jw/lightwallet/screens/MainScreen.java index 56ac0a7..87d8673 100644 --- a/core/src/com/jw/lightwallet/screens/MainScreen.java +++ b/core/src/com/jw/lightwallet/screens/MainScreen.java @@ -10,15 +10,20 @@ import java.io.PrintWriter; import java.util.ArrayList; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Button; +import com.badlogic.gdx.scenes.scene2d.ui.Dialog; import com.badlogic.gdx.scenes.scene2d.ui.Image; +import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; +import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.AtomicQueue; import com.jw.lightwallet.LightWallet; import com.jw.lightwallet.daemon.DaemonRPC; @@ -91,7 +96,7 @@ public class MainScreen extends AbstractScreen { balancerpc = new BalanceRPC(); balancevalues = new BalanceValues(); - walletsaverpc = new WalletSaveRPC(); + walletsaverpc = new WalletSaveRPC(game); walletsavevalues= new WalletSaveValues(); stage = new Stage(); @@ -146,16 +151,19 @@ public class MainScreen extends AbstractScreen { buttonrow.add(txhistorybutton).width(Constants.WORLD_WIDTH/4); screenlayout.add(buttonrow).top().row(); - screenlayout.add(logo).pad(10).center().row(); + screenlayout.add(logo).pad(5).center().row(); daemonview = new DaemonView(game); - walletview = new WalletView(game); + walletview = new WalletView(game, walletsaverpc); transactionview = new TransactionView(); historyview = new HistoryView(); - viewcontainer.add(daemonview.daemonlayout).expand().bottom(); + viewcontainer.add(daemonview.daemonlayout).expand().fill(); screenlayout.add(viewcontainer); + // viewcontainer.setFillParent(true); screenlayout.setFillParent(true); + screenlayout.top(); + // screenlayout.setDebug(true); stage.addActor(screenlayout); } @@ -214,7 +222,7 @@ public class MainScreen extends AbstractScreen { } if (sixtysectimer == true) { // Timer task to try and save wallet every minute - walletsaverpc.trysave(walletsavevalues); + walletsaverpc.trysave(walletview.getSavewalletlabel()); dumptxlist(); sixtysectimer = false; accum60s = 0; @@ -225,11 +233,12 @@ public class MainScreen extends AbstractScreen { } + // Launch simplewallet in its own thread public void launchsimplewallet() { new Thread(new Runnable() { @Override public void run() { - // do something important here, asynchronously to the rendering thread + // Setup simplewallet command with the specified file, password, and node address try { Gdx.app.log(LightWallet.LOG, "Command is: " + "simplewallet --wallet-file " + game.walletvalues.getName() + " --password " + game.walletvalues.getPw() + " --daemon-address " + game.walletvalues.getNode() @@ -240,6 +249,7 @@ public class MainScreen extends AbstractScreen { wr = new BufferedReader(new InputStreamReader(wp.getInputStream())); while (true) { String str = wr.readLine(); + // Gdx.app.log(LightWallet.LOG, str); wq.put(str); } } catch (IOException e) { @@ -263,19 +273,50 @@ public class MainScreen extends AbstractScreen { String queuepoll = wq.poll(); // Gdx.app.log(LightWallet.LOG, "Queue result: " + queuepoll); try{ + // If the string contains "height" then update the daemon and wallet screen blockheight if (queuepoll != null && queuepoll.contains("height:")) { String height = queuepoll.split("height: ")[1].split(",")[0]; + Gdx.app.log(LightWallet.LOG, "Queue result: " + queuepoll); walletview.syncvalue.setText(height + " / " + daemonvalues.getBlockheight()); + if (height.equals(Integer.toString(daemonvalues.getBlockheight()))) { + walletview.syncvalue.setStyle(game.uiSkin.get("greenlabel", LabelStyle.class)); + } else {walletview.syncvalue.setStyle(game.uiSkin.get("default", LabelStyle.class));} } else if (queuepoll != null && queuepoll.contains("height ")) { String height = queuepoll.split("height ")[1].split(",")[0]; + Gdx.app.log(LightWallet.LOG, "Queue result: " + queuepoll); walletview.syncvalue.setText(height + " / " + daemonvalues.getBlockheight()); + if (height.equals(Integer.toString(daemonvalues.getBlockheight()))) { + walletview.syncvalue.setStyle(game.uiSkin.get("greenlabel", LabelStyle.class)); + } else {walletview.syncvalue.setStyle(game.uiSkin.get("default", LabelStyle.class));} } + // If the string contains "money" then create a tx and add it to the txlist else if (queuepoll != null && queuepoll.contains("money")) { txlist.add(Tx.StringToTx(queuepoll)); // Gdx.app.log(LightWallet.LOG, "Found tx: " + txlist.get(txlist.size()-1).txid + // " of type " + txlist.get(txlist.size()-1).type + " and amount " + txlist.get(txlist.size()-1).amount); - } + } + // If the string contains "invalid password" then show popup and return to password screen + else if (queuepoll != null && queuepoll.contains("invalid password")) { + wp.destroy(); + Dialog failuredialog = new Dialog("Password incorrect!", game.uiSkin, "dialog") { + public void result(Object obj) { + System.out.println("result "+obj); + } + }; + Label failurelabel = new Label("Simplewallet reports you entered an invalid password, click to try again.", game.uiSkin); + failurelabel.setWrap(true); + failuredialog.add(failurelabel).width(400).row(); + failuredialog.button("Continue", true).addListener(new ClickListener() { + @Override + public void clicked (InputEvent event, float x, float y) { + game.setScreen(new PasswordScreen(game)); + stage.dispose(); + } + }); + failuredialog.key(Keys.ENTER, true); //sends "true" when the ENTER key is pressed + failuredialog.show(stage); + } } catch (NullPointerException e){e.printStackTrace();} } diff --git a/core/src/com/jw/lightwallet/screens/WalletView.java b/core/src/com/jw/lightwallet/screens/WalletView.java index 4a8aec8..50aabe3 100644 --- a/core/src/com/jw/lightwallet/screens/WalletView.java +++ b/core/src/com/jw/lightwallet/screens/WalletView.java @@ -8,28 +8,33 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.jw.lightwallet.LightWallet; +import com.jw.lightwallet.wallet.WalletSaveRPC; public class WalletView { + LightWallet game; + WalletSaveRPC walletsaverpc; - Table walletlayout; + Table walletlayout; - Label namelabel; - Label namevalue; - TextButton addresslabel; - Label addressvalue; - Label synclabel; - Label syncvalue; - Label unlockedlabel; - Label unlockedvalue; - Label lockedlabel; - Label lockedvalue; - TextButton savewalletbtn; - Label savewalletlabel; + Label namelabel; + Label namevalue; + TextButton addresslabel; + Label addressvalue; + Label synclabel; + Label syncvalue; + Label unlockedlabel; + Label unlockedvalue; + Label lockedlabel; + Label lockedvalue; + TextButton savewalletbtn; + Label savewalletlabel; - int padvalue; + int padvalue; - public WalletView (final LightWallet game) { + public WalletView (final LightWallet game, final WalletSaveRPC walletsaverpc) { + this.game = game; + this.walletsaverpc = walletsaverpc; walletlayout = new Table(); padvalue = 16; @@ -63,7 +68,16 @@ public class WalletView { savewalletbtn = new TextButton("Save wallet\n(will attempt autosave every 60 s)", game.uiSkin); savewalletlabel = new Label("Not saved :(", game.uiSkin, "redlabel"); walletlayout.add(savewalletbtn); - walletlayout.add(savewalletlabel).fill().expand(); + walletlayout.add(savewalletlabel); + + walletlayout.add(new Label("", game.uiSkin)).fill().expand(); + + savewalletbtn.addListener(new ClickListener() { + @Override + public void clicked (InputEvent event, float x, float y) { + walletsaverpc.trysave(savewalletlabel); + } + }); addresslabel.addListener(new ClickListener() { @Override @@ -73,4 +87,14 @@ public class WalletView { }); } + + + public Label getSavewalletlabel() { + return savewalletlabel; + } + + + public void setSavewalletlabel(Label savewalletlabel) { + this.savewalletlabel = savewalletlabel; + } } diff --git a/core/src/com/jw/lightwallet/utils/WalletSaveValues.java b/core/src/com/jw/lightwallet/utils/WalletSaveValues.java index 53b3ad8..e02f1a8 100644 --- a/core/src/com/jw/lightwallet/utils/WalletSaveValues.java +++ b/core/src/com/jw/lightwallet/utils/WalletSaveValues.java @@ -1,5 +1,6 @@ package com.jw.lightwallet.utils; public class WalletSaveValues { + } diff --git a/core/src/com/jw/lightwallet/wallet/WalletSaveRPC.java b/core/src/com/jw/lightwallet/wallet/WalletSaveRPC.java index 5ccf87a..6d7918a 100644 --- a/core/src/com/jw/lightwallet/wallet/WalletSaveRPC.java +++ b/core/src/com/jw/lightwallet/wallet/WalletSaveRPC.java @@ -5,21 +5,27 @@ import com.badlogic.gdx.Net.HttpMethods; import com.badlogic.gdx.Net.HttpRequest; import com.badlogic.gdx.Net.HttpResponse; import com.badlogic.gdx.Net.HttpResponseListener; +import com.badlogic.gdx.scenes.scene2d.ui.Label; +import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; import com.google.gson.Gson; +import com.jw.lightwallet.LightWallet; +import com.jw.lightwallet.screens.WalletView; import com.jw.lightwallet.utils.WalletSaveValues; public class WalletSaveRPC { + LightWallet game; public String swaddress; String response; String store; - public WalletSaveRPC () { + public WalletSaveRPC (LightWallet game) { + this.game = game; swaddress = "http://localhost:19091/json_rpc"; store = "{\"jsonrpc\":\"2.0\",\"id\":\"test\",\"method\":\"store\",\"params\":[]}"; } - public void trysave (final WalletSaveValues walletsavevalues) { + public void trysave (final Label savestatuslabel) { HttpRequest httpGet = new HttpRequest(HttpMethods.POST); httpGet.setUrl(swaddress); httpGet.setContent(store); @@ -28,14 +34,14 @@ public class WalletSaveRPC { public void handleHttpResponse(HttpResponse httpResponse) { response = httpResponse.getResultAsString(); //System.out.print("Balance response: " + response); - - + savestatuslabel.setText("Successfully saved"); + savestatuslabel.setStyle(game.uiSkin.get("greenlabel", LabelStyle.class)); } public void failed(Throwable t) { response = "failed"; - //System.out.print("Failed response status is: " + response); - //do stuff here based on the failed attempt + savestatuslabel.setText("Save failed"); + savestatuslabel.setStyle(game.uiSkin.get("redlabel", LabelStyle.class)); } @Override