1
+ package net.code.gdsc_app.Attendance.Adapter
2
+
3
+ import android.view.LayoutInflater
4
+ import android.view.View
5
+ import android.view.ViewGroup
6
+ import androidx.fragment.app.FragmentActivity
7
+ import androidx.recyclerview.widget.DiffUtil
8
+ import androidx.recyclerview.widget.ListAdapter
9
+ import androidx.recyclerview.widget.RecyclerView
10
+ import net.code.gdsc_app.Attendance.Database.Attendance
11
+ import net.code.gdsc_app.Attendance.Database.AttendanceViewmodel
12
+ import net.code.gdsc_app.databinding.ItemAttendanceBinding
13
+
14
+ class AttendanceAdapter (
15
+ val attendanceViewmodel : AttendanceViewmodel ,
16
+ val parentView : View ,
17
+ val activity : FragmentActivity ?
18
+ ) :
19
+ ListAdapter <Attendance , AttendanceAdapter .ViewHolder >(
20
+ ListDiffCallbacks ()
21
+ ) {
22
+
23
+ var attendanceList = ArrayList <Attendance >()
24
+
25
+ override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): ViewHolder {
26
+ val layoutInflater = LayoutInflater .from(parent.context)
27
+ val binding = ItemAttendanceBinding .inflate(layoutInflater, parent, false )
28
+ return ViewHolder (binding)
29
+ }
30
+
31
+ override fun onBindViewHolder (holder : ViewHolder , position : Int ) {
32
+ val item = getItem(position)
33
+ holder.bind(item, attendanceViewmodel)
34
+ // if (position == 0) {
35
+ // introDelete(holder.binding.itemText)
36
+ // }
37
+ }
38
+
39
+ class ViewHolder (val binding : ItemAttendanceBinding ) :
40
+ RecyclerView .ViewHolder (binding.root) {
41
+
42
+ fun bind (item : Attendance , attendanceViewmodel : AttendanceViewmodel ) {
43
+ binding.subName.text = item.subject
44
+ binding.went.text = item.attended.toString()
45
+ binding.total.text = item.total.toString()
46
+ binding.per.text = item.percentage.toString()+ " %"
47
+ binding.increment.setOnClickListener {
48
+ item.attended + = 1
49
+ item.total + = 1
50
+ item.percentage = (item.attended * 100 ) / item.total
51
+ attendanceViewmodel.update(item)
52
+ binding.went.text = item.attended.toString()
53
+ binding.total.text = item.total.toString()
54
+ binding.per.text = item.percentage.toString()+ " %"
55
+ }
56
+ binding.decrement.setOnClickListener {
57
+ item.total + = 1
58
+ item.percentage = (item.attended * 100 ) / item.total
59
+ attendanceViewmodel.update(item)
60
+ binding.total.text = item.total.toString()
61
+ binding.per.text = item.percentage.toString()+ " %"
62
+ }
63
+ }
64
+ }
65
+
66
+ class ListDiffCallbacks : DiffUtil .ItemCallback <Attendance >() {
67
+ override fun areItemsTheSame (oldItem : Attendance , newItem : Attendance ): Boolean {
68
+ return oldItem.id == newItem.id
69
+ }
70
+
71
+ override fun areContentsTheSame (oldItem : Attendance , newItem : Attendance ): Boolean {
72
+ return oldItem == newItem
73
+ }
74
+ }
75
+
76
+ fun getList () = attendanceList
77
+
78
+ fun removeitem (position : Int ) {
79
+ attendanceViewmodel.delete(attendanceList[position])
80
+ notifyItemRemoved(position)
81
+ }
82
+
83
+ fun restoreItem (attendance : Attendance , position : Int ) {
84
+ attendanceList.add(position, attendance)
85
+ notifyItemChanged(position)
86
+ attendanceViewmodel.insert(attendance)
87
+ }
88
+ }
0 commit comments