From 7e0be475f8382db76f8b70e08b748c02d537cc1a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 24 Jan 2021 22:12:03 +0100 Subject: [PATCH] ObjectivesExamDialog -> jetpack --- .../activities/ObjectivesExamDialog.kt | 59 ++++++++++++------- .../res/layout/objectives_exam_fragment.xml | 40 ++++++------- 2 files changed, 58 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt index e44135eb45..97da62bbac 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt @@ -6,6 +6,7 @@ import android.view.View import android.view.ViewGroup import dagger.android.support.DaggerDialogFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.databinding.ObjectivesExamFragmentBinding import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.constraints.objectives.events.EventObjectivesUpdateGui import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Objective @@ -15,28 +16,36 @@ import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.resources.ResourceHelper -import kotlinx.android.synthetic.main.objectives_exam_fragment.* import javax.inject.Inject class ObjectivesExamDialog : DaggerDialogFragment() { + @Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var dateUtil: DateUtil companion object { + var objective: Objective? = null } private var currentTask = 0 + private var _binding: ObjectivesExamFragmentBinding? = null + + // This property is only valid between onCreateView and + // onDestroyView. + private val binding get() = _binding!! + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { + savedInstanceState: Bundle?): View { // load data from bundle (savedInstanceState ?: arguments)?.let { bundle -> currentTask = bundle.getInt("currentTask", 0) } - return inflater.inflate(R.layout.objectives_exam_fragment, container, false) + _binding = ObjectivesExamFragmentBinding.inflate(inflater, container, false) + return binding.root } override fun onStart() { @@ -55,13 +64,21 @@ class ObjectivesExamDialog : DaggerDialogFragment() { bundle.putInt("currentTask", currentTask) } + @Synchronized + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + + @Synchronized fun updateGui() { + if (_binding == null) return objective?.let { objective -> val task: ExamTask = objective.tasks[currentTask] as ExamTask - objectives_exam_name.setText(task.task) - objectives_exam_question.setText(task.question) + binding.examName.setText(task.task) + binding.examQuestion.setText(task.question) // Options - objectives_exam_options.removeAllViews() + binding.examOptions.removeAllViews() task.options.forEach { val cb = it.generate(context) if (task.answered) { @@ -69,19 +86,19 @@ class ObjectivesExamDialog : DaggerDialogFragment() { if (it.isCorrect) cb.isChecked = true } - objectives_exam_options.addView(cb) + binding.examOptions.addView(cb) } // Hints - objectives_exam_hints.removeAllViews() + binding.examHints.removeAllViews() for (h in task.hints) { - objectives_exam_hints.addView(h.generate(context)) + binding.examHints.addView(h.generate(context)) } // Disabled to - objectives_exam_disabledto.text = resourceHelper.gs(R.string.answerdisabledto, dateUtil.timeString(task.disabledTo)) - objectives_exam_disabledto.visibility = if (task.isEnabledAnswer) View.GONE else View.VISIBLE + binding.examDisabledto.text = resourceHelper.gs(R.string.answerdisabledto, dateUtil.timeString(task.disabledTo)) + binding.examDisabledto.visibility = if (task.isEnabledAnswer) View.GONE else View.VISIBLE // Buttons - objectives_exam_verify.isEnabled = !task.answered && task.isEnabledAnswer - objectives_exam_verify.setOnClickListener { + binding.examVerify.isEnabled = !task.answered && task.isEnabledAnswer + binding.examVerify.setOnClickListener { var result = true for (o in task.options) { val option: Option = o as Option @@ -95,26 +112,26 @@ class ObjectivesExamDialog : DaggerDialogFragment() { updateGui() rxBus.send(EventObjectivesUpdateGui()) } - close.setOnClickListener { dismiss() } - objectives_exam_reset.setOnClickListener { + binding.close.setOnClickListener { dismiss() } + binding.examReset.setOnClickListener { task.answered = false //task.disabledTo = 0 updateGui() rxBus.send(EventObjectivesUpdateGui()) } - objectives_back_button.isEnabled = currentTask != 0 - objectives_back_button.setOnClickListener { + binding.backButton.isEnabled = currentTask != 0 + binding.backButton.setOnClickListener { currentTask-- updateGui() } - objectives_next_button.isEnabled = currentTask != objective.tasks.size - 1 - objectives_next_button.setOnClickListener { + binding.nextButton.isEnabled = currentTask != objective.tasks.size - 1 + binding.nextButton.setOnClickListener { currentTask++ updateGui() } - objectives_next_unanswered_button.isEnabled = !objective.isCompleted - objectives_next_unanswered_button.setOnClickListener { + binding.nextUnansweredButton.isEnabled = !objective.isCompleted + binding.nextUnansweredButton.setOnClickListener { for (i in (currentTask + 1) until objective.tasks.size) { if (!objective.tasks[i].isCompleted) { currentTask = i diff --git a/app/src/main/res/layout/objectives_exam_fragment.xml b/app/src/main/res/layout/objectives_exam_fragment.xml index 2fe02877bd..1a8f2f84e4 100644 --- a/app/src/main/res/layout/objectives_exam_fragment.xml +++ b/app/src/main/res/layout/objectives_exam_fragment.xml @@ -12,38 +12,38 @@ android:layout_margin="10dp"> + app:layout_constraintTop_toBottomOf="@+id/exam_disabledto">