Skip to content

Commit 003195e

Browse files
committed
✨ issue #8 done
1 parent 39a3b01 commit 003195e

8 files changed

Lines changed: 50 additions & 34 deletions

File tree

.idea/misc.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/codexo/notes/adapters/BindingAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class BindingAdapter {
2929
view.visibility = if (emptyDb.value == true) View.VISIBLE else View.INVISIBLE
3030
}
3131

32-
@BindingAdapter("android:marked_favorite")
32+
@BindingAdapter("android:is_favorite")
3333
@JvmStatic
34-
fun markFavorite(img: ImageView, marked: Boolean) {
34+
fun isFavorite(img: ImageView, marked: Boolean) {
3535
if (marked) {
3636
img.setImageResource(R.drawable.ic_favorite_on)
3737
} else img.setImageResource(R.drawable.ic_favorite_off)

app/src/main/java/com/codexo/notes/adapters/NoteAdapter.kt

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,46 @@ import androidx.recyclerview.widget.RecyclerView
77
import com.codexo.notes.data.Note
88
import com.codexo.notes.databinding.ItemNotesBinding
99

10-
class NotesAdapter : RecyclerView.Adapter<NotesAdapter.TodoViewHolder>() {
10+
class NotesAdapter(private val listener: OnItemClickListener) :
11+
RecyclerView.Adapter<NotesAdapter.NoteViewHolder>() {
1112

12-
var todoList = emptyList<Note>()
13+
var NoteList = emptyList<Note>()
1314

14-
class TodoViewHolder(private val binding: ItemNotesBinding) :
15-
RecyclerView.ViewHolder(binding.root) {
16-
fun bind(note: Note) {
17-
binding.note = note
18-
binding.executePendingBindings()
19-
}
20-
21-
companion object {
22-
fun from(parent: ViewGroup): TodoViewHolder {
23-
val layoutInflater = LayoutInflater.from(parent.context)
24-
val binding = ItemNotesBinding.inflate(layoutInflater, parent, false)
25-
return TodoViewHolder(binding)
26-
}
27-
}
15+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
16+
val binding = ItemNotesBinding.inflate(LayoutInflater.from(parent.context), parent, false)
17+
return NoteViewHolder(binding)
2818
}
2919

30-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
31-
return TodoViewHolder.from(parent)
32-
}
33-
34-
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
35-
val currentItem = todoList[position]
20+
override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
21+
val currentItem = NoteList[position]
3622
holder.bind(currentItem)
3723
}
3824

3925
override fun getItemCount(): Int {
40-
return todoList.size
26+
return NoteList.size
4127
}
4228

4329
fun setData(notesData: List<Note>) {
44-
val notesDiffUtil = DiffUtil(todoList, notesData)
30+
val notesDiffUtil = DiffUtil(NoteList, notesData)
4531
val notesDiffResult = DiffUtil.calculateDiff(notesDiffUtil)
4632

47-
this.todoList = notesData
33+
this.NoteList = notesData
4834
notesDiffResult.dispatchUpdatesTo(this)
4935
}
36+
37+
inner class NoteViewHolder(private val binding: ItemNotesBinding) :
38+
RecyclerView.ViewHolder(binding.root) {
39+
fun bind(note: Note) {
40+
binding.note = note
41+
binding.ivFavorite.setOnClickListener {
42+
listener.onFavoriteClicked(!note.favorite, note.id)
43+
}
44+
45+
binding.executePendingBindings()
46+
}
47+
}
48+
49+
interface OnItemClickListener {
50+
fun onFavoriteClicked(markedFavorite: Boolean, id: Long)
51+
}
5052
}

app/src/main/java/com/codexo/notes/ui/notes/NotesFragment.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@ import com.codexo.notes.ui.SharedViewModel
1919
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2020
import com.google.android.material.snackbar.Snackbar
2121

22-
class NotesFragment : Fragment(R.layout.fragment_notes) {
22+
class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClickListener {
2323

2424
private val viewModel: NotesViewModel by viewModels()
2525
private val sharedViewModel: SharedViewModel by viewModels()
2626
private var _binding: FragmentNotesBinding? = null
2727
private val binding
2828
get() = _binding
29-
private val notesAdapter = NotesAdapter()
29+
private val notesAdapter = NotesAdapter(this)
3030

3131
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
3232
super.onViewCreated(view, savedInstanceState)
33-
3433
_binding = FragmentNotesBinding.bind(view)
3534

3635
binding?.apply {
@@ -145,6 +144,11 @@ class NotesFragment : Fragment(R.layout.fragment_notes) {
145144
})
146145
}
147146

147+
private fun markAsFavorite(markedFavorite: Boolean, id: Long) {
148+
viewModel.markAsFavorite(markedFavorite, id)
149+
150+
}
151+
148152
override fun onDestroyView() {
149153
super.onDestroyView()
150154
_binding = null
@@ -158,4 +162,8 @@ class NotesFragment : Fragment(R.layout.fragment_notes) {
158162
super.onResume()
159163
viewModel.allNotes.observe(viewLifecycleOwner) { notesAdapter.setData(it) }
160164
}
165+
166+
override fun onFavoriteClicked(markedFavorite: Boolean, id: Long) {
167+
markAsFavorite(markedFavorite, id)
168+
}
161169
}

app/src/main/java/com/codexo/notes/ui/notes/NotesViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData
66
import androidx.lifecycle.viewModelScope
77
import com.codexo.notes.data.Note
88
import com.codexo.notes.data.NoteDatabase
9+
import kotlinx.coroutines.Dispatchers
910
import kotlinx.coroutines.launch
1011

1112
class NotesViewModel constructor(application: Application) : AndroidViewModel(application) {
@@ -23,6 +24,9 @@ class NotesViewModel constructor(application: Application) : AndroidViewModel(ap
2324

2425
fun deleteAllNotes() = viewModelScope.launch { noteDao.clearNotes() }
2526

27+
fun markAsFavorite(fave: Boolean, id: Long) =
28+
viewModelScope.launch(Dispatchers.IO) { noteDao.markAsFavorite(fave, id) }
29+
2630
fun initDummyNote() = viewModelScope.launch {
2731
noteDao.insert(
2832
Note(

app/src/main/res/drawable/ic_favorite_off.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
android:height="24dp"
44
android:viewportWidth="24"
55
android:viewportHeight="24"
6-
android:tint="@color/white">
6+
android:tint="@color/golden">
77
<path
88
android:fillColor="@android:color/white"
99
android:pathData="M22,9.24l-7.19,-0.62L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21 12,17.27 18.18,21l-1.63,-7.03L22,9.24zM12,15.4l-3.76,2.27 1,-4.28 -3.32,-2.88 4.38,-0.38L12,6.1l1.71,4.04 4.38,0.38 -3.32,2.88 1,4.28L12,15.4z"/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
android:layout_width="wrap_content"
1616
android:layout_height="wrap_content"
1717
app:lottie_autoPlay="false"
18-
app:lottie_loop="true"
18+
app:lottie_loop="false"
1919
app:lottie_rawRes="@raw/ic_no_data" />
2020

2121
<androidx.recyclerview.widget.RecyclerView

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
<ImageView
4040
android:id="@+id/iv_favorite"
4141
style="@style/FavoriteStyle"
42-
android:marked_favorite="@{Note.favorite}"
42+
android:is_favorite="@{Note.favorite}"
4343
android:src="@drawable/selector_favorite"
44+
tools:src="@drawable/ic_favorite_on"
4445
app:layout_constraintBottom_toBottomOf="@+id/tv_title"
4546
app:layout_constraintEnd_toEndOf="parent"
4647
app:layout_constraintTop_toTopOf="@+id/tv_title"

0 commit comments

Comments
 (0)