Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #63 from nhaarman/release-0.14.0
Browse files Browse the repository at this point in the history
Release 0.14.0
  • Loading branch information
nhaarman committed Oct 29, 2015
2 parents 5949e65 + a61f580 commit 0fa0361
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 67 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

Version 0.14.0 *(2015-10-29)*
-----------------------------------

* Use view ids as a reference for creating presenters instead of a class.

Version 0.13.2 *(2015-10-27)*
-----------------------------------

Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ public class CounterScreen extends Screen<ApplicationComponent> {
}

@Override
public Presenter<?, ?> createPresenter(@NonNull final Class<? extends Presenter<?,?>> presenterClass) {
if (presenterClass.equals(CounterPresenter.class) {
return new CounterPresenter();
}

throw new AssertionError("Unknown Presenter class: " + presenterClass);
public Presenter<?, ?> createPresenter(final int viewId) {
return new CounterPresenter();
}
}
```
Expand All @@ -67,11 +63,11 @@ public class CounterView extends RelativeLayoutContainer<CounterPresenter, Activ
protected TextView mCounterTV;

public MyView(Context context, AttributeSet attrs) {
super(context, attrs, CounterPresenter.class);
super(context, attrs);
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr, CounterPresenter.class);
super(context, attrs, defStyleAttr);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,14 @@ protected int getLayoutResId() {

@NonNull
@Override
protected Presenter<?, ?> createPresenter(@NonNull final Class<? extends Presenter<?, ?>> presenterClass) {
if (presenterClass.equals(EditNotePresenter.class)) {
return new EditNotePresenter(
mNote,
applicationComponent().noteValidator(),
applicationComponent().noteCreator(),
applicationComponent().noteRepository(),
applicationComponent().triad()
);
}

throw new AssertionError("Unknown class: " + presenterClass);
protected Presenter<?, ?> createPresenter(final int viewId) {
return new EditNotePresenter(
mNote,
applicationComponent().noteValidator(),
applicationComponent().noteCreator(),
applicationComponent().noteRepository(),
applicationComponent().triad()
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public class EditNoteView extends RelativeLayoutContainer<EditNotePresenter, Act
protected EditText mContentsET;

public EditNoteView(final Context context, final AttributeSet attrs) {
super(context, attrs, EditNotePresenter.class);
super(context, attrs);
}

public EditNoteView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle, EditNotePresenter.class);
super(context, attrs, defStyle);
}

@OnClick(R.id.view_editnote_savebutton)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ protected int getLayoutResId() {

@NonNull
@Override
protected Presenter<?, ?> createPresenter(@NonNull final Class<? extends Presenter<?, ?>> presenterClass) {
if (presenterClass.equals(NotesPresenter.class)) {
protected Presenter<?, ?> createPresenter(final int viewId) {
if (viewId == R.id.view_notes) {
return new NotesPresenter(
applicationComponent().triad()
);
}

if (presenterClass.equals(NotesListPresenter.class)) {
if (viewId == R.id.view_notes_noteslistview) {
return new NotesListPresenter(
applicationComponent().noteRepository(),
(NotesListPresenter.OnNoteClickedListener) getPresenter(NotesPresenter.class)
(NotesListPresenter.OnNoteClickedListener) getPresenter(R.id.view_notes)
);
}

throw new AssertionError("Unknown presenter class: " + presenterClass);
throw new AssertionError("Unknown presenter class for view with id: " + viewId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public class NotesView extends RelativeLayoutContainer<NotesPresenter, ActivityC
protected NotesListView mNotesListView;

public NotesView(final Context context, final AttributeSet attrs) {
super(context, attrs, NotesPresenter.class);
super(context, attrs);
}

public NotesView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle, NotesPresenter.class);
super(context, attrs, defStyle);
}

@OnClick(R.id.view_notes_createnotebutton)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public NotesListView(final Context context, final AttributeSet attrs) {
}

public NotesListView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle, NotesListPresenter.class);
super(context, attrs, defStyle);
mAdapter = new MyAdapter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

@SuppressWarnings("rawtypes")
public class NotesListPresenterTest {

private NotesListPresenter mNotesListPresenter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected int getLayoutResId() {

@NonNull
@Override
protected Presenter<?, ?> createPresenter(@NonNull final Class<? extends Presenter<?, ?>> presenterClass) {
protected Presenter<?, ?> createPresenter(final int viewId) {
return new FirstScreenPresenter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public class FirstScreenView extends RelativeLayoutContainer<FirstScreenPresente
implements FirstScreenContainer {

public FirstScreenView(final Context context, final AttributeSet attrs) {
super(context, attrs, FirstScreenPresenter.class);
super(context, attrs);
}

public FirstScreenView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle, FirstScreenPresenter.class);
super(context, attrs, defStyle);
}

@OnClick(com.nhaarman.triad.tests.R.id.view_screen_first_button)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected int getLayoutResId() {

@NonNull
@Override
protected Presenter<?, ?> createPresenter(@NonNull final Class<? extends Presenter<?, ?>> presenterClass) {
protected Presenter<?, ?> createPresenter(final int viewId) {
return new SecondScreenPresenter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
public class SecondScreenView extends RelativeLayoutContainer<SecondScreenPresenter, ActivityComponent> implements SecondScreenContainer {

public SecondScreenView(final Context context, final AttributeSet attrs) {
super(context, attrs, SecondScreenPresenter.class);
super(context, attrs);
}

public SecondScreenView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle, SecondScreenPresenter.class);
super(context, attrs, defStyle);
}
}
8 changes: 4 additions & 4 deletions triad/src/main/java/com/nhaarman/triad/ListViewContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public abstract class ListViewContainer<
@NonNull
private final ActivityComponent mActivityComponent;

public ListViewContainer(final Context context, final AttributeSet attrs, final Class<P> presenterClass) {
this(context, attrs, 0, presenterClass);
public ListViewContainer(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}

public ListViewContainer(final Context context, final AttributeSet attrs, final int defStyle, final Class<P> presenterClass) {
public ListViewContainer(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);

mActivityComponent = findActivityComponent(context);
mPresenter = findPresenter(context, presenterClass);
mPresenter = findPresenter(context, getId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public abstract class RelativeLayoutContainer<
@NonNull
private final ActivityComponent mActivityComponent;

public RelativeLayoutContainer(final Context context, final AttributeSet attrs, final Class<P> presenterClass) {
this(context, attrs, 0, presenterClass);
public RelativeLayoutContainer(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}

public RelativeLayoutContainer(final Context context, final AttributeSet attrs, final int defStyle, final Class<P> presenterClass) {
public RelativeLayoutContainer(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);

mActivityComponent = findActivityComponent(context);
mPresenter = findPresenter(context, presenterClass);
mPresenter = findPresenter(context, getId());
}

/**
Expand Down
15 changes: 7 additions & 8 deletions triad/src/main/java/com/nhaarman/triad/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.HashMap;
import java.util.Map;

import static com.nhaarman.triad.Preconditions.checkNotNull;

public abstract class Screen<ApplicationComponent> implements TransitionAnimator {

@NonNull
private final Map<Class<?>, Presenter<?, ?>> mPresenters = new HashMap<>();
private final SparseArray<Presenter<?, ?>> mPresenters = new SparseArray<>();

@Nullable
private ApplicationComponent mApplicationComponent;
Expand All @@ -44,16 +43,16 @@ protected ViewGroup createView(@NonNull final ViewGroup parent) {
}

@NonNull
public Presenter<?, ?> getPresenter(@NonNull final Class<? extends Presenter<?, ?>> presenterClass) {
if (!mPresenters.containsKey(presenterClass)) {
mPresenters.put(presenterClass, createPresenter(presenterClass));
public Presenter<?, ?> getPresenter(final int viewId) {
if (mPresenters.valueAt(viewId) == null) {
mPresenters.put(viewId, createPresenter(viewId));
}

return mPresenters.get(presenterClass);
return mPresenters.get(viewId);
}

@NonNull
protected abstract Presenter<?, ?> createPresenter(@NonNull Class<? extends Presenter<?, ?>> presenterClass);
protected abstract Presenter<?, ?> createPresenter(int viewId);

@Override
public boolean animateTransition(@Nullable final View oldView,
Expand Down
4 changes: 2 additions & 2 deletions triad/src/main/java/com/nhaarman/triad/TriadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public static <ActivityComponent> ActivityComponent findActivityComponent(@NonNu
}

@NonNull
public static <P extends Presenter<?, ?>> P findPresenter(@NonNull final Context context, @NonNull final Class<P> presenterClass) {
public static <P extends Presenter<?, ?>> P findPresenter(@NonNull final Context context, final int viewId) {
Context baseContext = context;
while (!(baseContext instanceof Activity) && baseContext instanceof ContextWrapper) {
baseContext = ((ContextWrapper) baseContext).getBaseContext();
}

if (baseContext instanceof ScreenProvider) {
//noinspection unchecked
return (P) ((ScreenProvider) baseContext).getCurrentScreen().getPresenter(presenterClass);
return (P) ((ScreenProvider) baseContext).getCurrentScreen().getPresenter(viewId);
} else {
/* We return null, since the layout editor can not return the ScreenProvider. */
//noinspection ConstantConditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;

public class RelativeLayoutContainerTest {

Expand All @@ -30,7 +27,7 @@ public void setUp() {

Screen<ApplicationComponent> screen = mock(Screen.class);
mPresenterMock = mock(Presenter.class);
when(screen.getPresenter(any(Class.class))).thenReturn(mPresenterMock);
when(screen.getPresenter(anyInt())).thenReturn(mPresenterMock);

when(((ScreenProvider<ApplicationComponent>) activity).getCurrentScreen()).thenReturn(screen);
mActivityComponentMock = mock(ActivityComponent.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class TestRelativeLayoutContainer extends RelativeLayoutContainer<TestPresenter, ActivityComponent> {

public TestRelativeLayoutContainer(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle, TestPresenter.class);
super(context, attrs, defStyle);
}

@Override
Expand Down

0 comments on commit 0fa0361

Please sign in to comment.