Skip to content

Commit 5c5e2c2

Browse files
author
chetan-fueled
committed
feat(*): add close upto
refactor(strings.xml) refactor(FragmentResultPublisherImpl) refactor(FragmentResultPublisher): rename
1 parent 38f3cac commit 5c5e2c2

File tree

11 files changed

+210
-45
lines changed

11 files changed

+210
-45
lines changed

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,11 @@ protected final String getTagPrefix() {
250250
return tagPrefix;
251251
}
252252

253-
protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionData<T> data) {
253+
protected <T extends Fragment & FlowrFragment> int displayFragment(TransactionData<T> data) {
254+
int identifier = -1;
254255
try {
255256
if (screen == null) {
256-
return;
257+
return identifier;
257258
}
258259

259260
injectDeepLinkInfo(data);
@@ -282,7 +283,7 @@ protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionD
282283
transaction.add(mainContainerId, fragment);
283284
}
284285

285-
transaction.commit();
286+
identifier = transaction.commit();
286287

287288
if (data.isSkipBackStack()) {
288289
setCurrentFragment(fragment);
@@ -291,6 +292,7 @@ protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionD
291292
} catch (Exception e) {
292293
Log.e(TAG, "Error while displaying fragment.", e);
293294
}
295+
return identifier;
294296
}
295297

296298
/**
@@ -402,6 +404,26 @@ public void close(int n) {
402404
}
403405
}
404406

407+
/**
408+
* Closes the current activity if the fragments back stack is empty,
409+
* otherwise pop the top n fragments from the stack.
410+
*
411+
* @param id Identifier of the stated to be popped.
412+
*/
413+
public void closeUpto(int id) {
414+
if (screen == null) {
415+
return;
416+
}
417+
418+
int count = screen.getScreenFragmentManager().getBackStackEntryCount();
419+
if (count > 1) {
420+
screen.getScreenFragmentManager()
421+
.popBackStackImmediate(id, FragmentManager.POP_BACK_STACK_INCLUSIVE);
422+
} else {
423+
close();
424+
}
425+
}
426+
405427
/**
406428
* Closes the current activity if the fragments back stack is empty,
407429
* otherwise pop the top fragment from the stack and publish the results response.
@@ -427,6 +449,21 @@ public void closeWithResults(ResultResponse resultResponse, int n) {
427449
}
428450
}
429451

452+
/**
453+
* Closes the current activity if the fragments back stack is empty,
454+
* otherwise pop the top n fragments from the stack and publish the results response.
455+
*
456+
* @param resultResponse the results response to be published
457+
* @param id Identifier of the stated to be popped.
458+
*/
459+
public void closeUptoWithResults(ResultResponse resultResponse, int id) {
460+
closeUpto(id);
461+
462+
if (resultResponse != null) {
463+
resultPublisher.publishResult(resultResponse);
464+
}
465+
}
466+
430467
/**
431468
* Clears the fragments back stack.
432469
*/
@@ -740,9 +777,11 @@ public Builder noTransactionAnimation() {
740777

741778
/**
742779
* Displays the fragment using this builder configurations.
780+
*
781+
* @return id Identifier of the stated to be popped.
743782
*/
744-
public void displayFragment() {
745-
Flowr.this.displayFragment(data);
783+
public int displayFragment() {
784+
return Flowr.this.displayFragment(data);
746785
}
747786

748787
/**
@@ -752,8 +791,10 @@ public void displayFragment() {
752791
* it will be later used to deliver the results to the correct fragment instance.
753792
* @param requestCode this code will be returned in {@link ResultResponse} when the fragment is closed,
754793
* and it can be used to identify the request from which the results were returned.
794+
*
795+
* @return id Identifier of the stated to be popped.
755796
*/
756-
public void displayFragmentForResults(String fragmentId, int requestCode) {
797+
public int displayFragmentForResults(String fragmentId, int requestCode) {
757798
if (!TextUtils.isEmpty(fragmentId)) {
758799
if (data.getArgs() == null) {
759800
data.setArgs(new Bundle());
@@ -763,7 +804,7 @@ public void displayFragmentForResults(String fragmentId, int requestCode) {
763804
getResultRequestBundle(fragmentId, requestCode));
764805
}
765806

766-
Flowr.this.displayFragment(data);
807+
return Flowr.this.displayFragment(data);
767808
}
768809

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

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

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fueled.flowr.sample;
2+
3+
import android.view.View;
4+
5+
import com.fueled.flowr.NavigationIconType;
6+
import com.fueled.flowr.annotations.DeepLink;
7+
import com.fueled.flowr.sample.core.AbstractFragment;
8+
9+
import static com.fueled.flowr.sample.core.FragmentResultPublisherImpl.sourceFragmentId;
10+
11+
/**
12+
* Created by [email protected] on 18/05/2017.
13+
* Copyright (c) 2017 Fueled. All rights reserved.
14+
*/
15+
16+
@DeepLink(value = "/first")
17+
public class FirstFragment extends AbstractFragment implements View.OnClickListener {
18+
19+
@Override
20+
public int getLayoutId() {
21+
return R.layout.fragment_first;
22+
}
23+
24+
@Override
25+
protected void setupView(View view) {
26+
view.findViewById(R.id.add_stack_button).setOnClickListener(this);
27+
}
28+
29+
@Override
30+
public String getTitle() {
31+
return "First Fragment";
32+
}
33+
34+
@Override
35+
public NavigationIconType getNavigationIconType() {
36+
return NavigationIconType.HAMBURGER;
37+
}
38+
39+
@Override
40+
public void onClick(View view) {
41+
getFlowr().open("/second")
42+
.displayFragmentForResults(sourceFragmentId, HomeFragment.RC_STACK);
43+
}
44+
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
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;
810
import com.fueled.flowr.sample.databinding.FragmentHomeBinding;
911

12+
import static com.fueled.flowr.sample.core.FragmentResultPublisherImpl.backStackIdentifier;
13+
import static com.fueled.flowr.sample.core.FragmentResultPublisherImpl.sourceFragmentId;
14+
1015
/**
1116
* Created by [email protected] on 13/02/2017.
1217
* Copyright (c) 2017 Fueled. All rights reserved.
1318
*/
1419
public class HomeFragment extends AbstractFragment implements View.OnClickListener {
1520

21+
public static final int RC_STACK = 101;
22+
1623
private FragmentHomeBinding binding;
1724

1825
@Override
@@ -25,6 +32,7 @@ protected void setupView(View view) {
2532
binding = DataBindingUtil.bind(view);
2633
binding.homeOpenViewButton.setOnClickListener(this);
2734
binding.homeOpenLinkButton.setOnClickListener(this);
35+
binding.homeOpenFirstButton.setOnClickListener(this);
2836
}
2937

3038
@Override
@@ -41,8 +49,12 @@ public NavigationIconType getNavigationIconType() {
4149
public void onClick(View view) {
4250
if (view.getId() == R.id.home_open_view_button) {
4351
displayViewFragment();
44-
} else {
52+
} else if (view.getId() == R.id.home_open_link_button) {
4553
displayLinkFragment();
54+
} else if (view.getId() == R.id.home_open_first_button) {
55+
displayFirstFragment();
56+
} else {
57+
4658
}
4759
}
4860

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

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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import static com.fueled.flowr.sample.core.FragmentResultPublisherImpl.backStackIdentifier;
14+
15+
/**
16+
* Created by [email protected] on 18/05/2017.
17+
* Copyright (c) 2017 Fueled. All rights reserved.
18+
*/
19+
20+
@DeepLink(value = "/second")
21+
public class SecondFragment extends AbstractFragment implements View.OnClickListener {
22+
23+
public static final String RESULT_FROM_SECOND = "RESULT_FROM_SECOND";
24+
25+
@Override
26+
public int getLayoutId() {
27+
return R.layout.fragment_stack2;
28+
}
29+
30+
@Override
31+
protected void setupView(View view) {
32+
view.findViewById(R.id.return_home_button).setOnClickListener(this);
33+
}
34+
35+
@Override
36+
public String getTitle() {
37+
return "Second Fragment";
38+
}
39+
40+
@Override
41+
public NavigationIconType getNavigationIconType() {
42+
return NavigationIconType.HAMBURGER;
43+
}
44+
45+
@Override
46+
public void onClick(View view) {
47+
getFlowr().closeUptoWithResults(
48+
Flowr.getResultsResponse(getArguments(), Activity.RESULT_OK, getBundle()),
49+
backStackIdentifier);
50+
}
51+
52+
@NonNull
53+
private Bundle getBundle() {
54+
Bundle args = new Bundle();
55+
args.putString(RESULT_FROM_SECOND, "Wow! Whata Stack!");
56+
return args;
57+
}
58+
}

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

Lines changed: 3 additions & 1 deletion
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) {
@@ -53,4 +53,6 @@ public static FragmentResultPublisherImpl getInstance() {
5353
return instance;
5454
}
5555

56+
public static int backStackIdentifier;
57+
public static String sourceFragmentId;
5658
}

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>

0 commit comments

Comments
 (0)