Permalink
Browse files

a25: remove unnecessary Activity parameter, add KeyManager.register/u…

…nregisterKey
  • Loading branch information...
1 parent 15e5af6 commit 2be26af70d68afd4faa03f488a3134765ffb8503 @Zhuinden committed Jan 1, 2017
Showing with 79 additions and 71 deletions.
  1. +5 −0 CHANGELOG.md
  2. +3 −3 README.md
  3. +3 −1 flow-sample-orientation-lock/src/main/java/flow/sample/orientation/OrientationSampleDispatcher.java
  4. +19 −4 flowless-library/src/main/java/flowless/InternalLifecycleIntegration.java
  5. +13 −1 flowless-library/src/main/java/flowless/KeyManager.java
  6. +1 −5 flowless-library/src/main/java/flowless/preset/BaseDispatcher.java
  7. +0 −5 flowless-library/src/main/java/flowless/preset/ContainerRootDispatcher.java
  8. +0 −5 flowless-library/src/main/java/flowless/preset/SingleRootDispatcher.java
  9. +1 −1 flowless-sample-master-detail/src/main/java/com/zhuinden/flow_alpha_master_detail/MainActivity.java
  10. +1 −2 ...sample-master-detail/src/main/java/com/zhuinden/flow_alpha_master_detail/SinglePaneContainer.java
  11. +0 −5 ...aster-detail/src/main/java/com/zhuinden/flow_alpha_master_detail/extracted/ExampleDispatcher.java
  12. +1 −1 flowless-sample-single-root/src/main/java/com/zhuinden/flowless_dispatcher_sample/MainActivity.java
  13. +1 −5 ...ingle-root/src/main/java/com/zhuinden/flowless_dispatcher_sample/extracted/ExampleDispatcher.java
  14. +30 −13 flowless-sample-transitions/src/main/java/com/zhuinden/flowtransitions/MainActivity.java
  15. +0 −14 flowless-sample-transitions/src/main/java/com/zhuinden/flowtransitions/TransitionDispatcher.java
  16. +1 −1 flowless-viewpager-example/src/main/java/com/zhuinden/flowless_viewpager_example/MainActivity.java
  17. +0 −5 ...er-example/src/main/java/com/zhuinden/flowless_viewpager_example/extracted/ExampleDispatcher.java
