Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrjr committed Jan 28, 2014
1 parent 74cefdc commit 348db4d
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 53 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

Version 0.4 *(2014-??-??)*
----------------------------
* API break: @Screen(layout=R.layout.foo> > @Layout(R.layout.foo), and Screens > Layouts.
Support for view class literals is gone. They break theming and the fix isn't worth the bother.

Version 0.3 *(2014-01-21)*
----------------------------
* New: Backstack#fromUpChain(Object), allows backstack to be created from
Expand Down
23 changes: 23 additions & 0 deletions flow-sample/res/layout/conversation_list_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright 2014 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.
-->

<com.example.flow.view.ConversationListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
23 changes: 23 additions & 0 deletions flow-sample/res/layout/conversation_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright 2014 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.
-->

<com.example.flow.view.ConversationView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
23 changes: 23 additions & 0 deletions flow-sample/res/layout/friend_list_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright 2014 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.
-->

<com.example.flow.view.FriendListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
23 changes: 23 additions & 0 deletions flow-sample/res/layout/friend_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright 2014 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.
-->

<com.example.flow.view.FriendView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
12 changes: 6 additions & 6 deletions flow-sample/src/main/java/com/example/flow/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
import com.example.flow.view.FriendView;
import com.example.flow.view.MessageView;
import flow.HasParent;
import flow.Screen;
import flow.Layout;
import dagger.Module;
import dagger.Provides;

public @interface App {
@Screen(ConversationListView.class) //
@Layout(R.layout.conversation_list_view) //
@Module(injects = ConversationListView.class, addsTo = MainActivity.ActivityModule.class)
public static class ConversationList {
}

@Screen(ConversationView.class) //
@Layout(R.layout.conversation_view) //
@Module(injects = ConversationView.class, addsTo = MainActivity.ActivityModule.class)
public static class Conversation implements HasParent<ConversationList> {
public final int conversationIndex;
Expand All @@ -51,7 +51,7 @@ public Conversation(int conversationIndex) {
}
}

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

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

@Screen(FriendView.class) //
@Layout(R.layout.friend_view) //
@Module(injects = FriendView.class)
public static class Friend implements HasParent<FriendList> {
public final int index;
Expand Down
18 changes: 7 additions & 11 deletions flow-sample/src/main/java/com/example/flow/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,24 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import butterknife.ButterKnife;
import butterknife.InjectView;
import com.example.flow.model.Conversation;
import com.example.flow.model.User;
import com.example.flow.view.ContainerView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import butterknife.ButterKnife;
import butterknife.InjectView;
import dagger.Module;
import dagger.ObjectGraph;
import dagger.Provides;
import flow.Backstack;
import flow.Flow;
import flow.HasParent;
import flow.Layouts;
import flow.Parcer;
import flow.Screens;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;

import static android.view.MenuItem.SHOW_AS_ACTION_ALWAYS;

Expand Down Expand Up @@ -137,7 +133,7 @@ private Backstack getInitialBackstack(Bundle savedInstanceState) {
private View getView(Object screen) {
ObjectGraph graph = activityGraph.plus(screen);
Context scopedContext = new ScopedContext(this, graph);
return Screens.createView(scopedContext, screen);
return Layouts.createView(scopedContext, screen);
}

@Module(injects = MainActivity.class, library = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,22 @@

package 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;

/**
* 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.
* Marks a class that designates a screen and specifies its layout. A screen is a distinct part of
* an application containing all information that describes this state.
*
* <p>For example, <pre><code>
* {@literal@}Screen(WelcomeScreenView.class)
* public class WelcomeScreen { ... }
*
* {@literal@}Screen(layout=R.layout.conversation_screen_layout)
* {@literal@}Screen(R.layout.conversation_screen_layout)
* public class ConversationScreen { ... }
* </code></pre>
*/
@Retention(RUNTIME) @Target(TYPE)
public @interface Screen {
int layout() default View.NO_ID;
Class<? extends android.view.View> value() default View.class;
public @interface Layout {
int value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,32 @@
package flow;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
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
};
public final class Layouts {

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

int layout = screen.layout();
if (layout != View.NO_ID) return inflateLayout(context, layout);

return instantiateView(context, screen.value());
int layout = screen.value();
return inflateLayout(context, layout);
}

private static android.view.View inflateLayout(Context context, int layoutId) {
return LayoutInflater.from(context).inflate(layoutId, null);
}

private static android.view.View instantiateView(Context context,
Class<? extends android.view.View> type) {
try {
Constructor<? extends android.view.View> constructor = type.getConstructor(VIEW_CONSTRUCTOR);
return constructor.newInstance(context, null);
} catch (Exception e) {
throw new IllegalStateException("View could not be created", e);
}
}

private Screens() {
private Layouts() {
}
}

0 comments on commit 348db4d

Please sign in to comment.