Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //calculate mins and seconds from milliseconds to show in the screen
- fun minuteCalc(data: Long): String {
- var data = data
- val min: Long
- val sec: Long
- data /= 1000
- if (maxTimer < data) {
- maxTimer = data
- progressBar.max = maxTimer.toFloat()
- }
- if (data > 60) {
- min = data / 60
- sec = data % 60
- return "Timer: " + min + "m " + sec + "s"
- } else {
- sec = data
- return "Timer: " + sec + "s"
- }
- }
- fun preTimerText() {
- timerText!!.text = minuteCalc(timer)
- }
- fun startTimer(v: View) {
- timer()
- }
- fun stopTimer(v: View) {
- if (timerFunction != null) {
- timerFunction!!.cancel()
- }
- progressBar.progress = 0f
- progressBar.max = 60000f
- timer = 60000
- maxTimer = 60000
- timerText!!.text = "Cancelled"
- handler.postDelayed({ preTimerText() }, 3000)
- }
- fun timer() {
- timerOn = true
- timerFunction = object : CountDownTimer(timer, 1000) {
- override fun onTick(millisUntilFinished: Long) {
- timerText!!.text = minuteCalc(millisUntilFinished)
- progressBar.progress = (millisUntilFinished / 1000).toFloat()
- timer = millisUntilFinished
- }
- override fun onFinish() {
- timerText!!.text = "done!"
- progressBar.progress = 0f
- timerOn = false
- }
- }.start()
- }
- //add 30s in the timer
- fun add30(v: View) {
- if (timerFunction != null) {
- timerFunction!!.cancel()
- }
- timer += 30000
- progressBar.max = progressBar.max + 30
- if (!timerOn) {
- preTimerText()
- } else {
- timer()
- }
- }
- //add 1min in the timer
- fun add1m(v: View) {
- if (timerFunction != null) {
- timerFunction!!.cancel()
- }
- timer += 60000
- progressBar.max = progressBar.max + 60
- if (!timerOn) {
- preTimerText()
- } else {
- timer()
- }
- }
- //add 5m in the timer
- fun add5m(v: View) {
- if (timerFunction != null) {
- timerFunction!!.cancel()
- }
- timer += 300000
- progressBar.max = progressBar.max + 300
- if (!timerOn) {
- preTimerText()
- } else {
- timer()
- }
- }
Add Comment
Please, Sign In to add comment