Skip to content

Instantly share code, notes, and snippets.

View ziginsider's full-sized avatar
🤴
καὶ σύ, τέκνον

Aliaksei ziginsider

🤴
καὶ σύ, τέκνον
View GitHub Profile
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var current = 0L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
class CustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private var periodMs = 0L
private var currentMs = 0L
private var color = 0
private var style = FILL
<resources>
<declare-styleable name="CustomView">
<attr name="custom_color" format="color" />
<attr name="custom_style" format="enum">
<enum name="fill" value="0" />
<enum name="stroke" value="1" />
</attr>
</declare-styleable>
</resources>
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val stopwatchAdapter = StopwatchAdapter()
private val stopwatches = mutableListOf<Stopwatch>()
private var nextId = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
class StopwatchViewHolder(
private val binding: StopwatchItemBinding,
private val listener: StopwatchListener,
private val resources: Resources
) : RecyclerView.ViewHolder(binding.root) {
private var timer: CountDownTimer? = null
fun bind(stopwatch: Stopwatch) {
binding.stopwatchTimer.text = stopwatch.currentMs.displayTime()
/*...*/
override fun start(id: Int) {
changeStopwatch(id, null, true)
}
override fun stop(id: Int, currentMs: Long) {
changeStopwatch(id, currentMs, false)
}
override fun reset(id: Int) {
interface StopwatchListener {
fun start(id: Int)
fun stop(id: Int, currentMs: Long)
fun reset(id: Int)
fun delete(id: Int)
}
class StopwatchViewHolder(
private val binding: StopwatchItemBinding
): RecyclerView.ViewHolder(binding.root) {
private var timer: CountDownTimer? = null
fun bind(stopwatch: Stopwatch) {
binding.stopwatchTimer.text = stopwatch.currentMs.displayTime()
if (stopwatch.isStarted) {
class StopwatchAdapter: ListAdapter<Stopwatch, StopwatchViewHolder>(itemComparator) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StopwatchViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = StopwatchItemBinding.inflate(layoutInflater, parent, false)
return StopwatchViewHolder(binding)
}
override fun onBindViewHolder(holder: StopwatchViewHolder, position: Int) {
holder.bind(getItem(position))
class StopwatchViewHolder(
private val binding: StopwatchItemBinding
): RecyclerView.ViewHolder(binding.root) {
fun bind(stopwatch: Stopwatch) {
binding.stopwatchTimer.text = stopwatch.currentMs.displayTime()
}
private fun Long.displayTime(): String {
if (this <= 0L) {