3636import com .google .android .material .card .MaterialCardView ;
3737import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
3838import com .google .android .material .textview .MaterialTextView ;
39+ import com .shawnlin .numberpicker .NumberPicker ;
3940
4041import java .util .ArrayList ;
4142import java .util .List ;
@@ -53,6 +54,7 @@ public class HomeFragment extends Fragment implements View.OnClickListener, Popu
5354 private NavController mNavController ;
5455 private NotesViewModel mViewModel ;
5556 private MaterialCardView cardView ;
57+ private TodoRecyclerAdapter todoRecyclerAdapter ;
5658
5759 public HomeFragment () {
5860 // Required empty public constructor
@@ -108,33 +110,43 @@ private void init() {
108110
109111 HomeNoteRecyclerAdapter noteRecyclerAdapter = new HomeNoteRecyclerAdapter (ID_NOT_SET );
110112 mViewModel .getRandomNotes ().observe (getViewLifecycleOwner (), notes -> {
111- if (notes != null )
113+ if (notes != null ) {
112114 noteRecyclerAdapter .submitList (notes );
115+ btnShowAllNotes .setClickable (true );
116+ }
113117 else {
114118 tvAddNoteDescription .setVisibility (View .VISIBLE );
115119 btnAddNote .setVisibility (View .VISIBLE );
120+ btnShowAllNotes .setClickable (false );
116121 }
117122 });
123+
118124 recyclerNotes .setAdapter (noteRecyclerAdapter );
119125 recyclerNotes .setLayoutManager (noteLayoutManager );
120126 recyclerNotes .setClipToPadding (false );
121127 recyclerNotes .setClipChildren (false );
128+ noteRecyclerAdapter .setOnNoteClickedListener (() -> mNavController .navigate (R .id .notesFragment ));
122129
123130 HomeCourseRecyclerAdapter courseRecyclerAdapter = new HomeCourseRecyclerAdapter ();
124131 mViewModel .getRandomCourses ().observe (getViewLifecycleOwner (), courses -> {
125- if (courses != null )
132+ if (courses != null ) {
126133 courseRecyclerAdapter .submitList (courses );
134+ btnShowAllCourses .setClickable (true );
135+ }
127136 else {
128137 tvAddCourseDescription .setVisibility (View .VISIBLE );
129138 btnAddCourse .setVisibility (View .VISIBLE );
139+ btnShowAllCourses .setClickable (false );
130140 }
131141 });
142+
132143 recyclerCourses .setLayoutManager (courseLayoutManager );
133144 recyclerCourses .setAdapter (courseRecyclerAdapter );
134145 recyclerCourses .setClipToPadding (false );
135146 recyclerCourses .setClipChildren (false );
147+ courseRecyclerAdapter .setOnCourseClickedListener (() -> mNavController .navigate (R .id .coursesFragment ));
136148
137- TodoRecyclerAdapter todoRecyclerAdapter = new TodoRecyclerAdapter (getContext (), mViewModel );
149+ todoRecyclerAdapter = new TodoRecyclerAdapter (getContext (), mViewModel );
138150 mViewModel .getAllTodos ().observe (getViewLifecycleOwner (), todos -> {
139151 if (todos .size () != 0 )
140152 todoRecyclerAdapter .submitList (todos );
@@ -144,8 +156,10 @@ private void init() {
144156 btnAddTodo .setVisibility (View .VISIBLE );
145157 }
146158 });
159+
147160 recyclerTodos .setLayoutManager (todoLayoutManager );
148161 recyclerTodos .setAdapter (todoRecyclerAdapter );
162+
149163 todoRecyclerAdapter .setOnTodoClickedListener (todo -> {
150164 LayoutInflater inflater = this .getLayoutInflater ();
151165 View view = inflater .inflate (R .layout .dialog_new_todo , null );
@@ -231,7 +245,7 @@ private void launchNoteDialog() {
231245 builder .setBackground (getContext ().getDrawable (R .drawable .alert_dialog_bg ));
232246 }
233247 AlertDialog alertDialog = builder .create ();
234- alertDialog .setCancelable (false );
248+ // alertDialog.setCancelable(false);
235249 alertDialog .setOnShowListener (dialog -> {
236250 alertDialog .getButton (AlertDialog .BUTTON_NEGATIVE ).setTextColor (Color .RED );
237251 alertDialog .getButton (AlertDialog .BUTTON_POSITIVE ).setTextColor (Color .RED );
@@ -292,13 +306,88 @@ private void launchNoteDialog() {
292306 }
293307
294308 public void launchCourseDialog () {
295- btnAddCourse .setVisibility (View .GONE );
296- tvAddCourseDescription .setVisibility (View .GONE );
309+ LayoutInflater inflater = getLayoutInflater ();
310+ View view = inflater .inflate (R .layout .dialog_new_course , null );
311+ MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder (requireContext ());
312+
313+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP )
314+ builder .setBackground (getContext ().getDrawable (R .drawable .alert_dialog_bg ));
315+
316+ AlertDialog alertDialog = builder .create ();
317+ alertDialog .setOnShowListener (v -> {
318+ alertDialog .getButton (AlertDialog .BUTTON_NEGATIVE ).setTextColor (Color .RED );
319+ alertDialog .getButton (AlertDialog .BUTTON_POSITIVE ).setTextColor (Color .RED );
320+ });
321+ alertDialog .setView (view );
322+
323+ MaterialTextView tvCourseDialogTitle = view .findViewById (R .id .tv_course_dialog_title );
324+ EditText etCourseCode = view .findViewById (R .id .et_course_code );
325+ EditText etCourseTitle = view .findViewById (R .id .et_course_title );
326+ NumberPicker picker = view .findViewById (R .id .number_picker_course_unit );
327+ MaterialButton btnSave = view .findViewById (R .id .btn_save );
328+ MaterialButton btnCancel = view .findViewById (R .id .btn_cancel );
329+
330+ picker .setMinValue (1 );
331+ picker .setMaxValue (4 );
332+ tvCourseDialogTitle .setText (getString (R .string .add_course ));
333+ btnCancel .setOnClickListener (v -> alertDialog .dismiss ());
334+ btnSave .setOnClickListener (v -> {
335+ String courseCode = etCourseCode .getText ().toString ().trim ();
336+ String courseTitle = etCourseTitle .getText ().toString ().trim ();
337+ int courseUnit = picker .getValue ();
338+ int MARK_NOT_SET = 0 ;
339+ int GRADE_POINT_NOT_SET = 0 ;
340+ if (!courseCode .isEmpty () && !courseTitle .isEmpty ()) {
341+ Course course = new Course (courseCode , courseTitle , courseUnit , MARK_NOT_SET , "F" , GRADE_POINT_NOT_SET );
342+ mViewModel .insertCourse (course );
343+
344+ btnAddCourse .setVisibility (View .GONE );
345+ tvAddCourseDescription .setVisibility (View .GONE );
346+
347+ alertDialog .dismiss ();
348+ } else
349+ Toast .makeText (getContext (), getString (R .string .all_fields_are_required ), Toast .LENGTH_SHORT ).show ();
350+ });
351+ alertDialog .show ();
297352 }
298353
299354 public void launchTodoDialog () {
300- btnAddTodo .setVisibility (View .GONE );
301- tvAddTodoDescription .setVisibility (View .GONE );
355+ LayoutInflater inflater = getLayoutInflater ();
356+ View view = inflater .inflate (R .layout .dialog_new_todo , null );
357+ MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder (requireContext ());
358+
359+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP )
360+ builder .setBackground (getContext ().getDrawable (R .drawable .alert_dialog_bg ));
361+
362+ AlertDialog alertDialog = builder .create ();
363+ alertDialog .setOnShowListener (v -> {
364+ alertDialog .getButton (AlertDialog .BUTTON_NEGATIVE ).setTextColor (Color .RED );
365+ alertDialog .getButton (AlertDialog .BUTTON_POSITIVE ).setTextColor (Color .RED );
366+ });
367+ alertDialog .setView (view );
368+
369+ MaterialTextView tvTodoDialogTitle = view .findViewById (R .id .tv_todo_dialog_title );
370+ EditText etTodo = view .findViewById (R .id .et_todo );
371+ MaterialButton btnSave = view .findViewById (R .id .btn_save );
372+ MaterialButton btnCancel = view .findViewById (R .id .btn_cancel );
373+
374+ tvTodoDialogTitle .setText (getString (R .string .add_todo ));
375+ btnCancel .setOnClickListener (v -> alertDialog .dismiss ());
376+ btnSave .setOnClickListener (v -> {
377+ String todoContent = etTodo .getText ().toString ().trim ();
378+ if (!todoContent .isEmpty ()) {
379+ Todo todo = new Todo (todoContent , false );
380+ mViewModel .insertTodo (todo );
381+
382+ btnAddTodo .setVisibility (View .GONE );
383+ tvAddTodoDescription .setVisibility (View .GONE );
384+ todoRecyclerAdapter .notifyDataSetChanged ();
385+
386+ alertDialog .dismiss ();
387+ } else
388+ Toast .makeText (getContext (), "" , Toast .LENGTH_SHORT ).show ();
389+ });
390+ alertDialog .show ();
302391 }
303392
304393 private void launchDeleteDialog (int id ) {
0 commit comments