Skip to content

Commit

Permalink
Consolidates Screen, @layout, @view into @screen.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrjr committed Nov 1, 2013
1 parent 6ac8eb7 commit 7628649
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 128 deletions.
37 changes: 20 additions & 17 deletions flow/src/main/java/com/squareup/flow/Backstack.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
import java.util.Iterator;
import java.util.List;

/** Describes the history of a {@link Flow} at a specific point in time. For persisting the
* supplied {@link Parcer} needs to be able to handle all screen types. */
/**
* Describes the history of a {@link Flow} at a specific point in time. For persisting the supplied
* {@link Parcer} needs to be able to handle all screen types.
*/
public final class Backstack implements Iterable<Backstack.Entry> {
private final long highestId;
private final Deque<Entry> backstack;

/** Restore a saved backstack from a {@link Parcelable} using the supplied {@link Parcer}. */
public static Backstack from(Parcelable parcelable, Parcer<Screen> parcer) {
public static Backstack from(Parcelable parcelable, Parcer<Object> parcer) {
ParcelableBackstack backstack = (ParcelableBackstack) parcelable;
return backstack.getBackstack(parcer);
}
Expand All @@ -43,7 +45,7 @@ public static Builder emptyBuilder() {
}

/** Create a backstack that contains a single screen. */
public static Backstack single(Screen screen) {
public static Backstack single(Object screen) {
return emptyBuilder().push(screen).build();
}

Expand All @@ -61,7 +63,7 @@ public Iterator<Entry> reverseIterator() {
}

/** Get a {@link Parcelable} of this backstack using the supplied {@link Parcer}. */
public Parcelable getParcelable(Parcer<Screen> parcer) {
public Parcelable getParcelable(Parcer<Object> parcer) {
return new ParcelableBackstack.Memory(this, parcer);
}

Expand All @@ -84,9 +86,9 @@ public Builder buildUpon() {

public static final class Entry {
private final long id;
private final Screen screen;
private final Object screen;

private Entry(long id, Screen screen) {
private Entry(long id, Object screen) {
this.id = id;
this.screen = screen;
}
Expand All @@ -95,7 +97,7 @@ public long getId() {
return id;
}

public Screen getScreen() {
public Object getScreen() {
return screen;
}

Expand All @@ -113,14 +115,14 @@ private Builder(long highestId, Collection<Entry> backstack) {
this.backstack = new ArrayDeque<Entry>(backstack);
}

public Builder push(Screen screen) {
public Builder push(Object screen) {
backstack.push(new Entry(++highestId, screen));

return this;
}

public Builder addAll(Collection<Screen> c) {
for (Screen screen : c) {
public Builder addAll(Collection<Object> c) {
for (Object screen : c) {
backstack.push(new Entry(++highestId, screen));
}

Expand Down Expand Up @@ -167,8 +169,9 @@ public ReadIterator(Iterator<T> iterator) {
}

private interface ParcelableBackstack extends Parcelable {
Backstack getBackstack(Parcer<Screen> parcer);
Backstack getBackstack(Parcer<Object> parcer);

@SuppressWarnings("UnusedDeclaration")
Parcelable.Creator<ParcelableBackstack> CREATOR =
new Parcelable.Creator<ParcelableBackstack>() {
@Override public ParcelableBackstack createFromParcel(Parcel in) {
Expand All @@ -185,14 +188,14 @@ private interface ParcelableBackstack extends Parcelable {

static class Memory implements ParcelableBackstack {
private final Backstack backstack;
private final Parcer<Screen> parcer;
private final Parcer<Object> parcer;

Memory(Backstack backstack, Parcer<Screen> parcer) {
Memory(Backstack backstack, Parcer<Object> parcer) {
this.backstack = backstack;
this.parcer = parcer;
}

@Override public Backstack getBackstack(Parcer<Screen> parcer) {
@Override public Backstack getBackstack(Parcer<Object> parcer) {
return backstack;
}

Expand Down Expand Up @@ -220,7 +223,7 @@ static class Parcelled implements ParcelableBackstack {
this.entries = entries;
}

@Override public Backstack getBackstack(Parcer<Screen> parcer) {
@Override public Backstack getBackstack(Parcer<Object> parcer) {
List<Entry> backstack = new ArrayList<Entry>();
for (ParcelableEntry entry : entries) {
backstack.add(entry.toRealEntry(parcer));
Expand Down Expand Up @@ -249,7 +252,7 @@ private static class ParcelableEntry implements Parcelable {
this.parcelable = parcelable;
}

public Entry toRealEntry(Parcer<Screen> parcer) {
public Entry toRealEntry(Parcer<Object> parcer) {
return new Entry(id, parcer.unwrap(parcelable));
}

Expand Down
12 changes: 6 additions & 6 deletions flow/src/main/java/com/squareup/flow/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public Backstack getBackstack() {
}

/** Push the screen onto the backstack. */
public void goTo(Screen screen) {
public void goTo(Object screen) {
Backstack newBackstack = backstack.buildUpon().push(screen).build();
forward(newBackstack);
}

/** Reset to the specified screen. Pops until the screen is found (inclusive) and appends. */
public void resetTo(Screen screen) {
public void resetTo(Object screen) {
Backstack.Builder builder = backstack.buildUpon();
int count = 0;
for (Iterator<Backstack.Entry> it = backstack.reverseIterator(); it.hasNext();) {
Expand All @@ -70,10 +70,10 @@ public void resetTo(Screen screen) {
}

/** Replaces the current backstack with the up stack of the screen. */
public void replaceTo(Screen screen) {
LinkedList<Screen> newBackstack = new LinkedList<Screen>();
public void replaceTo(Object screen) {
LinkedList<Object> newBackstack = new LinkedList<Object>();

Screen current = screen;
Object current = screen;
while (current instanceof HasParent<?>) {
newBackstack.addFirst(current);
current = ((HasParent) current).getParent();
Expand All @@ -91,7 +91,7 @@ public void replaceTo(Screen screen) {
* @return false if going up is not possible.
*/
public boolean goUp() {
Screen current = backstack.current().getScreen();
Object current = backstack.current().getScreen();
if (current instanceof HasParent<?>) {
replaceTo(((HasParent) current).getParent());
return true;
Expand Down
2 changes: 1 addition & 1 deletion flow/src/main/java/com/squareup/flow/HasParent.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
* Describes the parent of a specific screen which is used to support the up affordance.
* Implementing screens are required to be able to return an instance of their parent.
*/
public interface HasParent<T extends Screen> {
public interface HasParent<T> {
T getParent();
}
24 changes: 21 additions & 3 deletions flow/src/main/java/com/squareup/flow/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,27 @@

package com.squareup.flow;

import android.view.View;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Marker interface that designates a screen. A screen is a distinct part of an application
* containing all information that describes this state.
* Marks a class that designates a screen. A screen is a distinct part of an application
* containing all information that describes this state. Optionally specifies its view or layout.
*
* <p>For example, <pre><code>
* {@literal@}Screen(R.layout.welcome_layout)
* public class WelcomeScreen { ... }
*
* {@literal@}Screen(view=Conversation.class)
* public class ConversationScreen { ... }
* </code></pre>
*/
public interface Screen {
@Retention(RUNTIME) @Target(TYPE)
public @interface Screen {
int layout() default View.NO_ID;
Class<? extends android.view.View> value() default View.class;
}
28 changes: 13 additions & 15 deletions flow/src/main/java/com/squareup/flow/Screens.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,32 @@
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import com.squareup.flow.annotation.Layout;
import com.squareup.flow.annotation.View;
import android.view.View;
import java.lang.reflect.Constructor;

public final class Screens {
private static final Class<?>[] VIEW_CONSTRUCTOR = new Class[] {
Context.class, AttributeSet.class
};

/** Create an instance of the view specified in a {@link View} or {@link Layout} annotation. */
public static android.view.View createView(Context context, Screen screen) {
/** Create an instance of the view specified in a {@link Screen} annotation. */
public static android.view.View createView(Context context, Object screen) {
return createView(context, screen.getClass());
}

/** Create an instance of the view specified in a {@link View} or {@link Layout} annotation. */
public static android.view.View createView(Context context, Class<? extends Screen> screenType) {
View view = screenType.getAnnotation(View.class);
if (view != null) {
return instantiateView(context, view.value());
/** Create an instance of the view specified in a {@link Screen} annotation. */
public static android.view.View createView(Context context, Class<?> screenType) {
Screen screen = screenType.getAnnotation(Screen.class);
if (screen == null) {
throw new IllegalArgumentException(
String.format("@%s annotation not found on class %s", Screen.class.getSimpleName(),
screenType.getName()));
}

Layout layout = screenType.getAnnotation(Layout.class);
if (layout != null) {
return inflateLayout(context, layout.value());
}
int layout = screen.layout();
if (layout != View.NO_ID) return inflateLayout(context, layout);

throw new IllegalArgumentException(
screenType + " does not have either a @View or @Layout annotation");
return instantiateView(context, screen.value());
}

private static android.view.View inflateLayout(Context context, int layoutId) {
Expand Down
33 changes: 0 additions & 33 deletions flow/src/main/java/com/squareup/flow/annotation/Layout.java

This file was deleted.

33 changes: 0 additions & 33 deletions flow/src/main/java/com/squareup/flow/annotation/View.java

This file was deleted.

24 changes: 11 additions & 13 deletions sample/src/main/java/com/example/flow/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@
import com.example.flow.view.MessageView;
import com.squareup.flow.HasParent;
import com.squareup.flow.Screen;
import com.squareup.flow.annotation.Layout;
import com.squareup.flow.annotation.View;
import dagger.Module;
import dagger.Provides;

public @interface App {
@View(ConversationListView.class) //
@Module(injects = ConversationListView.class, complete = false)
public static class ConversationList implements Screen {
@Screen(ConversationListView.class) //
@Module(injects = ConversationListView.class, complete = false)
public static class ConversationList {
}

@View(ConversationView.class) //
@Screen(ConversationView.class) //
@Module(injects = ConversationView.class, addsTo = MainActivity.ActivityModule.class)
public static class Conversation implements Screen, HasParent<ConversationList> {
public static class Conversation implements HasParent<ConversationList> {
public final int conversationIndex;

public Conversation(int conversationIndex) {
Expand All @@ -53,9 +51,9 @@ public Conversation(int conversationIndex) {
}
}

@Layout(R.layout.message_view) //
@Screen(layout = R.layout.message_view) //
@Module(injects = MessageView.class, addsTo = MainActivity.ActivityModule.class)
public static class Message implements Screen, HasParent<Conversation> {
public static class Message implements HasParent<Conversation> {
public final int conversationIndex;
public final int messageId;

Expand All @@ -73,17 +71,17 @@ public Message(int conversationIndex, int messageId) {
}
}

@View(FriendListView.class) //
@Screen(FriendListView.class) //
@Module(injects = FriendListView.class, addsTo = MainActivity.ActivityModule.class)
public static class FriendList implements Screen, HasParent<ConversationList> {
public static class FriendList implements HasParent<ConversationList> {
@Override public ConversationList getParent() {
return new ConversationList();
}
}

@View(FriendView.class) //
@Screen(FriendView.class) //
@Module(injects = FriendView.class)
public static class Friend implements Screen, HasParent<FriendList> {
public static class Friend implements HasParent<FriendList> {
public final int index;

public Friend(int index) {
Expand Down
Loading

0 comments on commit 7628649

Please sign in to comment.