View
@@ -1,6 +1,11 @@
Change Log
==========
+Flowless Version 1.0-alpha1.25 *(2017-01-01)*
+---------------------------------------------
+* BREAKING CHANGE: `BaseDispatcher` (and therefore `SingleRootDispatcher` and `ContainerRootDispatcher`) no longer take Activity constructor parameter
+* Added: `KeyManager.registerKey()` and `KeyManager.unregisterKey()` to allow registration of arbitrary key. The state bound to registered keys also gets retained.
+
Flowless Version 1.0-alpha1.24 *(2016-12-26)*
---------------------------------------------
* Fix: `context.getSystemService()` integration should throw IllegalStateException if Flow is not found after process death, instead of crashing with NPE
View
@@ -44,7 +44,7 @@ In order to use Flow(less), you need to add jitpack to your project root gradle:
and add the compile dependency to your module level gradle.
- compile 'com.github.Zhuinden:flowless:1.0-alpha1.24'
+ compile 'com.github.Zhuinden:flowless:1.0-alpha1.25'
Then, install Flow into your Activity:
@@ -58,7 +58,7 @@ public class MainActivity {
@Override
protected void attachBaseContext(Context newBase) {
- flowDispatcher = new TransitionDispatcher(this);
+ flowDispatcher = new TransitionDispatcher();
newBase = Flow.configure(newBase, this) //
.defaultKey(FirstKey.create()) //
.dispatcher(flowDispatcher) //
@@ -199,7 +199,7 @@ Android is a hostile environment. One of its greatest challenges is that your Ac
You [supply the serialization](https://github.com/Zhuinden/flowless/blob/master/flowless-library/src/main/java/flowless/KeyParceler.java) for your keys, and Flow does the rest. The default parceler uses Parcelable objects. Flow automatically saves and restores your History (including any state you've saved), taking care of all of the Android lifecycle events so you don't have to worry about them.
-**Note:** If you use the `ContainerDispatcherRoot`, you must call `ForceBundler.saveToBundle(activity, view)` manually in the `preSaveViewState()` method on the child you wish to persist in your container, because this cannot be handled automatically.
+**Note:** If you use the `ContainerDispatcherRoot`, you must call `ForceBundler.saveToBundle(view)` manually in the `preSaveViewState()` method on the child you wish to persist in your container, because this cannot be handled automatically.
## Pre-set dispatchers for common use-cases
@@ -51,8 +51,10 @@ public static void finishPendingTraversal() {
}
}
+ protected final Activity activity;
+
public OrientationSampleDispatcher(Activity activity) {
- super(activity);
+ this.activity = activity;
}
@Override
@@ -134,6 +134,8 @@ static void addHistoryToIntent(Intent intent, History history, KeyParceler parce
if(keyManager != null) {
innerBundle.putParcelableArrayList(KeyManager.GLOBAL_KEYS,
collectStatesFromKeys(keyManager, parceler, keyManager.globalKeys.iterator(), keyManager.globalKeys.size()));
+ innerBundle.putParcelableArrayList(KeyManager.REGISTERED_KEYS,
+ collectStatesFromKeys(keyManager, parceler, keyManager.registeredKeys.iterator(), keyManager.registeredKeys.size()));
}
bundle.putBundle(PERSISTENCE_KEY, innerBundle);
intent.putExtra(INTENT_KEY, bundle);
@@ -256,9 +258,14 @@ private static void save(Bundle bundle, KeyParceler parceler, History history, K
parceler,
keyManager.globalKeys.iterator(),
keyManager.globalKeys.size());
+ ArrayList<Parcelable> registeredKeyStates = collectStatesFromKeys(keyManager,
+ parceler,
+ keyManager.registeredKeys.iterator(),
+ keyManager.registeredKeys.size());
Bundle innerBundle = new Bundle();
innerBundle.putParcelableArrayList(KeyManager.GLOBAL_KEYS, globalStates);
innerBundle.putParcelableArrayList(KeyManager.HISTORY_KEYS, historyStates);
+ innerBundle.putParcelableArrayList(KeyManager.REGISTERED_KEYS, registeredKeyStates);
bundle.putBundle(PERSISTENCE_KEY, innerBundle);
}
@@ -273,19 +280,21 @@ private static void save(Bundle bundle, KeyParceler parceler, History history, K
return parcelables;
}
- private static void loadStatesIntoManager(ArrayList<Parcelable> stateBundles, KeyParceler parceler, KeyManager keyManager, History.Builder builder, boolean addToHistory) {
+ private static void loadStatesIntoManager(ArrayList<Parcelable> stateBundles, KeyParceler parceler, KeyManager keyManager, History.Builder builder, boolean addToHistory, boolean addToRegisteredKeys) {
if(stateBundles != null) {
for(Parcelable stateBundle : stateBundles) {
State state = State.fromBundle((Bundle) stateBundle, parceler);
if(addToHistory) {
builder.push(state.getKey());
}
+ if(addToRegisteredKeys) {
+ keyManager.registeredKeys.add(state.getKey());
+ }
if(!keyManager.hasState(state.getKey())) {
keyManager.addState(state);
}
}
}
-
}
private static void load(Bundle bundle, KeyParceler parceler, History.Builder builder, KeyManager keyManager) {
@@ -295,8 +304,14 @@ private static void load(Bundle bundle, KeyParceler parceler, History.Builder bu
Bundle innerBundle = bundle.getBundle(PERSISTENCE_KEY);
if(innerBundle != null) {
//noinspection ConstantConditions
- loadStatesIntoManager(innerBundle.getParcelableArrayList(KeyManager.HISTORY_KEYS), parceler, keyManager, builder, true);
- loadStatesIntoManager(innerBundle.getParcelableArrayList(KeyManager.GLOBAL_KEYS), parceler, keyManager, builder, false);
+ loadStatesIntoManager(innerBundle.getParcelableArrayList(KeyManager.REGISTERED_KEYS),
+ parceler,
+ keyManager,
+ builder,
+ false,
+ true);
+ loadStatesIntoManager(innerBundle.getParcelableArrayList(KeyManager.HISTORY_KEYS), parceler, keyManager, builder, true, false);
+ loadStatesIntoManager(innerBundle.getParcelableArrayList(KeyManager.GLOBAL_KEYS), parceler, keyManager, builder, false, false);
}
}
}
@@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@@ -43,9 +44,11 @@ public static KeyManager get(@NonNull View view) {
static final String GLOBAL_KEYS = "GLOBAL_KEYS";
static final String HISTORY_KEYS = "HISTORY_KEYS";
+ static final String REGISTERED_KEYS = "REGISTERED_KEYS";
/* private */ final Map<Object, State> states = new LinkedHashMap<>();
final Set<Object> globalKeys;
+ final Set<Object> registeredKeys = new HashSet<>();
KeyManager() {
this(null);
@@ -76,11 +79,20 @@ public State getState(Object key) {
return state;
}
+ public boolean registerKey(Object key) {
+ getState(key); // initialize state if does not exist
+ return registeredKeys.add(key);
+ }
+
+ public boolean unregisterKey(Object key) {
+ return registeredKeys.remove(key);
+ }
+
void clearNonGlobalStatesExcept(List<Object> keep) {
Iterator<Object> keys = states.keySet().iterator();
while(keys.hasNext()) {
final Object key = keys.next();
- if(!globalKeys.contains(key) && !keep.contains(key)) {
+ if(!globalKeys.contains(key) && !registeredKeys.contains(key) && !keep.contains(key)) {
keys.remove();
}
}
@@ -1,6 +1,5 @@
package flowless.preset;
-import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.ViewGroup;
@@ -32,10 +31,7 @@ public void setRoot(ViewGroup root) {
protected Context baseContext;
protected final RootHolder rootHolder;
- protected final Activity activity;
-
- public BaseDispatcher(Activity activity) {
- this.activity = activity; // required by ForceBundler
+ public BaseDispatcher() {
this.rootHolder = createRootHolder();
}
@@ -1,6 +1,5 @@
package flowless.preset;
-import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.CheckResult;
@@ -18,10 +17,6 @@
public class ContainerRootDispatcher
extends BaseDispatcher
implements FlowContainerLifecycleListener {
- public ContainerRootDispatcher(Activity activity) {
- super(activity);
- }
-
private boolean hasActiveView() {
return rootHolder != null && rootHolder.root != null;
}
@@ -1,6 +1,5 @@
package flowless.preset;
-import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.CheckResult;
@@ -122,8 +121,4 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
FlowLifecycleProvider.onRequestPermissionsResult(rootHolder.root.getChildAt(0), requestCode, permissions, grantResults);
}
}
-
- public SingleRootDispatcher(Activity activity) {
- super(activity);
- }
}
@@ -24,7 +24,7 @@
@Override
protected void attachBaseContext(Context newBase) {
- flowDispatcher = new ContainerRootDispatcher(this);
+ flowDispatcher = new ContainerRootDispatcher();
newBase = Flow.configure(newBase, this) //
.defaultKey(FirstKey.create()) //
.dispatcher(flowDispatcher) //
@@ -11,7 +11,6 @@
import com.zhuinden.flow_alpha_master_detail.extracted.ExampleDispatcher;
-import flowless.ActivityUtils;
import flowless.Dispatcher;
import flowless.Traversal;
import flowless.TraversalCallback;
@@ -48,7 +47,7 @@ public SinglePaneContainer(Context context, AttributeSet attrs, int defStyleAttr
SingleRootDispatcher singleRootDispatcher;
private void init() {
- singleRootDispatcher = new ExampleDispatcher(ActivityUtils.getActivity(getContext()));
+ singleRootDispatcher = new ExampleDispatcher();
singleRootDispatcher.getRootHolder().setRoot(this);
singleRootDispatcher.setBaseContext(((ContextWrapper)getContext()).getBaseContext());
}
@@ -2,7 +2,6 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
-import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
@@ -18,10 +17,6 @@
*/
public class ExampleDispatcher extends SingleRootDispatcher {
- public ExampleDispatcher(Activity activity) {
- super(activity);
- }
-
@Override
public void dispatch(@NonNull Traversal traversal, final @NonNull TraversalCallback callback) {
final ViewGroup root = rootHolder.getRoot();
@@ -24,7 +24,7 @@
@Override
protected void attachBaseContext(Context newBase) {
- flowDispatcher = new ExampleDispatcher(this);
+ flowDispatcher = new ExampleDispatcher();
newBase = Flow.configure(newBase, this) //
.defaultKey(FirstKey.create()) //
.dispatcher(flowDispatcher) //
@@ -2,7 +2,6 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
-import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
@@ -11,16 +10,13 @@
import flowless.Traversal;
import flowless.TraversalCallback;
import flowless.ViewUtils;
-import flowless.preset.*;
+import flowless.preset.SingleRootDispatcher;
/**
* Created by Zhuinden on 2016.12.03..
*/
public class ExampleDispatcher extends SingleRootDispatcher {
- public ExampleDispatcher(Activity activity) {
- super(activity);
- }
@Override
public void dispatch(@NonNull Traversal traversal, final @NonNull TraversalCallback callback) {
@@ -1,25 +1,16 @@
package com.zhuinden.flowtransitions;
import android.content.Context;
-import android.os.AsyncTask;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
-import android.os.Bundle;
-import android.util.Log;
-import android.view.LayoutInflater;
import android.view.ViewGroup;
-import android.widget.Button;
-import android.widget.TextView;
-
-import com.transitionseverywhere.AutoTransition;
-import com.transitionseverywhere.Scene;
-import com.transitionseverywhere.TransitionManager;
import butterknife.BindView;
import butterknife.ButterKnife;
-import butterknife.OnClick;
import flowless.Flow;
-import flowless.Installer;
public class MainActivity
extends AppCompatActivity {
@@ -32,7 +23,7 @@
@Override
protected void attachBaseContext(Context newBase) {
- transitionDispatcher = new TransitionDispatcher(this);
+ transitionDispatcher = new TransitionDispatcher();
newBase = Flow.configure(newBase, this)
.defaultKey(SceneMainDefaultKey.create())
.dispatcher(transitionDispatcher)
@@ -48,6 +39,32 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
ButterKnife.bind(this);
transitionDispatcher.getRootHolder().setRoot(root);
}
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ super.onActivityResult(requestCode, resultCode, data);
+ transitionDispatcher.onActivityResult(requestCode, resultCode, data);
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+ transitionDispatcher.onRequestPermissionsResult(requestCode, permissions, grantResults);
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ transitionDispatcher.preSaveViewState();
+ super.onSaveInstanceState(outState);
+ }
+
+ @Override
+ public void onBackPressed() {
+ if(transitionDispatcher.onBackPressed()) {
+ return;
+ }
+ super.onBackPressed();
+ }
}
// @BindView(R.id.main_button)
Oops, something went wrong.

0 comments on commit 2be26af

Please sign in to comment.