Skip to content

Commit 8ba71c8

Browse files
author
Hussein Aladeen
authored
Merge pull request #36 from Fueled/add-closeUpto
feat(*): add close upto
2 parents 38f3cac + a214ab3 commit 8ba71c8

File tree

10 files changed

+180
-19
lines changed

10 files changed

+180
-19
lines changed

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public class Flowr implements FragmentManager.OnBackStackChangedListener,
5656

5757
private List<FlowrDeepLinkHandler> deepLinkHandlers;
5858

59-
6059
/**
6160
* Constructor to use when creating a new router for an activity
6261
* that has no toolbar.
@@ -250,10 +249,17 @@ protected final String getTagPrefix() {
250249
return tagPrefix;
251250
}
252251

253-
protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionData<T> data) {
252+
/**
253+
*
254+
* @param data TransactionData used to configure fragment transaction
255+
* @param <T> type Fragment & FlowrFragment
256+
* @return id Identifier of the committed transaction.
257+
*/
258+
protected <T extends Fragment & FlowrFragment> int displayFragment(TransactionData<T> data) {
259+
int identifier = -1;
254260
try {
255261
if (screen == null) {
256-
return;
262+
return identifier;
257263
}
258264

259265
injectDeepLinkInfo(data);
@@ -282,15 +288,15 @@ protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionD
282288
transaction.add(mainContainerId, fragment);
283289
}
284290

285-
transaction.commit();
291+
identifier = transaction.commit();
286292

287293
if (data.isSkipBackStack()) {
288294
setCurrentFragment(fragment);
289295
}
290-
291296
} catch (Exception e) {
292297
Log.e(TAG, "Error while displaying fragment.", e);
293298
}
299+
return identifier;
294300
}
295301

