Skip to content

Commit

Permalink
Fixed Counter
Browse files Browse the repository at this point in the history
  • Loading branch information
AIDEA775 committed Nov 8, 2018
1 parent 515e704 commit 01b1e53
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> newInstance(factory: () -> T, fragment: Fragment, code: Int, user: User) : T {
val args = Bundle()
args.putSerializable("user", user)
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/java/com/uncmorfi/counter/CounterFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -144,20 +144,24 @@ 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<PieEntry>()
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())

counterPieChart.centerText = generatePieChartText(total, percent)
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")

Expand All @@ -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<ILineDataSet>()
val minutes: Double

mEstimateList.add(Entry(time.toFloat(), windows.toFloat()))

Expand All @@ -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, "")
Expand All @@ -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<Entry>?) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_counter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<Button
Expand Down

0 comments on commit 01b1e53

Please sign in to comment.