Fix timer page flashing 00:00 on first navigate
This commit is contained in:
parent
9cd205686f
commit
744ed928f0
|
@ -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() {
|
||||
|
|
14
use-timer.ts
14
use-timer.ts
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue