Skip to content

Commit

Permalink
remap speed in list
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr-ru committed May 7, 2023
1 parent 35d4b16 commit 58dd26a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
17 changes: 17 additions & 0 deletions app/src/main/java/ru/aleksandr/dccppthrottle/Utility.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2023. Aleksandr.ru
* @link http://aleksandr.ru
*
* If you're using this code, please keep above information.
*/

package ru.aleksandr.dccppthrottle

object Utility {
// https://stackoverflow.com/a/929107
fun remap(value: Float, oldMin: Float, oldMax: Float, newMin: Float, newMax: Float) : Float {
val oldRange = (oldMax - oldMin)
val newRange = (newMax - newMin)
return (((value - oldMin) * newRange) / oldRange) + newMin
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.widget.*
import androidx.fragment.app.Fragment
import ru.aleksandr.dccppthrottle.cs.CommandStation
import ru.aleksandr.dccppthrottle.R
import ru.aleksandr.dccppthrottle.Utility.remap
import ru.aleksandr.dccppthrottle.store.LocomotivesStore
import kotlin.math.ceil
import kotlin.math.roundToInt
Expand Down Expand Up @@ -141,13 +142,6 @@ class LocoCabFragment : Fragment() {
}
}

// https://stackoverflow.com/a/929107
private fun remap(value: Float, oldMin: Float, oldMax: Float, newMin: Float, newMax: Float) : Float {
val oldRange = (oldMax - oldMin)
val newRange = (newMax - newMin)
return (((value - oldMin) * newRange) / oldRange) + newMin
}

companion object {

const val ARG_SLOT = "slot"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import androidx.fragment.app.FragmentManager
import ru.aleksandr.dccppthrottle.cs.CommandStation
import ru.aleksandr.dccppthrottle.LocoCabActivity
import ru.aleksandr.dccppthrottle.R
import ru.aleksandr.dccppthrottle.Utility.remap
import ru.aleksandr.dccppthrottle.dialogs.LocomotiveDialog
import ru.aleksandr.dccppthrottle.store.LocomotivesStore
import ru.aleksandr.dccppthrottle.store.LocomotivesStore.LocomotiveState
import kotlin.math.roundToInt
import ru.aleksandr.dccppthrottle.databinding.FragmentLocoListItemBinding as FragmentLocoBinding

class LocoRecyclerViewAdapter(
Expand Down Expand Up @@ -61,8 +63,16 @@ class LocoRecyclerViewAdapter(
holder.slot.text = ""
}
holder.title.text = item.toString()
holder.progress.progress = item.speed
holder.address.text = holder.itemView.context.getString(R.string.dcc_addr, item.address)
holder.progress.progress =
if (item.speed == 0) 0
else remap(
item.speed.toFloat(),
item.minSpeed.toFloat(),
item.maxSpeed.toFloat(),
1F,
100F
).roundToInt()

if (item.speed > 0) {
if (item.reverse) {
Expand Down
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sun May 07 13:41:47 MSK 2023
VERSION_CODE=891
#Sun May 07 14:32:08 MSK 2023
VERSION_CODE=894

0 comments on commit 58dd26a

Please sign in to comment.