From 9be36febb2a44772401491d4f2f304098a9ed7ec Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Tue, 16 Aug 2016 19:54:55 +1000 Subject: [PATCH 1/7] Update inline doc --- .../nav/TestCaseNavigationAndInjection.java | 32 +++++++++---------- .../lib/android/mvc/MvcFragment.java | 16 +++++----- .../java/com/shipdream/lib/poke/Graph.java | 4 +-- .../lib/poke/TestInjectionReferenceCount.java | 24 +++++++------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/library/android-mvc-test/src/androidTest/java/com/shipdream/lib/android/mvc/view/nav/TestCaseNavigationAndInjection.java b/library/android-mvc-test/src/androidTest/java/com/shipdream/lib/android/mvc/view/nav/TestCaseNavigationAndInjection.java index e2d62e5..295c4cf 100644 --- a/library/android-mvc-test/src/androidTest/java/com/shipdream/lib/android/mvc/view/nav/TestCaseNavigationAndInjection.java +++ b/library/android-mvc-test/src/androidTest/java/com/shipdream/lib/android/mvc/view/nav/TestCaseNavigationAndInjection.java @@ -52,16 +52,16 @@ class CountMonitor implements Graph.Monitor { int fragDReleaseCount = 0; @Override - public void onInject(Object target) { + public void onInject(Object intoTarget) { synchronized (this) { if (valid) { - if (target instanceof NavFragmentA) { + if (intoTarget instanceof NavFragmentA) { fragAInjectCount++; - } else if (target instanceof NavFragmentB) { + } else if (intoTarget instanceof NavFragmentB) { fragBInjectCount++; - } else if (target instanceof NavFragmentC) { + } else if (intoTarget instanceof NavFragmentC) { fragCInjectCount++; - } else if (target instanceof NavFragmentD) { + } else if (intoTarget instanceof NavFragmentD) { fragDInjectCount++; } } @@ -69,17 +69,17 @@ public void onInject(Object target) { } @Override - public void onRelease(Object target) { + public void onRelease(Object fromTarget) { synchronized (this) { if (valid) { - if (target instanceof NavFragmentA) { + if (fromTarget instanceof NavFragmentA) { fragAReleaseCount++; - } else if (target instanceof NavFragmentB) { + } else if (fromTarget instanceof NavFragmentB) { fragBReleaseCount++; - } else if (target instanceof NavFragmentC) { + } else if (fromTarget instanceof NavFragmentC) { fragCReleaseCount++; - } else if (target instanceof NavFragmentD) { + } else if (fromTarget instanceof NavFragmentD) { fragDReleaseCount++; } } @@ -184,9 +184,9 @@ class WaitMonitor extends CountMonitor { boolean released = false; @Override - public void onRelease(Object target) { - super.onRelease(target); - if (target instanceof NavFragmentA) { + public void onRelease(Object fromTarget) { + super.onRelease(fromTarget); + if (fromTarget instanceof NavFragmentA) { released = true; synchronized (this) { notify(); @@ -453,9 +453,9 @@ class WaitMonitor extends CountMonitor { boolean released = false; @Override - public void onRelease(Object target) { - super.onRelease(target); - if (target instanceof NavFragmentA) { + public void onRelease(Object fromTarget) { + super.onRelease(fromTarget); + if (fromTarget instanceof NavFragmentA) { released = true; synchronized (this) { notify(); diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java index 5cc84f5..e249c3c 100644 --- a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java @@ -452,28 +452,28 @@ public void onSaveInstanceState(Bundle outState) { * life cycles and gets created and ready to use. If this is one time action, use * {@link #unregisterOnViewReadyListener(Runnable)} (Runnable)} unregister itself in the given onCreateViewAction. * - * @param onCreateViewAction The action to register + * @param action The action to registered to be run after view is ready */ - public void registerOnViewReadyListener(Runnable onCreateViewAction) { + public void registerOnViewReadyListener(Runnable action) { if (onViewReadyListeners == null) { onViewReadyListeners = new CopyOnWriteArrayList<>(); } - onViewReadyListeners.add(onCreateViewAction); + onViewReadyListeners.add(action); } /** - * Unregister the callback that to be called in {@link #onViewCreated(View, Bundle)} + * Unregister the callback that to be called in {@link #onViewReady(View, Bundle, Reason)} * - * @param onResumeAction The action to run in onResume callback + * @param action The action to unregistered */ - public void unregisterOnViewReadyListener(Runnable onResumeAction) { + public void unregisterOnViewReadyListener(Runnable action) { if (onViewReadyListeners != null) { - onViewReadyListeners.remove(onResumeAction); + onViewReadyListeners.remove(action); } } /** - * Unregister callbacks that to be called after {@link #onViewReady(View, Bundle, Reason)} + * Unregister all actions to be called after {@link #onViewReady(View, Bundle, Reason)} */ public void clearOnViewReadyListener() { if (onViewReadyListeners != null) { diff --git a/library/poke/src/main/java/com/shipdream/lib/poke/Graph.java b/library/poke/src/main/java/com/shipdream/lib/poke/Graph.java index e6d365e..d93f119 100644 --- a/library/poke/src/main/java/com/shipdream/lib/poke/Graph.java +++ b/library/poke/src/main/java/com/shipdream/lib/poke/Graph.java @@ -611,14 +611,14 @@ public interface Monitor { /** * Called when the graph is about to inject dependencies into the given object * - * @param target The object to inject into + * @param target The object whose injectable fields have been injected */ void onInject(Object target); /** * Called when the graph is about to release dependencies from the given object * - * @param target The object to release + * @param target The object whose injectable fields have been released */ void onRelease(Object target); } diff --git a/library/poke/src/test/java/com/shipdream/lib/poke/TestInjectionReferenceCount.java b/library/poke/src/test/java/com/shipdream/lib/poke/TestInjectionReferenceCount.java index 969bd78..83e5081 100644 --- a/library/poke/src/test/java/com/shipdream/lib/poke/TestInjectionReferenceCount.java +++ b/library/poke/src/test/java/com/shipdream/lib/poke/TestInjectionReferenceCount.java @@ -375,13 +375,13 @@ public void onRelease(Object target) {} final MonitorProxy proxy = mock(MonitorProxy.class); Graph.Monitor monitor = new Graph.Monitor() { @Override - public void onInject(Object target) { - proxy.onInject(target); + public void onInject(Object intoTarget) { + proxy.onInject(intoTarget); } @Override - public void onRelease(Object target) { - proxy.onRelease(target); + public void onRelease(Object fromTarget) { + proxy.onRelease(fromTarget); } }; graph.registerMonitor(monitor); @@ -413,13 +413,13 @@ public void onRelease(Object target) {} final MonitorProxy proxy = mock(MonitorProxy.class); Graph.Monitor monitor = new Graph.Monitor() { @Override - public void onInject(Object target) { - proxy.onInject(target); + public void onInject(Object intoTarget) { + proxy.onInject(intoTarget); } @Override - public void onRelease(Object target) { - proxy.onRelease(target); + public void onRelease(Object fromTarget) { + proxy.onRelease(fromTarget); } }; graph.registerMonitor(monitor); @@ -453,13 +453,13 @@ public void onRelease(Object target) {} final MonitorProxy proxy = mock(MonitorProxy.class); Graph.Monitor monitor = new Graph.Monitor() { @Override - public void onInject(Object target) { - proxy.onInject(target); + public void onInject(Object intoTarget) { + proxy.onInject(intoTarget); } @Override - public void onRelease(Object target) { - proxy.onRelease(target); + public void onRelease(Object fromTarget) { + proxy.onRelease(fromTarget); } }; graph.registerMonitor(monitor); From 414d9d65e2ae09809d90bcc4c00a7c109ecce8e6 Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Wed, 17 Aug 2016 20:36:13 +1000 Subject: [PATCH 2/7] Add MvcDialog and deprecate MvcDialogFragment --- build.gradle | 2 +- .../shipdream/lib/android/mvc/MvcDialog.java | 138 ++++++++++++++++++ .../lib/android/mvc/MvcDialogFragment.java | 2 + .../lib/android/mvc/MvcFragment.java | 18 ++- .../shipdream/lib/android/mvc/MvcService.java | 21 ++- 5 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialog.java diff --git a/build.gradle b/build.gradle index 314705f..48d6756 100644 --- a/build.gradle +++ b/build.gradle @@ -74,7 +74,7 @@ ext { version = [ major: 3, minor: 1, - patch : 0 + patch : 1 ] libGroup = 'com.shipdream' libVersion = "${version.major}.${version.minor}.${version.patch}" diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialog.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialog.java new file mode 100644 index 0000000..50bc620 --- /dev/null +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialog.java @@ -0,0 +1,138 @@ +/* + * Copyright 2016 Kejun Xia + * + * 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. + */ + +package com.shipdream.lib.android.mvc; + +import android.os.Bundle; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentTransaction; +import android.support.v7.app.AppCompatDialogFragment; + +import com.shipdream.lib.android.mvc.event.bus.EventBus; +import com.shipdream.lib.android.mvc.event.bus.annotation.EventBusV; +import com.shipdream.lib.poke.Graph; +import com.shipdream.lib.poke.exception.PokeException; +import com.shipdream.lib.poke.util.ReflectUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; + +/** + * Abstract class for dialogs. Don't use it as a normal fragment but only for Dialog. To share logic + * for similar dialog and fragment use shared controller. + * @param The class type for the controller of this dialog + */ +public abstract class MvcDialog extends AppCompatDialogFragment + implements UiView { + @Inject + @EventBusV + private EventBus eventBusV; + + protected Logger logger = LoggerFactory.getLogger(getClass()); + protected CONTROLLER controller; + protected abstract Class getControllerClass(); + private Graph.Monitor graphMonitor; + + /** + * Show dialog. + * @param fragmentManager The fragment manager. Usually it's the child fragment manager of the + * fragment on which the dialog will show + * @param dialogClass The class type of the dialog extending {@link MvcDialog} + */ + public static void show(FragmentManager fragmentManager, Class dialogClass) { + FragmentTransaction ft = fragmentManager.beginTransaction(); + MvcDialog dialogFragment = (MvcDialog) fragmentManager.findFragmentByTag(dialogClass.getName()); + if (dialogFragment == null) { + try { + dialogFragment = new ReflectUtils.newObjectByType<>(dialogClass).newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + ft.addToBackStack(null); + dialogFragment.show(ft, dialogClass.getName()); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + graphMonitor = new Graph.Monitor() { + @Override + public void onInject(Object target) { + if (controller != null && target == MvcDialog.this) { + controller.view = MvcDialog.this; + } + } + + @Override + public void onRelease(Object target) { + } + }; + Mvc.graph().registerMonitor(graphMonitor); + + if (getParentFragment() == null) { + setRetainInstance(true); + } + + try { + controller = Mvc.graph().reference(getControllerClass(), null); + } catch (PokeException e) { + logger.error(e.getMessage(), e); + } + Mvc.graph().inject(this); + + eventBusV.register(this); + } + + @Override + public void onDestroyView() { + //====================================== + //workaround of this bug + //https://code.google.com/p/android/issues/detail?id=17423 + if (getDialog() != null && (getParentFragment() != null || getRetainInstance())) { + getDialog().setDismissMessage(null); + } + //============================================ + super.onDestroyView(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + Mvc.graph().unregisterMonitor(graphMonitor); + eventBusV.register(this); + Mvc.graph().release(this); + try { + Mvc.graph().dereference(controller, getControllerClass(), null); + } catch (PokeException e) { + logger.error(e.getMessage(), e); + } + } + + /** + * Handy method to post an event to other views directly. However, when possible, it's + * recommended to post events from controllers to views to keep views' logic simple. + * @param event + */ + protected void postEvent2V(Object event) { + eventBusV.post(event); + } + +} diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialogFragment.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialogFragment.java index 39136ad..fd971d1 100644 --- a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialogFragment.java +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcDialogFragment.java @@ -27,6 +27,8 @@ * doesn't need to be designed with awareness how it is going to be used and can be reused as a * normal fragment as well. This class is FINAL and don't extend this class to custom your dialog. *

+ * + * @deprecated Use {@link MvcDialog} instead */ public class MvcDialogFragment extends DialogFragment { private EventRegister eventRegister; diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java index e249c3c..85dfc83 100644 --- a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java @@ -24,6 +24,7 @@ import android.view.View; import android.view.ViewGroup; +import com.shipdream.lib.poke.Graph; import com.shipdream.lib.poke.exception.PokeException; import com.shipdream.lib.poke.exception.ProviderMissingException; @@ -66,6 +67,7 @@ public abstract class MvcFragment extends private Object newInstanceChecker; boolean isStateManagedByRootDelegateFragment; protected CONTROLLER controller; + private Graph.Monitor graphMonitor; /** * @@ -151,6 +153,20 @@ private void releaseDependencies() { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + graphMonitor = new Graph.Monitor() { + @Override + public void onInject(Object target) { + if (controller != null && target == MvcFragment.this) { + controller.view = MvcFragment.this; + } + } + + @Override + public void onRelease(Object target) { + } + }; + Mvc.graph().registerMonitor(graphMonitor); + eventRegister = new EventRegister(this); if (savedInstanceState == null) { @@ -220,7 +236,6 @@ public void run() { private void doOnViewCreatedCallBack(View view, Bundle savedInstanceState, boolean restoring) { int currentOrientation = getResources().getConfiguration().orientation; if (controller != null) { - controller.view = this; controller.orientation = parseOrientation(currentOrientation); } @@ -437,6 +452,7 @@ public void onDestroy() { releaseDependencies(); + Mvc.graph().unregisterMonitor(graphMonitor); eventRegister = null; } diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java index 821cac3..48b3425 100644 --- a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java @@ -18,6 +18,7 @@ import android.app.Service; +import com.shipdream.lib.poke.Graph; import com.shipdream.lib.poke.exception.PokeException; import com.shipdream.lib.poke.exception.ProviderMissingException; @@ -31,6 +32,7 @@ public abstract class MvcService extends Service implements UiView { private EventRegister eventRegister; protected CONTROLLER controller; + private Graph.Monitor graphMonitor; /** * Specify the controller of this service. Returns null if this service doesn't need a controller @@ -45,6 +47,21 @@ public abstract class MvcService extends Service public void onCreate() { super.onCreate(); + graphMonitor = new Graph.Monitor() { + @Override + public void onInject(Object target) { + if (controller != null && target == MvcService.this) { + controller.view = MvcService.this; + } + } + + @Override + public void onRelease(Object target) { + + } + }; + Mvc.graph().registerMonitor(graphMonitor); + if (getControllerClass() != null) { try { controller = Mvc.graph().reference(getControllerClass(), null); @@ -53,8 +70,6 @@ public void onCreate() { + getControllerClass().getName() + ". Either create a controller with " + "default constructor or register it to Mvc.graph().getRootComponent()"); } - - controller.view = this; } Mvc.graph().inject(this); @@ -82,6 +97,8 @@ public void onDestroy() { } Mvc.graph().release(this); + + Mvc.graph().unregisterMonitor(graphMonitor); } /** From a619e3dabf678a6fdc38a7dd48de60433c612650 Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Thu, 18 Aug 2016 22:40:28 +1000 Subject: [PATCH 3/7] Better description for injection errors --- .../com/shipdream/lib/android/mvc/MvcActivity.java | 1 - .../com/shipdream/lib/android/mvc/MvcFragment.java | 5 ++--- .../com/shipdream/lib/android/mvc/MvcService.java | 14 +++++++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcActivity.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcActivity.java index 12d8044..8350475 100644 --- a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcActivity.java +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcActivity.java @@ -320,7 +320,6 @@ public void onViewStateRestored(Bundle savedInstanceState) { Bundle mvcOutState = savedInstanceState.getBundle(MVC_STATE_BUNDLE_KEY); long ts = System.currentTimeMillis(); - //TODO: if its rotation, consider not restore since the fragment is retained MvcStateKeeperHolder.restoreState(mvcOutState); logger.trace("Restored state of all active controllers, {}ms used.", System.currentTimeMillis() - ts); diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java index 85dfc83..2694400 100644 --- a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcFragment.java @@ -114,9 +114,8 @@ private void injectDependencies() { try { controller = Mvc.graph().reference(getControllerClass(), null); } catch (PokeException e) { - throw new IllegalArgumentException("Unable to find controller " - + getControllerClass().getName() + ".\nYou may miss some dependencies otherwise either create a controller with " + - "default constructor or register it to Mvc.graph().getRootComponent()", e); + throw new IllegalStateException("Unable to inject " + + getControllerClass().getName() + ".\n" + e.getMessage(), e); } } diff --git a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java index 48b3425..87fb27f 100644 --- a/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java +++ b/library/android-mvc/src/main/java/com/shipdream/lib/android/mvc/MvcService.java @@ -19,7 +19,8 @@ import android.app.Service; import com.shipdream.lib.poke.Graph; -import com.shipdream.lib.poke.exception.PokeException; +import com.shipdream.lib.poke.exception.CircularDependenciesException; +import com.shipdream.lib.poke.exception.ProvideException; import com.shipdream.lib.poke.exception.ProviderMissingException; import org.slf4j.Logger; @@ -65,10 +66,13 @@ public void onRelease(Object target) { if (getControllerClass() != null) { try { controller = Mvc.graph().reference(getControllerClass(), null); - } catch (PokeException e) { - throw new IllegalArgumentException("Unable to find controller " - + getControllerClass().getName() + ". Either create a controller with " + - "default constructor or register it to Mvc.graph().getRootComponent()"); + } catch (CircularDependenciesException e) { + e.printStackTrace(); + } catch (ProvideException e) { + e.printStackTrace(); + } catch (ProviderMissingException e) { + throw new IllegalStateException("Unable to inject " + + getControllerClass().getName() + ".\n" + e.getMessage(), e); } } From 683fba0fa97c6bf16f80fc83c49f8fe542f333e8 Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Thu, 18 Aug 2016 22:41:59 +1000 Subject: [PATCH 4/7] Update change log --- ChangeLog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 622935d..44673b8 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,7 @@ +Version: 3.1.1 +* Add MvcDialog and deprecate MvcDialogFragment +* Better description for injection errors for fragments and services + Version: 3.1.0 * Uplift Android Support Library to 24.1.1 From 28b90b3f392eac2104936daf6dc23cfc5b73a570 Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Fri, 26 Aug 2016 15:51:30 +1000 Subject: [PATCH 5/7] Allow controllers to send events to other controllers and managers --- ChangeLog.md | 1 + build.gradle | 2 +- .../shipdream/lib/android/mvc/Controller.java | 77 ++++++++++++++++++- .../lib/android/mvc/TestController.java | 46 ++++++++++- 4 files changed, 120 insertions(+), 6 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 44673b8..11a404d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,7 @@ Version: 3.1.1 * Add MvcDialog and deprecate MvcDialogFragment * Better description for injection errors for fragments and services +* Allow controllers to send events to other controllers and managers Version: 3.1.0 * Uplift Android Support Library to 24.1.1 diff --git a/build.gradle b/build.gradle index 48d6756..0a67c08 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:2.2.0-beta1' + classpath 'com.android.tools.build:gradle:2.2.0-beta2' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' diff --git a/library/android-mvc-core/src/main/java/com/shipdream/lib/android/mvc/Controller.java b/library/android-mvc-core/src/main/java/com/shipdream/lib/android/mvc/Controller.java index 0bc3925..f1df8d6 100644 --- a/library/android-mvc-core/src/main/java/com/shipdream/lib/android/mvc/Controller.java +++ b/library/android-mvc-core/src/main/java/com/shipdream/lib/android/mvc/Controller.java @@ -308,6 +308,80 @@ public void run() { return monitor; } + /** + *

+ * Post the event to views. It automatically guarantees the event will be received + * and run on UI thread of Android. + *

+ * + *

+ * The event will be captured by views or any objects registered to {@link EventBus} annotated + * by {@link EventBusV} and has corresponding method named onEvent() with single parameter with + * the same type of the event. For example + *

+ *
+     *  public class OnTextChangedEvent {
+     *      private String text;
+     *
+     *      public OnTextChangedEvent(String text) {
+     *          this.text = text;
+     *      }
+     *
+     *      public String getText() {
+     *          return text;
+     *      }
+     *  }
+     *
+     *  public class SomeView {
+     *      @ Inject
+     *      @ EventBusV
+     *      private EventBus eventBusV;
+     *
+     *      private TextView textView;
+     *
+     *      public class SomeView() {
+     *          //This is just needed when you have a view not inheriting MvcFragment, MvcService or etc.
+     *          //In MvcFragment or MvcService will register to the event bus in onCreate automatically.
+     *          eventBusV.register(this);
+     *      }
+     *
+     *      public void onEvent(OnTextChangedEvent onTextChangedEvent) {
+     *          textView.setText(onTextChangedEvent.getText());
+     *      }
+     *  }
+     *
+     *  public class SomeController{
+     *      private void func() {
+     *          postEvent(new OnTextChangedEvent("Controller Wants to change text"));
+     *      }
+     *  }
+     * 
+ * + * @param event2V The event + * @deprecated Use {@link #postEvent2V(Object)} instead + */ + protected void postEvent(final Object event2V) { + postEvent2V(event2V); + } + + /** + *

+ * Post the event to other core components such as controllers and managers. Event will be + * captured on the thread of the invoker. + *

+ * + *

+ * The event will be captured by {@link Controller}s, {@link Manager}s or others registered to + * {@link EventBus} annotated by {@link EventBusC} and has corresponding method named onEvent() + * with single parameter with the same type of the event. For example + *

+ * + * @param event2C The event to other {@link Controller}s, {@link Manager}s + */ + protected void postEvent2C(final Object event2C) { + eventBusC.post(event2C); + } + /** *

* Post the event to views. It automatically guarantees the event will be received @@ -359,8 +433,7 @@ public void run() { * * @param eventV The event */ - protected void postEvent(final Object eventV) { + protected void postEvent2V(final Object eventV) { eventBusV.post(eventV); } - } diff --git a/library/android-mvc-core/src/test/java/com/shipdream/lib/android/mvc/TestController.java b/library/android-mvc-core/src/test/java/com/shipdream/lib/android/mvc/TestController.java index 2cb8019..b22fa43 100644 --- a/library/android-mvc-core/src/test/java/com/shipdream/lib/android/mvc/TestController.java +++ b/library/android-mvc-core/src/test/java/com/shipdream/lib/android/mvc/TestController.java @@ -17,6 +17,7 @@ package com.shipdream.lib.android.mvc; import com.shipdream.lib.android.mvc.event.bus.EventBus; +import com.shipdream.lib.android.mvc.event.bus.annotation.EventBusC; import com.shipdream.lib.android.mvc.event.bus.annotation.EventBusV; import com.shipdream.lib.poke.Provides; @@ -34,7 +35,6 @@ public class TestController extends BaseTest{ private UiThreadRunner uiThreadRunner; - private EventBus eventBusV; @Override @Before @@ -50,6 +50,8 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } }).when(uiThreadRunner).post(any(Runnable.class)); + eventBusC = mock(EventBus.class); + eventBusV = mock(EventBus.class); MvcComponent component = new MvcComponent(""); @@ -59,9 +61,15 @@ UiThreadRunner uiThreadRunner() { return uiThreadRunner; } + @Provides + @EventBusC + EventBus eventBus2C() { + return eventBusC; + } + @Provides @EventBusV - EventBus eventBus() { + EventBus eventBus2V() { return eventBusV; } }); @@ -69,7 +77,7 @@ EventBus eventBus() { } @Test - public void should_post_event_to_event2v_channel() throws Exception { + public void should_post_event_to_event2v_channel_old() throws Exception { Controller controller = new Controller() { @Override public Class modelType() { @@ -84,6 +92,38 @@ public Class modelType() { verify(eventBusV).post(eq(event)); } + @Test + public void should_post_event_to_event2v_channel() throws Exception { + Controller controller = new Controller() { + @Override + public Class modelType() { + return null; + } + }; + graph.inject(controller); + + String event = ";"; + controller.postEvent2V(event); + + verify(eventBusV).post(eq(event)); + } + + @Test + public void should_post_event_to_event2c_channel() throws Exception { + Controller controller = new Controller() { + @Override + public Class modelType() { + return null; + } + }; + graph.inject(controller); + + String event = ";"; + controller.postEvent2C(event); + + verify(eventBusC).post(eq(event)); + } + @Test public void should_post_wrapped_MvcGraphException_when_run_async_task() { Controller controller = new Controller() { From 96d8ecad39f54f0eaa2ffd934ce9ce6f033ba631 Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Mon, 12 Sep 2016 20:21:15 +1000 Subject: [PATCH 6/7] Uplift support lib to 24.2.0 --- build.gradle | 5 ++--- library/android-mvc/build.gradle | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 0a67c08..0b796a9 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:2.2.0-beta2' + classpath 'com.android.tools.build:gradle:2.2.0-rc1' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' @@ -83,12 +83,11 @@ ext { androidMinSdkVersion = 14 androidCompileSdkVersion = 24 androidBuildToolVersion = "23.0.3" - supportLibVersion = "24.1.1" + supportLibVersion = "24.2.0" androidTargetSdkVersion = androidCompileSdkVersion lib = [ intelljAnnoatations: 'com.intellij:annotations:12.0', androidMinSdk: 'com.google.android:android:4.0.1.2', - androidSupportLib: "com.android.support:appcompat-v7:$supportLibVersion", junit: 'junit:junit:4.12', mokito: 'org.mockito:mockito-core:1.9.5', slf4jApi: "org.slf4j:slf4j-api:$log4jVersion", diff --git a/library/android-mvc/build.gradle b/library/android-mvc/build.gradle index 262d2fc..ce52e37 100644 --- a/library/android-mvc/build.gradle +++ b/library/android-mvc/build.gradle @@ -23,7 +23,7 @@ apply plugin: 'com.github.dcendents.android-maven' dependencies { compile project(':library:android-mvc-core') - compile rootProject.lib.androidSupportLib + compile "com.android.support:appcompat-v7:$rootProject.supportLibVersion" } android { From 9ca52be7f14375d0d0813b4518683f87bfad88a3 Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Mon, 12 Sep 2016 20:28:33 +1000 Subject: [PATCH 7/7] Update change log --- ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog.md b/ChangeLog.md index 11a404d..7b63b55 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,7 @@ Version: 3.1.1 * Add MvcDialog and deprecate MvcDialogFragment * Better description for injection errors for fragments and services * Allow controllers to send events to other controllers and managers +* Uplift Android support library to 24.2.0 Version: 3.1.0 * Uplift Android Support Library to 24.1.1