Skip to content

Commit

Permalink
Added the bottom dialog feature
Browse files Browse the repository at this point in the history
  • Loading branch information
certified84 committed Feb 28, 2021
1 parent 21f481e commit ec498af
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 287 deletions.
60 changes: 30 additions & 30 deletions app/src/main/java/com/certified/notes/room/NotesDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,36 @@ public void onCreate(@NonNull SupportSQLiteDatabase db) {
NotesDao notesDao = instance.mNotesDao();

// notesDao.deleteAllNotes();
// notesDao.insertNote(note1);
// notesDao.insertNote(note2);
// notesDao.insertNote(note3);
// notesDao.insertNote(note4);
// notesDao.insertNote(note5);
// notesDao.insertNote(note6);
// notesDao.insertNote(note7);
// notesDao.insertNote(note8);
//
//// notesDao.deleteAllCourses();
// notesDao.insertCourse(course1);
// notesDao.insertCourse(course2);
// notesDao.insertCourse(course3);
// notesDao.insertCourse(course4);
// notesDao.insertCourse(course5);
// notesDao.insertCourse(course6);
// notesDao.insertCourse(course7);
// notesDao.insertCourse(course8);
// notesDao.insertCourse(course9);
// notesDao.insertCourse(course10);
//
//// notesDao.deleteAllTodos();
// notesDao.insertTodo(todo1);
// notesDao.insertTodo(todo2);
// notesDao.insertTodo(todo3);
// notesDao.insertTodo(todo4);
// notesDao.insertTodo(todo5);
// notesDao.insertTodo(todo6);
// notesDao.insertTodo(todo7);
// notesDao.insertTodo(todo8);
notesDao.insertNote(note1);
notesDao.insertNote(note2);
notesDao.insertNote(note3);
notesDao.insertNote(note4);
notesDao.insertNote(note5);
notesDao.insertNote(note6);
notesDao.insertNote(note7);
notesDao.insertNote(note8);

// notesDao.deleteAllCourses();
notesDao.insertCourse(course1);
notesDao.insertCourse(course2);
notesDao.insertCourse(course3);
notesDao.insertCourse(course4);
notesDao.insertCourse(course5);
notesDao.insertCourse(course6);
notesDao.insertCourse(course7);
notesDao.insertCourse(course8);
notesDao.insertCourse(course9);
notesDao.insertCourse(course10);

// notesDao.deleteAllTodos();
notesDao.insertTodo(todo1);
notesDao.insertTodo(todo2);
notesDao.insertTodo(todo3);
notesDao.insertTodo(todo4);
notesDao.insertTodo(todo5);
notesDao.insertTodo(todo6);
notesDao.insertTodo(todo7);
notesDao.insertTodo(todo8);

notesDao.insertUser(user);
});
Expand Down
26 changes: 10 additions & 16 deletions app/src/main/java/com/certified/notes/ui/CoursesFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
Expand All @@ -28,6 +29,7 @@
import com.certified.notes.model.Course;
import com.certified.notes.model.Note;
import com.certified.notes.room.NotesViewModel;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textview.MaterialTextView;
Expand Down Expand Up @@ -69,7 +71,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

mViewModel = new NotesViewModel(getActivity().getApplication());
mNavController = Navigation.findNavController(view);
mViewModel = new NotesViewModel(requireActivity().getApplication());
ivCoursePopupMenu.setOnClickListener(this::showPopupMenu);

init();
Expand Down Expand Up @@ -158,17 +161,7 @@ public boolean onMenuItemClick(MenuItem item) {
private void launchEditCourseDialog(@NonNull Course course) {
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_new_course, null);

MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setBackground(getContext().getDrawable(R.drawable.alert_dialog_bg));
}
AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(dialog1 -> {
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(RED);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(RED);
});
alertDialog.setView(view);
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(requireContext(), R.style.BottomSheetDialogTheme);

MaterialTextView tvCourseDialogTitle = view.findViewById(R.id.tv_course_dialog_title);
EditText etCourseCode = view.findViewById(R.id.et_course_code);
Expand All @@ -185,7 +178,7 @@ private void launchEditCourseDialog(@NonNull Course course) {
etCourseTitle.setText(course.getCourseTitle());
numberPickerCourseUnit.setValue(course.getCourseUnit());

btnCancel.setOnClickListener(v -> alertDialog.dismiss());
btnCancel.setOnClickListener(v -> bottomSheetDialog.dismiss());
btnSave.setText(R.string.update);
btnSave.setOnClickListener(v -> {
String courseCode = etCourseCode.getText().toString().trim();
Expand Down Expand Up @@ -216,14 +209,14 @@ private void launchEditCourseDialog(@NonNull Course course) {
}
});
mViewModel.updateCourse(course1);
alertDialog.dismiss();
bottomSheetDialog.dismiss();
} else
Toast.makeText(getContext(), "Course not changed", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getContext(), "All fields are required", Toast.LENGTH_SHORT).show();
});

alertDialog.show();
bottomSheetDialog.setContentView(view);
bottomSheetDialog.show();
}

