GithubHelp home page GithubHelp logo

zhuinden / flowless Goto Github PK

View Code? Open in Web Editor NEW
142.0 7.0 7.0 1 MB

[DEPRECATED] Based on Flow 1.0-alpha. To keep your life simple, use zhuinden/simple-stack instead.

License: Apache License 2.0

Java 100.00%

flowless's Introduction

[DEPRECATED]

Deprecated in favor of Simple-Stack.

Due to the way Flow initializes itself, the Backstack cannot be accessed before the bootstrap traversal, and doesn't exist before onPostCreate().

That, and restoring application state from a History added to an Intent using Flow.addHistoryToIntent() is a nightmare in terms of extendability, and this is entirely because only Flow's internal classes know how to restore the backstack from an intent directly.

A migration guide (along with the motivation behind the change) is available here.

BackstackDelegate has since been replaced (for most use-cases) with Navigator.

For replacing ServiceFactory and MultiKey/TreeKey, use ScopedServices and ScopeKey/ScopeKey.Child.

Flow(less)

"Name-giving will be the foundation of our science." - Linnaeus

"Memory is the treasury and guardian of all things." - Cicero

"It's better if you're good at one thing than if you're bad at many things just because you're trying too hard. Especially if you're a backstack library." - Zhuinden

Flow(less) gives names to your Activity's UI states, navigates between them, and remembers where it's been.

This used to be a fork of Flow 1.0-alpha by Square, with the "resource management" aspect removed. Now it provides more than that, both in terms of bug fixes and some additional features (specifically the dispatcher lifecycle integration) alike.

Features

Navigate between UI states. Support the back button easily without confusing your users with surprising results.

Remember the UI state, and its history, as you navigate and across configuration changes and process death.

Manage resources. UI states can create and share resources, and you can dispose of them when no longer needed.

Manage all types of UIs-- complex master-detail views, multiple layers, and window-based dialogs are all simple to manage.

Using Flow(less)

In order to use Flow(less), you need to add jitpack to your project root gradle:

buildscript {
    repositories {
        // ...
        maven { url "https://jitpack.io" }
    }
    // ...
}
allprojects {
    repositories {
        // ...
        maven { url "https://jitpack.io" }
    }
    // ...
}

and add the compile dependency to your module level gradle.

compile 'com.github.Zhuinden:flowless:1.0-RC2'

Then, install Flow into your Activity:

public class MainActivity {
    @BindView(R.id.main_root)
    ViewGroup root;

    TransitionDispatcher flowDispatcher; // extends SingleRootDispatcher

    @Override
    protected void attachBaseContext(Context newBase) {
        flowDispatcher = new TransitionDispatcher();
        newBase = Flow.configure(newBase, this) //
                .defaultKey(FirstKey.create()) //
                .dispatcher(flowDispatcher) //
                .install(); //
        flowDispatcher.setBaseContext(this);
        super.attachBaseContext(newBase);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        flowDispatcher.getRootHolder().setRoot(root);
    }

