1616
1717package com .example .android .architecture .blueprints .todoapp .taskdetail ;
1818
19- import static com .google .common .base .Preconditions .checkNotNull ;
20-
2119import android .app .Activity ;
2220import android .content .Intent ;
2321import android .os .Bundle ;
2422import android .support .annotation .NonNull ;
25- import android .support .annotation .Nullable ;
2623import android .support .design .widget .FloatingActionButton ;
2724import android .support .design .widget .Snackbar ;
28- import android .support .v4 .app .Fragment ;
25+ import android .support .v7 .app .ActionBar ;
2926import android .view .LayoutInflater ;
3027import android .view .Menu ;
3128import android .view .MenuInflater ;
3633import android .widget .CompoundButton ;
3734import android .widget .TextView ;
3835
36+ import com .example .android .architecture .blueprints .todoapp .BaseController ;
37+ import com .example .android .architecture .blueprints .todoapp .Injection ;
3938import com .example .android .architecture .blueprints .todoapp .R ;
39+ import com .example .android .architecture .blueprints .todoapp .util .BundleBuilder ;
4040import com .google .common .base .Preconditions ;
4141
4242/**
4343 * Main UI for the task detail screen.
4444 */
45- public class TaskDetailFragment extends Fragment implements TaskDetailContract .View {
45+ public class TaskDetailFragment extends BaseController implements TaskDetailContract .View {
4646
4747 @ NonNull
4848 private static final String ARGUMENT_TASK_ID = "TASK_ID" ;
@@ -52,30 +52,28 @@ public class TaskDetailFragment extends Fragment implements TaskDetailContract.V
5252
5353 private TaskDetailContract .Presenter mPresenter ;
5454
55+ private final String mTaskId ;
56+
5557 private TextView mDetailTitle ;
5658
5759 private TextView mDetailDescription ;
5860
5961 private CheckBox mDetailCompleteStatus ;
6062
61- public static TaskDetailFragment newInstance (@ Nullable String taskId ) {
62- Bundle arguments = new Bundle ();
63- arguments .putString (ARGUMENT_TASK_ID , taskId );
64- TaskDetailFragment fragment = new TaskDetailFragment ();
65- fragment .setArguments (arguments );
66- return fragment ;
63+ public TaskDetailFragment (String taskId ) {
64+ this (new BundleBuilder (new Bundle ())
65+ .putString (ARGUMENT_TASK_ID , taskId )
66+ .build ());
6767 }
6868
69- @ Override
70- public void onResume () {
71- super .onResume ();
72- mPresenter .start ();
69+ public TaskDetailFragment (Bundle args ) {
70+ super (args );
71+ mTaskId = args .getString (ARGUMENT_TASK_ID );
7372 }
7473
75- @ Nullable
74+ @ NonNull
7675 @ Override
77- public View onCreateView (LayoutInflater inflater , ViewGroup container ,
78- Bundle savedInstanceState ) {
76+ protected View onCreateView (@ NonNull LayoutInflater inflater , @ NonNull ViewGroup container ) {
7977 View root = inflater .inflate (R .layout .taskdetail_frag , container , false );
8078 setHasOptionsMenu (true );
8179 mDetailTitle = (TextView ) root .findViewById (R .id .task_detail_title );
@@ -84,7 +82,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
8482
8583 // Set up floating action button
8684 FloatingActionButton fab =
87- (FloatingActionButton ) getActivity () .findViewById (R .id .fab_edit_task );
85+ (FloatingActionButton ) root .findViewById (R .id .fab_edit_task );
8886
8987 fab .setOnClickListener (new View .OnClickListener () {
9088 @ Override
@@ -93,12 +91,33 @@ public void onClick(View v) {
9391 }
9492 });
9593
94+ setActive (true );
95+
96+ mPresenter = new TaskDetailPresenter (
97+ mTaskId ,
98+ Injection .provideTasksRepository (getApplicationContext ()),
99+ this );
100+
96101 return root ;
97102 }
98103
104+ @ Override
105+ protected void onAttach (@ NonNull View view ) {
106+ super .onAttach (view );
107+
108+ // Configure Activity level UI.
109+ ActionBar actionBar = getActionBar ();
110+ actionBar .setTitle (R .string .app_name );
111+ actionBar .setDisplayHomeAsUpEnabled (true );
112+ actionBar .setDisplayShowHomeEnabled (true );
113+
114+ // Start Presenter
115+ mPresenter .start ();
116+ }
117+
99118 @ Override
100119 public void setPresenter (@ NonNull TaskDetailContract .Presenter presenter ) {
101- mPresenter = checkNotNull ( presenter );
120+ // todo: remove method
102121 }
103122
104123 @ Override
@@ -120,7 +139,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
120139 public void setLoadingIndicator (boolean active ) {
121140 if (active ) {
122141 mDetailTitle .setText ("" );
123- mDetailDescription .setText (getString (R .string .loading ));
142+ mDetailDescription .setText (getResources (). getString (R .string .loading ));
124143 }
125144 }
126145
@@ -165,17 +184,17 @@ public void showEditTask(@NonNull String taskId) {
165184
166185 @ Override
167186 public void showTaskDeleted () {
168- getActivity ().finish ();
187+ getRouter ().popCurrentController ();
169188 }
170189
171190 public void showTaskMarkedComplete () {
172- Snackbar .make (getView (), getString (R .string .task_marked_complete ), Snackbar .LENGTH_LONG )
191+ Snackbar .make (getView (), getResources (). getString (R .string .task_marked_complete ), Snackbar .LENGTH_LONG )
173192 .show ();
174193 }
175194
176195 @ Override
177196 public void showTaskMarkedActive () {
178- Snackbar .make (getView (), getString (R .string .task_marked_active ), Snackbar .LENGTH_LONG )
197+ Snackbar .make (getView (), getResources (). getString (R .string .task_marked_active ), Snackbar .LENGTH_LONG )
179198 .show ();
180199 }
181200
@@ -198,12 +217,6 @@ public void showTitle(@NonNull String title) {
198217 @ Override
199218 public void showMissingTask () {
200219 mDetailTitle .setText ("" );
201- mDetailDescription .setText (getString (R .string .no_data ));
220+ mDetailDescription .setText (getResources (). getString (R .string .no_data ));
202221 }
203-
204- @ Override
205- public boolean isActive () {
206- return isAdded ();
207- }
208-
209- }
222+ }
0 commit comments