1616
1717package com .example .android .architecture .blueprints .todoapp .addedittask ;
1818
19- import android .app .Activity ;
2019import android .os .Bundle ;
2120import android .support .annotation .NonNull ;
22- import android .support .annotation .Nullable ;
2321import android .support .design .widget .FloatingActionButton ;
2422import android .support .design .widget .Snackbar ;
25- import android .support .v4 .app .Fragment ;
23+ import android .support .v7 .app .ActionBar ;
2624import android .view .LayoutInflater ;
2725import android .view .View ;
2826import android .view .ViewGroup ;
2927import android .widget .TextView ;
3028
29+ import com .example .android .architecture .blueprints .todoapp .BaseController ;
30+ import com .example .android .architecture .blueprints .todoapp .Injection ;
3131import com .example .android .architecture .blueprints .todoapp .R ;
32-
33- import static com .google .common .base .Preconditions .checkNotNull ;
32+ import com .example .android .architecture .blueprints .todoapp .util .BundleBuilder ;
3433
3534/**
3635 * Main UI for the add task screen. Users can enter a task title and description.
3736 */
38- public class AddEditTaskFragment extends Fragment implements AddEditTaskContract .View {
37+ public class AddEditTaskFragment extends BaseController implements AddEditTaskContract .View {
3938
40- public static final String ARGUMENT_EDIT_TASK_ID = "EDIT_TASK_ID" ;
39+ private static final String ARGUMENT_EDIT_TASK_ID = "EDIT_TASK_ID" ;
4140
4241 private AddEditTaskContract .Presenter mPresenter ;
4342
43+ private final String mTaskId ;
44+
4445 private TextView mTitle ;
4546
4647 private TextView mDescription ;
4748
48- public static AddEditTaskFragment newInstance () {
49- return new AddEditTaskFragment ( );
49+ public AddEditTaskFragment () {
50+ this ( new Bundle () );
5051 }
5152
52- public AddEditTaskFragment () {
53- // Required empty public constructor
53+ public AddEditTaskFragment (String taskId ) {
54+ this (new BundleBuilder (new Bundle ())
55+ .putString (ARGUMENT_EDIT_TASK_ID , taskId )
56+ .build ());
5457 }
5558
56- @ Override
57- public void onResume () {
58- super .onResume ();
59- mPresenter .start ();
59+ public AddEditTaskFragment (Bundle args ) {
60+ super (args );
61+ mTaskId = args .getString (ARGUMENT_EDIT_TASK_ID );
6062 }
6163
6264 @ Override
6365 public void setPresenter (@ NonNull AddEditTaskContract .Presenter presenter ) {
64- mPresenter = checkNotNull ( presenter );
66+ // todo: remove
6567 }
6668
69+ @ NonNull
6770 @ Override
68- public void onActivityCreated (Bundle savedInstanceState ) {
69- super .onActivityCreated (savedInstanceState );
71+ protected View onCreateView (@ NonNull LayoutInflater inflater , @ NonNull ViewGroup container ) {
72+ View root = inflater .inflate (R .layout .addtask_frag , container , false );
73+ mTitle = (TextView ) root .findViewById (R .id .add_task_title );
74+ mDescription = (TextView ) root .findViewById (R .id .add_task_description );
7075
7176 FloatingActionButton fab =
72- (FloatingActionButton ) getActivity () .findViewById (R .id .fab_edit_task_done );
77+ (FloatingActionButton ) root .findViewById (R .id .fab_edit_task_done );
7378 fab .setImageResource (R .drawable .ic_done );
7479 fab .setOnClickListener (new View .OnClickListener () {
7580 @ Override
7681 public void onClick (View v ) {
7782 mPresenter .saveTask (mTitle .getText ().toString (), mDescription .getText ().toString ());
7883 }
7984 });
80- }
81-
82- @ Nullable
83- @ Override
84- public View onCreateView (LayoutInflater inflater , ViewGroup container ,
85- Bundle savedInstanceState ) {
86- View root = inflater .inflate (R .layout .addtask_frag , container , false );
87- mTitle = (TextView ) root .findViewById (R .id .add_task_title );
88- mDescription = (TextView ) root .findViewById (R .id .add_task_description );
8985
9086 setHasOptionsMenu (true );
91- setRetainInstance (true );
87+
88+ // Create the presenter
89+ mPresenter = new AddEditTaskPresenter (
90+ mTaskId ,
91+ Injection .provideTasksRepository (getApplicationContext ()),
92+ this );
93+
9294 return root ;
9395 }
9496
97+ @ Override
98+ protected void onAttach (@ NonNull View view ) {
99+ super .onAttach (view );
100+
101+ // Configure Activity level UI.
102+ ActionBar actionBar = getActionBar ();
103+ if (mTaskId == null ) {
104+ actionBar .setTitle (R .string .add_task );
105+ } else {
106+ actionBar .setTitle (R .string .edit_task );
107+ }
108+ actionBar .setDisplayHomeAsUpEnabled (true );
109+ actionBar .setDisplayShowHomeEnabled (true );
110+
111+ // Start Presenter
112+ mPresenter .start ();
113+ }
114+
95115 @ Override
96116 public void showEmptyTaskError () {
97- Snackbar .make (mTitle , getString (R .string .empty_task_message ), Snackbar .LENGTH_LONG ).show ();
117+ Snackbar .make (mTitle , getResources (). getString (R .string .empty_task_message ), Snackbar .LENGTH_LONG ).show ();
98118 }
99119
100120 @ Override
101121 public void showTasksList () {
102- getActivity ().setResult (Activity .RESULT_OK );
103- getActivity ().finish ();
122+ getRouter ().popCurrentController ();
104123 }
105124
106125 @ Override
@@ -112,9 +131,4 @@ public void setTitle(String title) {
112131 public void setDescription (String description ) {
113132 mDescription .setText (description );
114133 }
115-
116- @ Override
117- public boolean isActive () {
118- return isAdded ();
119- }
120- }
134+ }
0 commit comments