From 01b1e53baece831d0b88eea82c1d599c8d4b895b Mon Sep 17 00:00:00 2001 From: Alejandro Ismael Silva Date: Thu, 8 Nov 2018 19:05:08 -0300 Subject: [PATCH] Fixed Counter --- .../balance/dialogs/BaseDialogHelper.kt | 5 ++++ .../com/uncmorfi/counter/CounterFragment.kt | 25 +++++++++++-------- app/src/main/res/layout/fragment_counter.xml | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/uncmorfi/balance/dialogs/BaseDialogHelper.kt b/app/src/main/java/com/uncmorfi/balance/dialogs/BaseDialogHelper.kt index 2bf7a18..1474a8f 100644 --- a/app/src/main/java/com/uncmorfi/balance/dialogs/BaseDialogHelper.kt +++ b/app/src/main/java/com/uncmorfi/balance/dialogs/BaseDialogHelper.kt @@ -30,6 +30,11 @@ open class BaseDialogHelper : AppCompatDialogFragment() { companion object { const val ARG_USER : String = "user" + /* + * Esta función me llevó toda una noche, y al despertar no recordé como funciona xD + * La intención era modularizar el código de arriba que se repetía en dos dialogos. + * Y la idea es devolver una instancia de la clase hija con los parametros asignados. + */ fun newInstance(factory: () -> T, fragment: Fragment, code: Int, user: User) : T { val args = Bundle() args.putSerializable("user", user) diff --git a/app/src/main/java/com/uncmorfi/counter/CounterFragment.kt b/app/src/main/java/com/uncmorfi/counter/CounterFragment.kt index df8a11c..503e67d 100644 --- a/app/src/main/java/com/uncmorfi/counter/CounterFragment.kt +++ b/app/src/main/java/com/uncmorfi/counter/CounterFragment.kt @@ -107,7 +107,7 @@ class CounterFragment : Fragment(), SeekBar.OnSeekBarChangeListener { override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { if (fromUser) { - val windows = progress + 5 // Mínimo 5 ventanas + val windows = progress + 3 // Mínimo 3 ventanas counterDistance.text = String.format(getString(R.string.counter_distance), windows) } } @@ -144,11 +144,12 @@ class CounterFragment : Fragment(), SeekBar.OnSeekBarChangeListener { for (entry in result) total += entry.y.toInt() - val percent = (total/2000f)*100f + val percent = (total.toFloat()/FOOD_RATIONS)*100f + val percentPie = minOf(percent, 100f) val entries = ArrayList() - entries.add(PieEntry(percent, "Total")) - entries.add(PieEntry(100f - percent, "Empty")) + entries.add(PieEntry(percentPie, "Total")) + entries.add(PieEntry(100f - percentPie, "Empty")) val dataSet = PieDataSet(entries, "").style(requireContext()) @@ -156,8 +157,11 @@ class CounterFragment : Fragment(), SeekBar.OnSeekBarChangeListener { counterPieChart.update(dataSet) } + /* + * Perdón, esta función es horrible. Pero funciona. + */ private fun generatePieChartText(total: Int, percent: Float) : SpannableString { - val pText = "$percent%" + val pText = "%.2f%%".format(percent) val tText = String.format(getString(R.string.counter_rations_title), total, FOOD_RATIONS) val s = SpannableString("$pText\n$tText") @@ -173,13 +177,12 @@ class CounterFragment : Fragment(), SeekBar.OnSeekBarChangeListener { } /* - * Perdón, esta función es horrible de entender. Pero funciona. + * Perdón, esta función tambien horrible de entender. */ private fun updateEstimate() { - val windows = counterSeek.progress + 5 // Mínimo 5 ventanas + val windows = counterSeek.progress + 3 // Mínimo 3 ventanas val time = Date().clearDate() val dataSets = ArrayList() - val minutes: Double mEstimateList.add(Entry(time.toFloat(), windows.toFloat())) @@ -193,13 +196,11 @@ class CounterFragment : Fragment(), SeekBar.OnSeekBarChangeListener { root = mEstimateFirst + getEstimateFromPosition(windows) * 60 estimateLine.add(Entry(mEstimateFirst.toFloat(), windows.toFloat())) estimateLine.add(Entry(root.toFloat(), 0f)) - minutes = (root - time) / 60 } else { // Estimación con 2 o más datos estimateLine.add(Entry(mEstimateFirst.toFloat(), mSimpleRegression.predict(mEstimateFirst).toFloat())) estimateLine.add(Entry(root.toFloat(), 0f)) - minutes = (root - time) / 60 } val lineSet = LineDataSet(estimateLine, "") @@ -218,7 +219,9 @@ class CounterFragment : Fragment(), SeekBar.OnSeekBarChangeListener { val timeStamp = Date(root.toLong() * 1000) val text = timeStamp.toString("HH:mm") - counterEstimateText.text = String.format(getString(R.string.counter_estimate), minutes.roundToInt(), text) + val minutes = (root - time) / 60 + counterEstimateText.text = getString(R.string.counter_estimate) + .format(minutes.roundToInt(), text) } private fun updateCharts(data: List?) { diff --git a/app/src/main/res/layout/fragment_counter.xml b/app/src/main/res/layout/fragment_counter.xml index 108ee6e..57cca1e 100644 --- a/app/src/main/res/layout/fragment_counter.xml +++ b/app/src/main/res/layout/fragment_counter.xml @@ -84,7 +84,7 @@ style="@style/Widget.AppCompat.SeekBar" android:layout_width="match_parent" android:layout_height="wrap_content" - android:max="25" + android:max="27" android:progress="1" />