296302
/**
@@ -402,6 +408,26 @@ public void close(int n) {
402408
}
403409
}
404410

411+
/**
412+
* Closes the current activity if the fragments back stack is empty,
413+
* otherwise pop upto fragments with id Identifier from the stack.
414+
*
415+
* @param id Identifier of the committed transaction.
416+
*/
417+
public void closeUpto(int id) {
418+
if (screen == null) {
419+
return;
420+
}
421+
422+
int count = screen.getScreenFragmentManager().getBackStackEntryCount();
423+
if (count > 1) {
424+
screen.getScreenFragmentManager()
425+
.popBackStackImmediate(id, FragmentManager.POP_BACK_STACK_INCLUSIVE);
426+
} else {
427+
close();
428+
}
429+
}
430+
405431
/**
406432
* Closes the current activity if the fragments back stack is empty,
407433
* otherwise pop the top fragment from the stack and publish the results response.
@@ -427,6 +453,21 @@ public void closeWithResults(ResultResponse resultResponse, int n) {
427453
}
428454
}
429455

456+
/**
457+
* Closes the current activity if the fragments back stack is empty,
458+
* otherwise pop upto fragments with id Identifier from the stack and publish the results response.
459+
*
460+
* @param resultResponse the results response to be published
461+
* @param id Identifier of the committed transaction.
462+
*/
463+
public void closeUptoWithResults(ResultResponse resultResponse, int id) {
464+
closeUpto(id);
465+
466+
if (resultResponse != null) {
467+
resultPublisher.publishResult(resultResponse);
468+
}
469+
}
470+
430471
/**
431472
* Clears the fragments back stack.
432473
*/
@@ -740,9 +781,11 @@ public Builder noTransactionAnimation() {
740781

741782
/**
742783
* Displays the fragment using this builder configurations.
784+
*
785+
* @return id Identifier of the committed transaction.
743786
*/
744-
public void displayFragment() {
745-
Flowr.this.displayFragment(data);
787+
public int displayFragment() {
788+
return Flowr.this.displayFragment(data);
746789
}
747790

748791
/**
@@ -752,8 +795,10 @@ public void displayFragment() {
752795
* it will be later used to deliver the results to the correct fragment instance.
753796
* @param requestCode this code will be returned in {@link ResultResponse} when the fragment is closed,
754797
* and it can be used to identify the request from which the results were returned.
798+
*
799+
* @return id Identifier of the committed transaction.
755800
*/
756-
public void displayFragmentForResults(String fragmentId, int requestCode) {
801+
public int displayFragmentForResults(String fragmentId, int requestCode) {
757802
if (!TextUtils.isEmpty(fragmentId)) {
758803
if (data.getArgs() == null) {
759804
data.setArgs(new Bundle());
@@ -763,7 +808,7 @@ public void displayFragmentForResults(String fragmentId, int requestCode) {
763808
getResultRequestBundle(fragmentId, requestCode));
764809
}
765810

766-
Flowr.this.displayFragment(data);
811+
return Flowr.this.displayFragment(data);
767812
}
768813

769814
private Bundle getResultRequestBundle(String fragmentId, int requestCode) {

sample/src/main/java/com/fueled/flowr/sample/CategoriesFragment.java renamed to sample/src/main/java/com/fueled/flowr/sample/FirstFragment.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,40 @@
33
import android.view.View;
44

55
import com.fueled.flowr.NavigationIconType;
6+
import com.fueled.flowr.annotations.DeepLink;
67
import com.fueled.flowr.sample.core.AbstractFragment;
78

89
/**
910
* Created by [email protected] on 18/05/2017.
1011
* Copyright (c) 2017 Fueled. All rights reserved.
1112
*/
12-
public class CategoriesFragment extends AbstractFragment {
13+
14+
@DeepLink(value = "/first")
15+
public class FirstFragment extends AbstractFragment implements View.OnClickListener {
1316

1417
@Override
1518
public int getLayoutId() {
16-
return R.layout.fragment_categories;
19+
return R.layout.fragment_first;
1720
}
1821

1922
@Override
2023
protected void setupView(View view) {
21-
24+
view.findViewById(R.id.add_stack_button).setOnClickListener(this);
2225
}
2326

2427
@Override
2528
public String getTitle() {
26-
return "Categories Fragment";
29+
return "First Fragment";
2730
}
2831

2932
@Override
3033
public NavigationIconType getNavigationIconType() {
3134
return NavigationIconType.HAMBURGER;
3235
}
36+
37+
@Override
38+
public void onClick(View view) {
39+
getFlowr().open("/second")
40+
.displayFragmentForResults(HomeFragment.targetFragmentId, HomeFragment.RC_STACK);
41+
}
3342
}

sample/src/main/java/com/fueled/flowr/sample/HomeFragment.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.fueled.flowr.sample;
22

33
import android.databinding.DataBindingUtil;
4+
import android.os.Bundle;
45
import android.view.View;
6+
import android.widget.Toast;
57

68
import com.fueled.flowr.NavigationIconType;
79
import com.fueled.flowr.sample.core.AbstractFragment;
@@ -13,6 +15,10 @@
1315
*/
1416
public class HomeFragment extends AbstractFragment implements View.OnClickListener {
1517

18+
public static final int RC_STACK = 101;
19+
public static int backStackIdentifier;
20+
public static String targetFragmentId;
21+
1622
private FragmentHomeBinding binding;
1723

1824
@Override
@@ -25,6 +31,7 @@ protected void setupView(View view) {
2531
binding = DataBindingUtil.bind(view);
2632
binding.homeOpenViewButton.setOnClickListener(this);
2733
binding.homeOpenLinkButton.setOnClickListener(this);
34+
binding.homeOpenFirstButton.setOnClickListener(this);
2835
}
2936

3037
@Override
@@ -41,8 +48,12 @@ public NavigationIconType getNavigationIconType() {
4148
public void onClick(View view) {
4249
if (view.getId() == R.id.home_open_view_button) {
4350
displayViewFragment();
44-
} else {
51+
} else if (view.getId() == R.id.home_open_link_button) {
4552
displayLinkFragment();
53+
} else if (view.getId() == R.id.home_open_first_button) {
54+
displayFirstFragment();
55+
} else {
56+
4657
}
4758
}
4859

@@ -57,4 +68,19 @@ private void displayLinkFragment() {
5768
getFlowr().open("/hello")
5869
.displayFragment();
5970
}
71+
72+
private void displayFirstFragment() {
73+
targetFragmentId = getFragmentId();
74+
backStackIdentifier = getFlowr().open("/first")
75+
.displayFragmentForResults(targetFragmentId, RC_STACK);
76+
}
77+
78+
@Override
79+
protected void onFragmentResults(int requestCode, int resultCode, Bundle data) {
80+
super.onFragmentResults(requestCode, resultCode, data);
81+
if (requestCode == RC_STACK && data.containsKey(SecondFragment.RESULT_FROM_SECOND)) {
82+
String resultFromStack = data.getString(SecondFragment.RESULT_FROM_SECOND);
83+
Toast.makeText(getContext(), resultFromStack, Toast.LENGTH_SHORT).show();
84+
}
85+
}
6086
}

sample/src/main/java/com/fueled/flowr/sample/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
8181
displayNavigationScreen(HomeFragment.class);
8282
break;
8383
case R.id.navigation_menu_categories:
84-
displayNavigationScreen(CategoriesFragment.class);
84+
displayNavigationScreen(FirstFragment.class);
8585
break;
8686
default:
8787
break;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fueled.flowr.sample;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.support.annotation.NonNull;
6+
import android.view.View;
7+
8+
import com.fueled.flowr.Flowr;
9+
import com.fueled.flowr.NavigationIconType;
10+
import com.fueled.flowr.annotations.DeepLink;
11+
import com.fueled.flowr.sample.core.AbstractFragment;
12+
13+
/**
14+
* Created by [email protected] on 18/05/2017.
15+
* Copyright (c) 2017 Fueled. All rights reserved.
16+
*/
17+
18+
@DeepLink(value = "/second")
19+
public class SecondFragment extends AbstractFragment implements View.OnClickListener {
20+
21+
public static final String RESULT_FROM_SECOND = "RESULT_FROM_SECOND";
22+
23+
@Override
24+
public int getLayoutId() {
25+
return R.layout.fragment_stack2;
26+
}
27+
28+
@Override
29+
protected void setupView(View view) {
30+
view.findViewById(R.id.return_home_button).setOnClickListener(this);
31+
}
32+
33+
@Override
34+
public String getTitle() {
35+
return "Second Fragment";
36+
}
37+
38+
@Override
39+
public NavigationIconType getNavigationIconType() {
40+
return NavigationIconType.HAMBURGER;
41+
}
42+
43+
@Override
44+
public void onClick(View view) {
45+
getFlowr().closeUptoWithResults(
46+
Flowr.getResultsResponse(getArguments(), Activity.RESULT_OK, getBundle()),
47+
HomeFragment.backStackIdentifier);
48+
}
49+
50+
@NonNull
51+
private Bundle getBundle() {
52+
Bundle args = new Bundle();
53+
args.putString(RESULT_FROM_SECOND, "Wow! Whata Stack!");
54+
return args;
55+
}
56+
}

sample/src/main/java/com/fueled/flowr/sample/core/FragmentResultPublisherImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private FragmentResultPublisherImpl() {
2525

2626
@Override
2727
public void publishResult(ResultResponse resultResponse) {
28-
28+
publishSubject.onNext(resultResponse);
2929
}
3030

3131
public Disposable observeResultsForFragment(final String fragmentId, Consumer<ResultResponse> consumer) {
@@ -52,5 +52,4 @@ public static FragmentResultPublisherImpl getInstance() {
5252

5353
return instance;
5454
}
55-
5655
}

sample/src/main/res/layout/fragment_categories.xml renamed to sample/src/main/res/layout/fragment_first.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
android:background="#FFFFFF"
66
android:orientation="vertical">
77

8-
<TextView
8+
<Button
9+
android:id="@+id/add_stack_button"
910
android:layout_width="wrap_content"
1011
android:layout_height="wrap_content"
1112
android:layout_gravity="center"
12-
android:text="@string/categories_message"/>
13+
android:text="@string/first_message"/>
1314

1415
</FrameLayout>

sample/src/main/res/layout/fragment_home.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
android:layout_marginTop="10dp"
2323
android:text="@string/open_link_fragment"/>
2424

25+
<Button
26+
android:id="@+id/home_open_first_button"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:layout_marginTop="10dp"
30+
android:text="@string/open_first_fragment"/>
31+
2532
</LinearLayout>
2633

2734
</layout>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="#FFFFFF"
6+
android:orientation="vertical">
7+
8+
<Button
9+
android:id="@+id/return_home_button"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:layout_gravity="center"
13+
android:text="@string/second_message"/>
14+
15+
</FrameLayout>

sample/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
<!-- Home Fragment -->
1010
<string name="open_view_fragment">Open View Fragment</string>
1111
<string name="open_link_fragment">Open Link Fragment</string>
12+
<string name="open_first_fragment">Open First Fragment</string>
1213

1314
<!-- Categories Fragment -->
1415
<string name="categories_message">Categories Fragment!</string>
16+
<string name="first_message">Open Second Fragment</string>
17+
<string name="second_message">Return to Home Fragment</string>
1518

1619
<!-- View Fragment -->
1720
<string name="view_pick_nav_color">Pick Navigation Bar Color</string>

0 commit comments

Comments
 (0)