Fix timer page flashing 00:00 on first navigate

This commit is contained in:
Brandon Presley 2023-11-04 13:53:19 +13:00
parent 9cd205686f
commit 744ed928f0
2 changed files with 20 additions and 2 deletions

View File

@ -68,6 +68,14 @@ class AlarmModule constructor(context: ReactApplicationContext?) :
reactApplicationContext.stopService(intent)
}
@ReactMethod(isBlockingSynchronousMethod = true)
fun getCurrent(): Int {
Log.d("AlarmModule", "currentMs=$currentMs")
if (running)
return currentMs.toInt();
return 0;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@ReactMethod
fun stop() {

View File

@ -2,6 +2,7 @@ import { useFocusEffect } from "@react-navigation/native";
import { useCallback, useState } from "react";
import { emitter } from "./emitter";
import { TickEvent } from "./TimerPage";
import { NativeModules } from "react-native";
export default function useTimer() {
const [minutes, setMinutes] = useState("00");
@ -9,8 +10,17 @@ export default function useTimer() {
useFocusEffect(
useCallback(() => {
setMinutes("00");
setSeconds("00");
const current: number = NativeModules.AlarmModule.getCurrent();
setMinutes(
Math.floor(current / 1000 / 60)
.toString()
.padStart(2, "0")
);
setSeconds(
Math.floor((current / 1000) % 60)
.toString()
.padStart(2, "0")
);
const listener = emitter.addListener("tick", (event: TickEvent) => {
console.log(`${useTimer.name}.tick:`, { event });
setMinutes(event.minutes);