2024-09-02 ほぼOK
This commit is contained in:
@ -29,12 +29,18 @@ import 'dart:async';
|
||||
|
||||
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
|
||||
import 'package:gifunavi/widgets/debug_widget.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'package:image_gallery_saver/image_gallery_saver.dart';
|
||||
|
||||
import 'package:gifunavi/pages/permission/permission.dart' ;
|
||||
|
||||
// 新しいインポート
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
|
||||
// 目的地に関連する状態管理とロジックを担当するクラスです。
|
||||
//
|
||||
class DestinationController extends GetxController {
|
||||
@ -423,11 +429,11 @@ class DestinationController extends GetxController {
|
||||
// 写真撮影モードでない場合
|
||||
|
||||
if (ds.isEmpty) {
|
||||
debugPrint("* 目的地がない場合 ==> 検知半径=-1の場合");
|
||||
//debugPrint("* 目的地がない場合 ==> ds=${ds}");
|
||||
|
||||
// print("----- in location popup cp - ${d.cp}----");
|
||||
if ((d.cp == -1 || d.cp==0 ) && DateTime.now().difference(lastGoalAt).inHours >= 10) {
|
||||
debugPrint("**1: 開始CPで、最後にゴールしてから24時間経過していれば、");
|
||||
debugPrint("**1: 目的地がない場合で、スタート地点。開始CPで、最後にゴールしてから24時間経過していれば、");
|
||||
|
||||
chekcs = 1;
|
||||
//start
|
||||
@ -461,15 +467,15 @@ class DestinationController extends GetxController {
|
||||
// 以下の条件分岐を追加
|
||||
} else if (ds.isNotEmpty && ds[0].checkedin == true) {
|
||||
// 目的地がDBに存在し、すでにチェックインしている場合は自動ポップアップを表示しない
|
||||
debugPrint("チェックイン済み");
|
||||
debugPrint("目的地がない場合で、チェックイン済み");
|
||||
return;
|
||||
|
||||
} else if (isInRog.value == true &&
|
||||
indexController.rogMode.value == 1 &&
|
||||
(locationAlreadyCheckedIn==false) &&
|
||||
d.cp != -1 && d.cp != 0 && d.cp != -2) {
|
||||
} else if (isInRog.value == true && // 常にfalse だよ。。
|
||||
indexController.rogMode.value == 1 && // マップではなくリストページだよ。
|
||||
(locationAlreadyCheckedIn==false) && // まだチェックインしてないよ。
|
||||
d.cp != -1 && d.cp != 0 && d.cp != -2) { // スタートでもゴールでもないよ。
|
||||
|
||||
debugPrint("**2: 標準CP まだチェックインしていない。");
|
||||
debugPrint("**2: 目的地がない場合で、標準CP まだチェックインしていない。");
|
||||
|
||||
// print("----- in location popup checkin cp - ${d.cp}----");
|
||||
chekcs = 2; // 標準CP
|
||||
@ -494,14 +500,20 @@ class DestinationController extends GetxController {
|
||||
});
|
||||
}
|
||||
return;
|
||||
}else{
|
||||
debugPrint("**Else: isInRog=${isInRog.value}, rogMode=${indexController.rogMode.value},locationAlreadyCheckedIn=${locationAlreadyCheckedIn},d.cp=${d.cp}");
|
||||
}
|
||||
}
|
||||
|
||||
// 以降、検知範囲にある場合。
|
||||
//debugPrint("検知範囲にある場合");
|
||||
debugPrint("検知範囲にある場合だよ...");
|
||||
|
||||
debugPrint("---- 検知範囲: ${d.checkin_radious} > 距離:${distance} ----");
|
||||
debugPrint("---- チェックイン済みか? $locationAlreadyCheckedIn ----");
|
||||
debugPrint("---- isInRog : ${isInRog.value}, checkingin = ${isCheckingIn.value}");
|
||||
debugPrint("---- buyPointImageAdded: ${buyPointImageAdded}, ds.isNotEmpty?: ${ds.isNotEmpty},buyPoint:${buyPoint},buyPointCanceled=${buyPointCanceled}");
|
||||
debugPrint(" ");
|
||||
|
||||
// print("---- location checkin radious ${d.checkin_radious} ----");
|
||||
// print("---- already checked in $locationAlreadyCheckedIn ----");
|
||||
if ((checkinRadious >= distance || checkinRadious == -1) &&
|
||||
locationAlreadyCheckedIn == false &&
|
||||
isInRog.value == true &&
|
||||
@ -515,9 +527,9 @@ class DestinationController extends GetxController {
|
||||
//print(
|
||||
// "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ make checkin ${d.sub_loc_id}@@@@@@@@@@@");
|
||||
makeCheckin(d, true, ""); // チェックインして
|
||||
if (d.cp != -1 && d.cp != -2 && d.cp != 0 ) {
|
||||
rogainingCounted.value = true; // ゴール用チェックイン済み
|
||||
}
|
||||
//if (d.cp != -1 && d.cp != -2 && d.cp != 0 ) {
|
||||
// rogainingCounted.value = true; // ゴール用チェックイン済み
|
||||
//}
|
||||
skipGps = false;
|
||||
}
|
||||
return; // 戻る
|
||||
@ -552,7 +564,9 @@ class DestinationController extends GetxController {
|
||||
))).whenComplete(() {
|
||||
shouldShowBottomSheet = true;
|
||||
skipGps = false;
|
||||
rogainingCounted.value = true;
|
||||
if( isInRog.value==true ) {
|
||||
rogainingCounted.value = true;
|
||||
}
|
||||
chekcs = 0;
|
||||
isInCheckin.value = false;
|
||||
isCheckingIn.value = false;
|
||||
@ -729,13 +743,15 @@ class DestinationController extends GetxController {
|
||||
DatabaseHelper db = DatabaseHelper.instance;
|
||||
|
||||
if (isgoal == false) {
|
||||
await db.deleteAllDestinations();
|
||||
await db.deleteAllRogaining();
|
||||
// await db.deleteAllDestinations();
|
||||
// await db.deleteAllRogaining();
|
||||
await db.deleteAllDestinationsExceptTodayCheckins();
|
||||
await db.deleteAllRogainingExceptToday();
|
||||
}
|
||||
|
||||
int? latgoal = await db.latestGoal();
|
||||
lastGoalAt = DateTime.fromMicrosecondsSinceEpoch(latgoal!);
|
||||
//print("===== last goal : $last_goal_at =====");
|
||||
debugPrint("===== last goal : $lastGoalAt =====");
|
||||
dbService.updateDatabase();
|
||||
}
|
||||
|
||||
@ -743,7 +759,7 @@ class DestinationController extends GetxController {
|
||||
//
|
||||
void deleteAllDestinations() {
|
||||
DatabaseHelper db = DatabaseHelper.instance;
|
||||
db.deleteAllDestinations().then((value) {
|
||||
db.deleteAllDestinationsExceptTodayCheckins().then((value) {
|
||||
populateDestinations();
|
||||
});
|
||||
}
|
||||
@ -760,6 +776,18 @@ class DestinationController extends GetxController {
|
||||
photos.add(File(imagePath));
|
||||
},
|
||||
destination: destination,
|
||||
onCameraStatusChanged: (isAvailable) {
|
||||
// カメラの状態が変更されたときの処理
|
||||
if (!isAvailable) {
|
||||
// カメラが利用できない場合の処理
|
||||
Get.snackbar(
|
||||
'エラー',
|
||||
'カメラを初期化できませんでした。',
|
||||
backgroundColor: Colors.red,
|
||||
colorText: Colors.white,
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
/*
|
||||
builder: (_) => CameraCamera(
|
||||
@ -859,7 +887,9 @@ class DestinationController extends GetxController {
|
||||
dbDest: dss,
|
||||
))).whenComplete(() {
|
||||
skipGps = false;
|
||||
rogainingCounted.value = true;
|
||||
if( isInRog.value == true ) { // ロゲ開始していれば、遠くまで来たことにする。
|
||||
rogainingCounted.value = true;
|
||||
}
|
||||
chekcs = 0;
|
||||
isInCheckin.value = false;
|
||||
//Get.back();
|
||||
@ -984,6 +1014,7 @@ class DestinationController extends GetxController {
|
||||
//
|
||||
// 2024-4-8 Akira : See 2809
|
||||
// checkForCheckinメソッドの再帰呼び出しをunawaitedで囲んで、非同期処理の結果を待たずに先に進むようにしました。また、再帰呼び出しの前に一定時間待機するようにしました。
|
||||
// 2024-8-24 ... 佐伯呼び出しが必要なのか?
|
||||
//
|
||||
Future<void> checkForCheckin() async {
|
||||
//print("--- Start of checkForCheckin function ---");
|
||||
@ -1022,13 +1053,15 @@ class DestinationController extends GetxController {
|
||||
} catch (e) {
|
||||
print("An error occurred: $e");
|
||||
// await checkForCheckin();
|
||||
} finally {
|
||||
await Future.delayed(const Duration(seconds: 1)); // 一定時間待機してから再帰呼び出し
|
||||
//} finally {
|
||||
// await Future.delayed(const Duration(seconds: 1)); // 一定時間待機してから再帰呼び出し
|
||||
//print("--- End of checkForCheckin function, calling recursively ---");
|
||||
unawaited( checkForCheckin() );
|
||||
//unawaited( checkForCheckin() );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// GPSデータをサーバーにプッシュする関数です。
|
||||
//
|
||||
Future<void> pushGPStoServer() async {
|
||||
@ -1089,55 +1122,46 @@ class DestinationController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _saveImageToGallery(String imagePath) async {
|
||||
Future<String?> _saveImageToGallery(String imagePath) async {
|
||||
final status = await PermissionController.checkStoragePermission();
|
||||
if(!status){
|
||||
await PermissionController.requestStoragePermission();
|
||||
}
|
||||
|
||||
/*
|
||||
final status = await Permission.storage.status;
|
||||
if (!status.isGranted) {
|
||||
final result = await Permission.storage.request();
|
||||
if (!result.isGranted) {
|
||||
// ユーザーがストレージの権限を拒否した場合の処理
|
||||
showDialog(
|
||||
context: Get.context!,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text('ストレージの権限が必要です'),
|
||||
content: Text(
|
||||
'画像をギャラリーに保存するには、ストレージの権限が必要です。アプリの設定画面で権限を許可してください。'),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text('キャンセル'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: Text('設定'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
openAppSettings(); // アプリの設定画面を開く
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
);
|
||||
try {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
//final fileName = path.basename(imagePath);
|
||||
final fileName = 'checkin_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
final savedImage = await File(imagePath).copy('${appDir.path}/$fileName');
|
||||
|
||||
return;
|
||||
debugPrint("fileName=${fileName}, appDir=${appDir} => ${savedImage}");
|
||||
|
||||
// ギャラリーにも保存
|
||||
//await ImageGallerySaver.saveFile(savedImage.path);
|
||||
await Future.delayed(const Duration(seconds: 3), () async {
|
||||
final result = await ImageGallerySaver.saveFile(savedImage.path);
|
||||
print("Save result: $result");
|
||||
}).timeout(const Duration(seconds: 5));
|
||||
|
||||
|
||||
debugPrint('Image saved to: ${savedImage.path}');
|
||||
return savedImage.path;
|
||||
|
||||
/*
|
||||
final result = await ImageGallerySaver.saveFile(imagePath);
|
||||
debugPrint('Image saved to gallery: $result');
|
||||
if (result['isSuccess']) {
|
||||
return result['filePath'];
|
||||
}
|
||||
*/
|
||||
} catch (e) {
|
||||
if (e is TimeoutException) {
|
||||
print("Operation timed out");
|
||||
} else {
|
||||
print('Failed to save image to gallery: $e');
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
try {
|
||||
final result = await ImageGallerySaver.saveFile(imagePath);
|
||||
print('Image saved to gallery: $result');
|
||||
} catch (e) {
|
||||
print('Failed to save image to gallery: $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 買い物ポイントを作成する関数です。 指定された目的地に対して買い物ポイントの処理を行います。
|
||||
@ -1145,13 +1169,10 @@ class DestinationController extends GetxController {
|
||||
// 買い物ポイントの作成に失敗した場合のエラーハンドリングを追加することを検討してください。
|
||||
//
|
||||
Future<void> makeBuyPoint(Destination destination, String imageurl) async {
|
||||
String? savedImagePath = await _saveImageToGallery(imageurl);
|
||||
DatabaseHelper db = DatabaseHelper.instance;
|
||||
await db.updateBuyPoint(destination, imageurl);
|
||||
await db.updateBuyPoint(destination, savedImagePath ?? imageurl);
|
||||
populateDestinations();
|
||||
//await _saveImageFromPath(imageurl);
|
||||
await _saveImageToGallery(imageurl);
|
||||
|
||||
|
||||
|
||||
if (indexController.currentUser.isNotEmpty) {
|
||||
double cpNum = destination.cp!;
|
||||
@ -1160,6 +1181,7 @@ class DestinationController extends GetxController {
|
||||
|
||||
int userId = indexController.currentUser[0]["user"]["id"];
|
||||
//print("--- Pressed -----");
|
||||
debugPrint("user=${indexController.currentUser[0]["user"]}");
|
||||
String team = indexController.currentUser[0]["user"]['team_name'];
|
||||
//print("--- _team : ${_team}-----");
|
||||
String eventCode = indexController.currentUser[0]["user"]["event_code"];
|
||||
@ -1172,7 +1194,7 @@ class DestinationController extends GetxController {
|
||||
//print("------ checkin event $eventCode ------");
|
||||
ExternalService()
|
||||
.makeCheckpoint(userId, token, formattedDate, team, cpNum.round(),
|
||||
eventCode, imageurl)
|
||||
eventCode, savedImagePath ?? imageurl)
|
||||
.then((value) {
|
||||
//print("------Ext service check point $value ------");
|
||||
});
|
||||
@ -1197,7 +1219,10 @@ class DestinationController extends GetxController {
|
||||
|
||||
if (ddd.isEmpty) {
|
||||
destination.checkedin = true;
|
||||
destination.checkin_image = imageurl;
|
||||
if (imageurl.isNotEmpty) {
|
||||
String? savedImagePath = await _saveImageToGallery(imageurl);
|
||||
destination.checkin_image = savedImagePath ?? imageurl;
|
||||
}
|
||||
await db.insertDestination(destination);
|
||||
// print("~~~~ inserted into db ~~~~");
|
||||
}
|
||||
@ -1212,7 +1237,8 @@ class DestinationController extends GetxController {
|
||||
//await _saveImageFromPath(imageurl!);
|
||||
}
|
||||
if (imageurl.isNotEmpty) {
|
||||
await _saveImageToGallery(imageurl);
|
||||
String? savedImagePath = await _saveImageToGallery(imageurl);
|
||||
destination.checkin_image = savedImagePath ?? imageurl;
|
||||
}
|
||||
|
||||
populateDestinations();
|
||||
@ -1299,11 +1325,11 @@ class DestinationController extends GetxController {
|
||||
void onInit() async {
|
||||
super.onInit();
|
||||
|
||||
/*
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
await PermissionController.checkAndRequestPermissions();
|
||||
});
|
||||
*/
|
||||
|
||||
|
||||
startGPSCheckTimer();
|
||||
|
||||
@ -1810,7 +1836,7 @@ class DestinationController extends GetxController {
|
||||
//
|
||||
void deleteDBDestinations() {
|
||||
DatabaseHelper db = DatabaseHelper.instance;
|
||||
db.deleteAllDestinations().then((value) {
|
||||
db.deleteAllDestinationsExceptTodayCheckins().then((value) {
|
||||
populateDestinations();
|
||||
});
|
||||
dbService.updateDatabase();
|
||||
|
||||
Reference in New Issue
Block a user