ObjectivesExamDialog -> jetpack

This commit is contained in:
Milos Kozak 2021-01-24 22:12:03 +01:00
parent 55da01d4a9
commit 7e0be475f8
2 changed files with 58 additions and 41 deletions

View file

@ -6,6 +6,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import dagger.android.support.DaggerDialogFragment import dagger.android.support.DaggerDialogFragment
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.databinding.ObjectivesExamFragmentBinding
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.constraints.objectives.events.EventObjectivesUpdateGui import info.nightscout.androidaps.plugins.constraints.objectives.events.EventObjectivesUpdateGui
import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Objective 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.T
import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import kotlinx.android.synthetic.main.objectives_exam_fragment.*
import javax.inject.Inject import javax.inject.Inject
class ObjectivesExamDialog : DaggerDialogFragment() { class ObjectivesExamDialog : DaggerDialogFragment() {
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
companion object { companion object {
var objective: Objective? = null var objective: Objective? = null
} }
private var currentTask = 0 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?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? { savedInstanceState: Bundle?): View {
// load data from bundle // load data from bundle
(savedInstanceState ?: arguments)?.let { bundle -> (savedInstanceState ?: arguments)?.let { bundle ->
currentTask = bundle.getInt("currentTask", 0) 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() { override fun onStart() {
@ -55,13 +64,21 @@ class ObjectivesExamDialog : DaggerDialogFragment() {
bundle.putInt("currentTask", currentTask) bundle.putInt("currentTask", currentTask)
} }
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
@Synchronized
fun updateGui() { fun updateGui() {
if (_binding == null) return
objective?.let { objective -> objective?.let { objective ->
val task: ExamTask = objective.tasks[currentTask] as ExamTask val task: ExamTask = objective.tasks[currentTask] as ExamTask
objectives_exam_name.setText(task.task) binding.examName.setText(task.task)
objectives_exam_question.setText(task.question) binding.examQuestion.setText(task.question)
// Options // Options
objectives_exam_options.removeAllViews() binding.examOptions.removeAllViews()
task.options.forEach { task.options.forEach {
val cb = it.generate(context) val cb = it.generate(context)
if (task.answered) { if (task.answered) {
@ -69,19 +86,19 @@ class ObjectivesExamDialog : DaggerDialogFragment() {
if (it.isCorrect) if (it.isCorrect)
cb.isChecked = true cb.isChecked = true
} }
objectives_exam_options.addView(cb) binding.examOptions.addView(cb)
} }
// Hints // Hints
objectives_exam_hints.removeAllViews() binding.examHints.removeAllViews()
for (h in task.hints) { for (h in task.hints) {
objectives_exam_hints.addView(h.generate(context)) binding.examHints.addView(h.generate(context))
} }
// Disabled to // Disabled to
objectives_exam_disabledto.text = resourceHelper.gs(R.string.answerdisabledto, dateUtil.timeString(task.disabledTo)) binding.examDisabledto.text = resourceHelper.gs(R.string.answerdisabledto, dateUtil.timeString(task.disabledTo))
objectives_exam_disabledto.visibility = if (task.isEnabledAnswer) View.GONE else View.VISIBLE binding.examDisabledto.visibility = if (task.isEnabledAnswer) View.GONE else View.VISIBLE
// Buttons // Buttons
objectives_exam_verify.isEnabled = !task.answered && task.isEnabledAnswer binding.examVerify.isEnabled = !task.answered && task.isEnabledAnswer
objectives_exam_verify.setOnClickListener { binding.examVerify.setOnClickListener {
var result = true var result = true
for (o in task.options) { for (o in task.options) {
val option: Option = o as Option val option: Option = o as Option
@ -95,26 +112,26 @@ class ObjectivesExamDialog : DaggerDialogFragment() {
updateGui() updateGui()
rxBus.send(EventObjectivesUpdateGui()) rxBus.send(EventObjectivesUpdateGui())
} }
close.setOnClickListener { dismiss() } binding.close.setOnClickListener { dismiss() }
objectives_exam_reset.setOnClickListener { binding.examReset.setOnClickListener {
task.answered = false task.answered = false
//task.disabledTo = 0 //task.disabledTo = 0
updateGui() updateGui()
rxBus.send(EventObjectivesUpdateGui()) rxBus.send(EventObjectivesUpdateGui())
} }
objectives_back_button.isEnabled = currentTask != 0 binding.backButton.isEnabled = currentTask != 0
objectives_back_button.setOnClickListener { binding.backButton.setOnClickListener {
currentTask-- currentTask--
updateGui() updateGui()
} }
objectives_next_button.isEnabled = currentTask != objective.tasks.size - 1 binding.nextButton.isEnabled = currentTask != objective.tasks.size - 1
objectives_next_button.setOnClickListener { binding.nextButton.setOnClickListener {
currentTask++ currentTask++
updateGui() updateGui()
} }
objectives_next_unanswered_button.isEnabled = !objective.isCompleted binding.nextUnansweredButton.isEnabled = !objective.isCompleted
objectives_next_unanswered_button.setOnClickListener { binding.nextUnansweredButton.setOnClickListener {
for (i in (currentTask + 1) until objective.tasks.size) { for (i in (currentTask + 1) until objective.tasks.size) {
if (!objective.tasks[i].isCompleted) { if (!objective.tasks[i].isCompleted) {
currentTask = i currentTask = i

View file

@ -12,38 +12,38 @@
android:layout_margin="10dp"> android:layout_margin="10dp">
<LinearLayout <LinearLayout
android:id="@+id/objectives_exam_options" android:id="@+id/exam_options"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/objectives_exam_hint" app:layout_constraintTop_toBottomOf="@+id/exam_hint"
tools:layout_editor_absoluteX="3dp"> tools:layout_editor_absoluteX="3dp">
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/objectives_exam_hints" android:id="@+id/exam_hints"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:layout_marginBottom="10dp" android:layout_marginBottom="10dp"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/objectives_exam_options" app:layout_constraintTop_toBottomOf="@+id/exam_options"
tools:layout_editor_absoluteX="3dp" /> tools:layout_editor_absoluteX="3dp" />
<LinearLayout <LinearLayout
android:id="@+id/objectives_buttons" android:id="@+id/buttons"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:background="#3C3C3C" android:background="#3C3C3C"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="4dp" android:padding="4dp"
app:layout_constraintTop_toBottomOf="@+id/objectives_exam_disabledto"> app:layout_constraintTop_toBottomOf="@+id/exam_disabledto">
<Button <Button
android:id="@+id/objectives_exam_reset" android:id="@+id/exam_reset"
style="@style/Widget.AppCompat.Button.Borderless.Colored" style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -63,7 +63,7 @@
android:text="@string/close" /> android:text="@string/close" />
<Button <Button
android:id="@+id/objectives_exam_verify" android:id="@+id/exam_verify"
style="@style/Widget.AppCompat.Button.Borderless.Colored" style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -72,26 +72,26 @@
</LinearLayout> </LinearLayout>
<TextView <TextView
android:id="@+id/objectives_exam_question" android:id="@+id/exam_question"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:text="Question" android:text="Question"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/objectives_exam_name" /> app:layout_constraintTop_toBottomOf="@id/exam_name" />
<TextView <TextView
android:id="@+id/objectives_exam_hint" android:id="@+id/exam_hint"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:text="@string/objectives_hint" android:text="@string/objectives_hint"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/objectives_exam_question" /> app:layout_constraintTop_toBottomOf="@id/exam_question" />
<TextView <TextView
android:id="@+id/objectives_exam_name" android:id="@+id/exam_name"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Name" android:text="Name"
@ -101,7 +101,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView <TextView
android:id="@+id/objectives_exam_disabledto" android:id="@+id/exam_disabledto"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
@ -109,10 +109,10 @@
android:textColor="#FF5722" android:textColor="#FF5722"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/objectives_exam_hints" /> app:layout_constraintTop_toBottomOf="@+id/exam_hints" />
<LinearLayout <LinearLayout
android:id="@+id/objectives_navigation" android:id="@+id/navigation"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="2dp" android:layout_marginTop="2dp"
@ -120,10 +120,10 @@
android:orientation="horizontal" android:orientation="horizontal"
android:padding="4dp" android:padding="4dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/objectives_buttons"> app:layout_constraintTop_toBottomOf="@+id/buttons">
<Button <Button
android:id="@+id/objectives_back_button" android:id="@+id/back_button"
style="@style/Widget.AppCompat.Button.Borderless.Colored" style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -136,14 +136,14 @@
android:text="" /> android:text="" />
<Button <Button
android:id="@+id/objectives_next_button" android:id="@+id/next_button"
style="@style/Widget.AppCompat.Button.Borderless.Colored" style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/next_button" /> android:text="@string/next_button" />
<Button <Button
android:id="@+id/objectives_next_unanswered_button" android:id="@+id/next_unanswered_button"
style="@style/Widget.AppCompat.Button.Borderless.Colored" style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"