4f63a9292e0d2d59700e1fb0507400c9e88ef8ac
[nit.git] / lib / android / examples / src / ui_test.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Test for app.nit's UI services
18 module ui_test is
19 app_name "app.nit UI test"
20 app_version(0, 1, git_revision)
21 app_namespace "org.nitlanguage.ui_test"
22 android_manifest_activity """android:theme="@android:style/Theme.Light""""
23 android_api_target 15
24 end
25
26 import android::ui
27 import android::toast
28 import android::notification
29
30 redef class App
31 redef fun on_create
32 do
33 self.window = new Window
34 super
35 end
36 end
37
38 redef class Window
39
40 private var layout = new VerticalLayout(parent=self)
41
42 private var but_notif = new Button(parent=layout, text="Show Notification")
43 private var but_toast = new Button(parent=layout, text="Show Toast")
44
45 private var notif: nullable Notification = null
46
47 init
48 do
49 but_notif.observers.add self
50 but_toast.observers.add self
51 end
52
53 # Action when pressing `but_notif`
54 fun act_notif
55 do
56 var notif = self.notif
57 if notif == null then
58 notif = new Notification("From app.nit", "Some content...")
59 notif.ticker = "Ticker text..."
60 notif.show
61 self.notif = notif
62 else
63 notif.cancel
64 self.notif = null
65 end
66 end
67
68 # Action when pressing `but_toast`
69 fun act_toast
70 do
71 app.toast("Sample toast from app.nit at {get_time}", false)
72 end
73
74 redef fun on_event(event)
75 do
76 print "on_event {event}"
77 if event isa ButtonPressEvent then
78 var sender = event.sender
79 if sender == but_notif then
80 act_notif
81 else if sender == but_toast then
82 act_toast
83 end
84 end
85 end
86 end