Skip to content

Commit

Permalink
M3-107 Feat : android_notification.dart 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
koomin1227 committed Jul 6, 2024
1 parent a5d54c1 commit 2e9caa6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
12 changes: 10 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
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<void> 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 {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
var test = WalkingServiceFactory.getWalkingService();
print('메인 생성${test.hashCode}');
initForegroundTask();
// initForegroundTask();
return GetMaterialApp(
title: 'Ground Flip',
theme: ThemeData(
Expand Down
20 changes: 16 additions & 4 deletions lib/service/android_walking_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AndroidWalkingService implements WalkingService {

late Stream<StepCount> _stepCountStream;

bool isInit = false;
int currentSteps = 0;
int totalSteps = 0;
int pastSteps = 0;
Expand All @@ -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;
Expand All @@ -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<void> init() async {
await GetStorage.init();
}

void initPlatformState() {
_stepCountStream = Pedometer.stepCountStream.asBroadcastStream();
_stepCountStream.listen(updateStep).onError(onStepCountError);
isInit = true;
}

void onStepCountError(error) {
Expand Down
18 changes: 12 additions & 6 deletions lib/utils/android_notification.dart
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -37,7 +40,7 @@ void initForegroundTask() {
);
FlutterForegroundTask.startService(
notificationTitle: "걸음수",
notificationText: androidWalkingController.currentSteps.value.toString(),
notificationText: androidWalkingService.currentSteps.toString(),
callback: startCallback,
);
}
Expand All @@ -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(),
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/walking_service_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('지원하지 않는 플랫폼입니다.');
Expand Down

0 comments on commit 2e9caa6

Please sign in to comment.