From 2e9caa65499aed3d7211fc4d5865e463926914c1 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Wed, 3 Jul 2024 20:41:49 +0900 Subject: [PATCH] =?UTF-8?q?M3-107=20Feat=20:=20android=5Fnotification.dart?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/main.dart | 12 ++++++++++-- lib/service/android_walking_service.dart | 20 ++++++++++++++++---- lib/utils/android_notification.dart | 18 ++++++++++++------ lib/utils/walking_service_factory.dart | 6 ++++++ 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index a04621d8..7696dd4d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,16 +1,21 @@ import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; - import 'package:get/get.dart'; +import 'package:get_storage/get_storage.dart'; +import 'package:ground_flip/utils/android_notification.dart'; -import '../utils/android_notification.dart'; import 'screens/main_screen.dart'; +import 'utils/walking_service_factory.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await dotenv.load(fileName: ".env"); + await GetStorage.init(); runApp(const MyApp()); + // var test = WalkingServiceFactory.getWalkingService(); + // print(test.hashCode); + // initForegroundTask(); } class MyApp extends StatelessWidget { @@ -18,7 +23,10 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { + var test = WalkingServiceFactory.getWalkingService(); + print('메인 생성${test.hashCode}'); initForegroundTask(); + // initForegroundTask(); return GetMaterialApp( title: 'Ground Flip', theme: ThemeData( diff --git a/lib/service/android_walking_service.dart b/lib/service/android_walking_service.dart index ae6aca4d..ada629ad 100644 --- a/lib/service/android_walking_service.dart +++ b/lib/service/android_walking_service.dart @@ -13,6 +13,7 @@ class AndroidWalkingService implements WalkingService { late Stream _stepCountStream; + bool isInit = false; int currentSteps = 0; int totalSteps = 0; int pastSteps = 0; @@ -21,10 +22,12 @@ class AndroidWalkingService implements WalkingService { AndroidWalkingService._internal(); AndroidWalkingService._internal() { - print('생성'); - initPlatformState(); - init(); + // print('생성'); + print('AndroidWalkingService instance created: ${identityHashCode(this)}'); + // initPlatformState(); + // // init(); Timer.periodic(Duration(minutes: 5), (t) { + print('---------------day init------------'); if (checkDay != DateTime.now().day) { resetStepTimer(); checkDay = DateTime.now().day; @@ -33,16 +36,25 @@ class AndroidWalkingService implements WalkingService { } factory AndroidWalkingService() { + // initPlatformState(); + // // init(); + // Timer.periodic(Duration(minutes: 5), (t) { + // if (checkDay != DateTime.now().day) { + // resetStepTimer(); + // checkDay = DateTime.now().day; + // } + // }); return _instance; } - void init() async { + Future init() async { await GetStorage.init(); } void initPlatformState() { _stepCountStream = Pedometer.stepCountStream.asBroadcastStream(); _stepCountStream.listen(updateStep).onError(onStepCountError); + isInit = true; } void onStepCountError(error) { diff --git a/lib/utils/android_notification.dart b/lib/utils/android_notification.dart index 5e6961a6..816c8e8a 100644 --- a/lib/utils/android_notification.dart +++ b/lib/utils/android_notification.dart @@ -1,14 +1,17 @@ import 'dart:isolate'; import 'package:flutter_foreground_task/flutter_foreground_task.dart'; -import 'package:get/get.dart'; -import '../controllers/android_walking_controller.dart'; +import '../service/android_walking_service.dart'; -AndroidWalkingController androidWalkingController = - Get.put(AndroidWalkingController()); +// AndroidWalkingController androidWalkingController = +// Get.put(AndroidWalkingController()); + +// print(androidWalkingService); void initForegroundTask() { + AndroidWalkingService androidWalkingService = AndroidWalkingService(); + print('initForeGround 생성 ${androidWalkingService.hashCode}'); FlutterForegroundTask.init( androidNotificationOptions: AndroidNotificationOptions( channelId: 'ground-flip', @@ -37,7 +40,7 @@ void initForegroundTask() { ); FlutterForegroundTask.startService( notificationTitle: "걸음수", - notificationText: androidWalkingController.currentSteps.value.toString(), + notificationText: androidWalkingService.currentSteps.toString(), callback: startCallback, ); } @@ -48,12 +51,15 @@ void startCallback() { } class FirstTaskHandler extends TaskHandler { + AndroidWalkingService androidWalkingService = AndroidWalkingService(); + + // print('initForeGround 생성 ${androidWalkingService.hashCode}'); @override void onStart(DateTime timestamp, SendPort? sendPort) async {} @override void onRepeatEvent(DateTime timestamp, SendPort? sendPort) async { - int updateStep = androidWalkingController.currentSteps.value; + int updateStep = androidWalkingService.currentSteps; FlutterForegroundTask.updateService( notificationTitle: "걸음수", notificationText: updateStep.toString(), diff --git a/lib/utils/walking_service_factory.dart b/lib/utils/walking_service_factory.dart index 2e1ef0f5..0fc8de17 100644 --- a/lib/utils/walking_service_factory.dart +++ b/lib/utils/walking_service_factory.dart @@ -9,6 +9,12 @@ class WalkingServiceFactory { if (Platform.isIOS) { return IosWalkingService(); } else if (Platform.isAndroid) { + var androidWalkingService = AndroidWalkingService(); + if (androidWalkingService.isInit == false) { + print('초기화'); + androidWalkingService.initPlatformState(); + } + print('팩토리 생성 ${androidWalkingService.hashCode}'); return AndroidWalkingService(); } else { throw UnsupportedError('지원하지 않는 플랫폼입니다.');