calculator & benitlux: remove obsolete explicit observer declaration
[nit.git] / contrib / benitlux / src / client / ios.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # iOS variant using a button to check in/out and local notifications
16 module ios
17
18 import ::ios
19 intrude import app::ui
20
21 import client
22 import push
23 import checkins
24
25 redef class HomeWindow
26 init
27 do
28 title = "Benitlux"
29 update_checkin_text
30 end
31
32 # TODO hide when not logged in
33 private var layout_login_checkin = new HorizontalLayout(parent=layout_user)
34 private var checkin_label = new Label(parent=layout_login_checkin)
35 private var checkin_button = new Button(parent=layout_login_checkin)
36
37 redef fun on_event(event)
38 do
39 super
40
41 if event isa ButtonPressEvent then
42 var sender = event.sender
43 if sender == checkin_button then
44 if app.currently_on_location then
45 app.on_check_out
46 else app.on_check_in
47 end
48 end
49 end
50
51 private fun update_checkin_text
52 do
53 if app.currently_on_location then
54 checkin_label.text = "Leaving?".t
55 checkin_button.text = "Check out".t
56 else
57 checkin_label.text = "On location?".t
58 checkin_button.text = "Check in".t
59 end
60 end
61 end
62
63 redef class App
64 redef fun on_check_in
65 do
66 super
67 var window = window
68 if window isa HomeWindow then window.update_checkin_text
69 end
70
71 redef fun on_check_out
72 do
73 super
74 var window = window
75 if window isa HomeWindow then window.update_checkin_text
76 end
77
78 redef fun did_finish_launching_with_options
79 do
80 ui_application.register_user_notification_settings
81 return super
82 end
83 end
84
85 redef class UserWindow
86 init do title = "Preferences".t
87 end
88
89 redef class BeersWindow
90 init do title = "Beers".t
91 end
92
93 redef class SocialWindow
94 init do title = "People".t
95 end
96
97 # --- Notifications
98
99 redef fun notify(title, content, id)
100 do native_notify(title.to_nsstring, content.to_nsstring)
101
102 private fun native_notify(title, content: NSString) in "ObjC" `{
103 UILocalNotification* notif = [[UILocalNotification alloc] init];
104 notif.alertTitle = title;
105 notif.alertBody = content;
106 notif.timeZone = [NSTimeZone defaultTimeZone];
107 [[UIApplication sharedApplication] presentLocalNotificationNow: notif];
108 `}
109
110 redef class UIApplication
111
112 # Register this app to display notifications
113 private fun register_user_notification_settings
114 in "ObjC" `{
115 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
116 [self registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
117 }
118 `}
119 end
120
121 # ---
122 # Shorten labels
123
124 redef class Label
125 # Ellipsize `text` so it fits within `max_length` characters
126 #
127 # FIXME Remove this when labels are correctly ellipsized on iOS.
128 redef fun text=(text)
129 do
130 if text == null then
131 super
132 return
133 end
134
135 var max_length = 50
136 if parent isa HorizontalLayout and parent.parent isa BeerView then
137 # This is the name of a beer, remember its a hack
138 max_length = 20
139 end
140
141 if text.length > max_length then
142 text = text.substring(0, max_length - 3).to_s + "..."
143 end
144 super text
145 end
146 end