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 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

View file

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