Add noSound to AlarmModule

This commit is contained in:
Brandon Presley 2022-11-03 21:59:12 +13:00
parent facbfe4da5
commit 0e5de0e519

View File

@ -51,11 +51,11 @@ class AlarmModule constructor(context: ReactApplicationContext?) :
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
@ReactMethod @ReactMethod
fun add(vibrate: Boolean, sound: String?) { fun add(vibrate: Boolean, sound: String?, noSound: Boolean = false) {
Log.d("AlarmModule", "Add 1 min to alarm.") Log.d("AlarmModule", "Add 1 min to alarm.")
countdownTimer?.cancel() countdownTimer?.cancel()
val newMs = if (running) currentMs.toInt().plus(60000) else 60000 val newMs = if (running) currentMs.toInt().plus(60000) else 60000
countdownTimer = getTimer(newMs, vibrate, sound) countdownTimer = getTimer(newMs, vibrate, sound, noSound)
countdownTimer?.start() countdownTimer?.start()
running = true running = true
} }
@ -96,7 +96,7 @@ class AlarmModule constructor(context: ReactApplicationContext?) :
) )
) )
countdownTimer?.cancel() countdownTimer?.cancel()
countdownTimer = getTimer(milliseconds, vibrate, sound) countdownTimer = getTimer(milliseconds, vibrate, sound, noSound)
countdownTimer?.start() countdownTimer?.start()
running = true running = true
} }
@ -132,7 +132,7 @@ class AlarmModule constructor(context: ReactApplicationContext?) :
} }
@RequiresApi(Build.VERSION_CODES.M) @RequiresApi(Build.VERSION_CODES.M)
private fun getTimer(endMs: Int, vibrate: Boolean, sound: String?): CountDownTimer { private fun getTimer(endMs: Int, vibrate: Boolean, sound: String?, noSound: Boolean): CountDownTimer {
val builder = getBuilder() val builder = getBuilder()
return object : CountDownTimer(endMs.toLong(), 1000) { return object : CountDownTimer(endMs.toLong(), 1000) {
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
@ -176,9 +176,11 @@ class AlarmModule constructor(context: ReactApplicationContext?) :
val manager = getManager() val manager = getManager()
manager.notify(NOTIFICATION_ID_DONE, builder.build()) manager.notify(NOTIFICATION_ID_DONE, builder.build())
manager.cancel(NOTIFICATION_ID_PENDING) manager.cancel(NOTIFICATION_ID_PENDING)
val alarmIntent = Intent(context, AlarmService::class.java) val alarmIntent = Intent(context, AlarmService::class.java).apply {
alarmIntent.putExtra("vibrate", vibrate) putExtra("vibrate", vibrate)
alarmIntent.putExtra("sound", sound) putExtra("sound", sound)
putExtra("noSound", noSound)
}
context.startService(alarmIntent) context.startService(alarmIntent)
reactApplicationContext reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)