Add setting to disable sound on rest timers

Closes #50
This commit is contained in:
Brandon Presley 2022-10-27 17:28:27 +13:00
parent ef7342b788
commit 859fa2a89f
7 changed files with 45 additions and 19 deletions

View File

@ -26,6 +26,7 @@ export default function EditSet() {
milliseconds, milliseconds,
!!settings.vibrate, !!settings.vibrate,
settings.sound, settings.sound,
!!settings.noSound,
); );
}, },
[settings], [settings],

View File

@ -33,6 +33,7 @@ export default function SettingsPage() {
showSets, showSets,
theme, theme,
alarm, alarm,
noSound,
} = settings; } = settings;
const {color, setColor} = useColor(); const {color, setColor} = useColor();
const {toast} = useSnackbar(); const {toast} = useSnackbar();
@ -139,9 +140,19 @@ export default function SettingsPage() {
[toast, update], [toast, update],
); );
const changeNoSound = useCallback(
(enabled: boolean) => {
update(enabled, 'noSound');
if (enabled) toast('Disable sound on rest timer alarms.', 4000);
else toast('Enabled sound for rest timer alarms.', 4000);
},
[toast, update],
);
const switches: Input<boolean>[] = [ const switches: Input<boolean>[] = [
{name: 'Rest timers', value: !!alarm, onChange: changeAlarmEnabled}, {name: 'Rest timers', value: !!alarm, onChange: changeAlarmEnabled},
{name: 'Vibrate', value: !!vibrate, onChange: changeVibrate}, {name: 'Vibrate', value: !!vibrate, onChange: changeVibrate},
{name: 'Disable sound', value: !!noSound, onChange: changeNoSound},
{name: 'Record notifications', value: !!notify, onChange: changeNotify}, {name: 'Record notifications', value: !!notify, onChange: changeNotify},
{name: 'Show images', value: !!images, onChange: changeImages}, {name: 'Show images', value: !!images, onChange: changeImages},
{name: 'Show unit', value: !!showUnit, onChange: changeUnit}, {name: 'Show unit', value: !!showUnit, onChange: changeUnit},

View File

@ -48,12 +48,13 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
@ReactMethod @ReactMethod
fun timer(milliseconds: Int, vibrate: Boolean, sound: String?) { fun timer(milliseconds: Int, vibrate: Boolean, sound: String?, noSound: Boolean = false) {
Log.d("AlarmModule", "Queue alarm for $milliseconds delay") Log.d("AlarmModule", "Queue alarm for $milliseconds delay")
val intent = Intent(reactApplicationContext, TimerService::class.java) val intent = Intent(reactApplicationContext, TimerService::class.java)
intent.putExtra("milliseconds", milliseconds) intent.putExtra("milliseconds", milliseconds)
intent.putExtra("vibrate", vibrate) intent.putExtra("vibrate", vibrate)
intent.putExtra("sound", sound) intent.putExtra("sound", sound)
intent.putExtra("noSound", noSound)
reactApplicationContext.startService(intent) reactApplicationContext.startService(intent)
} }

View File

@ -21,11 +21,13 @@ class AlarmService : Service(), OnPreparedListener {
return START_STICKY return START_STICKY
} }
val sound = intent.extras?.getString("sound") val sound = intent.extras?.getString("sound")
if (sound == null) { val noSound = intent.extras?.getBoolean("noSound") == true
if (sound == null && !noSound) {
mediaPlayer = MediaPlayer.create(applicationContext, R.raw.argon) mediaPlayer = MediaPlayer.create(applicationContext, R.raw.argon)
mediaPlayer?.start() mediaPlayer?.start()
mediaPlayer?.setOnCompletionListener { vibrator?.cancel() } mediaPlayer?.setOnCompletionListener { vibrator?.cancel() }
} else { } else if (sound != null && !noSound) {
mediaPlayer = MediaPlayer().apply { mediaPlayer = MediaPlayer().apply {
setAudioAttributes( setAudioAttributes(
AudioAttributes.Builder() AudioAttributes.Builder()
@ -39,6 +41,7 @@ class AlarmService : Service(), OnPreparedListener {
setOnCompletionListener { vibrator?.cancel() } setOnCompletionListener { vibrator?.cancel() }
} }
} }
val pattern = longArrayOf(0, 300, 1300, 300, 1300, 300) val pattern = longArrayOf(0, 300, 1300, 300, 1300, 300)
vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = val vibratorManager =

View File

@ -20,11 +20,13 @@ class TimerService : Service() {
private var endMs: Int = 0 private var endMs: Int = 0
private var currentMs: Long = 0 private var currentMs: Long = 0
private var vibrate: Boolean = true private var vibrate: Boolean = true
private var noSound: Boolean = false
private var sound: String? = null private var sound: String? = 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 {
vibrate = intent?.extras?.getBoolean("vibrate") == true vibrate = intent?.extras?.getBoolean("vibrate") == true
noSound = intent?.extras?.getBoolean("noSound") == true
sound = intent?.extras?.getString("sound") sound = intent?.extras?.getString("sound")
val manager = getManager() val manager = getManager()
manager.cancel(NOTIFICATION_ID_DONE) manager.cancel(NOTIFICATION_ID_DONE)
@ -93,9 +95,11 @@ class TimerService : Service() {
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(applicationContext, AlarmService::class.java) val alarmIntent = Intent(applicationContext, AlarmService::class.java).apply {
alarmIntent.putExtra("vibrate", vibrate) putExtra("vibrate", vibrate)
alarmIntent.putExtra("sound", sound) putExtra("sound", sound)
putExtra("noSound", noSound)
}
applicationContext.startService(alarmIntent) applicationContext.startService(alarmIntent)
} }
} }
@ -124,11 +128,13 @@ class TimerService : Service() {
val stopIntent = Intent(context, StopTimer::class.java) val stopIntent = Intent(context, StopTimer::class.java)
val pendingStop = val pendingStop =
PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE) PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE)
val addIntent = Intent(context, TimerService::class.java) val addIntent = Intent(context, TimerService::class.java).apply {
addIntent.action = "add" action = "add"
addIntent.putExtra("vibrate", vibrate) putExtra("vibrate", vibrate)
addIntent.putExtra("sound", sound) putExtra("sound", sound)
addIntent.data = Uri.parse("$currentMs") putExtra("noSound", noSound)
data = Uri.parse("$currentMs")
}
val pendingAdd = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val pendingAdd = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_MUTABLE) PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_MUTABLE)
} else { } else {

19
db.ts
View File

@ -41,16 +41,16 @@ const migrations = [
) )
`, `,
` `
ALTER TABLE sets ADD COLUMN hidden DEFAULT 0 ALTER TABLE sets ADD COLUMN hidden DEFAULT false
`, `,
` `
ALTER TABLE settings ADD COLUMN notify DEFAULT 0 ALTER TABLE settings ADD COLUMN notify DEFAULT false
`, `,
` `
ALTER TABLE sets ADD COLUMN image TEXT NULL ALTER TABLE sets ADD COLUMN image TEXT NULL
`, `,
` `
ALTER TABLE settings ADD COLUMN images BOOLEAN DEFAULT 1 ALTER TABLE settings ADD COLUMN images BOOLEAN DEFAULT true
`, `,
` `
SELECT * FROM settings LIMIT 1 SELECT * FROM settings LIMIT 1
@ -74,7 +74,7 @@ const migrations = [
ALTER TABLE sets ADD COLUMN seconds INTEGER NOT NULL DEFAULT 30 ALTER TABLE sets ADD COLUMN seconds INTEGER NOT NULL DEFAULT 30
`, `,
` `
ALTER TABLE settings ADD COLUMN showUnit BOOLEAN DEFAULT 1 ALTER TABLE settings ADD COLUMN showUnit BOOLEAN DEFAULT true
`, `,
` `
ALTER TABLE sets ADD COLUMN steps TEXT NULL ALTER TABLE sets ADD COLUMN steps TEXT NULL
@ -94,10 +94,10 @@ const migrations = [
UPDATE settings SET showUnit = 1 UPDATE settings SET showUnit = 1
`, `,
` `
ALTER TABLE settings ADD COLUMN workouts BOOLEAN DEFAULT 1 ALTER TABLE settings ADD COLUMN workouts BOOLEAN DEFAULT true
`, `,
` `
ALTER TABLE settings ADD COLUMN steps BOOLEAN DEFAULT 1 ALTER TABLE settings ADD COLUMN steps BOOLEAN DEFAULT true
`, `,
` `
ALTER TABLE settings ADD COLUMN nextAlarm TEXT NULL ALTER TABLE settings ADD COLUMN nextAlarm TEXT NULL
@ -109,17 +109,20 @@ const migrations = [
ALTER TABLE settings ADD COLUMN date TEXT NULL ALTER TABLE settings ADD COLUMN date TEXT NULL
`, `,
` `
ALTER TABLE settings ADD COLUMN showDate BOOLEAN DEFAULT 0 ALTER TABLE settings ADD COLUMN showDate BOOLEAN DEFAULT false
`, `,
` `
ALTER TABLE settings ADD COLUMN theme TEXT ALTER TABLE settings ADD COLUMN theme TEXT
`, `,
` `
ALTER TABLE settings ADD COLUMN showSets BOOLEAN DEFAULT 1 ALTER TABLE settings ADD COLUMN showSets BOOLEAN DEFAULT true
`, `,
` `
CREATE INDEX sets_created ON sets(created) CREATE INDEX sets_created ON sets(created)
`, `,
`
ALTER TABLE settings ADD COLUMN noSound BOOLEAN DEFAULT false
`,
]; ];
export let db: SQLiteDatabase; export let db: SQLiteDatabase;

View File

@ -11,4 +11,5 @@ export default interface Settings {
showDate: number; showDate: number;
theme: 'system' | 'dark' | 'light'; theme: 'system' | 'dark' | 'light';
showSets: number; showSets: number;
noSound: number;
} }