Skip to content

Commit 2a1d653

Browse files
committed
Add darkmode
1 parent 33d0a9a commit 2a1d653

16 files changed

Lines changed: 158 additions & 24 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ dependencies {
7272

7373
// Kotlin Extensions and Coroutines support for Room
7474
implementation "androidx.room:room-ktx:$room_version"
75+
7576
}

app/src/main/java/com/example/mynotes/adapter/NotesAdapter.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ class NotesAdapter : RecyclerView.Adapter<NotesAdapter.NoteViewHolder>() {
4444
this.notes = note
4545
notifyDataSetChanged()
4646
}
47+
4748
}

app/src/main/java/com/example/mynotes/ui/activities/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.example.mynotes.ui.activities
33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
55
import androidx.navigation.findNavController
6+
import androidx.navigation.ui.NavigationUI
67
import androidx.navigation.ui.setupActionBarWithNavController
78
import com.example.mynotes.R
89

@@ -11,7 +12,7 @@ class MainActivity : AppCompatActivity() {
1112
super.onCreate(savedInstanceState)
1213
setContentView(R.layout.activity_main)
1314

14-
setupActionBarWithNavController(findNavController(R.id.nav_host_fragment))
15+
setupActionBarWithNavController(this.findNavController(R.id.nav_host_fragment))
1516
}
1617

1718
override fun onSupportNavigateUp(): Boolean {

app/src/main/java/com/example/mynotes/ui/fragments/NotesFragment.kt

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package com.example.mynotes.ui.fragments
22

3+
import android.content.res.Configuration
34
import android.os.Bundle
45
import android.view.*
6+
import androidx.appcompat.app.AppCompatDelegate
57
import androidx.databinding.DataBindingUtil
68
import androidx.fragment.app.Fragment
7-
import androidx.lifecycle.Observer
89
import androidx.lifecycle.ViewModelProvider
910
import androidx.navigation.fragment.findNavController
11+
import androidx.recyclerview.widget.ItemTouchHelper
1012
import androidx.recyclerview.widget.LinearLayoutManager
13+
import androidx.recyclerview.widget.RecyclerView
1114
import com.example.mynotes.R
1215
import com.example.mynotes.adapter.NotesAdapter
1316
import com.example.mynotes.databinding.FragmentNotesBinding
1417
import com.example.mynotes.viewmodel.NotesViewModel
18+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
19+
import com.google.android.material.snackbar.Snackbar
20+
import kotlinx.android.synthetic.main.notes_lyt.view.*
1521

1622
class NotesFragment : Fragment() {
1723

@@ -42,7 +48,12 @@ class NotesFragment : Fragment() {
4248
}
4349

4450
viewModel.getAllNotes.observe(viewLifecycleOwner, {
45-
adapter.setNotes(it)
51+
if (it.isEmpty()) {
52+
binding.imageLlyt.visibility = View.VISIBLE
53+
} else {
54+
binding.recyclerView.visibility = View.VISIBLE
55+
adapter.setNotes(it)
56+
}
4657
})
4758

4859
return binding.root
@@ -54,20 +65,49 @@ class NotesFragment : Fragment() {
5465
}
5566

5667
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
57-
return inflater.inflate(R.menu.delete_menu, menu)
58-
68+
return inflater.inflate(R.menu.main_menu, menu)
5969
}
6070

6171
override fun onOptionsItemSelected(item: MenuItem): Boolean {
62-
if (item.itemId == R.id.delete) {
63-
deleteAllNotes()
72+
73+
return when (item.itemId) {
74+
// R.id.mode -> {
75+
// val mode =
76+
// if ((resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO) {
77+
// AppCompatDelegate.MODE_NIGHT_YES
78+
// } else {
79+
// AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
80+
// }
81+
//
82+
// // Change UI Mode
83+
// AppCompatDelegate.setDefaultNightMode(mode)
84+
// true
85+
// }
86+
87+
R.id.delete -> {
88+
deleteAllNotes()
89+
true
90+
}
91+
else -> super.onOptionsItemSelected(item)
6492
}
65-
return super.onOptionsItemSelected(item)
6693
}
6794

6895
// delete all notes
6996
private fun deleteAllNotes() {
70-
viewModel.deleteAllNotes()
97+
98+
MaterialAlertDialogBuilder(requireContext())
99+
.setTitle("Delete notes")
100+
.setMessage("Are you sure you want to delete all your notes?")
101+
102+
.setNegativeButton("Cancel") { _, _ ->
103+
// Respond to negative button press
104+
}
105+
.setPositiveButton("Delete") { _, _ ->
106+
// Respond to positive button press
107+
viewModel.deleteAllNotes()
108+
binding.recyclerView.visibility = View.GONE
109+
}
110+
.show()
71111
}
72112

73113
}

app/src/main/java/com/example/mynotes/ui/fragments/UpdateFragment.kt

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package com.example.mynotes.ui.fragments
22

33
import android.os.Bundle
44
import android.text.TextUtils
5-
import android.view.LayoutInflater
6-
import android.view.View
7-
import android.view.ViewGroup
5+
import android.view.*
86
import android.widget.Toast
97
import androidx.databinding.DataBindingUtil
108
import androidx.fragment.app.Fragment
@@ -14,6 +12,7 @@ import com.example.mynotes.R
1412
import com.example.mynotes.databinding.FragmentUpdateBinding
1513
import com.example.mynotes.model.Note
1614
import com.example.mynotes.viewmodel.NotesViewModel
15+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1716
import com.google.android.material.snackbar.Snackbar
1817

1918
class UpdateFragment : Fragment() {
@@ -29,6 +28,8 @@ class UpdateFragment : Fragment() {
2928
// Inflate the layout for this fragment
3029
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_update, container, false)
3130

31+
setHasOptionsMenu(true)
32+
3233
args = UpdateFragmentArgs.fromBundle(requireArguments())
3334

3435
binding.updateTitleTxt.setText(args.currentNote.title)
@@ -65,4 +66,41 @@ class UpdateFragment : Fragment() {
6566
}
6667
}
6768

69+
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
70+
return inflater.inflate(R.menu.main_menu, menu)
71+
72+
}
73+
74+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
75+
if (item.itemId == R.id.delete) {
76+
deleteNote()
77+
}
78+
return super.onOptionsItemSelected(item)
79+
}
80+
81+
// delete current note
82+
private fun deleteNote() {
83+
84+
MaterialAlertDialogBuilder(requireContext())
85+
.setTitle("Delete note")
86+
.setMessage("Are you sure you want to delete this note?")
87+
88+
.setNegativeButton("Cancel") { _, _ ->
89+
// Respond to negative button press
90+
}
91+
.setPositiveButton("Delete") { _, _ ->
92+
// Respond to positive button press
93+
viewmodel.deleteNote(args.currentNote)
94+
95+
// navigate back to notes fragment
96+
findNavController().navigate(UpdateFragmentDirections.actionUpdateFragmentToNotesFragment())
97+
98+
view?.let {
99+
Snackbar.make(it, "Note deleted successfully", Snackbar.LENGTH_SHORT)
100+
.show()
101+
}
102+
}
103+
.show()
104+
}
105+
68106
}

