ObjectivesFragment -> jetpack

This commit is contained in:
Milos Kozak 2021-01-24 16:16:07 +01:00
parent fcf67cd014
commit 55da01d4a9
3 changed files with 108 additions and 112 deletions

View file

@ -9,8 +9,6 @@ import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
@ -19,11 +17,13 @@ import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.databinding.ObjectivesFragmentBinding
import info.nightscout.androidaps.databinding.ObjectivesItemBinding
import info.nightscout.androidaps.dialogs.NtpProgressDialog
import info.nightscout.androidaps.events.EventNtpStatus
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.constraints.objectives.activities.ObjectivesExamDialog
import info.nightscout.androidaps.dialogs.NtpProgressDialog
import info.nightscout.androidaps.events.EventNtpStatus
import info.nightscout.androidaps.plugins.constraints.objectives.events.EventObjectivesUpdateGui
import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Objective.ExamTask
import info.nightscout.androidaps.receivers.ReceiverStatusStore
@ -31,17 +31,17 @@ import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.SntpClient
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.extensions.plusAssign
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.objectives_fragment.*
import javax.inject.Inject
class ObjectivesFragment : DaggerFragment() {
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var sp: SP
@ -64,19 +64,26 @@ class ObjectivesFragment : DaggerFragment() {
}
}
private var _binding: ObjectivesFragmentBinding? = 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? {
return inflater.inflate(R.layout.objectives_fragment, container, false)
savedInstanceState: Bundle?): View {
_binding = ObjectivesFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
objectives_recyclerview.layoutManager = LinearLayoutManager(view.context)
objectives_recyclerview.adapter = objectivesAdapter
objectives_fake.setOnClickListener { updateGUI() }
objectives_reset.setOnClickListener {
binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
binding.recyclerview.adapter = objectivesAdapter
binding.fake.setOnClickListener { updateGUI() }
binding.reset.setOnClickListener {
objectivesPlugin.reset()
objectives_recyclerview.adapter?.notifyDataSetChanged()
binding.recyclerview.adapter?.notifyDataSetChanged()
scrollToCurrentObjective()
}
scrollToCurrentObjective()
@ -90,7 +97,7 @@ class ObjectivesFragment : DaggerFragment() {
.toObservable(EventObjectivesUpdateGui::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
objectives_recyclerview.adapter?.notifyDataSetChanged()
binding.recyclerview.adapter?.notifyDataSetChanged()
}, { fabricPrivacy.logException(it) }
)
}
@ -105,6 +112,7 @@ class ObjectivesFragment : DaggerFragment() {
override fun onDestroyView() {
super.onDestroyView()
handler.removeCallbacks(objectiveUpdater)
_binding = null
}
private fun startUpdateTimer() {
@ -129,7 +137,7 @@ class ObjectivesFragment : DaggerFragment() {
override fun calculateTimeForScrolling(dx: Int): Int = super.calculateTimeForScrolling(dx) * 4
}
smoothScroller.targetPosition = i
objectives_recyclerview.layoutManager?.startSmoothScroll(smoothScroller)
binding.recyclerview.layoutManager?.startSmoothScroll(smoothScroller)
}
break
}
@ -145,67 +153,66 @@ class ObjectivesFragment : DaggerFragment() {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val objective = objectivesPlugin.objectives[position]
holder.title.text = resourceHelper.gs(R.string.nth_objective, position + 1)
holder.binding.title.text = resourceHelper.gs(R.string.nth_objective, position + 1)
if (objective.objective != 0) {
holder.objective.visibility = View.VISIBLE
holder.objective.text = resourceHelper.gs(objective.objective)
holder.binding.objective.visibility = View.VISIBLE
holder.binding.objective.text = resourceHelper.gs(objective.objective)
} else
holder.objective.visibility = View.GONE
holder.binding.objective.visibility = View.GONE
if (objective.gate != 0) {
holder.gate.visibility = View.VISIBLE
holder.gate.text = resourceHelper.gs(objective.gate)
holder.binding.gate.visibility = View.VISIBLE
holder.binding.gate.text = resourceHelper.gs(objective.gate)
} else
holder.gate.visibility = View.GONE
holder.binding.gate.visibility = View.GONE
if (!objective.isStarted) {
holder.gate.setTextColor(-0x1)
holder.verify.visibility = View.GONE
holder.progress.visibility = View.GONE
holder.accomplished.visibility = View.GONE
holder.unFinish.visibility = View.GONE
holder.unStart.visibility = View.GONE
holder.binding.gate.setTextColor(-0x1)
holder.binding.verify.visibility = View.GONE
holder.binding.progress.visibility = View.GONE
holder.binding.accomplished.visibility = View.GONE
holder.binding.unfinish.visibility = View.GONE
holder.binding.unstart.visibility = View.GONE
if (position == 0 || objectivesPlugin.allPriorAccomplished(position))
holder.start.visibility = View.VISIBLE
holder.binding.start.visibility = View.VISIBLE
else
holder.start.visibility = View.GONE
holder.binding.start.visibility = View.GONE
} else if (objective.isAccomplished) {
holder.gate.setTextColor(-0xb350b0)
holder.verify.visibility = View.GONE
holder.progress.visibility = View.GONE
holder.start.visibility = View.GONE
holder.accomplished.visibility = View.VISIBLE
holder.unFinish.visibility = View.VISIBLE
holder.unStart.visibility = View.GONE
holder.binding.gate.setTextColor(-0xb350b0)
holder.binding.verify.visibility = View.GONE
holder.binding.progress.visibility = View.GONE
holder.binding.start.visibility = View.GONE
holder.binding.accomplished.visibility = View.VISIBLE
holder.binding.unfinish.visibility = View.VISIBLE
holder.binding.unstart.visibility = View.GONE
} else if (objective.isStarted) {
holder.gate.setTextColor(-0x1)
holder.verify.visibility = View.VISIBLE
holder.verify.isEnabled = objective.isCompleted || objectives_fake.isChecked
holder.start.visibility = View.GONE
holder.accomplished.visibility = View.GONE
holder.unFinish.visibility = View.GONE
holder.unStart.visibility = View.VISIBLE
holder.progress.visibility = View.VISIBLE
holder.progress.removeAllViews()
holder.binding.gate.setTextColor(-0x1)
holder.binding.verify.visibility = View.VISIBLE
holder.binding.verify.isEnabled = objective.isCompleted || binding.fake.isChecked
holder.binding.start.visibility = View.GONE
holder.binding.accomplished.visibility = View.GONE
holder.binding.unfinish.visibility = View.GONE
holder.binding.unstart.visibility = View.VISIBLE
holder.binding.progress.visibility = View.VISIBLE
holder.binding.progress.removeAllViews()
for (task in objective.tasks) {
if (task.shouldBeIgnored()) continue
// name
val name = TextView(holder.progress.context)
@Suppress("SetTextlI8n")
val name = TextView(holder.binding.progress.context)
name.text = resourceHelper.gs(task.task) + ":"
name.setTextColor(-0x1)
holder.progress.addView(name, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
holder.binding.progress.addView(name, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
// hint
task.hints.forEach { h ->
if (!task.isCompleted)
holder.progress.addView(h.generate(context))
holder.binding.progress.addView(h.generate(context))
}
// state
val state = TextView(holder.progress.context)
val state = TextView(holder.binding.progress.context)
state.setTextColor(-0x1)
val basicHTML = "<font color=\"%1\$s\"><b>%2\$s</b></font>"
val formattedHTML = String.format(basicHTML, if (task.isCompleted) "#4CAF50" else "#FF9800", task.progress)
state.text = HtmlHelper.fromHtml(formattedHTML)
state.gravity = Gravity.END
holder.progress.addView(state, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
holder.binding.progress.addView(state, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
if (task is ExamTask) {
state.setOnClickListener {
val dialog = ObjectivesExamDialog()
@ -218,16 +225,16 @@ class ObjectivesFragment : DaggerFragment() {
}
}
// horizontal line
val separator = View(holder.progress.context)
val separator = View(holder.binding.progress.context)
separator.setBackgroundColor(Color.DKGRAY)
holder.progress.addView(separator, LinearLayout.LayoutParams.MATCH_PARENT, 2)
holder.binding.progress.addView(separator, LinearLayout.LayoutParams.MATCH_PARENT, 2)
}
}
holder.accomplished.text = resourceHelper.gs(R.string.accomplished, dateUtil.dateAndTimeString(objective.accomplishedOn))
holder.accomplished.setTextColor(-0x3e3e3f)
holder.verify.setOnClickListener {
holder.binding.accomplished.text = resourceHelper.gs(R.string.accomplished, dateUtil.dateAndTimeString(objective.accomplishedOn))
holder.binding.accomplished.setTextColor(-0x3e3e3f)
holder.binding.verify.setOnClickListener {
receiverStatusStore.updateNetworkStatus()
if (objectives_fake.isChecked) {
if (binding.fake.isChecked) {
objective.accomplishedOn = DateUtil.now()
scrollToCurrentObjective()
startUpdateTimer()
@ -264,9 +271,9 @@ class ObjectivesFragment : DaggerFragment() {
}.start()
}
}
holder.start.setOnClickListener {
holder.binding.start.setOnClickListener {
receiverStatusStore.updateNetworkStatus()
if (objectives_fake.isChecked) {
if (binding.fake.isChecked) {
objective.startedOn = DateUtil.now()
scrollToCurrentObjective()
startUpdateTimer()
@ -298,7 +305,7 @@ class ObjectivesFragment : DaggerFragment() {
}, receiverStatusStore.isConnected)
}.start()
}
holder.unStart.setOnClickListener {
holder.binding.unstart.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.objectives), resourceHelper.gs(R.string.doyouwantresetstart), Runnable {
objective.startedOn = 0
@ -308,7 +315,7 @@ class ObjectivesFragment : DaggerFragment() {
})
}
}
holder.unFinish.setOnClickListener {
holder.binding.unfinish.setOnClickListener {
objective.accomplishedOn = 0
scrollToCurrentObjective()
rxBus.send(EventObjectivesUpdateGui())
@ -318,21 +325,21 @@ class ObjectivesFragment : DaggerFragment() {
// generate random request code if none exists
val request = sp.getString(R.string.key_objectives_request_code, String.format("%1$05d", (Math.random() * 99999).toInt()))
sp.putString(R.string.key_objectives_request_code, request)
holder.requestCode.text = resourceHelper.gs(R.string.requestcode, request)
holder.requestCode.visibility = View.VISIBLE
holder.enterButton.visibility = View.VISIBLE
holder.input.visibility = View.VISIBLE
holder.inputHint.visibility = View.VISIBLE
holder.enterButton.setOnClickListener {
val input = holder.input.text.toString()
holder.binding.requestcode.text = resourceHelper.gs(R.string.requestcode, request)
holder.binding.requestcode.visibility = View.VISIBLE
holder.binding.enterbutton.visibility = View.VISIBLE
holder.binding.input.visibility = View.VISIBLE
holder.binding.inputhint.visibility = View.VISIBLE
holder.binding.enterbutton.setOnClickListener {
val input = holder.binding.input.text.toString()
objective.specialAction(activity, input)
rxBus.send(EventObjectivesUpdateGui())
}
} else {
holder.enterButton.visibility = View.GONE
holder.input.visibility = View.GONE
holder.inputHint.visibility = View.GONE
holder.requestCode.visibility = View.GONE
holder.binding.enterbutton.visibility = View.GONE
holder.binding.input.visibility = View.GONE
holder.binding.inputhint.visibility = View.GONE
holder.binding.requestcode.visibility = View.GONE
}
}
@ -340,20 +347,9 @@ class ObjectivesFragment : DaggerFragment() {
return objectivesPlugin.objectives.size
}
inner class ViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
val title: TextView = itemView.findViewById(R.id.objective_title)
val objective: TextView = itemView.findViewById(R.id.objective_objective)
val gate: TextView = itemView.findViewById(R.id.objective_gate)
val accomplished: TextView = itemView.findViewById(R.id.objective_accomplished)
val progress: LinearLayout = itemView.findViewById(R.id.objective_progress)
val verify: Button = itemView.findViewById(R.id.objective_verify)
val start: Button = itemView.findViewById(R.id.objective_start)
val unFinish: Button = itemView.findViewById(R.id.objective_unfinish)
val unStart: Button = itemView.findViewById(R.id.objective_unstart)
val inputHint: TextView = itemView.findViewById(R.id.objective_inputhint)
val input: EditText = itemView.findViewById(R.id.objective_input)
val enterButton: Button = itemView.findViewById(R.id.objective_enterbutton)
val requestCode: TextView = itemView.findViewById(R.id.objective_requestcode)
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val binding = ObjectivesItemBinding.bind(itemView)
}
}

View file

@ -16,7 +16,7 @@
android:visibility="gone">
<CheckBox
android:id="@+id/objectives_fake"
android:id="@+id/fake"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
@ -24,7 +24,7 @@
android:text="Enable fake time and progress" />
<Button
android:id="@+id/objectives_reset"
android:id="@+id/reset"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -32,7 +32,7 @@
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/objectives_recyclerview"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"

View file

@ -6,8 +6,8 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="2dp"
app:cardUseCompatPadding="true"
@ -19,7 +19,7 @@
android:orientation="vertical">
<TextView
android:id="@+id/objective_title"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
@ -28,7 +28,7 @@
tools:text="1. Title" />
<TextView
android:id="@+id/objective_objective"
android:id="@+id/objective"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
@ -36,7 +36,7 @@
tools:text="Objective" />
<TextView
android:id="@+id/objective_gate"
android:id="@+id/gate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
@ -45,50 +45,50 @@
tools:text="Gate" />
<LinearLayout
android:id="@+id/objective_progress"
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="vertical" >
android:orientation="vertical">
</LinearLayout>
<Button
android:id="@+id/objective_verify"
android:id="@+id/verify"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/objectives_button_verify" />
<Button
android:id="@+id/objective_start"
android:id="@+id/start"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/objectives_button_start" />
<Button
android:id="@+id/objective_unfinish"
android:id="@+id/unfinish"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/objectives_button_unfinish" />
<Button
android:id="@+id/objective_unstart"
android:id="@+id/unstart"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/objectives_button_unstart" />
<TextView
android:id="@+id/objective_inputhint"
android:id="@+id/inputhint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/enter_code_obtained_from_developers_to_bypass_the_rest_of_objectives" />
<TextView
android:id="@+id/objective_requestcode"
android:id="@+id/requestcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Request code: XXXXX" />
@ -96,10 +96,10 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
android:orientation="horizontal">
<EditText
android:id="@+id/objective_input"
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
@ -108,7 +108,7 @@
android:inputType="text" />
<Button
android:id="@+id/objective_enterbutton"
android:id="@+id/enterbutton"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -117,7 +117,7 @@
</LinearLayout>
<TextView
android:id="@+id/objective_accomplished"
android:id="@+id/accomplished"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"