Guest User

Untitled

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. //calculate mins and seconds from milliseconds to show in the screen
  2. fun minuteCalc(data: Long): String {
  3. var data = data
  4.  
  5. val min: Long
  6. val sec: Long
  7. data /= 1000
  8.  
  9. if (maxTimer < data) {
  10. maxTimer = data
  11. progressBar.max = maxTimer.toFloat()
  12. }
  13.  
  14. if (data > 60) {
  15. min = data / 60
  16. sec = data % 60
  17. return "Timer: " + min + "m " + sec + "s"
  18. } else {
  19. sec = data
  20. return "Timer: " + sec + "s"
  21. }
  22.  
  23. }
  24.  
  25. fun preTimerText() {
  26. timerText!!.text = minuteCalc(timer)
  27. }
  28.  
  29. fun startTimer(v: View) {
  30. timer()
  31. }
  32.  
  33. fun stopTimer(v: View) {
  34. if (timerFunction != null) {
  35. timerFunction!!.cancel()
  36. }
  37. progressBar.progress = 0f
  38. progressBar.max = 60000f
  39. timer = 60000
  40. maxTimer = 60000
  41. timerText!!.text = "Cancelled"
  42. handler.postDelayed({ preTimerText() }, 3000)
  43. }
  44.  
  45. fun timer() {
  46.  
  47. timerOn = true
  48.  
  49. timerFunction = object : CountDownTimer(timer, 1000) {
  50.  
  51. override fun onTick(millisUntilFinished: Long) {
  52. timerText!!.text = minuteCalc(millisUntilFinished)
  53. progressBar.progress = (millisUntilFinished / 1000).toFloat()
  54. timer = millisUntilFinished
  55. }
  56.  
  57. override fun onFinish() {
  58. timerText!!.text = "done!"
  59. progressBar.progress = 0f
  60. timerOn = false
  61. }
  62.  
  63. }.start()
  64. }
  65.  
  66. //add 30s in the timer
  67. fun add30(v: View) {
  68. if (timerFunction != null) {
  69. timerFunction!!.cancel()
  70. }
  71. timer += 30000
  72. progressBar.max = progressBar.max + 30
  73. if (!timerOn) {
  74. preTimerText()
  75. } else {
  76. timer()
  77. }
  78. }
  79.  
  80. //add 1min in the timer
  81. fun add1m(v: View) {
  82. if (timerFunction != null) {
  83. timerFunction!!.cancel()
  84. }
  85. timer += 60000
  86. progressBar.max = progressBar.max + 60
  87. if (!timerOn) {
  88. preTimerText()
  89. } else {
  90. timer()
  91. }
  92. }
  93.  
  94. //add 5m in the timer
  95. fun add5m(v: View) {
  96. if (timerFunction != null) {
  97. timerFunction!!.cancel()
  98. }
  99. timer += 300000
  100. progressBar.max = progressBar.max + 300
  101. if (!timerOn) {
  102. preTimerText()
  103. } else {
  104. timer()
  105. }
  106. }
Add Comment
Please, Sign In to add comment