private void launchRelatedNotesDialog(@NonNull Course course) {
Expand All @@ -245,6 +238,7 @@ private void launchRelatedNotesDialog(@NonNull Course course) {
mViewModel.getNotesAt(course.getCourseCode()).observe(getViewLifecycleOwner(), noteRecyclerAdapter::submitList);
recyclerViewRelatedNotes.setAdapter(noteRecyclerAdapter);
recyclerViewRelatedNotes.setLayoutManager(noteLayoutManager);
noteRecyclerAdapter.setOnNoteClickedListener(() -> mNavController.navigate(R.id.notesFragment));

alertDialog.show();
}
Expand Down
53 changes: 1 addition & 52 deletions app/src/main/java/com/certified/notes/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.certified.notes.ui

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
Expand All @@ -15,7 +9,6 @@ import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.NotificationCompat
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import androidx.navigation.Navigation
Expand Down Expand Up @@ -51,16 +44,12 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
private lateinit var navController: NavController
private lateinit var bottomNavigationView: BottomNavigationView

private val PRIMARY_CHANNEL_ID = "primary_notification_channel"
private lateinit var notifyManager: NotificationManager
private val NOTES_NOTIFICATION_ID = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

isDarkModeEnabled()
createNotificationChannel()
// createNotificationChannel()
isFirstOpen()

notesViewModel =
Expand Down Expand Up @@ -122,49 +111,9 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
}
val alertDialog = alertDialogBuilder.create()
alertDialog.show()
} else
sendNotification()
}

private fun createNotificationChannel() {
notifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(
PRIMARY_CHANNEL_ID,
getString(R.string.notes_notification),
NotificationManager.IMPORTANCE_HIGH
)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.description = getString(R.string.notes_reminder_notification)
notifyManager.createNotificationChannel(notificationChannel)
}
}

private fun getNotificationBuilder(): NotificationCompat.Builder? {
val notificationIntent = Intent(this, MainActivity::class.java)
val notificationPendingIntent = PendingIntent.getActivity(
this, NOTES_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
val message = getString(R.string.notification_message)
return NotificationCompat.Builder(this, PRIMARY_CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_notes)
.setContentTitle(getString(R.string.notes_reminder))
.setColor(resources.getColor(R.color.colorAccent))
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setTicker("Notes")
.addAction(R.drawable.ic_add, "Add Note", notificationPendingIntent)
.setAutoCancel(true)
}

private fun sendNotification() {
val notifyBuilder = getNotificationBuilder()
notifyManager.notify(NOTES_NOTIFICATION_ID, notifyBuilder!!.build())
}

override fun onClick(v: View) {
when (v.id) {
R.id.fab -> {
Expand Down
24 changes: 11 additions & 13 deletions app/src/main/java/com/certified/notes/ui/NotesFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.certified.notes.model.Note;
import com.certified.notes.room.NotesViewModel;
import com.certified.notes.util.PreferenceKeys;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

Expand Down Expand Up @@ -130,13 +131,7 @@ private void init() {
noteRecyclerAdapter.setOnNoteClickedListener(note -> {
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_new_note, null);
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setBackground(getContext().getDrawable(R.drawable.alert_dialog_bg));
}
AlertDialog alertDialog = builder.create();
alertDialog.setView(view);
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(requireContext(), R.style.BottomSheetDialogTheme);

Spinner spinnerCourses = view.findViewById(R.id.spinner_courses);
EditText etNoteTitle = view.findViewById(R.id.et_note_title);
Expand All @@ -160,7 +155,7 @@ private void init() {

spinnerCourses.setSelection(coursePosition);

btnCancel.setOnClickListener(v -> alertDialog.dismiss());
btnCancel.setOnClickListener(v -> bottomSheetDialog.dismiss());
btnSave.setText(R.string.update);
btnSave.setOnClickListener(v -> {
String courseTitle = spinnerCourses.getSelectedItem().toString();
Expand All @@ -186,7 +181,7 @@ private void init() {
mViewModel.updateBookMark(bookMark1);
}
});
alertDialog.dismiss();
bottomSheetDialog.dismiss();
} else
Toast.makeText(getContext(), "Note not changed", Toast.LENGTH_SHORT).show();
} else {
Expand All @@ -202,7 +197,7 @@ private void init() {
mViewModel.updateBookMark(bookMark1);
}
});
alertDialog.dismiss();
bottomSheetDialog.dismiss();
} else
Toast.makeText(getContext(), "Note not changed", Toast.LENGTH_SHORT).show();
}
Expand All @@ -211,8 +206,8 @@ private void init() {
} else
Toast.makeText(getContext(), "All fields are required", Toast.LENGTH_SHORT).show();
});

alertDialog.show();
bottomSheetDialog.setContentView(view);
bottomSheetDialog.show();
});

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
Expand Down Expand Up @@ -241,7 +236,10 @@ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction)

dialog1.dismiss();
});
builder.setNegativeButton(getString(R.string.cancel), (dialog1, which) -> dialog1.dismiss());
builder.setNegativeButton(getString(R.string.cancel), (dialog1, which) -> {
noteRecyclerAdapter.notifyDataSetChanged();
dialog1.dismiss();
});
AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(dialog1 -> {
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(RED);
Expand Down
Loading

0 comments on commit ec498af

Please sign in to comment.