Merge: doc: fixed some typos and other misc. corrections
[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 import manual_checkins
25 intrude import user_views
26
27 redef class HomeWindow
28 init
29 do
30 title = "Benitlux"
31
32 # Force equal height for `news_header`
33 var b = new Button(text="", parent=news_header)
34 end
35 end
36
37 redef class SignupWindow
38 init
39 do
40 title = "Login or Signup".t
41 txt_name.native.disable_autocorrect
42 txt_name.placeholder = "Name".t
43 txt_pass.placeholder = "Password".t
44 txt_pass2.placeholder = "Password".t
45 txt_email.placeholder = "example@example.com".t
46 lbl_feedback.native.text_color = new UIColor.red_color
47 lbl_feedback.align = 0.5
48
49 for l in [layout_login, layout_signup] do
50 l.native.spacing = 8.0
51 l.native.set_layout_margins(16.0, 8.0)
52 end
53 end
54 end
55
56 redef class App
57 redef fun did_finish_launching_with_options
58 do
59 ui_application.register_user_notification_settings
60 return super
61 end
62 end
63
64 redef class UserWindow
65 init do
66 title = "Preferences".t
67 lbl_welcome.align = 0.5
68 end
69 end
70
71 redef class BeersWindow
72 init do title = "Beers".t
73 end
74
75 redef class SocialWindow
76 init do
77 title = "People".t
78 txt_query.placeholder = "Name".t
79 txt_query.align = 0.5
80 txt_query.native.disable_autocorrect
81 end
82 end
83
84 redef class DescLabel
85 init do native.text_color = new UIColor.dark_gray_color
86 end
87
88 redef class SectionTitle
89 init do
90 native.text_color = new UIColor.init_with_white_alpha(0.4, 1.0)
91 size = 1.0
92 end
93 end
94
95 redef class SectionHeader
96 init do
97 native.layout_margins_relative_arrangement = true
98 native.set_layout_margins(16.0, 4.0)
99 end
100 end
101
102 redef class ItemView
103 init do
104 var native = native
105 if native isa UIStackView then
106 native.set_layout_margins(16.0, 2.0)
107 native.layout_margins_relative_arrangement = true
108 end
109 native.background_color = new UIColor.white_color
110 native.set_white_background
111 end
112 end
113
114 redef class BeerView
115 init do
116 lbl_name.size = 1.0
117 lbl_desc.size = 0.5
118 native.set_layout_margins(16.0, 8.0)
119 end
120 end
121
122 redef class ListLayout
123 init do native.background_color = new UIColor.group_table_view_background_color
124 end
125
126 redef class CheckBox super ItemView end
127
128 # ---
129 # Extern classes
130
131 redef class UIView
132 private fun set_layout_margins(margin, margin_y: Float)
133 in "ObjC" `{
134 self.layoutMargins = UIEdgeInsetsMake(margin_y, margin, margin_y, margin);
135 `}
136
137 private fun set_white_background
138 in "ObjC" `{
139 UIView *colored_view = [[UIView alloc] initWithFrame:self.bounds];
140 colored_view.backgroundColor = [UIColor whiteColor];
141 colored_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
142 [self insertSubview:colored_view atIndex:0];
143
144 [colored_view.layer setBorderColor: [[UIColor colorWithWhite:0.8 alpha:1.0] CGColor]];
145 [colored_view.layer setBorderWidth: 0.5];
146 `}
147 end
148
149 redef class UITextField
150 private fun disable_autocorrect
151 in "ObjC" `{
152 self.autocorrectionType = UITextAutocorrectionTypeNo;
153 self.autocapitalizationType = UITextAutocapitalizationTypeNone;
154 `}
155 end
156
157 # ---
158 # Notifications
159
160 redef fun notify(title, content, id)
161 do native_notify(title.to_nsstring, content.to_nsstring)
162
163 private fun native_notify(title, content: NSString) in "ObjC" `{
164 UILocalNotification* notif = [[UILocalNotification alloc] init];
165 notif.alertTitle = title;
166 notif.alertBody = content;
167 notif.timeZone = [NSTimeZone defaultTimeZone];
168 [[UIApplication sharedApplication] presentLocalNotificationNow: notif];
169 `}
170
171 redef class UIApplication
172
173 # Register this app to display notifications
174 private fun register_user_notification_settings
175 in "ObjC" `{
176 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
177 [self registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
178 }
179 `}
180 end