@@ -4,6 +4,7 @@ import android.content.Context
4
4
import android.view.LayoutInflater
5
5
import android.view.View
6
6
import android.view.ViewGroup
7
+ import androidx.recyclerview.widget.DiffUtil
7
8
import androidx.recyclerview.widget.RecyclerView
8
9
import io.realm.Realm
9
10
import org.ole.planet.myplanet.R
@@ -13,65 +14,92 @@ import org.ole.planet.myplanet.model.RealmExamQuestion
13
14
import org.ole.planet.myplanet.model.RealmStepExam
14
15
import org.ole.planet.myplanet.model.RealmSubmission.Companion.getNoOfSubmissionByUser
15
16
import org.ole.planet.myplanet.model.RealmSubmission.Companion.getRecentSubmissionDate
17
+ import org.ole.planet.myplanet.model.RealmUserModel
18
+ import org.ole.planet.myplanet.service.UserProfileDbHandler
16
19
import org.ole.planet.myplanet.ui.submission.AdapterMySubmission
17
- import org.ole.planet.myplanet.ui.survey.AdapterSurvey.ViewHolderSurvey
18
20
import org.ole.planet.myplanet.utilities.TimeUtils.formatDate
19
- import org.ole.planet.myplanet.service.UserProfileDbHandler
20
- import org.ole.planet.myplanet.model.RealmUserModel
21
21
22
- class AdapterSurvey (private val context : Context , private val examList : List < RealmStepExam >, private val mRealm : Realm , private val userId : String ) : RecyclerView.Adapter<ViewHolderSurvey>() {
23
- private lateinit var rowSurveyBinding : RowSurveyBinding
22
+ class AdapterSurvey (private val context : Context , private val mRealm : Realm , private val userId : String ) : RecyclerView.Adapter<AdapterSurvey. ViewHolderSurvey>() {
23
+ private var examList : List < RealmStepExam > = emptyList()
24
24
private var listener: OnHomeItemClickListener ? = null
25
- var user: RealmUserModel ? = null
25
+ private var user: RealmUserModel ? = null
26
26
27
27
init {
28
28
if (context is OnHomeItemClickListener ) {
29
29
listener = context
30
30
}
31
+ user = UserProfileDbHandler (context).userModel
32
+ }
33
+
34
+ fun updateData (newList : List <RealmStepExam >) {
35
+ val diffCallback = SurveyDiffCallback (examList, newList)
36
+ val diffResult = DiffUtil .calculateDiff(diffCallback)
37
+ examList = newList
38
+ diffResult.dispatchUpdatesTo(this )
31
39
}
32
40
33
41
override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): ViewHolderSurvey {
34
- rowSurveyBinding = RowSurveyBinding .inflate(LayoutInflater .from(context), parent, false )
35
- return ViewHolderSurvey (rowSurveyBinding )
42
+ val binding = RowSurveyBinding .inflate(LayoutInflater .from(parent. context), parent, false )
43
+ return ViewHolderSurvey (binding )
36
44
}
37
45
38
46
override fun onBindViewHolder (holder : ViewHolderSurvey , position : Int ) {
39
- user = UserProfileDbHandler (context).userModel
40
- rowSurveyBinding.tvTitle.text = examList[position].name
41
- rowSurveyBinding.startSurvey.setOnClickListener {
42
- AdapterMySubmission .openSurvey(listener, examList[position].id, false )
43
- }
44
- val questions: List <RealmExamQuestion > = mRealm.where(RealmExamQuestion ::class .java).equalTo(" examId" , examList[position].id).findAll()
45
- if (questions.isEmpty()) {
46
- rowSurveyBinding.sendSurvey.visibility = View .GONE
47
- rowSurveyBinding.startSurvey.visibility = View .GONE
48
- }
49
- rowSurveyBinding.startSurvey.text = if (examList[position].isFromNation) context.getString(R .string.take_survey) else context.getString(
50
- R .string.record_survey
51
- )
52
- if (user?.id?.startsWith(" guest" ) == true ) {
53
- rowSurveyBinding.startSurvey.visibility = View .GONE
54
- }
55
- val noOfSubmission = getNoOfSubmissionByUser(examList[position].id, userId, mRealm)
56
- val subDate = getRecentSubmissionDate(examList[position].id, userId, mRealm)
57
- val createdDate = RealmStepExam .getSurveyCreationTime(examList[position].id!! , mRealm)
58
- rowSurveyBinding.tvNoSubmissions.text = noOfSubmission
59
- rowSurveyBinding.tvDateCompleted.text = subDate
60
- rowSurveyBinding.tvDate.text = formatDate(createdDate!! , " MMM dd, yyyy" )
47
+ holder.bind(examList[position])
61
48
}
62
49
63
- override fun getItemCount (): Int {
64
- return examList.size
65
- }
50
+ override fun getItemCount (): Int = examList.size
66
51
67
- inner class ViewHolderSurvey (rowSurveyBinding : RowSurveyBinding ) : RecyclerView.ViewHolder(rowSurveyBinding .root) {
52
+ inner class ViewHolderSurvey (private val binding : RowSurveyBinding ) : RecyclerView.ViewHolder(binding .root) {
68
53
init {
69
- rowSurveyBinding .startSurvey.visibility = View .VISIBLE
70
- rowSurveyBinding .sendSurvey.visibility = View .GONE
71
- rowSurveyBinding .sendSurvey.setOnClickListener {
54
+ binding .startSurvey.visibility = View .VISIBLE
55
+ binding .sendSurvey.visibility = View .GONE
56
+ binding .sendSurvey.setOnClickListener {
72
57
val current = examList[bindingAdapterPosition]
73
58
listener?.sendSurvey(current)
74
59
}
75
60
}
61
+
62
+ fun bind (exam : RealmStepExam ) {
63
+ binding.apply {
64
+ tvTitle.text = exam.name
65
+ startSurvey.setOnClickListener {
66
+ AdapterMySubmission .openSurvey(listener, exam.id, false )
67
+ }
68
+
69
+ val questions = mRealm.where(RealmExamQuestion ::class .java)
70
+ .equalTo(" examId" , exam.id)
71
+ .findAll()
72
+
73
+ if (questions.isEmpty()) {
74
+ sendSurvey.visibility = View .GONE
75
+ startSurvey.visibility = View .GONE
76
+ }
77
+
78
+ startSurvey.text = if (exam.isFromNation) {
79
+ context.getString(R .string.take_survey)
80
+ } else {
81
+ context.getString(R .string.record_survey)
82
+ }
83
+
84
+ if (user?.id?.startsWith(" guest" ) == true ) {
85
+ startSurvey.visibility = View .GONE
86
+ }
87
+
88
+ tvNoSubmissions.text = getNoOfSubmissionByUser(exam.id, userId, mRealm)
89
+ tvDateCompleted.text = getRecentSubmissionDate(exam.id, userId, mRealm)
90
+ tvDate.text = formatDate(RealmStepExam .getSurveyCreationTime(exam.id!! , mRealm)!! , " MMM dd, yyyy" )
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ class SurveyDiffCallback (private val oldList : List <RealmStepExam >, private val newList : List <RealmStepExam >) : DiffUtil.Callback() {
97
+ override fun getOldListSize (): Int = oldList.size
98
+ override fun getNewListSize (): Int = newList.size
99
+ override fun areItemsTheSame (oldItemPosition : Int , newItemPosition : Int ): Boolean {
100
+ return oldList[oldItemPosition].id == newList[newItemPosition].id
101
+ }
102
+ override fun areContentsTheSame (oldItemPosition : Int , newItemPosition : Int ): Boolean {
103
+ return oldList[oldItemPosition] == newList[newItemPosition]
76
104
}
77
105
}
0 commit comments