mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-06-05 22:19:17 +02:00
show laps in a recyclerview
This commit is contained in:
@ -0,0 +1,60 @@
|
||||
package com.simplemobiletools.clock.adapters
|
||||
|
||||
import android.view.Menu
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.simplemobiletools.clock.R
|
||||
import com.simplemobiletools.clock.activities.SimpleActivity
|
||||
import com.simplemobiletools.clock.extensions.formatStopwatchTime
|
||||
import com.simplemobiletools.clock.models.Lap
|
||||
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import kotlinx.android.synthetic.main.item_lap.view.*
|
||||
import java.util.*
|
||||
|
||||
class StopwatchAdapter(activity: SimpleActivity, var laps: ArrayList<Lap>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) :
|
||||
MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
|
||||
|
||||
override fun getActionMenuId() = 0
|
||||
|
||||
override fun prepareActionMode(menu: Menu) {}
|
||||
|
||||
override fun prepareItemSelection(view: View) {}
|
||||
|
||||
override fun markItemSelection(select: Boolean, view: View?) {}
|
||||
|
||||
override fun actionItemPressed(id: Int) {}
|
||||
|
||||
override fun getSelectableItemCount() = laps.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_lap, parent)
|
||||
|
||||
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
|
||||
val lap = laps[position]
|
||||
val view = holder.bindView(lap, true) { itemView, layoutPosition ->
|
||||
setupView(itemView, lap)
|
||||
}
|
||||
bindViewHolder(holder, position, view)
|
||||
}
|
||||
|
||||
override fun getItemCount() = laps.size
|
||||
|
||||
fun updateItems(newItems: ArrayList<Lap>) {
|
||||
laps = newItems
|
||||
notifyDataSetChanged()
|
||||
finishActMode()
|
||||
}
|
||||
|
||||
private fun setupView(view: View, lap: Lap) {
|
||||
view.apply {
|
||||
lap_order.text = lap.id.toString()
|
||||
lap_order.setTextColor(textColor)
|
||||
|
||||
lap_lap_time.text = lap.lapTime.formatStopwatchTime(false)
|
||||
lap_lap_time.setTextColor(textColor)
|
||||
|
||||
lap_total_time.text = lap.totalTime.formatStopwatchTime(false)
|
||||
lap_total_time.setTextColor(textColor)
|
||||
}
|
||||
}
|
||||
}
|
@ -10,9 +10,11 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.simplemobiletools.clock.R
|
||||
import com.simplemobiletools.clock.activities.SimpleActivity
|
||||
import com.simplemobiletools.clock.adapters.StopwatchAdapter
|
||||
import com.simplemobiletools.clock.extensions.config
|
||||
import com.simplemobiletools.clock.extensions.formatStopwatchTime
|
||||
import com.simplemobiletools.clock.models.StopwatchTime
|
||||
import com.simplemobiletools.clock.models.Lap
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import kotlinx.android.synthetic.main.fragment_stopwatch.view.*
|
||||
|
||||
@ -25,9 +27,9 @@ class StopwatchFragment : Fragment() {
|
||||
private var totalTicks = 0
|
||||
private var currentTicks = 0 // ticks that reset at pause
|
||||
private var lapTicks = 0
|
||||
private var currentLap = 0
|
||||
private var currentLap = 1
|
||||
private var isRunning = false
|
||||
private var laps = ArrayList<StopwatchTime>()
|
||||
private var laps = ArrayList<Lap>()
|
||||
|
||||
lateinit var view: ViewGroup
|
||||
|
||||
@ -46,11 +48,16 @@ class StopwatchFragment : Fragment() {
|
||||
}
|
||||
|
||||
stopwatch_lap.setOnClickListener {
|
||||
val lap = StopwatchTime(currentLap++, lapTicks * UPDATE_INTERVAL, totalTicks * UPDATE_INTERVAL)
|
||||
laps.add(lap)
|
||||
val lap = Lap(currentLap++, lapTicks * UPDATE_INTERVAL, totalTicks * UPDATE_INTERVAL)
|
||||
laps.add(0, lap)
|
||||
lapTicks = 0
|
||||
(stopwatch_list.adapter as StopwatchAdapter).updateItems(laps)
|
||||
}
|
||||
|
||||
val stopwatchAdapter = StopwatchAdapter(activity as SimpleActivity, ArrayList(), stopwatch_list) { }
|
||||
stopwatch_list.adapter = stopwatchAdapter
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
@ -113,7 +120,7 @@ class StopwatchFragment : Fragment() {
|
||||
isRunning = false
|
||||
currentTicks = 0
|
||||
totalTicks = 0
|
||||
currentLap = 0
|
||||
currentLap = 1
|
||||
lapTicks = 0
|
||||
laps.clear()
|
||||
updateIcons()
|
||||
|
@ -0,0 +1,3 @@
|
||||
package com.simplemobiletools.clock.models
|
||||
|
||||
data class Lap(val id: Int, var lapTime: Long, var totalTime: Long)
|
@ -1,3 +0,0 @@
|
||||
package com.simplemobiletools.clock.models
|
||||
|
||||
data class StopwatchTime(val id: Int, var lapTime: Long, var totalTime: Long)
|
Reference in New Issue
Block a user