96012023b243fa3429c8000d163c53fbac6a5e13
[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 checkin_button.observers.add self
31 end
32
33 # TODO hide when not logged in
34 private var layout_login_checkin = new HorizontalLayout(parent=layout_user)
35 private var checkin_label = new Label(parent=layout_login_checkin)
36 private var checkin_button = new Button(parent=layout_login_checkin)
37
38 redef fun on_event(event)
39 do
40 super
41
42 if event isa ButtonPressEvent then
43 var sender = event.sender
44 if sender == checkin_button then
45 if app.currently_on_location then
46 app.on_check_out
47 else app.on_check_in
48 end
49 end
50 end
51
52 private fun update_checkin_text
53 do
54 if app.currently_on_location then
55 checkin_label.text = "Leaving?".t
56 checkin_button.text = "Check out".t
57 else
58 checkin_label.text = "On location?".t
59 checkin_button.text = "Check in".t
60 end
61 end
62 end
63
64 redef class App
65 redef fun on_check_in
66 do
67 super
68 var window = window
69 if window isa HomeWindow then window.update_checkin_text
70 end
71
72 redef fun on_check_out
73 do
74 super
75 var window = window
76 if window isa HomeWindow then window.update_checkin_text
77 end
78
79 redef fun did_finish_launching_with_options
80 do
81 ui_application.register_user_notification_settings
82 return super
83 end
84 end
85
86 redef class UserWindow
87 init do title = "Preferences".t
88 end
89
90 redef class BeersWindow
91 init do title = "Beers".t
92 end
93
94 redef class SocialWindow
95 init do title = "People".t
96 end
97
98 # --- Notifications
99
100 redef fun notify(title, content, id)
101 do native_notify(title.to_nsstring, content.to_nsstring)
102
103 private fun native_notify(title, content: NSString) in "ObjC" `{
104 UILocalNotification* notif = [[UILocalNotification alloc] init];
105 notif.alertTitle = title;
106 notif.alertBody = content;
107 notif.timeZone = [NSTimeZone defaultTimeZone];
108 [[UIApplication sharedApplication] presentLocalNotificationNow: notif];
109 `}
110
111 redef class UIApplication
112
113 # Register this app to display notifications
114 private fun register_user_notification_settings
115 in "ObjC" `{
116 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
117 [self registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
118 }
119 `}
120 end
121
122 # ---
123 # Shorten labels
124
125 redef class Label
126 # Ellipsize `text` so it fits within `max_length` characters
127 #
128 # FIXME Remove this when labels are correctly ellipsized on iOS.
129 redef fun text=(text)
130 do
131 if text == null then
132 super
133 return
134 end
135
136 var max_length = 50
137 if parent isa HorizontalLayout and parent.parent isa BeerView then
138 # This is the name of a beer, remember its a hack
139 max_length = 20
140 end
141
142 if text.length > max_length then
143 text = text.substring(0, max_length - 3).to_s + "..."
144 end
145 super text
146 end
147 end