app/src/main/java/com/example/mynotes/viewmodel/NotesViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.example.mynotes.database.NotesDatabase
99
import com.example.mynotes.model.Note
1010
import com.example.mynotes.repository.NoteRepository
1111
import kotlinx.coroutines.Dispatchers
12+
import kotlinx.coroutines.Dispatchers.IO
1213
import kotlinx.coroutines.launch
1314

1415
class NotesViewModel(application: Application) : AndroidViewModel(application) {
10.6 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:width="30dp"
4+
android:height="30dp"
5+
android:viewportWidth="30"
6+
android:viewportHeight="30"
7+
android:tint="?attr/colorControlNormal">
8+
<path
9+
android:fillColor="#00000000"
10+
android:pathData="M11.25,20C10.2006,19.2129 9.4254,18.1156 9.0343,16.8635C8.6432,15.6114 8.6559,14.268 9.0707,13.0236C9.4855,11.7791 10.2814,10.6967 11.3456,9.9297C12.4097,9.1627 13.6882,8.75 15,8.75C16.3118,8.75 17.5903,9.1627 18.6544,9.9297C19.7186,10.6967 20.5145,11.7791 20.9293,13.0236C21.3441,14.268 21.3568,15.6114 20.9657,16.8635C20.5746,18.1156 19.7994,19.2129 18.75,20C18.262,20.4831 17.8945,21.0743 17.6773,21.7258C17.4602,22.3773 17.3994,23.0707 17.5,23.75C17.5,24.413 17.2366,25.0489 16.7678,25.5178C16.2989,25.9866 15.663,26.25 15,26.25C14.337,26.25 13.7011,25.9866 13.2322,25.5178C12.7634,25.0489 12.5,24.413 12.5,23.75C12.6006,23.0707 12.5398,22.3773 12.3227,21.7258C12.1055,21.0743 11.738,20.4831 11.25,20"
11+
android:strokeWidth="1.5"
12+
android:strokeColor="#F2F2F2"
13+
android:strokeLineCap="round"
14+
android:strokeLineJoin="round" />
15+
<path
16+
android:fillColor="#00000000"
17+
android:pathData="M12.125,21.25H17.875"
18+
android:strokeWidth="1.5"
19+
android:strokeColor="#F2F2F2"
20+
android:strokeLineCap="round"
21+
android:strokeLineJoin="round" />
22+
</vector>

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<merge xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
@@ -18,4 +18,4 @@
1818
app:layout_constraintTop_toTopOf="parent"
1919
app:navGraph="@navigation/my_nav" />
2020

21-
</androidx.constraintlayout.widget.ConstraintLayout>
21+
</merge>

app/src/main/res/layout/fragment_add_note.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
android:maxLength="60"
2424
android:maxLines="3"
2525
android:padding="12dp"
26-
android:textColor="@color/black"
2726
android:textColorHint="@android:color/darker_gray"
2827
android:textSize="30sp"
2928
app:layout_constraintEnd_toEndOf="parent"
3029
app:layout_constraintStart_toStartOf="parent"
3130
app:layout_constraintTop_toTopOf="parent"
3231
tools:ignore="Autofill" />
3332

34-
3533
<com.google.android.material.textfield.TextInputEditText
3634
android:id="@+id/desc_txt"
3735
android:layout_width="match_parent"

0 commit comments

Comments
 (0)