From 3f54543104086ed921fb8e8fb53c0f2fc7d722fd Mon Sep 17 00:00:00 2001 From: MinUk Date: Wed, 2 Oct 2024 18:57:57 +0900 Subject: [PATCH] =?UTF-8?q?M3-385=20Refactor=20:=20=EB=B0=B1=EA=B7=B8?= =?UTF-8?q?=EB=9D=BC=EC=9A=B4=EB=93=9C=20=EB=B2=84=ED=8A=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/controllers/main_controller.dart | 5 +++- lib/controllers/map_controller.dart | 22 +++++++++++++- lib/main.dart | 3 -- lib/service/location_service.dart | 10 +++++-- .../map/bottom_sheet/bottom_stats.dart | 30 +++++++++++++++++-- 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/lib/controllers/main_controller.dart b/lib/controllers/main_controller.dart index aafaae98..2f0f2ac5 100644 --- a/lib/controllers/main_controller.dart +++ b/lib/controllers/main_controller.dart @@ -29,12 +29,15 @@ class MainController extends GetxController { await checkBatteryPermission(); } - Future checkLocationPermission() async { + Future checkLocationPermission() async { PermissionStatus status = await Permission.locationAlways.status; if (status != PermissionStatus.granted) { _showRequestLocationAlways(); + return false; } + + return true; } Future checkBatteryPermission() async { diff --git a/lib/controllers/map_controller.dart b/lib/controllers/map_controller.dart index 5887d986..d29ec033 100644 --- a/lib/controllers/map_controller.dart +++ b/lib/controllers/map_controller.dart @@ -26,6 +26,7 @@ import '../utils/walking_service_factory.dart'; import '../widgets/map/filter_bottom_sheet.dart'; import '../widgets/pixel.dart'; import 'bottom_sheet_controller.dart'; +import 'main_controller.dart'; import 'my_page_controller.dart'; class MapController extends SuperController { @@ -82,6 +83,8 @@ class MapController extends SuperController { Timer? _timer; RxBool isRunning = false.obs; + RxBool isBackgroundEnabled = false.obs; + @override void onInit() async { super.onInit(); @@ -144,7 +147,9 @@ class MapController extends SuperController { void onCameraIdle() { if (!isBottomSheetShowUp) { - _cameraIdleTimer = Timer(Duration(milliseconds: 300), updateMap); + _cameraIdleTimer = Timer(Duration(milliseconds: 300), () { + updateMap(); + }); } } @@ -574,4 +579,19 @@ class MapController extends SuperController { Future deleteMyPlaceFromLocalStorage(String place) async { await box.remove(place); } + + void changeBackgroundMode(bool changedValue) async { + if (changedValue) { + bool isLocationAlwaysEnabled = await Get.find().checkLocationPermission(); + if (!isLocationAlwaysEnabled) { + return; + } + + LocationService().enableBackgroundLocation(); + isBackgroundEnabled.value = changedValue; + } else { + LocationService().disableBackgroundLocation(); + isBackgroundEnabled.value = changedValue; + } + } } diff --git a/lib/main.dart b/lib/main.dart index b5d3af67..141ae785 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -20,7 +20,6 @@ import 'screens/setting_screen.dart'; import 'screens/sign_up_screen.dart'; import 'service/alarm_service.dart'; import 'service/auth_service.dart'; -import 'service/location_service.dart'; import 'utils/user_manager.dart'; import 'utils/version_check.dart'; @@ -56,8 +55,6 @@ Future main() async { KakaoSdk.init(nativeAppKey: dotenv.env['NATIVE_APP_KEY']!); - LocationService().initBackgroundLocation(); - String initialRoute = await AuthService().isLogin() ? '/main' : '/permission'; VersionCheck versionCheck = VersionCheck(); diff --git a/lib/service/location_service.dart b/lib/service/location_service.dart index a3336f48..d36798b0 100644 --- a/lib/service/location_service.dart +++ b/lib/service/location_service.dart @@ -40,13 +40,17 @@ class LocationService { return locationHistory.getCurrentLocationSpeed(); } - initBackgroundLocation() { - location.enableBackgroundMode(enable: true); - location.changeNotificationOptions( + Future enableBackgroundLocation() async { + await location.changeNotificationOptions( title: '땅 따먹기 중!', subtitle: '백그라운드 작동 중입니다.', iconName: 'drawable/background_app_icon', ); + location.enableBackgroundMode(enable: true); + } + + void disableBackgroundLocation() { + location.enableBackgroundMode(enable: false); } calculateSpeed(LocationData previousLocation, LocationData currentLocation) { diff --git a/lib/widgets/map/bottom_sheet/bottom_stats.dart b/lib/widgets/map/bottom_sheet/bottom_stats.dart index 23117b6a..5684d4da 100644 --- a/lib/widgets/map/bottom_sheet/bottom_stats.dart +++ b/lib/widgets/map/bottom_sheet/bottom_stats.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import '../../../constants/app_colors.dart'; +import '../../../constants/text_styles.dart'; import '../../../controllers/map_controller.dart'; import '../../../screens/explore_mode_screen.dart'; @@ -10,9 +11,34 @@ class BottomStats extends StatelessWidget { @override Widget build(BuildContext context) { + final MapController mapController = Get.find(); + return Row( - crossAxisAlignment: CrossAxisAlignment.end, - children: [], + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Text( + '백그라운드 모드 ', + style: TextStyles.fs17w400cTextSecondary, + ), + Obx(() => + Switch( + value: mapController.isBackgroundEnabled.value, + onChanged: mapController.changeBackgroundMode, + activeColor: AppColors.primary, + inactiveThumbColor: AppColors.boxColorSecond, + inactiveTrackColor: AppColors.backgroundThird, + trackOutlineColor: WidgetStateProperty.resolveWith( + (final Set states) { + if (states.contains(WidgetState.selected)) { + return null; + } + return AppColors.backgroundThird; + }, + ), + ), + ), + ], ); } }