Skip to content

Commit 158d781

Browse files
author
julien-fueled
authored
Merge pull request #26 from Fueled/observe-fragment-change
Notify Flowr screen when displayed fragment is changed
2 parents dc93b06 + 0e4e4ea commit 158d781

File tree

3 files changed

+82
-24
lines changed

3 files changed

+82
-24
lines changed

flowr/src/main/java/com/fueled/flowr/AbstractFlowrActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.Bundle;
77
import android.support.annotation.ColorInt;
88
import android.support.annotation.Nullable;
9+
import android.support.v4.app.Fragment;
910
import android.support.v4.app.FragmentManager;
1011
import android.support.v7.app.AppCompatActivity;
1112

@@ -61,6 +62,11 @@ protected int getDefaultNavigationBarColor() {
6162
return Color.BLACK;
6263
}
6364

65+
@Override
66+
public void onCurrentFragmentChanged(@Nullable Fragment currentFragment) {
67+
// Do Nothing. No Default implementation is required.
68+
}
69+
6470
@SuppressWarnings("WrongConstant")
6571
@Override
6672
public void setScreenOrientation(int orientation) {

flowr/src/main/java/com/fueled/flowr/Flowr.java

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import android.os.Bundle;
77
import android.support.annotation.AnimRes;
88
import android.support.annotation.IdRes;
9+
import android.support.annotation.NonNull;
10+
import android.support.annotation.Nullable;
911
import android.support.v4.app.Fragment;
1012
import android.support.v4.app.FragmentManager;
1113
import android.support.v4.app.FragmentTransaction;
@@ -25,6 +27,7 @@
2527
* Created by [email protected] on 31/05/2016.
2628
* Copyright (c) 2016 Fueled. All rights reserved.
2729
*/
30+
@SuppressWarnings({"WeakerAccess", "UnusedDeclaration"}) // Public API.
2831
public class Flowr implements FragmentManager.OnBackStackChangedListener,
2932
View.OnClickListener {
3033

@@ -42,11 +45,11 @@ public class Flowr implements FragmentManager.OnBackStackChangedListener,
4245
private final FragmentsResultPublisher resultPublisher;
4346
private final int mainContainerId;
4447

45-
private FlowrScreen screen;
46-
private ToolbarHandler toolbarHandler;
47-
private DrawerHandler drawerHandler;
48+
@Nullable private FlowrScreen screen;
49+
@Nullable private ToolbarHandler toolbarHandler;
50+
@Nullable private DrawerHandler drawerHandler;
4851

49-
private Fragment currentFragment;
52+
@Nullable private Fragment currentFragment;
5053

5154
private boolean overrideBack;
5255
private String tagPrefix;
@@ -61,7 +64,7 @@ public class Flowr implements FragmentManager.OnBackStackChangedListener,
6164
* @param mainContainerId the id of the container where the fragments should be displayed
6265
* @param screen the fragment's parent screen
6366
*/
64-
public Flowr(@IdRes int mainContainerId, FlowrScreen screen,
67+
public Flowr(@IdRes int mainContainerId, @Nullable FlowrScreen screen,
6568
FragmentsResultPublisher resultPublisher) {
6669
this(mainContainerId, screen, null, null, resultPublisher);
6770
}
@@ -77,7 +80,7 @@ public Flowr(@IdRes int mainContainerId, FlowrScreen screen,
7780
* @param resultPublisher the result publish to be used to publish results from fragments
7881
* that where opened for results.
7982
*/
80-
public Flowr(@IdRes int mainContainerId, FlowrScreen screen, String tagPrefix,
83+
public Flowr(@IdRes int mainContainerId, @Nullable FlowrScreen screen, @NonNull String tagPrefix,
8184
FragmentsResultPublisher resultPublisher) {
8285
this(mainContainerId, screen, null, null, tagPrefix, resultPublisher);
8386
}
@@ -93,8 +96,9 @@ public Flowr(@IdRes int mainContainerId, FlowrScreen screen, String tagPrefix,
9396
* @param resultPublisher the result publish to be used to publish results from fragments
9497
* that where opened for results.
9598
*/
96-
public Flowr(@IdRes int mainContainerId, FlowrScreen screen, ToolbarHandler toolbarHandler,
97-
DrawerHandler drawerHandler, FragmentsResultPublisher resultPublisher) {
99+
public Flowr(@IdRes int mainContainerId, @Nullable FlowrScreen screen,
100+
@Nullable ToolbarHandler toolbarHandler, @Nullable DrawerHandler drawerHandler,
101+
FragmentsResultPublisher resultPublisher) {
98102
this(mainContainerId, screen, toolbarHandler, drawerHandler, "#id-", resultPublisher);
99103
}
100104

@@ -111,9 +115,9 @@ public Flowr(@IdRes int mainContainerId, FlowrScreen screen, ToolbarHandler tool
111115
* @param resultPublisher the result publish to be used to publish results from fragments
112116
* that where opened for results.
113117
*/
114-
public Flowr(@IdRes int mainContainerId, FlowrScreen screen, ToolbarHandler toolbarHandler,
115-
DrawerHandler drawerHandler, String tagPrefix,
116-
FragmentsResultPublisher resultPublisher) {
118+
public Flowr(@IdRes int mainContainerId, @Nullable FlowrScreen screen,
119+
@Nullable ToolbarHandler toolbarHandler, @Nullable DrawerHandler drawerHandler,
120+
@NonNull String tagPrefix, FragmentsResultPublisher resultPublisher) {
117121
this.resultPublisher = resultPublisher;
118122
this.mainContainerId = mainContainerId;
119123
this.tagPrefix = tagPrefix;
@@ -148,8 +152,10 @@ public static ResultResponse getResultsResponse(Bundle arguments, int resultCode
148152

149153
Bundle requestBundle = arguments.getBundle(KEY_REQUEST_BUNDLE);
150154

151-
resultResponse.fragmentId = requestBundle.getString(KEY_FRAGMENT_ID);
152-
resultResponse.requestCode = requestBundle.getInt(KEY_REQUEST_CODE);
155+
if (requestBundle != null) {
156+
resultResponse.fragmentId = requestBundle.getString(KEY_FRAGMENT_ID);
157+
resultResponse.requestCode = requestBundle.getInt(KEY_REQUEST_CODE);
158+
}
153159

154160
return resultResponse;
155161
}
@@ -160,6 +166,7 @@ public static ResultResponse getResultsResponse(Bundle arguments, int resultCode
160166
*
161167
* @return the router screen for this router
162168
*/
169+
@Nullable
163170
protected FlowrScreen getRouterScreen() {
164171
return screen;
165172
}
@@ -169,7 +176,7 @@ protected FlowrScreen getRouterScreen() {
169176
*
170177
* @param flowrScreen the router screen to be used
171178
*/
172-
public void setRouterScreen(FlowrScreen flowrScreen) {
179+
public void setRouterScreen(@Nullable FlowrScreen flowrScreen) {
173180
removeCurrentRouterScreen();
174181
if (flowrScreen != null) {
175182
this.screen = flowrScreen;
@@ -194,7 +201,7 @@ private void removeCurrentRouterScreen() {
194201
*
195202
* @param toolbarHandler the toolbar handler to be used.
196203
*/
197-
public void setToolbarHandler(ToolbarHandler toolbarHandler) {
204+
public void setToolbarHandler(@Nullable ToolbarHandler toolbarHandler) {
198205
removeCurrentToolbarHandler();
199206

200207
if (toolbarHandler != null) {
@@ -215,7 +222,7 @@ private void removeCurrentToolbarHandler() {
215222
*
216223
* @param drawerHandler the drawer handler to be used.
217224
*/
218-
public void setDrawerHandler(DrawerHandler drawerHandler) {
225+
public void setDrawerHandler(@Nullable DrawerHandler drawerHandler) {
219226
this.drawerHandler = drawerHandler;
220227
}
221228

@@ -238,12 +245,16 @@ public void setDeepLinkHandlers(FlowrDeepLinkHandler... handlers) {
238245
*
239246
* @return the prefix used for the backstack fragments tag
240247
*/
248+
@NonNull
241249
protected final String getTagPrefix() {
242250
return tagPrefix;
243251
}
244252

245253
protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionData<T> data) {
246254
try {
255+
if (screen == null) {
256+
return;
257+
}
247258

248259
injectDeepLinkInfo(data);
249260

@@ -337,6 +348,7 @@ private void setCustomAnimations(FragmentTransaction transaction, @AnimRes int e
337348
);
338349
}
339350

351+
@Nullable
340352
private Fragment retrieveCurrentFragment() {
341353
Fragment fragment = null;
342354

@@ -369,7 +381,10 @@ private void updateVisibilityState(Fragment fragment, boolean shown) {
369381
*/
370382
public void close() {
371383
overrideBack = true;
372-
screen.invokeOnBackPressed();
384+
385+
if (screen != null) {
386+
screen.invokeOnBackPressed();
387+
}
373388
}
374389

375390
/**
@@ -379,6 +394,10 @@ public void close() {
379394
* @param n the number of fragments to remove from the back stack
380395
*/
381396
public void close(int n) {
397+
if (screen == null) {
398+
return;
399+
}
400+
382401
int count = screen.getScreenFragmentManager().getBackStackEntryCount();
383402
if (count > 1) {
384403
String id = tagPrefix + (screen.getScreenFragmentManager().getBackStackEntryCount() - n);
@@ -418,9 +437,11 @@ public void closeWithResults(ResultResponse resultResponse, int n) {
418437
* Clears the fragments back stack.
419438
*/
420439
public void clearBackStack() {
421-
screen.getScreenFragmentManager()
422-
.popBackStackImmediate(tagPrefix + "0", FragmentManager.POP_BACK_STACK_INCLUSIVE);
423-
currentFragment = null;
440+
if (screen != null) {
441+
screen.getScreenFragmentManager()
442+
.popBackStackImmediate(tagPrefix + "0", FragmentManager.POP_BACK_STACK_INCLUSIVE);
443+
currentFragment = null;
444+
}
424445
}
425446

426447
/**
@@ -452,19 +473,20 @@ public void onNavigationIconClicked() {
452473
* @return true if the current fragment is the home fragment
453474
*/
454475
public boolean isHomeFragment() {
455-
return screen.getScreenFragmentManager().getBackStackEntryCount() == 0;
476+
return screen == null || screen.getScreenFragmentManager().getBackStackEntryCount() == 0;
456477
}
457478

458479
/**
459480
* Returns the fragment currently being displayed for this screen,
460481
*
461482
* @return the fragment currently being displayed
462483
*/
484+
@Nullable
463485
public Fragment getCurrentFragment() {
464486
return currentFragment;
465487
}
466488

467-
private void setCurrentFragment(Fragment newFragment) {
489+
private void setCurrentFragment(@Nullable Fragment newFragment) {
468490
if (currentFragment != newFragment) {
469491
updateVisibilityState(currentFragment, false);
470492
currentFragment = newFragment;
@@ -486,8 +508,11 @@ public void syncScreenState() {
486508
navigationBarColor = ((FlowrFragment) currentFragment).getNavigationBarColor();
487509
}
488510

489-
screen.setScreenOrientation(screenOrientation);
490-
screen.setNavigationBarColor(navigationBarColor);
511+
if (screen != null) {
512+
screen.onCurrentFragmentChanged(getCurrentFragment());
513+
screen.setScreenOrientation(screenOrientation);
514+
screen.setNavigationBarColor(navigationBarColor);
515+
}
491516

492517
syncToolbarState();
493518
syncDrawerState();

flowr/src/main/java/com/fueled/flowr/FlowrScreen.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fueled.flowr;
22

33
import android.support.annotation.ColorInt;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
46
import android.support.v4.app.FragmentManager;
57

68
/**
@@ -10,11 +12,36 @@
1012

1113
public interface FlowrScreen {
1214

15+
/**
16+
* Return the FragmentManager for interacting with fragments associated
17+
* with this screen.
18+
*/
1319
FragmentManager getScreenFragmentManager();
1420

21+
/**
22+
* Invoke the {@code onBackPressed} method for this screen.
23+
*/
1524
void invokeOnBackPressed();
1625

26+
/**
27+
* Indicates that the fragment currently being displayed has changed.
28+
*
29+
* @param currentFragment the fragment currently displayed or {@code null} if no fragment
30+
* is being displayed.
31+
*/
32+
void onCurrentFragmentChanged(@Nullable Fragment currentFragment);
33+
34+
/**
35+
* Specify the screen orientation to be used for this screen.
36+
*
37+
* @param orientation the screen orientation to be used.
38+
*/
1739
void setScreenOrientation(int orientation);
1840

41+
/**
42+
* Specify the navigation bar color to be used for this screen.
43+
*
44+
* @param color the navigation bar color to be used.
45+
*/
1946
void setNavigationBarColor(@ColorInt int color);
2047
}

0 commit comments

Comments
 (0)