Skip to content

Commit

Permalink
Fixes intent handling and adds sample app, test for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrjr committed Apr 13, 2017
1 parent 6971db9 commit 63741b1
Show file tree
Hide file tree
Showing 18 changed files with 355 additions and 1 deletion.
46 changes: 46 additions & 0 deletions flow-sample-intents/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
buildscript {
dependencies {
classpath deps.android_gradle_plugin
}
}

apply plugin: 'com.android.application'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
applicationId "flow.sample.intents"

minSdkVersion rootProject.ext.minSdkVersion
versionName VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
}
}

packagingOptions {
exclude 'LICENSE.txt'
}
}

dependencies {
compile project(':flow')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}

//noinspection GroovyAssignabilityCheck
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.1.1'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package flow.sample.basic;

import android.content.Context;
import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import flow.Flow;
import flow.History;
import flow.sample.intents.IntentsSampleActivity;
import flow.sample.intents.StringParceler;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.pressBack;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static java.util.Arrays.asList;

/**
* Demonstrates the use of Intents to drive espresso tests to specific screens.
*/
public class IntentsSampleTest {

@Rule public ActivityTestRule rule = new ActivityTestRule<>(IntentsSampleActivity.class);

@Test public void wellIntentioned() {
List<String> expected = asList("Able", "Baker", "Charlie");
History h = History.emptyBuilder().pushAll(expected).build();

Context context = getInstrumentation().getTargetContext().getApplicationContext();
Intent intent = new Intent(context, IntentsSampleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Flow.addHistory(intent, h, new StringParceler());
context.startActivity(intent);

onView(withText("Charlie")).perform(pressBack());
onView(withText("Baker")).perform(pressBack());
onView(withText("Able")).perform(click());
}
}
36 changes: 36 additions & 0 deletions flow-sample-intents/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Square Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="flow.sample.intents" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".IntentsSampleActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package flow.sample.intents;

import android.app.Activity;
import android.content.Context;
import flow.Flow;

public class IntentsSampleActivity extends Activity {

@Override protected void attachBaseContext(Context baseContext) {
baseContext = Flow.configure(baseContext, this).keyParceler(new StringParceler()).install();
super.attachBaseContext(baseContext);
}

@Override public void onBackPressed() {
if (!Flow.get(this).goBack()) {
super.onBackPressed();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flow.sample.intents;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import flow.KeyParceler;

public class StringParceler implements KeyParceler {
@NonNull @Override public Parcelable toParcelable(@NonNull Object key) {
Bundle bundle = new Bundle();
bundle.putString("stringKey", (String) key);
return bundle;
}

@NonNull @Override public Object toKey(@NonNull Parcelable parcelable) {
Bundle bundle = (Bundle) parcelable;
return bundle.getString("stringKey");
}
}
25 changes: 25 additions & 0 deletions flow-sample-intents/src/main/res/layout/basic_activity_frame.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Square Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/basic_activity_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IntentsSampleActivity"
/>
45 changes: 45 additions & 0 deletions flow-sample-intents/src/main/res/layout/hello_screen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Square Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<flow.sample.intents.HelloView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>
<TextView
android:id="@+id/hello_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="@+id/hello_increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Increment"
/>
<TextView
android:id="@+id/hello_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:freezesText="true"
android:text="0"
/>
</flow.sample.intents.HelloView>
36 changes: 36 additions & 0 deletions flow-sample-intents/src/main/res/layout/welcome_screen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Square Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<flow.sample.intents.WelcomeView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/welcome_screen_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hello, what's your name?"
/>
</android.support.design.widget.TextInputLayout>
</flow.sample.intents.WelcomeView>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions flow-sample-intents/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Square Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
19 changes: 19 additions & 0 deletions flow-sample-intents/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
~ Copyright 2016 Square Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>
<string name="app_name">Flow Basic Sample</string>
</resources>
27 changes: 27 additions & 0 deletions flow-sample-intents/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
~ Copyright 2016 Square Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>
3 changes: 2 additions & 1 deletion flow/src/main/java/flow/InternalLifecycleIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static void install(final Application app, final Activity activity,
}
// We always replace the dispatcher because it frequently references the Activity.
fragment.dispatcher = dispatcher;
fragment.intent = a.getIntent();
if (newFragment) {
activity.getFragmentManager() //
.beginTransaction() //
Expand Down Expand Up @@ -111,7 +112,7 @@ static void addHistoryToIntent(Intent intent, History history, KeyParceler parce
Object key = keys.next();
parcelables.add(State.empty(key).toBundle(parceler));
}
bundle.putParcelableArrayList("FLOW_STATE", parcelables);
bundle.putParcelableArrayList(PERSISTENCE_KEY, parcelables);
intent.putExtra(INTENT_KEY, bundle);
}

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include ':flow'
include ':flow-sample-helloworld'
include ':flow-sample-intents'
include ':flow-sample-basic'
include ':flow-sample-multikey'
include ':flow-sample-orientation-lock'
Expand Down

0 comments on commit 63741b1

Please sign in to comment.