diff --git a/lib/controllers/android_walking_controller.dart b/lib/controllers/android_walking_controller.dart deleted file mode 100644 index c7371e13..00000000 --- a/lib/controllers/android_walking_controller.dart +++ /dev/null @@ -1,94 +0,0 @@ -import 'dart:async'; - -import 'package:get/get.dart'; -import 'package:get_storage/get_storage.dart'; -import 'package:pedometer/pedometer.dart'; - - -const String LOCAL_STORE_STEP_KEY = 'pastSteps'; - -class AndroidWalkingController extends GetxController { - static double averageStride = 0.6; - static int metersPerKilometer = 1000; - static double averageCalorie = 0.04; - - static int checkDay = 0; - - RxInt currentSteps = 0.obs; - RxInt totalSteps = 0.obs; - RxInt pastSteps = 0.obs; - - final GetStorage box = GetStorage(); - - late Stream _stepCountStream; - - AndroidWalkingController._privateConstructor(); - - static final AndroidWalkingController _instance = - AndroidWalkingController._privateConstructor(); - - factory AndroidWalkingController() { - return _instance; - } - - String get getCurrentSteps => currentSteps.value.toString(); - - @override - void onInit() { - super.onInit(); - initPlatformState(); - init(); - - Timer.periodic(Duration(minutes: 5), (t) { - if (checkDay != DateTime.now().day) { - resetStepTimer(); - checkDay = DateTime.now().day; - } - }); - } - - void init() async { - await GetStorage.init(); - } - - void updateStep(StepCount event) async { - totalSteps.value = event.steps; - - int? value = box.read(LOCAL_STORE_STEP_KEY); - if (value == null || value == 0) { - pastSteps.value = totalSteps.value; - box.write(LOCAL_STORE_STEP_KEY, totalSteps.value); - } else { - pastSteps.value = value; - } - currentSteps.value = totalSteps.value - pastSteps.value; - } - - void resetStepTimer() async { - pastSteps.value = totalSteps.value; - currentSteps.value = 0; - box.write(LOCAL_STORE_STEP_KEY, totalSteps.value); - } - - void onStepCountError(error) { - currentSteps.value = 0; - } - - void initPlatformState() { - _stepCountStream = Pedometer.stepCountStream.asBroadcastStream(); - _stepCountStream.listen(updateStep).onError(onStepCountError); - } - - getCurrentTravelDistance() { - var currentTravelDistance = - (currentSteps.value * averageStride) / metersPerKilometer; - - return currentTravelDistance.toStringAsFixed(2); - } - - getCurrentCalorie() { - var calorie = currentSteps.value * averageCalorie; - - return calorie.toStringAsFixed(0); - } -} diff --git a/lib/utils/android_notification.dart b/lib/utils/android_notification.dart index 430defe0..4943fa53 100644 --- a/lib/utils/android_notification.dart +++ b/lib/utils/android_notification.dart @@ -45,10 +45,10 @@ Future initForegroundTask() async { @pragma('vm:entry-point') void startCallback() { - FlutterForegroundTask.setTaskHandler(FirstTaskHandler()); + FlutterForegroundTask.setTaskHandler(AndroidWalkingHandler()); } -class FirstTaskHandler extends TaskHandler { +class AndroidWalkingHandler extends TaskHandler { SendPort? _sendPort; late Stream _stepCountStream; final GetStorage _localStorage = GetStorage(); @@ -60,7 +60,7 @@ class FirstTaskHandler extends TaskHandler { @override void onStart(DateTime timestamp, SendPort? sendPort) async { _sendPort = sendPort; - currentSteps = (await _localStorage.read(todayStepKey)) ?? 0; + currentSteps = _localStorage.read(todayStepKey) ?? 0; _stepCountStream = Pedometer.stepCountStream.asBroadcastStream(); _stepCountStream.listen(updateStep).onError(onStepCountError); _initializeMidnightReset(); @@ -74,9 +74,10 @@ class FirstTaskHandler extends TaskHandler { DateTime now = DateTime.now(); DateTime midnight = DateTime(now.year, now.month, now.day + 1); Duration timeUntilMidnight = midnight.difference(now); - + _midnightTimer?.cancel(); _midnightTimer = Timer(timeUntilMidnight, () { _resetStepsAtMidnight(); + _midnightTimer?.cancel(); _midnightTimer = Timer.periodic(Duration(days: 1), (timer) { _resetStepsAtMidnight(); }); @@ -84,6 +85,7 @@ class FirstTaskHandler extends TaskHandler { } void _resetStepsAtMidnight() { + // TODO : 서버에 걸음수 저장하는 로직 추가 currentSteps = 0; _localStorage.write(todayStepKey, 0); @@ -97,8 +99,8 @@ class FirstTaskHandler extends TaskHandler { updateStep(StepCount event) async { int currentTotalStep = event.steps; - int lastSavedStepCount = await _localStorage.read(lastSavedStepKey); - int todaySteps = await _localStorage.read(todayStepKey) ?? 0; + int? lastSavedStepCount = _localStorage.read(lastSavedStepKey); + int todaySteps = _localStorage.read(todayStepKey) ?? 0; if (lastSavedStepCount == null) { _localStorage.write(lastSavedStepKey, currentTotalStep); diff --git a/lib/widgets/map/android_step_stats.dart b/lib/widgets/map/android_step_stats.dart deleted file mode 100644 index b6050ac4..00000000 --- a/lib/widgets/map/android_step_stats.dart +++ /dev/null @@ -1,42 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:get/get.dart'; - -import '../../controllers/android_walking_controller.dart'; - -class AndroidStepStats extends StatelessWidget { - const AndroidStepStats({super.key}); - - @override - Widget build(BuildContext context) { - var androidWalkingController = Get.put(AndroidWalkingController()); - return Obx( - () => Column( - children: [ - Text( - '${androidWalkingController.currentSteps.value} 걸음', - style: TextStyle( - fontSize: 30, - fontWeight: FontWeight.bold, - color: Colors.white, - ), - ), - SizedBox( - height: 5, - ), - Container( - padding: EdgeInsets.symmetric(horizontal: 7, vertical: 5), - decoration: BoxDecoration( - color: Colors.black, - borderRadius: BorderRadius.circular(20.0), - ), - child: Text( - '${androidWalkingController.getCurrentCalorie()} kcal | ${androidWalkingController.getCurrentTravelDistance()}km', - style: - TextStyle(color: Colors.white, fontWeight: FontWeight.bold), - ), - ), - ], - ), - ); - } -}