calculator: use Unicode strings instead of single chars for operators
[nit.git] / examples / calculator / src / calculator.nit
index 2cc50ec..c049d21 100644 (file)
@@ -24,13 +24,19 @@ app_name "app.nit Calc."
 end
 
 import app::ui
+import app::data_store
 import android::aware
 
 import calculator_logic
 
+# Show debug output?
+fun debug: Bool do return false
+
 redef class App
        redef fun on_create
        do
+               if debug then print "App::on_create"
+
                # Create the main window
                window = new CalculatorWindow
                super
@@ -55,11 +61,13 @@ class CalculatorWindow
 
        init
        do
+               if debug then print "CalculatorWindow::init"
+
                # All the button labels, row by row
                var rows = [["7", "8", "9", "+"],
                            ["4", "5", "6", "-"],
-                           ["1", "2", "3", "*"],
-                           ["0", ".", "C", "/"],
+                           ["1", "2", "3", "×"],
+                           ["0", ".", "C", "÷"],
                            ["="]]
 
                for row in rows do
@@ -75,6 +83,8 @@ class CalculatorWindow
 
        redef fun on_event(event)
        do
+               if debug then print "CalculatorWindow::on_event {event}"
+
                if event isa ButtonPressEvent then
 
                        var op = event.sender.text
@@ -84,12 +94,33 @@ class CalculatorWindow
                        else if op.is_numeric then
                                var n = op.to_i
                                context.push_digit n
-                       else
+                       else if op != null then
                                buttons["."].enabled = true
-                               context.push_op op.chars.first
+                               context.push_op op
                        end
 
                        display.text = context.display_text
                end
        end
+
+       redef fun on_save_state
+       do
+               if debug then print "CalculatorWindow::on_save_state"
+
+               app.data_store["context"] = context
+               super
+       end
+
+       redef fun on_restore_state
+       do
+               if debug then print "CalculatorWindow::on_restore_state"
+
+               super
+
+               var context = app.data_store["context"]
+               if not context isa CalculatorContext then return
+
+               self.context = context
+               display.text = context.display_text
+       end
 end