Skip to content

Commit 25ca077

Browse files
Update: Navigator injected on TvShowCatalogPresenter
* All the navigation logic has been moved into the navigator, even the fragment related one.
1 parent a197f57 commit 25ca077

File tree

4 files changed

+112
-129
lines changed

4 files changed

+112
-129
lines changed

app/src/main/java/com/github/pedrovgs/effectiveandroidui/ui/activity/MainActivity.java

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -16,121 +16,55 @@
1616
package com.github.pedrovgs.effectiveandroidui.ui.activity;
1717

1818
import android.os.Bundle;
19-
import android.support.v4.app.Fragment;
19+
2020
import com.github.pedrovgs.effectiveandroidui.R;
21-
import com.github.pedrovgs.effectiveandroidui.domain.tvshow.TvShow;
22-
import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowCatalogFragment;
2321
import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowDraggableFragment;
2422
import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowFragment;
2523
import com.github.pedrovgs.effectiveandroidui.ui.presenter.TvShowUIModule;
24+
2625
import java.util.LinkedList;
2726
import java.util.List;
28-
import javax.inject.Inject;
2927

3028
/**
3129
* Core activity of this application. This activity receives the launch intent and works as core of
3230
* the sample application.
33-
*
34-
* Review how this activity uses fragments to decide how to implement navigation using the
35-
* Navigator
36-
* entity.
3731
*/
38-
public class MainActivity extends BaseActivity implements TvShowCatalogFragment.Listener {
39-
40-
@Inject Navigator navigator;
32+
public class MainActivity extends BaseActivity {
4133

42-
private TvShowDraggableFragment tvShowDraggableFragment;
43-
private TvShowFragment tvShowFragment;
34+
private TvShowDraggableFragment tvShowDraggableFragment;
35+
private TvShowFragment tvShowFragment;
4436

45-
@Override
46-
protected void onCreate(Bundle savedInstanceState) {
47-
super.onCreate(savedInstanceState);
48-
setContentView(R.layout.activity_main);
49-
initializeTvShowFragment();
50-
initializeTvShowDraggableFragment();
51-
}
37+
@Override
38+
protected void onCreate(Bundle savedInstanceState) {
39+
super.onCreate(savedInstanceState);
40+
setContentView(R.layout.activity_main);
41+
initializeTvShowFragment();
42+
initializeTvShowDraggableFragment();
43+
}
5244

53-
@Override
54-
protected List<Object> getModules() {
55-
LinkedList<Object> modules = new LinkedList<Object>();
56-
modules.add(new TvShowUIModule());
57-
return modules;
58-
}
45+
@Override
46+
protected List<Object> getModules() {
47+
LinkedList<Object> modules = new LinkedList<>();
48+
modules.add(new TvShowUIModule());
49+
return modules;
50+
}
5951

60-
/*
61-
* This method contains the key of the application navigation. If there are no fragments attached
62-
* we will launch TvShowActivity.
63-
*
64-
* If any fragment is visible we will load the TvShow.
65-
*
66-
* Other approach to connect fragments could be based on a Bus event implementation. But this is
67-
* only valid if you only have fragments in your activity.
68-
*
69-
*/
70-
@Override public void onTvShowClicked(final TvShow tvShow) {
71-
if (canInteractWithFragments()) {
72-
showTvShowOnTvShowDraggableFragment(tvShow);
73-
showTvShowOnTvShowFragment(tvShow);
74-
} else {
75-
openTvShowActivity(tvShow.getTitle());
52+
private void initializeTvShowFragment() {
53+
tvShowFragment = (TvShowFragment) getSupportFragmentManager().findFragmentById(R.id.f_tv_show);
7654
}
77-
}
7855

79-
private void initializeTvShowDraggableFragment() {
80-
tvShowDraggableFragment =
81-
(TvShowDraggableFragment) getSupportFragmentManager().findFragmentById(
82-
R.id.f_tv_show_draggable);
56+
private void initializeTvShowDraggableFragment() {
57+
tvShowDraggableFragment =
58+
(TvShowDraggableFragment) getSupportFragmentManager().findFragmentById(
59+
R.id.f_tv_show_draggable);
8360
/*
8461
* If both fragments are visible we have to disable saved instance state in draggable
8562
* fragment because there are different fragment configurations in activity_main.xml
8663
* when the device is in portrait or landscape. Review layout- directory to get more
8764
* information.
8865
*/
89-
if (tvShowFragment != null && tvShowDraggableFragment != null) {
90-
tvShowDraggableFragment.disableSaveInstanceState();
91-
}
92-
}
93-
94-
private void initializeTvShowFragment() {
95-
tvShowFragment = (TvShowFragment) getSupportFragmentManager().findFragmentById(R.id.f_tv_show);
96-
}
97-
98-
/**
99-
* Method created to open TvShowActivity for Android 2.X versions. This method is going to use a
100-
* Navigator object to open TvShowActivity. This method could be inside a presenter or view
101-
* model, but to the sample we are going to use the Navigator object from this activity.
102-
*
103-
* Is possible to start an activity from a presenter or view model because we have a activity
104-
* scope module to provide the current activity context.
105-
*
106-
* @param tvShowId used to open TvShowActivity.
107-
*/
108-
private void openTvShowActivity(final String tvShowId) {
109-
navigator.openTvShowActivity(tvShowId);
110-
}
111-
112-
private boolean canInteractWithFragments() {
113-
return tvShowDraggableFragment != null || tvShowFragment != null;
114-
}
115-
116-
private void showTvShowOnTvShowDraggableFragment(TvShow tvShow) {
117-
if (isFragmentAvailable(tvShowDraggableFragment)) {
118-
tvShowDraggableFragment.showTvShow(tvShow.getTitle());
66+
if (tvShowFragment != null && tvShowDraggableFragment != null) {
67+
tvShowDraggableFragment.disableSaveInstanceState();
68+
}
11969
}
120-
}
121-
122-
private void showTvShowOnTvShowFragment(TvShow tvShow) {
123-
if (isFragmentAvailable(tvShowFragment)) {
124-
tvShowFragment.showTvShow(tvShow.getTitle());
125-
}
126-
}
127-
128-
/**
129-
* Check if the fragment is ready to be notified of a new TvShow loaded.
130-
*
131-
* @return true if the Fragment instance is not null and is attached.
132-
*/
133-
private boolean isFragmentAvailable(Fragment fragment) {
134-
return fragment != null && fragment.isAdded();
135-
}
13670
}

app/src/main/java/com/github/pedrovgs/effectiveandroidui/ui/activity/Navigator.java

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,101 @@
1717

1818
import android.content.Context;
1919
import android.content.Intent;
20+
import android.support.v4.app.Fragment;
21+
import android.support.v4.app.FragmentActivity;
22+
import android.support.v4.app.FragmentManager;
23+
24+
import com.github.pedrovgs.effectiveandroidui.R;
2025
import com.github.pedrovgs.effectiveandroidui.di.ActivityContext;
26+
import com.github.pedrovgs.effectiveandroidui.domain.tvshow.TvShow;
27+
import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowDraggableFragment;
28+
import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowFragment;
29+
2130
import javax.inject.Inject;
2231

2332
/**
2433
* Class created to handle all the navigation between activities. This class knows how to open
2534
* every activity in the application and provides to the client code different methods to start
2635
* activities with the information needed.
27-
*
36+
* <p/>
2837
* You can use other approach based on ActionCommands if you wish. To do that review MVVM
2938
* pattern and ActionCommand interface.
3039
*
3140
* @author Pedro Vicente Gómez Sánchez
3241
*/
3342
public class Navigator {
3443

35-
private final Context context;
44+
private TvShowFragment tvShowFragment;
45+
private TvShowDraggableFragment tvShowDraggableFragment;
46+
47+
private final Context activityContext;
48+
49+
@Inject
50+
public Navigator(@ActivityContext Context activityContext) {
51+
this.activityContext = activityContext;
52+
}
53+
54+
/*
55+
* This method contains the key of the application navigation. If there are no fragments attached
56+
* we will launch TvShowActivity.
57+
*
58+
* If any fragment is visible on injected activity, we will load the TvShow.
59+
*
60+
* Other approach to connect fragments could be based on a Bus event implementation. But this is
61+
* only valid if you only have fragments in your activity.
62+
*
63+
*/
64+
public void openTvShowDetails(TvShow tvShow) {
65+
66+
if (canInteractWithFragments()) {
67+
showTvShowOnTvShowDraggableFragment(tvShow);
68+
showTvShowOnTvShowFragment(tvShow);
69+
} else {
70+
openTvShowActivity(tvShow.getTitle());
71+
}
72+
}
73+
74+
private FragmentManager getFragmentManager() {
75+
return ((FragmentActivity)activityContext).getSupportFragmentManager();
76+
}
77+
78+
private boolean canInteractWithFragments() {
79+
tvShowFragment = (TvShowFragment) getFragmentManager().findFragmentById(R.id.f_tv_show);
80+
tvShowDraggableFragment = (TvShowDraggableFragment) getFragmentManager().findFragmentById(R.id.f_tv_show_draggable);;
81+
82+
return tvShowDraggableFragment != null || tvShowFragment != null;
83+
}
84+
85+
private void showTvShowOnTvShowDraggableFragment(TvShow tvShow) {
86+
if (isFragmentAvailable(tvShowDraggableFragment)) {
87+
tvShowDraggableFragment.showTvShow(tvShow.getTitle());
88+
}
89+
}
3690

37-
@Inject
38-
public Navigator(@ActivityContext Context context) {
39-
this.context = context;
40-
}
91+
private void showTvShowOnTvShowFragment(TvShow tvShow) {
92+
if (isFragmentAvailable(tvShowFragment)) {
93+
tvShowFragment.showTvShow(tvShow.getTitle());
94+
}
95+
}
4196

42-
/**
43-
* Open TvShowActivity using a tvShowId.
44-
*/
45-
public void openTvShowActivity(final String tvShowId) {
46-
Intent intent = TvShowActivity.getLaunchIntent(context, tvShowId);
47-
startActivity(intent);
48-
}
97+
/**
98+
* Check if the fragment is ready to be notified of a new TvShow loaded.
99+
*
100+
* @return true if the Fragment instance is not null and is attached.
101+
*/
102+
private boolean isFragmentAvailable(Fragment fragment) {
103+
return fragment != null && fragment.isAdded();
104+
}
105+
106+
/**
107+
* Open TvShowActivity using a tvShowId.
108+
*/
109+
public void openTvShowActivity(final String tvShowId) {
110+
Intent intent = TvShowActivity.getLaunchIntent(activityContext, tvShowId);
111+
startActivity(intent);
112+
}
49113

50-
private void startActivity(Intent intent) {
51-
context.startActivity(intent);
52-
}
114+
private void startActivity(Intent intent) {
115+
activityContext.startActivity(intent);
116+
}
53117
}

app/src/main/java/com/github/pedrovgs/effectiveandroidui/ui/fragment/TvShowCatalogFragment.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public class TvShowCatalogFragment extends BaseFragment implements TvShowCatalog
5555
private RendererAdapter<TvShow> adapter;
5656
private TvShowCollection tvShows = new TvShowCollection();
5757

58-
private Listener listener;
59-
6058
@InjectView(R.id.pb_loading) ProgressBar pb_loading;
6159
@InjectView(R.id.gv_tv_shows) GridView gv_tv_shows;
6260
@InjectView(R.id.v_empty_case) View v_empty_case;
@@ -70,9 +68,6 @@ public class TvShowCatalogFragment extends BaseFragment implements TvShowCatalog
7068

7169
@Override public void onAttach(Activity activity) {
7270
super.onAttach(activity);
73-
if (activity instanceof Listener) {
74-
this.listener = (Listener) activity;
75-
}
7671
}
7772

7873
@Override public void onResume() {
@@ -141,12 +136,6 @@ public class TvShowCatalogFragment extends BaseFragment implements TvShowCatalog
141136
ToastUtils.showShortMessage(tvShow.getTitle(), getActivity());
142137
}
143138

144-
@Override public void openTvShow(final TvShow tvShow) {
145-
if (listener != null) {
146-
listener.onTvShowClicked(tvShow);
147-
}
148-
}
149-
150139
@Override public boolean isReady() {
151140
return isAdded();
152141
}
@@ -173,9 +162,4 @@ private void updatePresenterWithSavedTvShow(TvShowCollection tvShowCollection) {
173162
private void refreshAdapter() {
174163
adapter.notifyDataSetChanged();
175164
}
176-
177-
public interface Listener {
178-
179-
void onTvShowClicked(final TvShow tvShow);
180-
}
181165
}

app/src/main/java/com/github/pedrovgs/effectiveandroidui/ui/presenter/TvShowCatalogPresenter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.github.pedrovgs.effectiveandroidui.domain.GetTvShows;
1919
import com.github.pedrovgs.effectiveandroidui.domain.tvshow.TvShow;
20+
import com.github.pedrovgs.effectiveandroidui.ui.activity.Navigator;
2021
import com.github.pedrovgs.effectiveandroidui.ui.renderer.tvshow.TvShowCollection;
2122
import java.util.Collection;
2223
import javax.inject.Inject;
@@ -37,13 +38,15 @@
3738
public class TvShowCatalogPresenter extends Presenter {
3839

3940
private GetTvShows getTvShowsInteractor;
41+
private Navigator navigator;
4042

4143
private View view;
4244
private TvShowCollection currentTvShowCollection;
4345

4446
@Inject
45-
public TvShowCatalogPresenter(GetTvShows getTvShowsInteractor) {
47+
public TvShowCatalogPresenter(GetTvShows getTvShowsInteractor, Navigator navigator) {
4648
this.getTvShowsInteractor = getTvShowsInteractor;
49+
this.navigator = navigator;
4750
}
4851

4952
public void setView(View view) {
@@ -80,7 +83,7 @@ public void loadCatalog(final TvShowCollection tvShowCollection) {
8083
}
8184

8285
public void onTvShowThumbnailClicked(final TvShow tvShow) {
83-
view.openTvShow(tvShow);
86+
navigator.openTvShowDetails(tvShow);
8487
}
8588

8689
public void onTvShowClicked(final TvShow tvShow) {
@@ -157,8 +160,6 @@ public interface View {
157160

158161
void showTvShowTitleAsMessage(TvShow tvShow);
159162

160-
void openTvShow(TvShow tvShow);
161-
162163
boolean isReady();
163164

164165
boolean isAlreadyLoaded();

0 commit comments

Comments
 (0)