    @Override
    public void onBackPressed() {
        if(!flowDispatcher.onBackPressed()) {
            super.onBackPressed();
        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        flowDispatcher.preSaveViewState(); // optional
        super.onSaveInstanceState(outState);
    }

    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        flowDispatcher.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        flowDispatcher.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

Defining UI states with key objects

Your Activity's UI states are represented in Flow by Objects, which Flow refers to as "keys". Keys are typically value objects with just enough information to identify a discrete UI state.

Flow relies on a key's equals and hashCode methods for its identity. Keys should be immutable-- that is, their equals and hashCode methods should always behave the same.

To give an idea of what keys might look like, here are some examples:

public enum TabKey {
  TIMELINE,
  NOTIFICATIONS,
  PROFILE
}

public final class HomeKey extends flowless.ClassKey {
}

public final class ArticleKey {
  public final String articleId;

  public ArticleKey(String articleId) {
    this.articleId = articleId;
  }

  public boolean equals(Object o) {
    return o instanceof ArticleKey
        && articleId.equals(((ArticleKey) o).articleId);
  }
  
  public int hashCode() {
    return articleId.hashCode();
  }
}

But if you want to be really cool, you can use Auto-Parcel to generate Parcelable immutable objects to define your keys.

public interface LayoutKey
        extends Parcelable {
    @LayoutRes int layout();

    FlowAnimation animation();
}

@AutoValue
public abstract class CalendarEventKey implements LayoutKey {
    abstract long eventId();

    public static CalendarEventKey create(long eventId) {
        return new AutoValue_CalendarEventKey(R.layout.path_calendarevent, FlowAnimation.SEGUE, eventId);
    }
}

Navigation and History

Flow offers simple commands for navigating within your app.

Flow#goBack() -- Goes back to the previous key. Think "back button".

Flow#set(key) -- Goes to the requested key. Goes back or forward depending on whether the key is already in the History.

Flow also lets you rewrite history safely and easily.

Flow#setHistory(history, direction) -- Change history to whatever you want.

To modify the history, you ought to use the operators provided by History. Here is an example:

History history = Flow.get(this).getHistory();
Flow.get(this).setHistory(history.buildUpon().pop(2).push(SomeKey.create()).build(), Direction.BACKWARD);

See the Flow class for other convenient operators.

As you navigate the app, Flow keeps track of where you've been. And Flow makes it easy to save view state (and any other state you wish) so that when your users go back to a place they've been before, it's just as they left it.

Controlling UI

Navigation only counts if it changes UI state. Because every app has different needs, Flow lets you plug in your own logic for responding to navigation and updating your UI.

The Dispatcher has the following tasks when a new state is set:

  • Check for short-circuit if new state is same as the old (DispatcherUtils.isPreviousKeySameAsNewKey()), and if true, callback and return
  • Inflate the new view with Flow's internal context using LayoutInflater.from(traversal.createContext(...))
  • Persist the current view (DispatcherUtils.persistViewToStateAndNotifyRemoval())
  • Restore state to new view (DispatcherUtils.restoreViewFromState())
  • Optionally animate the two views (with TransitionManager or AnimatorSet)
  • Remove the current view
  • Add the new view
  • Signal to Flow that the traversal is complete (callback.onTraversalCompleted())

Surviving configuration changes and process death

Android is a hostile environment. One of its greatest challenges is that your Activity or even your process can be destroyed and recreated under a variety of circumstances. Flow makes it easy to weather the storm, by automatically remembering your app's state and its history.

You supply the serialization 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(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

Two use-cases are supported out of the box. Both of them provide (optional) life-cycle hooks for easier usage within your custom viewgroups.

First is the SingleRootDispatcher, which works if your Activity has a single root, meaning you're changing the screens within a single ViewGroup within your Activity. This base class provides the default event delegation to the view inside your root. The dispatch() method has to be implemented by the user.

Second is the ContainerRootDispatcher. Its purpose is to delegate the dispatch() call and all other lifecycle method calls to your defined custom viewgroup. The Root provided to a container root dispatcher must implement Dispatcher. It must also delegate the lifecycle method call to its children. For easier access to all lifecycle methods, the FlowContainerLifecycleListener interface is introduced, and the FlowLifecycleProvider class tries to make delegation as simple as possible. Of course, delegation of the FlowLifecycles lifecycle methods are optional, and you can choose to delegate only what you actually need. An example is provided for this setup in the Master-Detail example.

Example Custom View Group

The most typical setup for a custom view group would look like so, using the Bundleable interface and listening to state restoration with ViewLifecycleListener.

public class FirstView
        extends RelativeLayout
        implements Bundleable, FlowLifecycles.ViewLifecycleListener {
    private static final String TAG = "FirstView";

    public FirstView(Context context) {
        super(context);
        init();
    }

    public FirstView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FirstView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(21)
    public FirstView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    FirstKey firstKey;

    public void init() {
        if(!isInEditMode()) {
            firstKey = Flow.getKey(this);
        }
    }

    @OnClick(R.id.first_button)
    public void firstButtonClick(View view) {
        Flow.get(view).set(SecondKey.create());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.bind(this);
    }

    @Override
    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        // persist state here
        return bundle;
    }

    @Override
    public void fromBundle(@Nullable Bundle bundle) {
        if(bundle != null) {
            // restore state here
        }
    }

    @Override
    public void onViewRestored() {
        // view was created and state has been restored
    }

    @Override
    public void onViewDestroyed(boolean removedByFlow) {
        // view is about to be destroyed, either by Activity death 
        // or the Flow dispatcher has removed it
    }
}

The view is created based on the key:

@AutoValue
public abstract class FirstKey
        implements LayoutKey {
    public static FirstKey create() {
        return new AutoValue_FirstKey(R.layout.path_first);
    }
}

And it's inflated based on the following XML:

<?xml version="1.0" encoding="utf-8"?>
<com.zhuinden.flowless_dispatcher_sample.FirstView 
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <Button
        android:id="@+id/first_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/go_to_second_view"/>
</com.zhuinden.flowless_dispatcher_sample.FirstView>

Managing resources (optional)

You can manage resources shared through your context manually using the ServiceProvider, which you can obtain through ServiceProvider.get().

This way, you can bind services you need when you initialize your View in its constructor (before onFinishInflate() is called) or before it's inflated in the Dispatcher itself, while also sharing them to additional views that belong to the same Context.

Here is a rather barebones implementation that creates services for elements that are currently within the history of keys.

ServiceProvider serviceProvider = ServiceProvider.get(newContext);

// destroyNotIn()
Iterator<Object> aElements = traversal.origin != null ? traversal.origin.reverseIterator() : Collections.emptyList().iterator();
Iterator<Object> bElements = traversal.destination.reverseIterator();
while(aElements.hasNext() && bElements.hasNext()) {
    BaseKey aElement = (BaseKey) aElements.next();
    BaseKey bElement = (BaseKey) bElements.next();
    if(!aElement.equals(bElement)) {
        serviceProvider.unbindServices(aElement);  // returns map of bound services
        break;
    }
}
while(aElements.hasNext()) {
    BaseKey aElement = (BaseKey) aElements.next();
    serviceProvider.unbindServices(aElement); // returns map of bound services
}
// end destroyNotIn

// create service for keys
for(Object destination : traversal.destination) {
    if(!serviceProvider.hasService(destination, DaggerService.TAG) {
        serviceProvider.bindService(destination, DaggerService.TAG, ((BaseKey) destination).createComponent());
    }
}

Which can now share the following service:

public class DaggerService {
    public static final String TAG = "DaggerService";

    @SuppressWarnings("unchecked")
    public static <T> T getComponent(Context context) {
        //noinspection ResourceType
        return (T) ServiceProvider.get(context).getService(Flow.getKey(context), TAG);
    }
}

And can be obtained like so:

private void init(Context context) {
    if(!isInEditMode()) {
        LoginComponent loginComponent = DaggerService.getComponent(context);
        loginComponent.inject(this);
    }
}

License

Copyright 2013 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.

flowless's People

Contributors

balintbabics avatar chrisjenx avatar chrisrenke avatar edenman avatar holmes avatar jakewharton avatar jameswald avatar jdreesen avatar jrodbx avatar lexs avatar loganj avatar manuelpeinado avatar mattprecious avatar pforhan avatar pyricau avatar rjrjr avatar wuman avatar zhuinden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

flowless's Issues

Apps using Flowless?

Are there any apps out there using this library that care to review what it's been like?

Flow is `null` but `InternalLifecycleIntegration` exists after process death, this results in NPE in `getSystemService` integration

                                                                             Caused by: java.lang.NullPointerException
                                                                                at flowless.InternalContextWrapper.getSystemService(InternalContextWrapper.java:69)
                                                                                at flowless.ServiceProvider.get(ServiceProvider.java:22)
                                                                                at com.zhuinden.examplegithubclient.presentation.activity.main.MainActivity.getSystemService(MainActivity.java:156)
                                                                                at android.view.ViewConfiguration.<init>(ViewConfiguration.java:282)
                                                                                at android.view.ViewConfiguration.get(ViewConfiguration.java:323)
                                                                                at android.view.View.<init>(View.java:3236)
                                                                                at android.view.ViewGroup.<init>(ViewGroup.java:420)
                                                                                at android.widget.FrameLayout.<init>(FrameLayout.java:93)

Consider adding means to add global State, not bound to history keys

The Activity should also be able to store state inside Flow, define its key but not be part of history (because it always exists).

How should this be done?

Should this be done?

Otherwise it should be "ROOT" key, but that doesn't really simplify things for the dispatcher at all.

Possible randomly occurring freeze where `pendingTraversal != null, state == DISPATCHED`

There can be a traversal in which nextHistory == null and state == DISPATCHED.

Image

Flowless1.0-alpha1.1 does not handle this special case

    void setDispatcher(@NonNull Dispatcher dispatcher, boolean created) {
        this.dispatcher = checkNotNull(dispatcher, "dispatcher");

        if(pendingTraversal == null && created) {
            // initialization should occur for current state if views are created or re-created
            move(new PendingTraversal() {
                @Override
                void doExecute() {
                    bootstrap(history);
                }
            });
        } else if(pendingTraversal == null) { // no-op if the view still exists and nothing to be done
            return;
        } else if(pendingTraversal.state == TraversalState.DISPATCHED) { // a traversal is in progress
            // do nothing, pending traversal will finish

created == true, but pendingTraversal != null, but the pending traversal will never finish.

The newly added fix for the midflight reentrance test might help

  if((pendingTraversal == null && created) || (pendingTraversal != null && pendingTraversal.state == TraversalState.DISPATCHED && pendingTraversal.next == null)) {

However, the traversal would only call traversal.next if onTraversalCompleted() is called, which in this case might be never.

This special case must be analyzed further and fixed.

  • Must find the root cause of why this traversal can occur
  • Kill root cause

Re-think `ForceBundler.saveToBundle()` and `onSaveInstanceState()`

https://github.com/Zhuinden/flowless/blob/master/flowless-library/src/main/java/flowless/ForceBundler.java#L12-L26

It is currently called from preSaveViewState() in SingleRootDispatcher (before super.onSaveInstanceState()), but this callback can be missed by users of SingleRootDispatcher. Also, currently toBundle() is used to create the bundle, and is mapped to the state directly.

But what if we want nested dispatchers (multiple nested container dispatchers, for example) and we don't want to write event delegation logic (and just use an encapsulated single root dispatcher instead)?

Question is:

  • Composite state management
  • calling preSaveViewState() manually
  • ForceBundler API (how to directly access keyManager without access to Activity, should it need the activity?)

Use Activity Context for inflating

Hey,

in my App i am working with the Vector Drawable support library. Internally it replaces normal ImageViews with it's AppCompat variant (android.support.v7.widget.AppCompatImageView).

The hooks which do this replacement are installed in the activity context. Yet (in the examples) the Dispatcher are given the Base context of the activity, which doesn't have these hooks on it.

override fun attachBaseContext(newBase: Context) {
    val baseContext = Flow.configure(newBase, this)
            .defaultKey(InsertDocumentKey())
            .dispatcher(flowDispatcher)
            .install()

    flowDispatcher.setBaseContext(baseContext)

    super.attachBaseContext(baseContext)
}

Using the Context in the Dispatcher looks like this:

val internalContext = traversal.createContext(newKey, baseContext)

val layoutInflater = LayoutInflater.from(internalContext)
val newView = layoutInflater.inflate(newKeyLayout, root, false)

Since we are using the base context, the replacement doesn't happen (which is the intended behaviour of the support library).

My proposal is, that the dispatcher should use the activity context instead of the base context of the activity.

I'm currently doing this to solve my problems:

flowDispatcher.setBaseContext(this) // this is an Activity

Are there any unintended behaviours i'm looking at with this approach?

Thank you very much for helping out :)

Re-add tests for library (and add new ones)

While converting the project to be a library project, I removed everything that was in the way of doing that.

The tests should be set up again, and aligned for some of the new code (for example the goBack() fix).

Make an MVP sample

The current samples are simply showing off how to use the presets, but a proper app example with MVP should be provided.

Make resource sharing examples with ServiceProvider

Currently there is an example in the README for when the resources of the context chain are retained.

in case of traversal A-B-C => A-B-D :

the matched states A and B have their resources retained

the missing state C has its resources destroyed

the new state D has its resource created

However, you might want to only retain the new state's resources, and you might want to share resources between states.

Such examples would be great to have. Also, think about key nesting.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.