From 19ebc13c024defb13c68e4111673f963df01dfa5 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sun, 16 Aug 2015 22:18:49 +0000
Subject: [PATCH] Add SVG icon file

---
 app/src/main/java/net/jacekk/bridge/android/MainActivity.java |  109 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 60 insertions(+), 49 deletions(-)

diff --git a/app/src/main/java/net/jacekk/bridge/android/MainActivity.java b/app/src/main/java/net/jacekk/bridge/android/MainActivity.java
index 9c60f4e..15d2906 100644
--- a/app/src/main/java/net/jacekk/bridge/android/MainActivity.java
+++ b/app/src/main/java/net/jacekk/bridge/android/MainActivity.java
@@ -1,18 +1,23 @@
 package net.jacekk.bridge.android;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.TextWatcher;
+import android.view.Menu;
+import android.view.MenuItem;
 import android.widget.CompoundButton;
 import android.widget.EditText;
 import android.widget.SeekBar;
 import android.widget.TextView;
 import android.widget.ToggleButton;
 
-import net.jacekk.bridge.R;
 import net.jacekk.bridge.BridgeCompute;
+import net.jacekk.bridge.BridgeInput;
+import net.jacekk.bridge.BridgeInputException;
 import net.jacekk.bridge.BridgeResult;
+import net.jacekk.bridge.R;
 
 public class MainActivity extends Activity {
     private SeekBar BidSlider;
@@ -34,7 +39,7 @@
     private EditText PCInput;
     private EditText TricksInput;
 
-    private TextView resultText;
+    private TextView ResultsText;
 
     public BridgeCompute bridgeCompute;
 
@@ -49,18 +54,36 @@
         InitializeApp();
     }
 
-    @Override
-    public void onBackPressed() {
-        // Reset
+    public boolean onCreateOptionsMenu(Menu menu) {
+        getMenuInflater().inflate(R.menu.main, menu);
+        return super.onCreateOptionsMenu(menu);
+    }
+
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+            case R.id.main_action_about:
+                Intent about = new Intent(getApplicationContext(), AboutActivity.class);
+                startActivity(about);
+                return true;
+            case R.id.main_action_reset:
+                resetInputs();
+                return true;
+        }
+
+        return super.onOptionsItemSelected(item);
+    }
+
+    private void resetInputs() {
         BidSlider.setProgress(0);
         MinorSuitsToggle.setChecked(true);
         ContractToggle.setChecked(true);
         NNToggle.setChecked(true);
         PCInput.setText("");
         TricksInput.setText("");
+        ResultsText.setText(getString(R.string.enter_data));
     }
 
-    private void InitializeApp() {
+    protected void InitializeApp() {
         bridgeCompute = new BridgeCompute();
 
         BidSlider = (SeekBar) findViewById(R.id.BidSlider);
@@ -82,7 +105,7 @@
         PCInput = (EditText) findViewById(R.id.PCInput);
         TricksInput = (EditText) findViewById(R.id.TricksInput);
 
-        resultText = (TextView) findViewById(R.id.ResultsText);
+        ResultsText = (TextView) findViewById(R.id.ResultsText);
 
         CompoundButton.OnCheckedChangeListener changeListener;
 
@@ -203,6 +226,27 @@
     }
 
     public void updateResults() {
+        BridgeInput input = getBridgeInput();
+
+        BridgeResult result;
+        try {
+            result = bridgeCompute.getResultForInput(input);
+        } catch (BridgeInputException e) {
+            int errorStringId = BridgeInputExceptionMapper.map(e);
+            ResultsText.setText(getString(errorStringId));
+            return;
+        }
+
+        ResultsText.setText(
+                (result.winners == BridgeResult.Side.We ? getString(R.string.result_for_us)
+                        : getString(R.string.result_for_them)) +
+                        "\n" + getString(R.string.result_pts_wo_pc) + " " + result.pointsBefore +
+                        "\n" + getString(R.string.result_pts) + " " + result.points +
+                        "\n" + getString(R.string.result_imps) + " " + result.IMPs +
+                        "\n" + getString(R.string.result_percent) + " " + result.percent);
+    }
+
+    private BridgeInput getBridgeInput() {
         int bid = BidSlider.getProgress();
 
         int color = 0;
@@ -237,47 +281,14 @@
         } catch (NumberFormatException ignored) {
         }
 
-        if (bid < 0 || bid > 7) {
-            resultText.setText(getString(R.string.invalid_bid));
-            return;
-        }
-
-        if (color != 0 && color != 1 && color != 2) {
-            resultText.setText(getString(R.string.invalid_color));
-            return;
-        }
-
-        if (dbl < 0 || dbl > 2) {
-            resultText.setText(getString(R.string.invalid_dbl));
-            return;
-        }
-
-        if (vulnerability < 0 || vulnerability > 3) {
-            resultText.setText(getString(R.string.invalid_baba));
-            return;
-        }
-
-        if (PC < 0 || PC > 40) {
-            resultText.setText(getString(R.string.invalid_pc));
-            return;
-        }
-
-        if (tricks < 0 || tricks > 13) {
-            resultText.setText(getString(R.string.invalid_tricks));
-            return;
-        }
-
-        if (bid == 0 && PC < 20) {
-            resultText.setText(getString(R.string.invalid_pass));
-            return;
-        }
-
-        BridgeResult result = bridgeCompute.compute(bid, color, dbl, vulnerability, PC, tricks);
-        resultText.setText(
-                (result.pointsFor == BridgeResult.PointsFor.Us ? getString(R.string.result_for_us) : getString(R.string.result_for_them)) +
-                        "\n" + getString(R.string.result_pts_wo_pc) + " " + result.pointsBefore +
-                        "\n" + getString(R.string.result_pts) + " " + result.points +
-                        "\n" + getString(R.string.result_imps) + " " + result.IMPs +
-                        "\n" + getString(R.string.result_percent) + " " + result.percent);
+        BridgeInput input = new BridgeInput();
+        input.bid = bid;
+        input.suit = BridgeInput.Suit.values()[color];
+        input.contract = BridgeInput.Contract.values()[dbl];
+        input.weVulnerable = vulnerability / 2 >= 1;
+        input.theyVulnerable = vulnerability % 2 >= 1;
+        input.PC = PC;
+        input.tricks = tricks;
+        return input;
     }
 }

--
Gitblit v1.9.1