lib/android: update `ui` to use NitActivity and be on the UI thread
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 27 Jan 2015 22:32:12 +0000 (17:32 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 18 Mar 2015 12:37:57 +0000 (08:37 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/android/ui.nit

index 9bd4fc0..6d0e0f8 100644 (file)
@@ -34,7 +34,7 @@
 # ~~~
 module ui is min_api_version 14
 
-import native_app_glue
+import nit_activity
 import pthreads::concurrent_collections
 
 in "Java" `{
@@ -78,30 +78,6 @@ end
 
 redef class App
        super EventCatcher
-
-       # Queue of events to be received by the main thread
-       var event_queue = new ConcurrentList[AppEvent]
-
-       # Call `react` on all `AppEvent` available in `event_queue`
-       protected fun loop_on_ui_callbacks
-       do
-               var queue = event_queue
-               while not queue.is_empty do
-                       var event = queue.pop
-                       event.react
-               end
-       end
-
-       redef fun run
-       do
-               loop
-                       # Process Android events
-                       poll_looper 100
-
-                       # Process app.nit events
-                       loop_on_ui_callbacks
-               end
-       end
 end
 
 redef extern class NativeActivity
@@ -186,23 +162,18 @@ class Button
 
        init
        do
-               var native = new NativeButton(app.native_activity, app.event_queue, self)
+               var native = new NativeButton(app.native_activity, self)
                self.native = native.new_global_ref
        end
 
-       # Click event on the Main thread
+       # Click event
        #
        # By default, this method calls `app.catch_event`. It can be specialized
        # with custom behavior or the receiver of `catch_event` can be changed
        # with `event_catcher=`.
        fun click(event: AppEvent) do event_catcher.catch_event(event)
 
-       # Click event on the UI thread
-       #
-       # This method is called on the UI thread and redirects the event to `click`
-       # throught `App::event_queue`. In most cases, you should implement `click`
-       # and leave `click_ui` as is.
-       fun click_ui do app.event_queue.add(new ClickEvent(self))
+       private fun click_from_native do click(new ClickEvent(self))
 end
 
 # An Android editable text field
@@ -213,7 +184,7 @@ class EditText
 
        init
        do
-               var native = new NativeEditText(app.native_activity)
+               var native = new NativeEditText(app.activities.first.native)
                self.native = native.new_global_ref
        end
 end
@@ -352,14 +323,15 @@ extern class NativeButton in "Java" `{ android.widget.Button `}
 
        redef type SELF: NativeButton
 
-       new (context: NativeActivity, queue: ConcurrentList[AppEvent], sender_object: Object) import Button.click_ui in "Java" `{
+       new (context: NativeActivity, sender_object: Object)
+       import Button.click_from_native in "Java" `{
                final int final_sender_object = sender_object;
 
                return new Button(context){
                        @Override
                        public boolean onTouchEvent(MotionEvent event) {
                                if(event.getAction() == MotionEvent.ACTION_DOWN) {
-                                       Button_click_ui(final_sender_object);
+                                       Button_click_from_native(final_sender_object);
                                        return true;
                                }
                                return false;