This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*...*/ | |
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface StopwatchListener { | |
fun start(id: Int) | |
fun stop(id: Int, currentMs: Long) | |
fun reset(id: Int) | |
fun delete(id: Int) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |