Fix adding 1 minute to timer

This commit is contained in:
Brandon Presley 2022-07-26 11:37:57 +12:00
parent 3d51d4c077
commit ba496e8c2d
2 changed files with 12 additions and 7 deletions

View File

@ -39,8 +39,8 @@ android {
missingDimensionStrategy "RNNotifications.reactNativeVersion", "reactNative60" missingDimensionStrategy "RNNotifications.reactNativeVersion", "reactNative60"
minSdkVersion rootProject.ext.minSdkVersion minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 2 versionCode 3
versionName "1.0" versionName "1.1"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString() buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
if (isNewArchitectureEnabled()) { if (isNewArchitectureEnabled()) {

View File

@ -17,18 +17,21 @@ import kotlin.math.floor
class TimerService : Service() { class TimerService : Service() {
private var notificationManager: NotificationManager? = null private var notificationManager: NotificationManager? = null
private var endMs: Int? = null private var endMs: Int? = null
private var currentMs: Long? = null
private var countdownTimer: CountDownTimer? = null private var countdownTimer: CountDownTimer? = null
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("TimerService", "Started timer service.") Log.d("TimerService", "Started timer service.")
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs")
if (intent?.action == "add") { if (intent?.action == "add") {
endMs = endMs?.plus(60000) endMs = currentMs!!.toInt().plus(60000)
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java)) applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
} }
else { else {
endMs = intent!!.extras!!.getInt("milliseconds") endMs = intent!!.extras!!.getInt("milliseconds")
} }
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs")
notificationManager = getManager(applicationContext) notificationManager = getManager(applicationContext)
val builder = getBuilder(applicationContext) val builder = getBuilder(applicationContext)
countdownTimer?.cancel() countdownTimer?.cancel()
@ -39,19 +42,21 @@ class TimerService : Service() {
private fun getTimer(builder: NotificationCompat.Builder, notificationManager: NotificationManager): CountDownTimer { private fun getTimer(builder: NotificationCompat.Builder, notificationManager: NotificationManager): CountDownTimer {
return object : CountDownTimer(endMs!!.toLong(), 1000) { return object : CountDownTimer(endMs!!.toLong(), 1000) {
override fun onTick(currentMs: Long) { override fun onTick(current: Long) {
val seconds = floor((currentMs / 1000).toDouble() % 60) currentMs = current
val seconds = floor((current / 1000).toDouble() % 60)
.toInt().toString().padStart(2, '0') .toInt().toString().padStart(2, '0')
val minutes = floor((currentMs / 1000).toDouble() / 60) val minutes = floor((current / 1000).toDouble() / 60)
.toInt().toString().padStart(2, '0') .toInt().toString().padStart(2, '0')
builder.setContentText("$minutes:$seconds") builder.setContentText("$minutes:$seconds")
.setAutoCancel(false) .setAutoCancel(false)
.setDefaults(0) .setDefaults(0)
.setProgress(endMs!!, currentMs.toInt(), false) .setProgress(endMs!!, current.toInt(), false)
.setCategory(NotificationCompat.CATEGORY_PROGRESS) .setCategory(NotificationCompat.CATEGORY_PROGRESS)
.priority = NotificationCompat.PRIORITY_LOW .priority = NotificationCompat.PRIORITY_LOW
notificationManager.notify(NOTIFICATION_ID, builder.build()) notificationManager.notify(NOTIFICATION_ID, builder.build())
} }
@RequiresApi(Build.VERSION_CODES.M)
override fun onFinish() { override fun onFinish() {
val finishIntent = Intent(applicationContext, StopAlarm::class.java) val finishIntent = Intent(applicationContext, StopAlarm::class.java)
val finishPending = val finishPending =