Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

백그라운드 동작 시작 버튼 만들기 #106

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/controllers/main_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ class MainController extends GetxController {
await checkBatteryPermission();
}

Future<void> checkLocationPermission() async {
Future<bool> checkLocationPermission() async {
PermissionStatus status = await Permission.locationAlways.status;

if (status != PermissionStatus.granted) {
_showRequestLocationAlways();
return false;
}

return true;
}

Future<void> checkBatteryPermission() async {
Expand Down
22 changes: 21 additions & 1 deletion lib/controllers/map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -82,6 +83,8 @@ class MapController extends SuperController {
Timer? _timer;
RxBool isRunning = false.obs;

RxBool isBackgroundEnabled = false.obs;

@override
void onInit() async {
super.onInit();
Expand Down Expand Up @@ -144,7 +147,9 @@ class MapController extends SuperController {

void onCameraIdle() {
if (!isBottomSheetShowUp) {
_cameraIdleTimer = Timer(Duration(milliseconds: 300), updateMap);
_cameraIdleTimer = Timer(Duration(milliseconds: 300), () {
updateMap();
});
}
}

Expand Down Expand Up @@ -574,4 +579,19 @@ class MapController extends SuperController {
Future<void> deleteMyPlaceFromLocalStorage(String place) async {
await box.remove(place);
}

void changeBackgroundMode(bool changedValue) async {
if (changedValue) {
bool isLocationAlwaysEnabled = await Get.find<MainController>().checkLocationPermission();
if (!isLocationAlwaysEnabled) {
return;
}

LocationService().enableBackgroundLocation();
isBackgroundEnabled.value = changedValue;
} else {
LocationService().disableBackgroundLocation();
isBackgroundEnabled.value = changedValue;
}
}
}
3 changes: 0 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -56,8 +55,6 @@ Future<void> main() async {

KakaoSdk.init(nativeAppKey: dotenv.env['NATIVE_APP_KEY']!);

LocationService().initBackgroundLocation();

String initialRoute = await AuthService().isLogin() ? '/main' : '/permission';

VersionCheck versionCheck = VersionCheck();
Expand Down
10 changes: 7 additions & 3 deletions lib/service/location_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ class LocationService {
return locationHistory.getCurrentLocationSpeed();
}

initBackgroundLocation() {
location.enableBackgroundMode(enable: true);
location.changeNotificationOptions(
Future<void> 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) {
Expand Down
30 changes: 28 additions & 2 deletions lib/widgets/map/bottom_sheet/bottom_stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -10,9 +11,34 @@ class BottomStats extends StatelessWidget {

@override
Widget build(BuildContext context) {
final MapController mapController = Get.find<MapController>();

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<WidgetState> states) {
if (states.contains(WidgetState.selected)) {
return null;
}
return AppColors.backgroundThird;
},
),
),
),
],
);
}
}
Expand Down