temporary update
This commit is contained in:
251
lib/main.dart
251
lib/main.dart
@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'dart:io';
|
||||
//import 'dart:convert';
|
||||
//import 'dart:developer';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:gifunavi/model/gps_data.dart';
|
||||
//import 'package:gifunavi/pages/home/home_page.dart';
|
||||
import 'package:gifunavi/utils/database_gps.dart';
|
||||
@ -198,6 +199,16 @@ void _showEventSelectionWarning() {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// main.dart の上部に追加
|
||||
const bool isDebugMode = true; // リリース時にfalseに変更
|
||||
// 各ファイルで使用
|
||||
void debugLog(String message) {
|
||||
if (isDebugMode) {
|
||||
debugPrint('DEBUG: $message');
|
||||
}
|
||||
}
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
@ -216,10 +227,7 @@ void main() async {
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
|
||||
await initServices();
|
||||
|
||||
runApp(const ProviderScope(child: MyApp()));
|
||||
}catch(e, stackTrace){
|
||||
print('Error during initialization: $e');
|
||||
@ -230,33 +238,89 @@ void main() async {
|
||||
}
|
||||
}
|
||||
|
||||
Future <void> _initializeControllers() async {
|
||||
final stopwatch = Stopwatch()..start();
|
||||
const timeout = Duration(seconds: 30); // タイムアウト時間を10秒から30秒に延長
|
||||
|
||||
try {
|
||||
while (!_areAllControllersRegistered()) {
|
||||
if (stopwatch.elapsed > timeout) {
|
||||
throw TimeoutException('Controller initialization timed out');
|
||||
}
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
final LocationController _locationController = Get.find<LocationController>();
|
||||
final IndexController _indexController = Get.find<IndexController>();
|
||||
final DestinationController _destinationController = Get.find<DestinationController>();
|
||||
final PermissionController _permissionController = Get.find<PermissionController>();
|
||||
|
||||
print('All controllers initialized successfully');
|
||||
} catch (e) {
|
||||
print('Error initializing controllers: $e');
|
||||
_handleInitializationError();
|
||||
}
|
||||
}
|
||||
|
||||
bool _areAllControllersRegistered() {
|
||||
return Get.isRegistered<LocationController>() &&
|
||||
Get.isRegistered<IndexController>() &&
|
||||
Get.isRegistered<DestinationController>() &&
|
||||
Get.isRegistered<PermissionController>();
|
||||
}
|
||||
|
||||
void _handleInitializationError() {
|
||||
// エラーハンドリングのロジックをここに実装
|
||||
// 例: エラーダイアログの表示、アプリの再起動など
|
||||
print("_handleInitializationError");
|
||||
}
|
||||
|
||||
Future<void> initServices() async {
|
||||
print('Starting services ...');
|
||||
try {
|
||||
//await Get.putAsync(() => ApiService().init());
|
||||
await _initApiService();
|
||||
await Get.putAsync(() => ApiService().init());
|
||||
//await _initApiService();
|
||||
debugPrint("1: start ApiService");
|
||||
|
||||
// コントローラーを初期化
|
||||
Get.put(IndexController(apiService: Get.find<ApiService>()), permanent: true);
|
||||
Get.put(SettingsController(), permanent: true);
|
||||
Get.put(DestinationController(), permanent: true);
|
||||
Get.put(LocationController(), permanent: true);
|
||||
if (Platform.isIOS ) {
|
||||
// コントローラーを初期化
|
||||
/*
|
||||
Get.put(IndexController(apiService: Get.find<ApiService>()), permanent: true);
|
||||
Get.put(SettingsController(), permanent: true);
|
||||
Get.put(DestinationController(), permanent: true);
|
||||
Get.put(LocationController(), permanent: true);
|
||||
*/
|
||||
// すべてのコントローラーとサービスを非同期で初期化
|
||||
Get.lazyPut(() => IndexController(apiService: Get.find<ApiService>()));
|
||||
debugPrint("2: start IndexController");
|
||||
|
||||
debugPrint("2: Controllers initialized");
|
||||
/*
|
||||
// すべてのコントローラーとサービスを非同期で初期化
|
||||
Get.lazyPut(() => IndexController(apiService: Get.find<ApiService>()));
|
||||
debugPrint("2: start IndexController");
|
||||
Get.lazyPut(() => MapController());
|
||||
debugPrint("2: start MapController");
|
||||
// その他のコントローラーを遅延初期化
|
||||
Get.lazyPut(() => SettingsController());
|
||||
debugPrint("2: start SettingsController");
|
||||
Get.lazyPut(() => DestinationController());
|
||||
debugPrint("3: start DestinationController");
|
||||
Get.lazyPut(() => LocationController());
|
||||
debugPrint("4: start LocationController");
|
||||
|
||||
// その他のコントローラーを遅延初期化
|
||||
Get.lazyPut(() => SettingsController());
|
||||
debugPrint("2: start SettingsController");
|
||||
Get.lazyPut(() => DestinationController());
|
||||
debugPrint("3: start DestinationController");
|
||||
Get.lazyPut(() => LocationController());
|
||||
debugPrint("4: start LocationController");
|
||||
*/
|
||||
//await _initializeControllers();
|
||||
|
||||
debugPrint("2: Controllers initialized");
|
||||
}else {
|
||||
// すべてのコントローラーとサービスを非同期で初期化
|
||||
Get.lazyPut(() => IndexController(apiService: Get.find<ApiService>()));
|
||||
debugPrint("2: start IndexController");
|
||||
|
||||
// その他のコントローラーを遅延初期化
|
||||
Get.lazyPut(() => SettingsController());
|
||||
debugPrint("2: start SettingsController");
|
||||
Get.lazyPut(() => DestinationController());
|
||||
debugPrint("3: start DestinationController");
|
||||
Get.lazyPut(() => LocationController());
|
||||
debugPrint("4: start LocationController");
|
||||
Get.lazyPut(() => PermissionController());
|
||||
}
|
||||
|
||||
// 非同期処理を並列実行
|
||||
await Future.wait([
|
||||
@ -267,10 +331,11 @@ Future<void> initServices() async {
|
||||
print('=== 5. Initialized TimeZone...');
|
||||
print('=== 6. CacheProvider started...');
|
||||
|
||||
Get.put(PermissionController());
|
||||
await _checkPermissions();
|
||||
debugPrint("7: start PermissionController");
|
||||
|
||||
//await PermissionController.checkAndRequestPermissions();
|
||||
|
||||
}catch(e){
|
||||
print('Error initializing : $e');
|
||||
}
|
||||
@ -294,7 +359,17 @@ Future<void> _initCacheProvider() async {
|
||||
}
|
||||
|
||||
Future<void> _checkPermissions() async {
|
||||
await PermissionController.checkAndRequestPermissions();
|
||||
int attempts = 0;
|
||||
while (Get.context == null && attempts < 10) {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
attempts++;
|
||||
}
|
||||
if (Get.context != null) {
|
||||
await PermissionController.checkAndRequestPermissions();
|
||||
} else {
|
||||
print('Context is still null, cannot check permissions');
|
||||
}
|
||||
//await PermissionController.checkAndRequestPermissions(); // main._checkPermissions
|
||||
}
|
||||
|
||||
Future<void> _initApiService() async {
|
||||
@ -474,7 +549,9 @@ Future<void> startBackgroundTracking() async {
|
||||
|
||||
try {
|
||||
// 位置情報の権限が許可されているかを確認
|
||||
await PermissionController.checkAndRequestPermissions();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
PermissionController.checkAndRequestPermissions();
|
||||
});
|
||||
} catch (e) {
|
||||
print('Error starting background tracking: $e');
|
||||
}
|
||||
@ -569,10 +646,29 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
|
||||
Future <void> _initializeControllers() async {
|
||||
|
||||
while (!Get.isRegistered<LocationController>() ) {
|
||||
print("LocationController is not up... ");
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
while (!Get.isRegistered<IndexController>() ) {
|
||||
print("IndexController is not up...");
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
while (!Get.isRegistered<DestinationController>() ) {
|
||||
print("DestinationController is not up...");
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
/*
|
||||
while (!Get.isRegistered<PermissionController>() ) {
|
||||
print("PermissionController is not up... ");
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
*/
|
||||
|
||||
while (!Get.isRegistered<LocationController>() ||
|
||||
!Get.isRegistered<IndexController>() ||
|
||||
!Get.isRegistered<DestinationController>() ||
|
||||
!Get.isRegistered<PermissionController>()) {
|
||||
!Get.isRegistered<DestinationController>() ) {
|
||||
print("LocationController status = Get.isRegistered<LocationController>() ");
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
@ -580,7 +676,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
_locationController = Get.find<LocationController>();
|
||||
_indexController = Get.find<IndexController>();
|
||||
_destinationController = Get.find<DestinationController>();
|
||||
_permissionController = Get.find<PermissionController>();
|
||||
//_permissionController = Get.find<PermissionController>();
|
||||
_isControllerInitialized = true;
|
||||
}
|
||||
|
||||
@ -785,15 +881,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
@override
|
||||
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
|
||||
try {
|
||||
if (!Get.isRegistered<IndexController>()) {
|
||||
_indexController = Get.find<IndexController>();
|
||||
}
|
||||
if (!Get.isRegistered<LocationController>()) {
|
||||
_locationController = Get.find<LocationController>();
|
||||
}
|
||||
if (!Get.isRegistered<DestinationController>()) {
|
||||
_destinationController = Get.find<DestinationController>();
|
||||
}
|
||||
await _initializeControllers();
|
||||
|
||||
switch (state) {
|
||||
case AppLifecycleState.resumed:
|
||||
@ -835,6 +923,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
Future<void> _onResumed() async {
|
||||
|
||||
debugPrint("==(Status Changed)==> RESUMED");
|
||||
try {
|
||||
if (!_isControllerInitialized) {
|
||||
@ -865,43 +954,76 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
Future<void> _onInactive() async {
|
||||
debugPrint("==(Status Changed)==> INACTIVE");
|
||||
if (Platform.isIOS && !_destinationController.isRunningBackgroundGPS) {
|
||||
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
|
||||
_locationController.stopPositionStream();
|
||||
_destinationController.isRunningBackgroundGPS = true;
|
||||
await startBackgroundTracking();
|
||||
} else if (Platform.isAndroid && !_destinationController.isRunningBackgroundGPS) {
|
||||
// Android特有の処理があれば追加
|
||||
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
|
||||
}else{
|
||||
debugPrint("==(Status Changed)==> INACTIVE 不明状態");
|
||||
try {
|
||||
debugPrint("==(Status Changed)==> INACTIVE");
|
||||
if (!_isControllerInitialized) {
|
||||
await _initializeControllers();
|
||||
}
|
||||
if (Platform.isIOS && !_destinationController.isRunningBackgroundGPS) {
|
||||
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
|
||||
_locationController.stopPositionStream();
|
||||
_destinationController.isRunningBackgroundGPS = true;
|
||||
await startBackgroundTracking();
|
||||
} else if (Platform.isAndroid &&
|
||||
!_destinationController.isRunningBackgroundGPS) {
|
||||
// Android特有の処理があれば追加
|
||||
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
|
||||
} else {
|
||||
debugPrint("==(Status Changed)==> INACTIVE 不明状態");
|
||||
}
|
||||
await saveGameState();
|
||||
} catch (e) {
|
||||
print('Error in _onInactive: $e');
|
||||
}
|
||||
await saveGameState();
|
||||
|
||||
}
|
||||
|
||||
Future<void> _onPaused() async {
|
||||
debugPrint(" ==(Status Changed)==> PAUSED. バックグラウンド処理。");
|
||||
if (Platform.isAndroid && !_destinationController.isRunningBackgroundGPS) {
|
||||
debugPrint(" ==(Status Changed)==> PAUSED. Android バックグラウンド処理。");
|
||||
_locationController.stopPositionStream();
|
||||
const platform = MethodChannel('location');
|
||||
await platform.invokeMethod('startLocationService');
|
||||
_destinationController.isRunningBackgroundGPS = true;
|
||||
try {
|
||||
debugPrint(" ==(Status Changed)==> PAUSED. バックグラウンド処理。");
|
||||
if (!_isControllerInitialized) {
|
||||
await _initializeControllers();
|
||||
}
|
||||
if (Platform.isAndroid &&
|
||||
!_destinationController.isRunningBackgroundGPS) {
|
||||
debugPrint(
|
||||
" ==(Status Changed)==> PAUSED. Android バックグラウンド処理。");
|
||||
_locationController.stopPositionStream();
|
||||
const platform = MethodChannel('location');
|
||||
await platform.invokeMethod('startLocationService');
|
||||
_destinationController.isRunningBackgroundGPS = true;
|
||||
}
|
||||
await saveGameState();
|
||||
} catch (e) {
|
||||
print('Error in _onPaused: $e');
|
||||
}
|
||||
await saveGameState();
|
||||
|
||||
}
|
||||
|
||||
Future<void> _onDetached() async {
|
||||
debugPrint(" ==(Status Changed)==> DETACHED アプリは終了します。");
|
||||
await saveGameState();
|
||||
// アプリ終了時の追加処理
|
||||
try {
|
||||
if (!_isControllerInitialized) {
|
||||
await _initializeControllers();
|
||||
}
|
||||
debugPrint(" ==(Status Changed)==> DETACHED アプリは終了します。");
|
||||
await saveGameState();
|
||||
// アプリ終了時の追加処理
|
||||
} catch (e) {
|
||||
print('Error in _onDetached: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<void> _onHidden() async {
|
||||
debugPrint(" ==(Status Changed)==> Hidden アプリが隠れた");
|
||||
await saveGameState();
|
||||
try {
|
||||
if (!_isControllerInitialized) {
|
||||
await _initializeControllers();
|
||||
}
|
||||
debugPrint(" ==(Status Changed)==> Hidden アプリが隠れた");
|
||||
await saveGameState();
|
||||
} catch (e) {
|
||||
print('Error in _onHidden: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@ -929,7 +1051,4 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
enableLog: true,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user