contrib/benitlux: intro iOS client
authorAlexis Laferrière <alexis.laf@xymus.net>
Mon, 14 Mar 2016 19:45:39 +0000 (15:45 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 19 May 2016 14:16:37 +0000 (10:16 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/benitlux/Makefile
contrib/benitlux/ios/.gitignore [new file with mode: 0644]
contrib/benitlux/src/client/ios.nit [new file with mode: 0644]

index fd7d3be..2142041 100644 (file)
@@ -62,3 +62,25 @@ android-release: $(shell ../../bin/nitls -M src/client/android.nit) android-res
        mkdir -p bin/ res/
        ../../bin/nitc -o bin/benitlux.apk src/client/android.nit \
                -D benitlux_rest_server_uri=http://xymus.net/benitlux/ --release
+
+# ---
+# iOS
+
+ios: bin/benitlux.app
+bin/benitlux.app: $(shell ../../bin/nitls -M src/client/ios.nit) ios/AppIcon.appiconset/Contents.json
+       mkdir -p bin/
+       rm -rf bin/benitlux.app/
+       ../../bin/nitc -o bin/benitlux.app src/client/ios.nit -D benitlux_rest_server_uri=http://$(SERVER)/
+
+bin/proto.app: $(shell ../../bin/nitls -M src/client/ios_proto.nit) ios/AppIcon.appiconset/Contents.json
+       mkdir -p bin/ res/
+       ../../bin/nitc -o $@ src/client/ios_proto.nit \
+               -D benitlux_rest_server_uri=http://$(SERVER)/
+
+ios-release: $(shell ../../bin/nitls -M src/client/ios.nit) ios/AppIcon.appiconset/Contents.json
+       mkdir -p bin/
+       ../../bin/nitc -o bin/benitlux.app src/client/ios.nit -D benitlux_rest_server_uri=http://$(SERVER)/
+
+ios/AppIcon.appiconset/Contents.json: art/icon.svg
+       mkdir -p ios
+       ../inkscape_tools/bin/svg_to_icons art/icon.svg --ios --out ios/AppIcon.appiconset/
diff --git a/contrib/benitlux/ios/.gitignore b/contrib/benitlux/ios/.gitignore
new file mode 100644 (file)
index 0000000..72e8ffc
--- /dev/null
@@ -0,0 +1 @@
+*
diff --git a/contrib/benitlux/src/client/ios.nit b/contrib/benitlux/src/client/ios.nit
new file mode 100644 (file)
index 0000000..9601202
--- /dev/null
@@ -0,0 +1,147 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# iOS variant using a button to check in/out and local notifications
+module ios
+
+import ::ios
+intrude import app::ui
+
+import client
+import push
+import checkins
+
+redef class HomeWindow
+       init
+       do
+               title = "Benitlux"
+               update_checkin_text
+               checkin_button.observers.add self
+       end
+
+       # TODO hide when not logged in
+       private var layout_login_checkin = new HorizontalLayout(parent=layout_user)
+       private var checkin_label = new Label(parent=layout_login_checkin)
+       private var checkin_button = new Button(parent=layout_login_checkin)
+
+       redef fun on_event(event)
+       do
+               super
+
+               if event isa ButtonPressEvent then
+                       var sender = event.sender
+                       if sender == checkin_button then
+                               if app.currently_on_location then
+                                       app.on_check_out
+                               else app.on_check_in
+                       end
+               end
+       end
+
+       private fun update_checkin_text
+       do
+               if app.currently_on_location then
+                       checkin_label.text = "Leaving?".t
+                       checkin_button.text = "Check out".t
+               else
+                       checkin_label.text = "On location?".t
+                       checkin_button.text = "Check in".t
+               end
+       end
+end
+
+redef class App
+       redef fun on_check_in
+       do
+               super
+               var window = window
+               if window isa HomeWindow then window.update_checkin_text
+       end
+
+       redef fun on_check_out
+       do
+               super
+               var window = window
+               if window isa HomeWindow then window.update_checkin_text
+       end
+
+       redef fun did_finish_launching_with_options
+       do
+               ui_application.register_user_notification_settings
+               return super
+       end
+end
+
+redef class UserWindow
+       init do title = "Preferences".t
+end
+
+redef class BeersWindow
+       init do title = "Beers".t
+end
+
+redef class SocialWindow
+       init do title = "People".t
+end
+
+# --- Notifications
+
+redef fun notify(title, content, id)
+do native_notify(title.to_nsstring, content.to_nsstring)
+
+private fun native_notify(title, content: NSString) in "ObjC" `{
+       UILocalNotification* notif = [[UILocalNotification alloc] init];
+       notif.alertTitle = title;
+       notif.alertBody = content;
+       notif.timeZone = [NSTimeZone defaultTimeZone];
+       [[UIApplication sharedApplication] presentLocalNotificationNow: notif];
+`}
+
+redef class UIApplication
+
+       # Register this app to display notifications
+       private fun register_user_notification_settings
+       in "ObjC" `{
+               if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
+                       [self registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
+               }
+       `}
+end
+
+# ---
+# Shorten labels
+
+redef class Label
+       # Ellipsize `text` so it fits within `max_length` characters
+       #
+       # FIXME Remove this when labels are correctly ellipsized on iOS.
+       redef fun text=(text)
+       do
+               if text == null then
+                       super
+                       return
+               end
+
+               var max_length = 50
+               if parent isa HorizontalLayout and parent.parent isa BeerView then
+                       # This is the name of a beer, remember its a hack
+                       max_length = 20
+               end
+
+               if text.length > max_length then
+                       text = text.substring(0, max_length - 3).to_s + "..."
+               end
+               super text
+       end
+end