2024-04-09 01:58:25 +09:00
|
|
|
|
import 'dart:async';
|
2024-05-02 11:51:52 +09:00
|
|
|
|
import 'dart:io';
|
2024-04-09 01:58:25 +09:00
|
|
|
|
//import 'dart:convert';
|
|
|
|
|
|
//import 'dart:developer';
|
2024-05-02 11:51:52 +09:00
|
|
|
|
import 'package:rogapp/model/gps_data.dart';
|
2024-07-19 16:54:49 +09:00
|
|
|
|
//import 'package:rogapp/pages/home/home_page.dart';
|
2024-05-02 11:51:52 +09:00
|
|
|
|
import 'package:rogapp/utils/database_gps.dart';
|
2022-03-14 12:28:57 +05:30
|
|
|
|
import 'package:flutter/material.dart';
|
2023-03-17 11:54:12 +05:30
|
|
|
|
import 'package:flutter_map_tile_caching/flutter_map_tile_caching.dart';
|
2024-05-02 11:51:52 +09:00
|
|
|
|
import 'package:geolocator/geolocator.dart';
|
2022-03-14 12:28:57 +05:30
|
|
|
|
import 'package:get/get.dart';
|
2024-04-09 01:58:25 +09:00
|
|
|
|
//import 'package:vm_service/vm_service.dart';
|
|
|
|
|
|
//import 'package:dart_vm_info/dart_vm_info.dart';
|
2024-08-07 14:24:05 +09:00
|
|
|
|
import 'package:timezone/data/latest.dart' as tz;
|
2024-04-09 01:58:25 +09:00
|
|
|
|
|
2024-04-26 18:44:22 +09:00
|
|
|
|
import 'package:rogapp/pages/settings/settings_controller.dart';
|
|
|
|
|
|
|
2023-08-16 14:53:32 +05:30
|
|
|
|
import 'package:rogapp/pages/destination/destination_controller.dart';
|
2022-05-12 02:17:08 +05:30
|
|
|
|
import 'package:rogapp/pages/index/index_binding.dart';
|
2024-04-30 17:08:23 +09:00
|
|
|
|
import 'package:rogapp/pages/index/index_controller.dart';
|
2022-03-14 12:28:57 +05:30
|
|
|
|
import 'package:rogapp/routes/app_pages.dart';
|
2024-02-06 15:22:27 +05:30
|
|
|
|
import 'package:rogapp/utils/location_controller.dart';
|
2022-03-14 12:28:57 +05:30
|
|
|
|
import 'package:rogapp/utils/string_values.dart';
|
2024-07-15 06:54:02 +09:00
|
|
|
|
import 'package:rogapp/widgets/debug_widget.dart';
|
2023-09-08 21:19:12 +05:30
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-09-04 22:46:53 +05:30
|
|
|
|
// import 'package:is_lock_screen/is_lock_screen.dart';
|
2023-03-17 11:54:12 +05:30
|
|
|
|
|
2024-04-04 21:33:07 +09:00
|
|
|
|
import 'package:rogapp/services/device_info_service.dart';
|
|
|
|
|
|
import 'package:rogapp/services/error_service.dart';
|
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-04-09 15:06:41 +09:00
|
|
|
|
//import 'dart:async';
|
|
|
|
|
|
//import 'package:get/get.dart';
|
2024-05-02 11:51:52 +09:00
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
|
2024-05-24 07:21:28 +09:00
|
|
|
|
import 'pages/permission/permission.dart';
|
2024-07-27 09:46:00 +09:00
|
|
|
|
import 'package:rogapp/services/api_service.dart';
|
2024-04-04 21:33:07 +09:00
|
|
|
|
|
|
|
|
|
|
Map<String, dynamic> deviceInfo = {};
|
|
|
|
|
|
|
2024-04-30 17:08:23 +09:00
|
|
|
|
/*
|
2023-09-11 00:45:54 +05:30
|
|
|
|
void saveGameState() async {
|
2023-10-06 16:25:21 +05:30
|
|
|
|
DestinationController destinationController =
|
|
|
|
|
|
Get.find<DestinationController>();
|
|
|
|
|
|
SharedPreferences pref = await SharedPreferences.getInstance();
|
|
|
|
|
|
pref.setBool("is_in_rog", destinationController.isInRog.value);
|
|
|
|
|
|
pref.setBool(
|
|
|
|
|
|
"rogaining_counted", destinationController.rogainingCounted.value);
|
2024-01-18 19:58:00 +05:30
|
|
|
|
pref.setBool("ready_for_goal", DestinationController.ready_for_goal);
|
2023-10-06 16:25:21 +05:30
|
|
|
|
}
|
2024-04-30 17:08:23 +09:00
|
|
|
|
*/
|
2023-09-11 00:45:54 +05:30
|
|
|
|
|
2024-04-30 17:08:23 +09:00
|
|
|
|
// 現在のユーザーのIDも一緒に保存するようにします。
|
|
|
|
|
|
void saveGameState() async {
|
|
|
|
|
|
DestinationController destinationController =
|
|
|
|
|
|
Get.find<DestinationController>();
|
|
|
|
|
|
IndexController indexController = Get.find<IndexController>();
|
|
|
|
|
|
SharedPreferences pref = await SharedPreferences.getInstance();
|
2024-05-06 00:06:38 +09:00
|
|
|
|
debugPrint("indexController.currentUser = ${indexController.currentUser}");
|
|
|
|
|
|
if(indexController.currentUser.isNotEmpty) {
|
|
|
|
|
|
pref.setInt("user_id", indexController.currentUser[0]["user"]["id"]);
|
|
|
|
|
|
}else{
|
|
|
|
|
|
debugPrint("User is empty....");
|
|
|
|
|
|
}
|
2024-04-30 17:08:23 +09:00
|
|
|
|
pref.setBool("is_in_rog", destinationController.isInRog.value);
|
|
|
|
|
|
pref.setBool(
|
|
|
|
|
|
"rogaining_counted", destinationController.rogainingCounted.value);
|
|
|
|
|
|
pref.setBool("ready_for_goal", DestinationController.ready_for_goal);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2023-10-06 16:25:21 +05:30
|
|
|
|
void restoreGame() async {
|
|
|
|
|
|
SharedPreferences pref = await SharedPreferences.getInstance();
|
|
|
|
|
|
DestinationController destinationController =
|
|
|
|
|
|
Get.find<DestinationController>();
|
|
|
|
|
|
destinationController.skipGps = false;
|
|
|
|
|
|
destinationController.isInRog.value = pref.getBool("is_in_rog") ?? false;
|
|
|
|
|
|
destinationController.rogainingCounted.value =
|
|
|
|
|
|
pref.getBool("rogaining_counted") ?? false;
|
2024-01-18 19:58:00 +05:30
|
|
|
|
DestinationController.ready_for_goal =
|
|
|
|
|
|
pref.getBool("ready_for_goal") ?? false;
|
2023-11-27 14:57:25 +05:30
|
|
|
|
//print(
|
|
|
|
|
|
// "--restored -- destinationController.isInRog.value ${pref.getBool("is_in_rog")} -- ${pref.getBool("rogaining_counted")}");
|
2023-10-06 16:25:21 +05:30
|
|
|
|
}
|
2024-04-30 17:08:23 +09:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void restoreGame() async {
|
|
|
|
|
|
SharedPreferences pref = await SharedPreferences.getInstance();
|
|
|
|
|
|
IndexController indexController = Get.find<IndexController>();
|
|
|
|
|
|
int? savedUserId = pref.getInt("user_id");
|
2024-05-06 00:06:38 +09:00
|
|
|
|
//int? currUserId = indexController.currentUser[0]['user']['id'];
|
|
|
|
|
|
//debugPrint("savedUserId=${savedUserId}, currentUser=${currUserId}");
|
2024-04-30 17:08:23 +09:00
|
|
|
|
if (indexController.currentUser.isNotEmpty &&
|
|
|
|
|
|
indexController.currentUser[0]["user"]["id"] == savedUserId) {
|
|
|
|
|
|
DestinationController destinationController =
|
|
|
|
|
|
Get.find<DestinationController>();
|
|
|
|
|
|
destinationController.skipGps = false;
|
|
|
|
|
|
destinationController.isInRog.value = pref.getBool("is_in_rog") ?? false;
|
|
|
|
|
|
destinationController.rogainingCounted.value =
|
|
|
|
|
|
pref.getBool("rogaining_counted") ?? false;
|
|
|
|
|
|
DestinationController.ready_for_goal =
|
|
|
|
|
|
pref.getBool("ready_for_goal") ?? false;
|
2024-07-27 09:46:00 +09:00
|
|
|
|
await Get.putAsync(() => ApiService().init());
|
2024-04-30 17:08:23 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-09-11 00:45:54 +05:30
|
|
|
|
|
2023-03-17 11:54:12 +05:30
|
|
|
|
void main() async {
|
|
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2023-09-04 22:46:53 +05:30
|
|
|
|
await FlutterMapTileCaching.initialise();
|
2024-04-04 21:33:07 +09:00
|
|
|
|
|
2023-03-17 11:54:12 +05:30
|
|
|
|
final StoreDirectory instanceA = FMTC.instance('OpenStreetMap (A)');
|
2023-09-04 22:46:53 +05:30
|
|
|
|
await instanceA.manage.createAsync();
|
|
|
|
|
|
await instanceA.metadata.addAsync(
|
|
|
|
|
|
key: 'sourceURL',
|
|
|
|
|
|
value: 'https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png',
|
|
|
|
|
|
);
|
|
|
|
|
|
await instanceA.metadata.addAsync(
|
|
|
|
|
|
key: 'validDuration',
|
|
|
|
|
|
value: '14',
|
|
|
|
|
|
);
|
|
|
|
|
|
await instanceA.metadata.addAsync(
|
|
|
|
|
|
key: 'behaviour',
|
|
|
|
|
|
value: 'cacheFirst',
|
|
|
|
|
|
);
|
2024-04-04 21:33:07 +09:00
|
|
|
|
|
|
|
|
|
|
deviceInfo = await DeviceInfoService.getDeviceInfo();
|
|
|
|
|
|
|
|
|
|
|
|
FlutterError.onError = (FlutterErrorDetails details) {
|
|
|
|
|
|
FlutterError.presentError(details);
|
2024-04-09 15:06:41 +09:00
|
|
|
|
Get.log('Flutter error: ${details.exception}');
|
|
|
|
|
|
Get.log('Stack trace: ${details.stack}');
|
2024-07-15 06:54:02 +09:00
|
|
|
|
ErrorService.reportError(details.exception, details.stack ?? StackTrace.current, deviceInfo, LogManager().operationLogs);
|
2024-04-04 21:33:07 +09:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-04-09 15:06:41 +09:00
|
|
|
|
//Get.put(LocationController());
|
|
|
|
|
|
|
2024-05-24 07:21:28 +09:00
|
|
|
|
//await PermissionController.checkAndRequestPermissions();
|
|
|
|
|
|
//requestLocationPermission();
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
|
2024-04-09 01:58:25 +09:00
|
|
|
|
// startMemoryMonitoring(); // 2024-4-8 Akira: メモリ使用量のチェックを開始 See #2810
|
2024-04-26 18:44:22 +09:00
|
|
|
|
Get.put(SettingsController()); // これを追加
|
2024-04-09 01:58:25 +09:00
|
|
|
|
|
2024-07-19 16:54:49 +09:00
|
|
|
|
|
2024-05-06 00:06:38 +09:00
|
|
|
|
/*
|
2024-04-04 21:33:07 +09:00
|
|
|
|
runZonedGuarded(() {
|
|
|
|
|
|
runApp(const ProviderScope(child: MyApp()));
|
|
|
|
|
|
}, (error, stackTrace) {
|
|
|
|
|
|
ErrorService.reportError(error, stackTrace, deviceInfo);
|
|
|
|
|
|
});
|
2024-05-06 00:06:38 +09:00
|
|
|
|
*/
|
|
|
|
|
|
|
2024-07-27 09:46:00 +09:00
|
|
|
|
try {
|
2024-08-07 14:24:05 +09:00
|
|
|
|
tz.initializeTimeZones();
|
|
|
|
|
|
|
2024-07-27 09:46:00 +09:00
|
|
|
|
// ApiServiceを初期化
|
|
|
|
|
|
//await Get.putAsync(() => ApiService().init());
|
|
|
|
|
|
await initServices();
|
2024-07-19 16:54:49 +09:00
|
|
|
|
|
2024-07-27 09:46:00 +09:00
|
|
|
|
runApp(const ProviderScope(child: MyApp()));
|
|
|
|
|
|
//runApp(HomePage()); // MyApp()からHomePage()に変更
|
|
|
|
|
|
//runApp(const MyApp());
|
|
|
|
|
|
}catch(e, stackTrace){
|
|
|
|
|
|
print('Error during initialization: $e');
|
|
|
|
|
|
print('Stack trace: $stackTrace');
|
|
|
|
|
|
}
|
2022-03-14 12:28:57 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-27 09:46:00 +09:00
|
|
|
|
Future<void> initServices() async {
|
|
|
|
|
|
print('Starting services ...');
|
|
|
|
|
|
try {
|
|
|
|
|
|
await Get.putAsync(() => ApiService().init());
|
|
|
|
|
|
print('All services started...');
|
|
|
|
|
|
}catch(e){
|
|
|
|
|
|
print('Error initializing ApiService: $e');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
Get.put(SettingsController());
|
|
|
|
|
|
print('SettingsController initialized successfully');
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print('Error initializing SettingsController: $e');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print('All services started...');
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-07-15 06:54:02 +09:00
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
Future<void> requestLocationPermission() async {
|
|
|
|
|
|
try {
|
|
|
|
|
|
final status = await Permission.locationAlways.request();
|
|
|
|
|
|
if (status == PermissionStatus.granted) {
|
|
|
|
|
|
print('Location permission granted');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
print('Location permission denied');
|
|
|
|
|
|
//await showLocationPermissionDeniedDialog(); // 追加
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print('Error requesting location permission: $e');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-07-15 06:54:02 +09:00
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
|
|
|
|
|
|
|
2024-04-09 01:58:25 +09:00
|
|
|
|
// メモリ使用量の解説:https://qiita.com/hukusuke1007/items/e4e987836412e9bc73b9
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
// 2024-4-8 Akira: メモリ使用量のチェックのため追加 See #2810
|
|
|
|
|
|
// startMemoryMonitoring関数が5分ごとに呼び出され、メモリ使用量をチェックします。
|
|
|
|
|
|
// メモリ使用量が閾値(ここでは500MB)を超えた場合、エラーメッセージとスタックトレースをレポートします。
|
|
|
|
|
|
//
|
|
|
|
|
|
void startMemoryMonitoring() {
|
|
|
|
|
|
const threshold = 500 * 1024 * 1024; // 500MB
|
|
|
|
|
|
|
|
|
|
|
|
// メモリ使用量情報を取得
|
|
|
|
|
|
final memoryUsage = MemoryUsage.fromJson(DartVMInfo.getAllocationProfile());
|
|
|
|
|
|
|
|
|
|
|
|
if (memoryUsage.heapUsage > threshold) {
|
|
|
|
|
|
final now = DateTime.now().toIso8601String();
|
|
|
|
|
|
final message = 'High memory usage detected at $now: ${memoryUsage.heapUsage} bytes';
|
|
|
|
|
|
print(message);
|
|
|
|
|
|
reportError(message, StackTrace.current);
|
|
|
|
|
|
showMemoryWarningDialog();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timer(const Duration(minutes: 5), startMemoryMonitoring);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MemoryUsage {
|
|
|
|
|
|
final int heapUsage;
|
|
|
|
|
|
|
|
|
|
|
|
MemoryUsage({required this.heapUsage});
|
|
|
|
|
|
|
|
|
|
|
|
factory MemoryUsage.fromJson(Map<String, dynamic> json) {
|
|
|
|
|
|
return MemoryUsage(
|
|
|
|
|
|
heapUsage: json['heapUsage'] as int,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// 2024-4-8 Akira: メモリ使用量のチェックのため追加 See #2810
|
|
|
|
|
|
// reportError関数でエラーレポートを送信します。具体的な実装は、利用するエラー報告サービスによって異なります。
|
|
|
|
|
|
void reportError(String message, StackTrace stackTrace) async {
|
|
|
|
|
|
// エラーレポートの送信処理を実装
|
|
|
|
|
|
// 例: SentryやFirebase Crashlyticsなどのエラー報告サービスを利用
|
|
|
|
|
|
print("ReportError : ${message} . stacktrace : ${stackTrace}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2024-4-8 Akira: メモリ使用量のチェックのため追加 See #2810
|
|
|
|
|
|
// showMemoryWarningDialog関数で、メモリ使用量が高い場合にユーザーに警告ダイアログを表示します。
|
|
|
|
|
|
//
|
|
|
|
|
|
void showMemoryWarningDialog() {
|
|
|
|
|
|
if (Get.context != null) {
|
|
|
|
|
|
showDialog(
|
|
|
|
|
|
context: Get.context!,
|
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
|
title: const Text('メモリ使用量の警告'),
|
|
|
|
|
|
content: const Text('アプリのメモリ使用量が高くなっています。アプリを再起動することをお勧めします。'),
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
TextButton(
|
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
|
|
child: const Text('OK'),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
StreamSubscription<Position>? positionStream;
|
|
|
|
|
|
bool background=false;
|
|
|
|
|
|
DateTime lastGPSCollectedTime=DateTime.now();
|
|
|
|
|
|
String team_name="";
|
|
|
|
|
|
String event_code="";
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> startBackgroundTracking() async {
|
|
|
|
|
|
if (Platform.isIOS && background==false) {
|
|
|
|
|
|
|
|
|
|
|
|
final IndexController indexController = Get.find<IndexController>();
|
|
|
|
|
|
if(indexController.currentUser.length>0) {
|
|
|
|
|
|
team_name = indexController.currentUser[0]["user"]['team_name'];
|
|
|
|
|
|
event_code = indexController.currentUser[0]["user"]["event_code"];
|
|
|
|
|
|
}
|
|
|
|
|
|
background = true;
|
|
|
|
|
|
debugPrint("バックグラウンド処理を開始しました。");
|
|
|
|
|
|
final LocationSettings locationSettings = LocationSettings(
|
|
|
|
|
|
accuracy: LocationAccuracy.high,
|
|
|
|
|
|
distanceFilter: 100,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
positionStream = Geolocator.getPositionStream(locationSettings: locationSettings)
|
|
|
|
|
|
.listen((Position? position) async {
|
|
|
|
|
|
if (position != null) {
|
|
|
|
|
|
final lat = position.latitude;
|
|
|
|
|
|
final lng = position.longitude;
|
|
|
|
|
|
//final timestamp = DateTime.now();
|
|
|
|
|
|
final accuracy = position.accuracy;
|
|
|
|
|
|
|
|
|
|
|
|
// GPS信号強度がlowの場合はスキップ
|
|
|
|
|
|
if (accuracy > 100) {
|
|
|
|
|
|
debugPrint("GPS signal strength is low. Skipping data saving.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Duration difference = lastGPSCollectedTime.difference(DateTime.now())
|
|
|
|
|
|
.abs();
|
|
|
|
|
|
// 最後にGPS信号を取得した時刻から10秒以上経過、かつ10m以上経過(普通に歩くスピード)
|
|
|
|
|
|
//debugPrint("時間差:${difference}");
|
|
|
|
|
|
if (difference.inSeconds >= 10 ) {
|
|
|
|
|
|
debugPrint("バックグラウンドでのGPS取得時の処理(10secおき) count=${difference.inSeconds}, time=${DateTime.now()}");
|
|
|
|
|
|
|
|
|
|
|
|
// DBにGPSデータを保存 pages/destination/destination_controller.dart
|
|
|
|
|
|
await addGPStoDB(lat, lng);
|
|
|
|
|
|
|
|
|
|
|
|
lastGPSCollectedTime = DateTime.now();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}, onError: (error) {
|
|
|
|
|
|
if (error is LocationServiceDisabledException) {
|
|
|
|
|
|
print('Location services are disabled');
|
|
|
|
|
|
} else if (error is PermissionDeniedException) {
|
|
|
|
|
|
print('Location permissions are denied');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
print('Location Error: $error');
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print('Error starting background tracking: $e');
|
|
|
|
|
|
}
|
2024-05-24 07:21:28 +09:00
|
|
|
|
}else if (Platform.isAndroid && background == false) {
|
|
|
|
|
|
background = true;
|
|
|
|
|
|
debugPrint("バックグラウンド処理を開始しました。");
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 位置情報の権限が許可されているかを確認
|
|
|
|
|
|
await PermissionController.checkAndRequestPermissions();
|
|
|
|
|
|
}catch(e){
|
|
|
|
|
|
print('Error starting background tracking: $e');
|
|
|
|
|
|
}
|
2024-05-02 11:51:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> addGPStoDB(double la, double ln) async {
|
|
|
|
|
|
//debugPrint("in addGPStoDB ${indexController.currentUser}");
|
|
|
|
|
|
GpsDatabaseHelper db = GpsDatabaseHelper.instance;
|
|
|
|
|
|
try {
|
|
|
|
|
|
GpsData gps_data = GpsData(
|
|
|
|
|
|
id: 0,
|
|
|
|
|
|
team_name: team_name,
|
|
|
|
|
|
event_code: event_code,
|
|
|
|
|
|
lat: la,
|
|
|
|
|
|
lon: ln,
|
|
|
|
|
|
is_checkin: 0,
|
|
|
|
|
|
created_at: DateTime.now().millisecondsSinceEpoch);
|
|
|
|
|
|
var res = await db.insertGps(gps_data);
|
2024-07-19 16:54:49 +09:00
|
|
|
|
debugPrint("バックグラウンドでのGPS保存:");
|
2024-05-02 11:51:52 +09:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
print("errr ready gps ${err}");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> stopBackgroundTracking() async {
|
|
|
|
|
|
if (Platform.isIOS && background==true) {
|
|
|
|
|
|
background=false;
|
|
|
|
|
|
debugPrint("バックグラウンド処理:停止しました。");
|
|
|
|
|
|
await positionStream?.cancel();
|
|
|
|
|
|
positionStream = null;
|
2024-05-06 00:06:38 +09:00
|
|
|
|
}else if(Platform.isAndroid && background==true){
|
|
|
|
|
|
background=false;
|
|
|
|
|
|
debugPrint("バックグラウンド処理:停止しました。");
|
2024-05-24 07:21:28 +09:00
|
|
|
|
const platform = MethodChannel('location');
|
|
|
|
|
|
try {
|
|
|
|
|
|
await platform.invokeMethod('stopLocationService');
|
|
|
|
|
|
} on PlatformException catch (e) {
|
|
|
|
|
|
print("Failed to stop location service: '${e.message}'.");
|
|
|
|
|
|
}
|
2024-05-02 11:51:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-16 14:53:32 +05:30
|
|
|
|
class MyApp extends StatefulWidget {
|
2023-09-04 22:46:53 +05:30
|
|
|
|
const MyApp({Key? key}) : super(key: key);
|
2023-08-16 14:53:32 +05:30
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
State<MyApp> createState() => _MyAppState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
2022-03-14 12:28:57 +05:30
|
|
|
|
// This widget is the root of your application.
|
2023-08-16 14:53:32 +05:30
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
super.initState();
|
2023-11-21 22:43:28 +05:30
|
|
|
|
if (context.mounted) {
|
|
|
|
|
|
restoreGame();
|
|
|
|
|
|
}
|
2023-09-04 22:46:53 +05:30
|
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2024-05-02 11:51:52 +09:00
|
|
|
|
|
2024-07-19 16:54:49 +09:00
|
|
|
|
// ウィジェットが構築された後に権限をチェック
|
2024-07-15 06:54:02 +09:00
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2024-07-19 16:54:49 +09:00
|
|
|
|
PermissionController.checkAndRequestPermissions();
|
2024-05-24 07:21:28 +09:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
debugPrint("Start MyAppState...");
|
2023-08-16 14:53:32 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-19 16:54:49 +09:00
|
|
|
|
/*
|
|
|
|
|
|
void showPermissionRequiredDialog() {
|
2024-07-15 06:54:02 +09:00
|
|
|
|
showDialog(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
barrierDismissible: false,
|
|
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
|
|
return AlertDialog(
|
2024-07-19 16:54:49 +09:00
|
|
|
|
title: Text('権限が必要です'),
|
|
|
|
|
|
content: Text('このアプリは機能するために位置情報の権限が必要です。設定で権限を許可してください。'),
|
2024-07-15 06:54:02 +09:00
|
|
|
|
actions: <Widget>[
|
|
|
|
|
|
TextButton(
|
2024-07-19 16:54:49 +09:00
|
|
|
|
child: Text('設定を開く'),
|
2024-07-15 06:54:02 +09:00
|
|
|
|
onPressed: () {
|
2024-07-19 16:54:49 +09:00
|
|
|
|
openAppSettings();
|
2024-07-15 06:54:02 +09:00
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
TextButton(
|
2024-07-19 16:54:49 +09:00
|
|
|
|
child: Text('アプリを終了'),
|
2024-07-15 06:54:02 +09:00
|
|
|
|
onPressed: () {
|
2024-07-19 16:54:49 +09:00
|
|
|
|
// アプリを終了
|
2024-07-15 06:54:02 +09:00
|
|
|
|
Navigator.of(context).pop();
|
2024-07-19 16:54:49 +09:00
|
|
|
|
// よりクリーンな終了のために 'flutter_exit_app' のようなプラグインを使用することをお勧めします
|
|
|
|
|
|
// 今回は単にすべてのルートをポップします
|
|
|
|
|
|
Navigator.of(context).popUntil((route) => false);
|
2024-07-15 06:54:02 +09:00
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2024-05-02 11:51:52 +09:00
|
|
|
|
|
2024-07-19 16:54:49 +09:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-16 14:53:32 +05:30
|
|
|
|
@override
|
2023-09-04 22:46:53 +05:30
|
|
|
|
void dispose() {
|
|
|
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
|
|
|
|
super.dispose();
|
2023-08-16 14:53:32 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-11 00:45:54 +05:30
|
|
|
|
// void saveGameState() async {
|
|
|
|
|
|
// DestinationController destinationController = Get.find<DestinationController>();
|
|
|
|
|
|
// SharedPreferences pref = await SharedPreferences.getInstance();
|
|
|
|
|
|
// pref.setBool("is_in_rog", destinationController.is_in_rog.value);
|
|
|
|
|
|
// pref.setBool("rogaining_counted", destinationController.rogaining_counted.value);
|
|
|
|
|
|
// }
|
2023-09-08 21:19:12 +05:30
|
|
|
|
|
2023-10-06 16:25:21 +05:30
|
|
|
|
@override
|
2023-09-04 22:46:53 +05:30
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
2024-02-06 15:22:27 +05:30
|
|
|
|
LocationController locationController = Get.find<LocationController>();
|
2024-05-02 11:51:52 +09:00
|
|
|
|
DestinationController destinationController = Get.find<DestinationController>();
|
2024-02-06 15:22:27 +05:30
|
|
|
|
|
2024-04-09 15:06:41 +09:00
|
|
|
|
//DestinationController destinationController =
|
|
|
|
|
|
// Get.find<DestinationController>();
|
2023-10-06 16:25:21 +05:30
|
|
|
|
switch (state) {
|
|
|
|
|
|
case AppLifecycleState.resumed:
|
2024-05-06 00:06:38 +09:00
|
|
|
|
// バックグラウンド処理を停止
|
2024-05-02 11:51:52 +09:00
|
|
|
|
if (Platform.isIOS && destinationController.isRunningBackgroundGPS) {
|
2024-05-06 00:06:38 +09:00
|
|
|
|
// Foreground に戻った時の処理
|
|
|
|
|
|
debugPrint(" ==(Status Changed)==> RESUMED. フォアグラウンドに戻りました");
|
|
|
|
|
|
locationController.resumePositionStream();
|
|
|
|
|
|
//print("RESUMED");
|
|
|
|
|
|
restoreGame();
|
|
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
stopBackgroundTracking();
|
|
|
|
|
|
destinationController.isRunningBackgroundGPS=false;
|
|
|
|
|
|
destinationController.restartGPS();
|
|
|
|
|
|
|
2024-05-06 00:06:38 +09:00
|
|
|
|
} else if (Platform.isAndroid ) {
|
|
|
|
|
|
if( destinationController.isRunningBackgroundGPS ){
|
|
|
|
|
|
const platform = MethodChannel('location');
|
|
|
|
|
|
platform.invokeMethod('stopLocationService');
|
|
|
|
|
|
destinationController.isRunningBackgroundGPS=false;
|
|
|
|
|
|
destinationController.restartGPS();
|
|
|
|
|
|
debugPrint("stopped android location service..");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
debugPrint("==(Status Changed)==> RESUMED. android フォアグラウンドに戻りました");
|
|
|
|
|
|
locationController.resumePositionStream();
|
|
|
|
|
|
//print("RESUMED");
|
|
|
|
|
|
restoreGame();
|
|
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
debugPrint("==(Status Changed)==> RESUMED 不明状態");
|
2024-05-02 11:51:52 +09:00
|
|
|
|
}
|
2023-10-06 16:25:21 +05:30
|
|
|
|
break;
|
|
|
|
|
|
case AppLifecycleState.inactive:
|
2024-05-02 11:51:52 +09:00
|
|
|
|
// アプリが非アクティブになったときに発生します。
|
|
|
|
|
|
|
2024-05-06 00:06:38 +09:00
|
|
|
|
if (Platform.isIOS && !destinationController.isRunningBackgroundGPS) { // iOSはバックグラウンドでもフロントの処理が生きている。
|
|
|
|
|
|
// これは、別のアプリやシステムのオーバーレイ(着信通話やアラームなど)によって一時的に中断された状態です。
|
|
|
|
|
|
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
|
|
|
|
|
|
//locationController.resumePositionStream();
|
|
|
|
|
|
|
|
|
|
|
|
// 追加: フロントエンドのGPS信号のlistenを停止
|
|
|
|
|
|
locationController.stopPositionStream();
|
2024-05-02 11:51:52 +09:00
|
|
|
|
|
|
|
|
|
|
destinationController.isRunningBackgroundGPS=true;
|
|
|
|
|
|
startBackgroundTracking();
|
2024-05-06 00:06:38 +09:00
|
|
|
|
}else if(Platform.isAndroid && !destinationController.isRunningBackgroundGPS){
|
|
|
|
|
|
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
|
|
|
|
|
|
}else{
|
|
|
|
|
|
debugPrint("==(Status Changed)==> INACTIVE 不明状態");
|
|
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
saveGameState();
|
2023-10-06 16:25:21 +05:30
|
|
|
|
break;
|
|
|
|
|
|
case AppLifecycleState.paused:
|
2024-05-02 11:51:52 +09:00
|
|
|
|
// バックグラウンドに移行したときの処理
|
|
|
|
|
|
//locationController.resumePositionStream();
|
|
|
|
|
|
debugPrint(" ==(Status Changed)==> PAUSED. バックグラウンド処理。");
|
2024-05-06 00:06:38 +09:00
|
|
|
|
if (Platform.isIOS && !destinationController.isRunningBackgroundGPS) {
|
|
|
|
|
|
debugPrint("iOS already running background GPS processing when it's inactive");
|
|
|
|
|
|
|
|
|
|
|
|
} else if(Platform.isAndroid && !destinationController.isRunningBackgroundGPS) {
|
|
|
|
|
|
debugPrint(
|
|
|
|
|
|
" ==(Status Changed)==> PAUSED. Android バックグラウンド処理。");
|
|
|
|
|
|
locationController.stopPositionStream();
|
|
|
|
|
|
const platform = MethodChannel('location');
|
|
|
|
|
|
platform.invokeMethod('startLocationService');
|
|
|
|
|
|
//platform.invokeMethod('stopLocationService');
|
|
|
|
|
|
destinationController.isRunningBackgroundGPS = true;
|
|
|
|
|
|
//startBackgroundTracking();
|
|
|
|
|
|
}
|
2023-10-06 16:25:21 +05:30
|
|
|
|
saveGameState();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AppLifecycleState.detached:
|
2024-05-02 11:51:52 +09:00
|
|
|
|
// アプリが終了する直前に発生します。この状態では、アプリはメモリから解放される予定です。
|
|
|
|
|
|
//locationController.resumePositionStream();
|
|
|
|
|
|
debugPrint(" ==(Status Changed)==> DETACHED アプリは終了します。");
|
2023-10-06 16:25:21 +05:30
|
|
|
|
saveGameState();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AppLifecycleState.hidden:
|
2024-05-02 11:51:52 +09:00
|
|
|
|
// Web用の特殊な状態で、モバイルアプリでは発生しません。
|
|
|
|
|
|
//locationController.resumePositionStream();
|
|
|
|
|
|
debugPrint(" ==(Status Changed)==> Hidden アプリが隠れた");
|
2023-10-06 16:25:21 +05:30
|
|
|
|
saveGameState();
|
|
|
|
|
|
break;
|
2023-09-04 22:46:53 +05:30
|
|
|
|
}
|
2023-10-06 16:25:21 +05:30
|
|
|
|
}
|
2023-08-16 14:53:32 +05:30
|
|
|
|
|
2024-05-02 11:51:52 +09:00
|
|
|
|
|
2022-03-14 12:28:57 +05:30
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-24 07:21:28 +09:00
|
|
|
|
|
2022-03-14 12:28:57 +05:30
|
|
|
|
return GetMaterialApp(
|
|
|
|
|
|
translations: StringValues(),
|
|
|
|
|
|
locale: const Locale('ja', 'JP'),
|
2022-10-30 21:43:29 +05:30
|
|
|
|
//locale: const Locale('en', 'US'),
|
2022-03-14 12:28:57 +05:30
|
|
|
|
fallbackLocale: const Locale('en', 'US'),
|
|
|
|
|
|
title: 'ROGAINING',
|
|
|
|
|
|
theme: ThemeData(
|
2023-10-06 16:25:21 +05:30
|
|
|
|
colorScheme: ColorScheme.fromSeed(
|
|
|
|
|
|
seedColor: const Color.fromARGB(255, 36, 135, 221)),
|
2023-09-04 22:46:53 +05:30
|
|
|
|
useMaterial3: true,
|
2022-03-14 12:28:57 +05:30
|
|
|
|
),
|
|
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
|
|
defaultTransition: Transition.cupertino,
|
|
|
|
|
|
opaqueRoute: Get.isOpaqueRouteDefault,
|
|
|
|
|
|
popGesture: Get.isPopGestureEnable,
|
2022-04-17 11:45:21 +05:30
|
|
|
|
transitionDuration: const Duration(milliseconds: 230),
|
|
|
|
|
|
initialBinding: IndexBinding(), //HomeBinding(),
|
2022-07-20 15:57:40 +05:30
|
|
|
|
initialRoute: AppPages.PERMISSION,
|
2022-03-14 12:28:57 +05:30
|
|
|
|
getPages: AppPages.routes,
|
2022-07-09 22:51:34 +05:30
|
|
|
|
enableLog: true,
|
2022-03-14 12:28:57 +05:30
|
|
|
|
);
|
|
|
|
|
|
}
|
2024-05-24 07:21:28 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-14 12:28:57 +05:30
|
|
|
|
}
|