1313import android .util .Log ;
1414import android .view .View ;
1515
16- import com .fueled .flowr .internal .FlowrConfig ;
1716import com .fueled .flowr .internal .FlowrDeepLinkHandler ;
1817import com .fueled .flowr .internal .FlowrDeepLinkInfo ;
1918import com .fueled .flowr .internal .TransactionData ;
2019
21- import java .lang . reflect . Constructor ;
22- import java .lang . reflect . InvocationTargetException ;
23-
20+ import java .util . ArrayList ;
21+ import java .util . Collections ;
22+ import java . util . List ;
2423
2524/**
2625 * Created by [email protected] on 31/05/2016. 2928public class Flowr implements FragmentManager .OnBackStackChangedListener ,
3029 View .OnClickListener {
3130
32- public final static String DEEP_LINK_URL = "DEEP_LINK_URL" ;//to be used as Bundle key for deep links.
33- public final static String FLOWR_CONFIG = "com.fueled.flowr.FlowrConfigImpl" ; //Generated file that contain the path to the DeepLinkHandler class.
31+ /**
32+ * To be used as Bundle key for deep links.
33+ */
34+ public final static String DEEP_LINK_URL = "DEEP_LINK_URL" ;
3435
3536 private final static String KEY_REQUEST_BUNDLE = "KEY_REQUEST_BUNDLE" ;
3637 private final static String KEY_FRAGMENT_ID = "KEY_FRAGMENT_ID" ;
3738 private final static String KEY_REQUEST_CODE = "KEY_REQUEST_CODE" ;
39+
3840 private final static String TAG = Flowr .class .getSimpleName ();
41+
3942 private final FragmentsResultPublisher resultPublisher ;
4043 private final int mainContainerId ;
44+
4145 private FlowrScreen screen ;
4246 private ToolbarHandler toolbarHandler ;
4347 private DrawerHandler drawerHandler ;
48+
4449 private Fragment currentFragment ;
50+
4551 private boolean overrideBack ;
4652 private String tagPrefix ;
4753
48- private FlowrDeepLinkHandler deepLinkHandler ;
54+ private List < FlowrDeepLinkHandler > deepLinkHandlers ;
4955
5056
5157 /**
@@ -117,6 +123,8 @@ public Flowr(@IdRes int mainContainerId, FlowrScreen screen, ToolbarHandler tool
117123 setToolbarHandler (toolbarHandler );
118124 setDrawerHandler (drawerHandler );
119125
126+ deepLinkHandlers = new ArrayList <>();
127+
120128 syncScreenState ();
121129 }
122130
@@ -146,16 +154,6 @@ public static ResultResponse getResultsResponse(Bundle arguments, int resultCode
146154 return resultResponse ;
147155 }
148156
149- private Constructor <? extends FlowrDeepLinkHandler > findBindingConstructorForClass () throws ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException {
150- //Use reflexion because:
151- //1. The FlowrDeepLinkHandlerImpl location is dynamic based on the location of all the Fragment that support deep link, so we can't rely on a static position like Dagger.
152- //2. Because the FlowrDeepLinkHandlerImpl is generated only when a @DeepLink is used, if the project doesn't use deep linking, the class will never be generated and the project won't compile
153- FlowrConfig config = (FlowrConfig ) getClass ().getClassLoader ().loadClass (FLOWR_CONFIG ).getConstructor ().newInstance ();
154- Class <?> bindingClass = getClass ().getClassLoader ().loadClass (config .getHandlerPackage ());
155- //noinspection unchecked
156- return (Constructor <? extends FlowrDeepLinkHandler >) bindingClass .getConstructor ();
157- }
158-
159157
160158 /**
161159 * Returns the {@link FlowrScreen} used for this router.
@@ -221,6 +219,20 @@ public void setDrawerHandler(DrawerHandler drawerHandler) {
221219 this .drawerHandler = drawerHandler ;
222220 }
223221
222+ /**
223+ * Specify a collection of {@link FlowrDeepLinkHandler} to be used when routing deep link
224+ * intents replacing all previously set handlers.
225+ *
226+ * @param handlers the collection of handlers to be used.
227+ */
228+ public void setDeepLinkHandlers (FlowrDeepLinkHandler ... handlers ) {
229+ this .deepLinkHandlers .clear ();
230+
231+ if (handlers != null ) {
232+ Collections .addAll (deepLinkHandlers , handlers );
233+ }
234+ }
235+
224236 /**
225237 * Returns the prefix used for the backstack fragments tag
226238 *
@@ -277,18 +289,20 @@ protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionD
277289 }
278290
279291 /**
280- * Parse the intent set by {@link TransactionData#deepLinkIntent} and if this intent contains Deep Link info, update the {@link #currentFragment} and the Transaction data.
292+ * Parse the intent set by {@link TransactionData#deepLinkIntent} and if this intent contains
293+ * Deep Link info, update the {@link #currentFragment} and the Transaction data.
281294 *
282- * @param data The Transaction data to extend if Deep link info are found in the {@link TransactionData#deepLinkIntent}.
295+ * @param data The Transaction data to extend if Deep link info are found in
296+ * the {@link TransactionData#deepLinkIntent}.
283297 * @param <T> The generic type for a valid Fragment.
284298 */
285299 @ SuppressWarnings ("unchecked" )
286300 private <T extends Fragment & FlowrFragment > void injectDeepLinkInfo (TransactionData <T > data ) {
287301 Intent deepLinkIntent = data .getDeepLinkIntent ();
288302 if (deepLinkIntent != null ) {
289- deepLinkHandler = getDeepLinkHandler ();
290- if ( deepLinkHandler != null ) {
291- FlowrDeepLinkInfo info = deepLinkHandler . routeIntentToScreen ( deepLinkIntent );
303+ for ( FlowrDeepLinkHandler handler : deepLinkHandlers ) {
304+ FlowrDeepLinkInfo info = handler . getDeepLinkInfoForIntent ( deepLinkIntent );
305+
292306 if (info != null ) {
293307 data .setFragmentClass (info .fragment );
294308 Bundle dataArgs = data .getArgs ();
@@ -297,6 +311,8 @@ private <T extends Fragment & FlowrFragment> void injectDeepLinkInfo(Transaction
297311 } else {
298312 data .setArgs (info .data );
299313 }
314+
315+ break ;
300316 }
301317 }
302318 }
@@ -604,25 +620,6 @@ public void onDestroy() {
604620 setDrawerHandler (null );
605621 }
606622
607- /**
608- * return the {@link FlowrDeepLinkHandler} if already initialized or initialize one.
609- *
610- * @return Initialized {@link FlowrDeepLinkHandler}
611- */
612- public FlowrDeepLinkHandler getDeepLinkHandler () {
613- try {
614- if (deepLinkHandler == null ) {
615- Constructor <? extends FlowrDeepLinkHandler > deepLinkCtor = findBindingConstructorForClass ();
616- if (deepLinkCtor != null ) {
617- deepLinkHandler = deepLinkCtor .newInstance ();
618- }
619- }
620- } catch (Exception e ) {
621- Log .e (TAG , "Error retrieving DeeplinkHandler" , e );
622- }
623- return deepLinkHandler ;
624- }
625-
626623 /**
627624 * This builder class is used to show a new fragment inside the current activity
628625 */
@@ -691,7 +688,8 @@ public Builder replaceCurrentFragment(boolean replaceCurrentFragment) {
691688 * @param exitAnim the fragment exit animation.
692689 */
693690 public Builder setCustomTransactionAnimation (@ AnimRes int enterAnim , @ AnimRes int exitAnim ) {
694- return setCustomTransactionAnimation (enterAnim , FragmentTransaction .TRANSIT_NONE , FragmentTransaction .TRANSIT_NONE , exitAnim );
691+ return setCustomTransactionAnimation (enterAnim , FragmentTransaction .TRANSIT_NONE ,
692+ FragmentTransaction .TRANSIT_NONE , exitAnim );
695693 }
696694
697695
@@ -703,8 +701,8 @@ public Builder setCustomTransactionAnimation(@AnimRes int enterAnim, @AnimRes in
703701 * @param popEnterAnim The animation resource to be used when the previous fragment enters on back pressed.
704702 * @param popExitAnim The animation resource to be used when the current fragment exits on back pressed..
705703 */
706- public Builder setCustomTransactionAnimation (@ AnimRes int enterAnim ,
707- @ AnimRes int exitAnim , @ AnimRes int popEnterAnim , @ AnimRes int popExitAnim ) {
704+ public Builder setCustomTransactionAnimation (@ AnimRes int enterAnim , @ AnimRes int exitAnim ,
705+ @ AnimRes int popEnterAnim , @ AnimRes int popExitAnim ) {
708706 data .setEnterAnim (enterAnim );
709707 data .setExitAnim (exitAnim );
710708 data .setPopEnterAnim (popEnterAnim );
0 commit comments