Skip to content

survey: smoother pending survey list (fixes #5731) #5735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class BellDashboardFragment : BaseDashboardFragment() {
private var networkStatusJob: Job? = null
private val viewModel: BellDashboardViewModel by viewModels()
var user: RealmUserModel? = null
data class SurveyInfo(val id: String, val title: String)

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
fragmentHomeBellBinding = FragmentHomeBellBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -132,15 +133,19 @@ class BellDashboardFragment : BaseDashboardFragment() {

private fun checkPendingSurveys() {
val pendingSurveys = getPendingSurveys(user?.id, mRealm)

if (pendingSurveys.isNotEmpty()) {
val surveyTitles = getSurveyTitlesFromSubmissions(pendingSurveys, mRealm)
val uniqueSurveys = pendingSurveys.groupBy {
submission -> submission.parentId?.split("@")?.firstOrNull() ?: ""
}

val surveyInfoList = getUniqueSurveyInfo(uniqueSurveys.keys.toList(), mRealm)

val dialogView = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_survey_list, null)
val recyclerView: RecyclerView = dialogView.findViewById(R.id.recyclerViewSurveys)
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
val alertDialog = AlertDialog.Builder(requireActivity(), R.style.AlertDialogTheme)
.setTitle(getString(R.string.surveys_to_complete, pendingSurveys.size, if (pendingSurveys.size > 1) "surveys" else "survey"))
.setTitle(getString(R.string.surveys_to_complete,
surveyInfoList.size, if (surveyInfoList.size > 1) "surveys" else "survey"))
.setView(dialogView)
.setPositiveButton(getString(R.string.ok)) { dialog, _ ->
homeItemClickListener?.openCallFragment(MySubmissionFragment.newInstance("survey"))
Expand All @@ -151,17 +156,40 @@ class BellDashboardFragment : BaseDashboardFragment() {
}
.create()

val adapter = SurveyAdapter(surveyTitles, { position ->
val selectedSurvey = pendingSurveys[position].id
AdapterMySubmission.openSurvey(homeItemClickListener, selectedSurvey, true, false, "")
}, alertDialog)
val adapter = SurveyAdapter(
surveyInfoList.map { it.title },
{ position ->
val selectedSurveyId = surveyInfoList[position].id
val selectedSubmission = pendingSurveys.find { submission ->
submission.parentId?.split("@")?.firstOrNull() == selectedSurveyId
}

selectedSubmission?.let { submission ->
AdapterMySubmission.openSurvey(homeItemClickListener, submission.id, true, false, "")
}
},
alertDialog
)

recyclerView.adapter = adapter
alertDialog.show()
alertDialog.window?.setBackgroundDrawableResource(R.color.card_bg)
}
}

private fun getUniqueSurveyInfo(examIds: List<String>, realm: Realm): List<SurveyInfo> {
val surveyInfo = mutableListOf<SurveyInfo>()
examIds.forEach { examId ->
val exam = realm.where(RealmStepExam::class.java)
.equalTo("id", examId)
.findFirst()
exam?.name?.let {
surveyInfo.add(SurveyInfo(examId, it))
}
}
return surveyInfo
}

private fun getPendingSurveys(userId: String?, realm: Realm): List<RealmSubmission> {
return realm.where(RealmSubmission::class.java)
.equalTo("userId", userId)
Expand Down