From ba496e8c2da3ddf8eafd2386c00ee07cbc6dce49 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Tue, 26 Jul 2022 11:37:57 +1200 Subject: [PATCH] Fix adding 1 minute to timer --- android/app/build.gradle | 4 ++-- .../app/src/main/java/com/massive/TimerService.kt | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index a2da4d7..ab62129 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -39,8 +39,8 @@ android { missingDimensionStrategy "RNNotifications.reactNativeVersion", "reactNative60" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 2 - versionName "1.0" + versionCode 3 + versionName "1.1" buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString() if (isNewArchitectureEnabled()) { diff --git a/android/app/src/main/java/com/massive/TimerService.kt b/android/app/src/main/java/com/massive/TimerService.kt index 0968221..dede4fc 100644 --- a/android/app/src/main/java/com/massive/TimerService.kt +++ b/android/app/src/main/java/com/massive/TimerService.kt @@ -17,18 +17,21 @@ import kotlin.math.floor class TimerService : Service() { private var notificationManager: NotificationManager? = null private var endMs: Int? = null + private var currentMs: Long? = null private var countdownTimer: CountDownTimer? = null @RequiresApi(Build.VERSION_CODES.O) override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Log.d("TimerService", "Started timer service.") + Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs") if (intent?.action == "add") { - endMs = endMs?.plus(60000) + endMs = currentMs!!.toInt().plus(60000) applicationContext.stopService(Intent(applicationContext, AlarmService::class.java)) } else { endMs = intent!!.extras!!.getInt("milliseconds") } + Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs") notificationManager = getManager(applicationContext) val builder = getBuilder(applicationContext) countdownTimer?.cancel() @@ -39,19 +42,21 @@ class TimerService : Service() { private fun getTimer(builder: NotificationCompat.Builder, notificationManager: NotificationManager): CountDownTimer { return object : CountDownTimer(endMs!!.toLong(), 1000) { - override fun onTick(currentMs: Long) { - val seconds = floor((currentMs / 1000).toDouble() % 60) + override fun onTick(current: Long) { + currentMs = current + val seconds = floor((current / 1000).toDouble() % 60) .toInt().toString().padStart(2, '0') - val minutes = floor((currentMs / 1000).toDouble() / 60) + val minutes = floor((current / 1000).toDouble() / 60) .toInt().toString().padStart(2, '0') builder.setContentText("$minutes:$seconds") .setAutoCancel(false) .setDefaults(0) - .setProgress(endMs!!, currentMs.toInt(), false) + .setProgress(endMs!!, current.toInt(), false) .setCategory(NotificationCompat.CATEGORY_PROGRESS) .priority = NotificationCompat.PRIORITY_LOW notificationManager.notify(NOTIFICATION_ID, builder.build()) } + @RequiresApi(Build.VERSION_CODES.M) override fun onFinish() { val finishIntent = Intent(applicationContext, StopAlarm::class.java) val finishPending =