1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Android calculator application
18 module calculator_android
is
19 app_name
"app.nit Calc."
20 app_version
(0, 1, git_revision
)
21 app_namespace
"org.nitlanguage.calculator"
23 # Lock in portrait mode
24 android_manifest_activity
"""android:screenOrientation="portrait""""
27 # FIXME the next line should import `android` only when it uses nit_activity
31 import calculator_logic
36 private var context = new CalculatorContext
38 # The main display, at the top of the screen
39 private var display: EditText
41 # Maps operators as `String` to their `Button`
42 private var op2but = new HashMap[String, Button]
44 # Has this window been initialized?
45 private var inited = false
55 var layout = new NativeLinearLayout(native)
59 var display = new EditText
60 layout.add_view_with_weight(display.native, 1.0)
61 display.text_size = 36.0
62 self.display = display
64 # Buttons; numbers and operators
65 var ops = [["7", "8", "9", "+"],
72 var buts_layout = new NativeLinearLayout(native)
73 buts_layout.set_horizontal
74 layout.add_view_with_weight(buts_layout, 1.0)
78 but.event_catcher = self
81 buts_layout.add_view_with_weight(but.native, 1.0)
86 native.content_view = layout
89 redef fun on_save_instance_state(state)
93 var nity = new Bundle.from(state)
94 nity["context
"] = context.to_json
97 redef fun on_restore_instance_state(state)
101 var nity = new Bundle.from(state)
102 if not nity.has("context
") then return
104 var json = nity.string("context
")
105 if json == null then return
107 context = new CalculatorContext.from_json(json)
112 display.text = context.display_text
115 redef fun catch_event(event)
117 if event isa ClickEvent then
118 var sender = event.sender
122 sender.enabled = false
123 context.switch_to_decimals
124 else if op.is_numeric then
128 op2but["."].enabled = true
129 context.push_op op.chars.first
132 display.text = context.display_text