From 4c185f0346518f6728a6fe20cc7a1d68882723bc Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Sat, 20 Aug 2022 16:37:59 +1200 Subject: [PATCH] Add vibration setting for timers --- App.tsx | 2 + EditSet.tsx | 9 +-- SetList.tsx | 3 +- SettingsPage.tsx | 61 ++++++++++++------- ViewBest.tsx | 2 +- .../src/main/java/com/massive/AlarmModule.kt | 3 +- .../src/main/java/com/massive/AlarmService.kt | 4 +- .../src/main/java/com/massive/TimerService.kt | 10 ++- set.ts | 2 +- 9 files changed, 62 insertions(+), 34 deletions(-) diff --git a/App.tsx b/App.tsx index b40bfc7..8846c2d 100644 --- a/App.tsx +++ b/App.tsx @@ -62,6 +62,8 @@ const App = () => { if (seconds === null) await setItem('seconds', '30'); const alarmEnabled = await getItem('alarmEnabled'); if (alarmEnabled === null) await setItem('alarmEnabled', 'false'); + const vibrate = await getItem('vibrate'); + if (vibrate === null) await setItem('vibrate', 'true'); if (!(await getItem('predictiveSets'))) await setItem('predictiveSets', 'true'); if (!(await getItem('maxSets'))) await setItem('maxSets', '3'); diff --git a/EditSet.tsx b/EditSet.tsx index 529277e..a04f1c4 100644 --- a/EditSet.tsx +++ b/EditSet.tsx @@ -29,13 +29,14 @@ export default function EditSet() { }, [navigation]), ); - const notify = useCallback(async () => { + const startTimer = useCallback(async () => { const enabled = await AsyncStorage.getItem('alarmEnabled'); if (enabled !== 'true') return; const minutes = await AsyncStorage.getItem('minutes'); const seconds = await AsyncStorage.getItem('seconds'); const milliseconds = Number(minutes) * 60 * 1000 + Number(seconds) * 1000; - NativeModules.AlarmModule.timer(milliseconds); + const vibrate = (await AsyncStorage.getItem('vibrate')) === 'true'; + NativeModules.AlarmModule.timer(milliseconds, vibrate); }, []); const update = useCallback( @@ -57,11 +58,11 @@ export default function EditSet() { INSERT INTO sets(name, reps, weight, created, unit) VALUES (?,?,?,strftime('%Y-%m-%dT%H:%M:%S', 'now', 'localtime'),?) `; + startTimer(); await db.executeSql(insert, [name, reps, weight, unit]); - notify(); navigation.goBack(); }, - [db, navigation, notify], + [db, navigation, startTimer], ); const save = useCallback( diff --git a/SetList.tsx b/SetList.tsx index 3221f13..d4f3803 100644 --- a/SetList.tsx +++ b/SetList.tsx @@ -121,7 +121,7 @@ export default function SetList() { todaysWorkouts[todaysWorkouts.indexOf(todaysSets[0].name!) + 1]; } const best = await getBest(nextWorkout); - setNextSet({...best, created: new Date().toISOString()}); + setNextSet({...best}); }, [getTodaysSets, getTodaysPlan, getBest]); useFocusEffect( @@ -171,7 +171,6 @@ export default function SetList() { reps: 0, weight: 0, unit: 'kg', - created: new Date().toISOString(), }; navigation.navigate('EditSet', {set: nextSet || set}); }, [navigation, nextSet]); diff --git a/SettingsPage.tsx b/SettingsPage.tsx index a57eb99..bcc109c 100644 --- a/SettingsPage.tsx +++ b/SettingsPage.tsx @@ -14,19 +14,20 @@ import MassiveSwitch from './MassiveSwitch'; const {getItem, setItem} = AsyncStorage; export default function SettingsPage() { + const [vibrate, setVibrate] = useState(true); const [minutes, setMinutes] = useState(''); const [maxSets, setMaxSets] = useState('3'); const [seconds, setSeconds] = useState(''); - const [alarmEnabled, setAlarmEnabled] = useState(false); - const [predictiveSets, setPredictiveSets] = useState(false); - const [showBattery, setShowBattery] = useState(false); + const [alarm, setAlarm] = useState(false); + const [predictive, setPredictive] = useState(false); + const [battery, setBattery] = useState(false); const [ignoring, setIgnoring] = useState(false); const refresh = useCallback(async () => { setMinutes((await getItem('minutes')) || ''); setSeconds((await getItem('seconds')) || ''); - setAlarmEnabled((await getItem('alarmEnabled')) === 'true'); - setPredictiveSets((await getItem('predictiveSets')) === 'true'); + setAlarm((await getItem('alarmEnabled')) === 'true'); + setPredictive((await getItem('predictiveSets')) === 'true'); setMaxSets((await getItem('maxSets')) || ''); NativeModules.AlarmModule.ignoringBattery(setIgnoring); }, []); @@ -37,27 +38,27 @@ export default function SettingsPage() { const changeAlarmEnabled = useCallback( (enabled: boolean) => { - setAlarmEnabled(enabled); - if (enabled && !ignoring) setShowBattery(true); + setAlarm(enabled); + if (enabled && !ignoring) setBattery(true); setItem('alarmEnabled', enabled ? 'true' : 'false'); }, - [setShowBattery, ignoring], + [setBattery, ignoring], ); const changePredictive = useCallback( (enabled: boolean) => { - setPredictiveSets(enabled); + setPredictive(enabled); setItem('predictiveSets', enabled ? 'true' : 'false'); ToastAndroid.show( 'Predictive sets guess whats next based on todays plan.', ToastAndroid.LONG, ); }, - [setPredictiveSets], + [setPredictive], ); - return ( - + const textInputs = ( + <> - - + + ); + const changeVibrate = useCallback( + (value: boolean) => { + setVibrate(value); + setItem('vibrate', value ? 'true' : 'false'); + }, + [setVibrate], + ); + + return ( + + {textInputs} Rest timers - + Vibrate + { NativeModules.AlarmModule.openSettings(); - setShowBattery(false); + setBattery(false); }}> Disable battery optimizations for Massive to use rest timers. - Predictive sets diff --git a/ViewBest.tsx b/ViewBest.tsx index 0c8e7f2..7585b96 100644 --- a/ViewBest.tsx +++ b/ViewBest.tsx @@ -83,7 +83,7 @@ export default function ViewBest() { formatMonth(sets[index].created)} + formatLabel={(_value, index) => formatMonth(sets[index].created!)} contentInset={{left: 10, right: 10}} svg={axesSvg} /> diff --git a/android/app/src/main/java/com/massive/AlarmModule.kt b/android/app/src/main/java/com/massive/AlarmModule.kt index 75127e6..9539c34 100644 --- a/android/app/src/main/java/com/massive/AlarmModule.kt +++ b/android/app/src/main/java/com/massive/AlarmModule.kt @@ -23,10 +23,11 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) : @RequiresApi(api = Build.VERSION_CODES.O) @ReactMethod - fun timer(milliseconds: Int) { + fun timer(milliseconds: Int, vibrate: Boolean) { Log.d("AlarmModule", "Queue alarm for $milliseconds delay") val intent = Intent(reactApplicationContext, TimerService::class.java) intent.putExtra("milliseconds", milliseconds) + intent.putExtra("vibrate", vibrate) reactApplicationContext.startService(intent) } diff --git a/android/app/src/main/java/com/massive/AlarmService.kt b/android/app/src/main/java/com/massive/AlarmService.kt index d2505da..23972b8 100644 --- a/android/app/src/main/java/com/massive/AlarmService.kt +++ b/android/app/src/main/java/com/massive/AlarmService.kt @@ -30,7 +30,9 @@ class AlarmService : Service(), OnPreparedListener { .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_ALARM) .build() - vibrator!!.vibrate(VibrationEffect.createWaveform(pattern, 1), audioAttributes) + val vibrate = intent.extras!!.getBoolean("vibrate") + if (vibrate) + vibrator!!.vibrate(VibrationEffect.createWaveform(pattern, 1), audioAttributes) return START_STICKY } diff --git a/android/app/src/main/java/com/massive/TimerService.kt b/android/app/src/main/java/com/massive/TimerService.kt index dede4fc..40bac39 100644 --- a/android/app/src/main/java/com/massive/TimerService.kt +++ b/android/app/src/main/java/com/massive/TimerService.kt @@ -19,17 +19,19 @@ class TimerService : Service() { private var endMs: Int? = null private var currentMs: Long? = null private var countdownTimer: CountDownTimer? = null + private var vibrate: Boolean = true @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") { + vibrate = intent!!.extras!!.getBoolean("vibrate") + if (intent.action == "add") { endMs = currentMs!!.toInt().plus(60000) applicationContext.stopService(Intent(applicationContext, AlarmService::class.java)) } else { - endMs = intent!!.extras!!.getInt("milliseconds") + endMs = intent.extras!!.getInt("milliseconds") } Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs") notificationManager = getManager(applicationContext) @@ -68,7 +70,9 @@ class TimerService : Service() { .setCategory(NotificationCompat.CATEGORY_ALARM) .priority = NotificationCompat.PRIORITY_HIGH notificationManager.notify(NOTIFICATION_ID, builder.build()) - applicationContext.startService(Intent(applicationContext, AlarmService::class.java)) + val alarmIntent = Intent(applicationContext, AlarmService::class.java) + alarmIntent.putExtra("vibrate", vibrate) + applicationContext.startService(alarmIntent) } } } diff --git a/set.ts b/set.ts index 5b0b4c8..edd6c17 100644 --- a/set.ts +++ b/set.ts @@ -3,6 +3,6 @@ export default interface Set { name: string; reps: number; weight: number; - created: string; + created?: string; unit?: string; }