|
16 | 16 | package com.github.pedrovgs.effectiveandroidui.ui.activity;
|
17 | 17 |
|
18 | 18 | import android.os.Bundle;
|
19 |
| -import android.support.v4.app.Fragment; |
| 19 | + |
20 | 20 | import com.github.pedrovgs.effectiveandroidui.R;
|
21 |
| -import com.github.pedrovgs.effectiveandroidui.domain.tvshow.TvShow; |
22 |
| -import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowCatalogFragment; |
23 | 21 | import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowDraggableFragment;
|
24 | 22 | import com.github.pedrovgs.effectiveandroidui.ui.fragment.TvShowFragment;
|
25 | 23 | import com.github.pedrovgs.effectiveandroidui.ui.presenter.TvShowUIModule;
|
| 24 | + |
26 | 25 | import java.util.LinkedList;
|
27 | 26 | import java.util.List;
|
28 |
| -import javax.inject.Inject; |
29 | 27 |
|
30 | 28 | /**
|
31 | 29 | * Core activity of this application. This activity receives the launch intent and works as core of
|
32 | 30 | * the sample application.
|
33 |
| - * |
34 |
| - * Review how this activity uses fragments to decide how to implement navigation using the |
35 |
| - * Navigator |
36 |
| - * entity. |
37 | 31 | */
|
38 |
| -public class MainActivity extends BaseActivity implements TvShowCatalogFragment.Listener { |
39 |
| - |
40 |
| - @Inject Navigator navigator; |
| 32 | +public class MainActivity extends BaseActivity { |
41 | 33 |
|
42 |
| - private TvShowDraggableFragment tvShowDraggableFragment; |
43 |
| - private TvShowFragment tvShowFragment; |
| 34 | + private TvShowDraggableFragment tvShowDraggableFragment; |
| 35 | + private TvShowFragment tvShowFragment; |
44 | 36 |
|
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 | + } |
52 | 44 |
|
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 | + } |
59 | 51 |
|
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); |
76 | 54 | }
|
77 |
| - } |
78 | 55 |
|
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); |
83 | 60 | /*
|
84 | 61 | * If both fragments are visible we have to disable saved instance state in draggable
|
85 | 62 | * fragment because there are different fragment configurations in activity_main.xml
|
86 | 63 | * when the device is in portrait or landscape. Review layout- directory to get more
|
87 | 64 | * information.
|
88 | 65 | */
|
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 | + } |
119 | 69 | }
|
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 |
| - } |
136 | 70 | }
|
0 commit comments