Skip to content

Commit b53ccd6

Browse files
committed
Added ScreenFlow.java
1 parent 5514c36 commit b53ccd6

File tree

3 files changed

+232
-1
lines changed

3 files changed

+232
-1
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codekit/build.gradle

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 CtrlBytes Technologies
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
126
apply plugin: 'com.android.library'
227

328
android {
@@ -7,7 +32,7 @@ android {
732
minSdkVersion 19
833
targetSdkVersion 29
934
versionCode 1
10-
versionName "0.1.0"
35+
versionName "0.1.1"
1136

1237
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1338
consumerProguardFiles "consumer-rules.pro"
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 CtrlBytes Technologies
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package com.ctrlbytes.codekit.ui;
27+
28+
import android.app.Activity;
29+
import android.content.Intent;
30+
import android.os.Build;
31+
import android.os.Bundle;
32+
import android.view.View;
33+
34+
import java.util.ArrayList;
35+
36+
import androidx.annotation.CheckResult;
37+
import androidx.core.app.ActivityOptionsCompat;
38+
import androidx.core.util.Pair;
39+
import androidx.fragment.app.Fragment;
40+
41+
@SuppressWarnings("unused")
42+
public class ScreenFlow {
43+
44+
private Activity mActivity;
45+
private Fragment mFragment;
46+
private int mResultCode;
47+
private Class<? extends Activity> mDestinationActivity;
48+
private boolean hasSharedElements;
49+
private Pair<View, String>[] mSharedElements;
50+
private Bundle mBundle;
51+
private int mFlags;
52+
private boolean hasFlags;
53+
54+
private ScreenFlow(ScreenFlowBuilder mBuilder) {
55+
this.mActivity = mBuilder.mActivity;
56+
this.mFragment = mBuilder.mFragment;
57+
this.mDestinationActivity = mBuilder.mDestinationActivity;
58+
this.mResultCode = mBuilder.mResultCode;
59+
this.hasSharedElements = mBuilder.hasSharedElements;
60+
this.mSharedElements = mBuilder.mSharedElements;
61+
this.mBundle = mBuilder.mBundle;
62+
this.mFlags = mBuilder.mFlags;
63+
this.hasFlags = mBuilder.hasFlags;
64+
}
65+
66+
void launch() {
67+
if (!hasSharedElements) {
68+
if (mActivity != null) {
69+
if (mResultCode == -1) {
70+
mActivity.startActivity(createIntent());
71+
} else {
72+
mActivity.startActivityForResult(createIntent(), mResultCode);
73+
}
74+
} else if (mFragment != null) {
75+
if (mResultCode == -1) {
76+
mFragment.startActivity(createIntent());
77+
} else {
78+
mFragment.startActivityForResult(createIntent(), mResultCode);
79+
}
80+
}
81+
} else {
82+
if (mActivity != null) {
83+
ActivityOptionsCompat options = ActivityOptionsCompat
84+
.makeSceneTransitionAnimation(mActivity, mSharedElements);
85+
if (mResultCode == -1) {
86+
mActivity.startActivity(createIntent(), options.toBundle());
87+
} else {
88+
mActivity.startActivityForResult(createIntent(), mResultCode, options.toBundle());
89+
}
90+
} else if (mFragment != null) {
91+
ActivityOptionsCompat options = ActivityOptionsCompat
92+
.makeSceneTransitionAnimation(mFragment.requireActivity(), mSharedElements);
93+
if (mResultCode == -1) {
94+
mFragment.startActivity(createIntent(), options.toBundle());
95+
} else {
96+
mFragment.startActivityForResult(createIntent(), mResultCode, options.toBundle());
97+
}
98+
}
99+
}
100+
}
101+
102+
private Intent createIntent() {
103+
Intent intent;
104+
if (mActivity != null) {
105+
intent = new Intent(mActivity, mDestinationActivity);
106+
} else {
107+
intent = new Intent(mFragment.getActivity(), mDestinationActivity);
108+
}
109+
intent.putExtras(mBundle);
110+
111+
if (hasFlags) {
112+
intent.setFlags(mFlags);
113+
}
114+
return intent;
115+
}
116+
117+
@CheckResult
118+
public static ScreenFlowBuilder from(Activity mActivity) {
119+
return new ScreenFlowBuilder(mActivity);
120+
}
121+
122+
@CheckResult
123+
public static ScreenFlowBuilder from(Fragment fragment) {
124+
return new ScreenFlowBuilder(fragment);
125+
}
126+
127+
public static class ScreenFlowBuilder {
128+
129+
private Activity mActivity = null;
130+
private Fragment mFragment = null;
131+
private Class<? extends Activity> mDestinationActivity;
132+
private int mResultCode = -1;
133+
private boolean hasSharedElements = false;
134+
private Pair<View, String>[] mSharedElements;
135+
private int mFlags;
136+
private boolean hasFlags = false;
137+
private Bundle mBundle = new Bundle();
138+
139+
private ScreenFlowBuilder(Activity activity) {
140+
this.mActivity = activity;
141+
}
142+
143+
private ScreenFlowBuilder(Fragment fragment) {
144+
this.mFragment = fragment;
145+
}
146+
147+
@CheckResult
148+
public ScreenFlowBuilder useResult(int resultCode) {
149+
this.mResultCode = resultCode;
150+
return this;
151+
}
152+
153+
@CheckResult
154+
public ScreenFlowBuilder useFlags(int flags) {
155+
hasFlags = true;
156+
this.mFlags = flags;
157+
return this;
158+
}
159+
160+
@SafeVarargs
161+
@CheckResult
162+
public final ScreenFlowBuilder withSharedElements(Pair<View, String>... sharedElements) {
163+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
164+
hasSharedElements = true;
165+
mSharedElements = sharedElements;
166+
}
167+
return this;
168+
}
169+
170+
@CheckResult
171+
public ScreenFlowBuilder withArg(String Key, Integer object) {
172+
mBundle.putInt(Key, object);
173+
return this;
174+
}
175+
176+
@CheckResult
177+
public ScreenFlowBuilder withArg(String Key, String object) {
178+
mBundle.putString(Key, object);
179+
return this;
180+
}
181+
182+
@CheckResult
183+
public ScreenFlowBuilder withArg(String Key, Double object) {
184+
mBundle.putDouble(Key, object);
185+
return this;
186+
}
187+
188+
@CheckResult
189+
public ScreenFlowBuilder withIntegerListArg(String Key, ArrayList<Integer> object) {
190+
mBundle.putIntegerArrayList(Key, object);
191+
return this;
192+
}
193+
194+
@CheckResult
195+
public ScreenFlowBuilder withStringListArg(String Key, ArrayList<String> object) {
196+
mBundle.putStringArrayList(Key, object);
197+
return this;
198+
}
199+
200+
public void to(Class<? extends Activity> destinationActivity) {
201+
mDestinationActivity = destinationActivity;
202+
new ScreenFlow(this).launch();
203+
}
204+
}
205+
}

0 commit comments

Comments
 (0)