Fixed Cameraでクラッシュ、日付ズレ、遠くのチェックポイント表示、1日1回のプロテクト

This commit is contained in:
2024-08-07 21:17:13 +09:00
parent 347861e5a1
commit bdf6dd3c04
7 changed files with 227 additions and 21 deletions

View File

@ -21,6 +21,10 @@ import 'package:rogapp/widgets/bottom_sheet_controller.dart';
import 'package:rogapp/widgets/debug_widget.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
import 'package:get/get.dart';
// BottomSheetNewは、StatelessWidgetを継承したクラスで、目的地の詳細情報を表示するボトムシートのUIを構築します。
// コンストラクタでは、destination目的地オブジェクトとisAlreadyCheckedInすでにチェックイン済みかどうかのフラグを受け取ります。
// buildメソッドでは、detailsSheetメソッドを呼び出して、目的地の詳細情報を表示します。
@ -38,6 +42,15 @@ class BottomSheetNew extends GetView<BottomSheetController> {
final RxBool isButtonDisabled = false.obs;
static bool _timezoneInitialized = false;
void _initializeTimezone() {
if (!_timezoneInitialized) {
tz.initializeTimeZones();
_timezoneInitialized = true;
}
}
// 目的地の画像を取得するためのメソッドです。
// indexController.rogModeの値に基づいて、適切な画像を返します。画像が見つからない場合は、デフォルトの画像を返します。
//
@ -146,6 +159,7 @@ class BottomSheetNew extends GetView<BottomSheetController> {
// ボタンがタップされたときの処理も含まれています。
//
Widget getActionButton(BuildContext context, Destination destination) {
_initializeTimezone(); // タイムゾーンの初期化
/*
debugPrint("getActionButton ${destinationController.rogainingCounted.value}");
debugPrint("getActionButton ${destinationController.distanceToStart()}");
@ -181,6 +195,51 @@ class BottomSheetNew extends GetView<BottomSheetController> {
onPressed: destinationController.isInRog.value
? null
: () async {
// Check if the event is for today
bool isEventToday = await checkIfEventIsToday();
if (!isEventToday) {
Get.dialog(
AlertDialog(
title: Text("警告"),
content: Text("参加したエントリーは別日のものですので、ロゲの開始はできません。当日のエントリーを選択するか、エントリーを今日に変更してからロゲ開始を行ってください。"),
actions: <Widget>[
TextButton(
child: Text("OK"),
onPressed: () {
Get.back(); // Close the dialog
Get.back(); // Close the bottom sheet
},
),
],
),
);
return;
}
// Check if user has already completed a rogaining event today
bool hasCompletedToday = await checkIfCompletedToday();
if (hasCompletedToday) {
Get.dialog(
AlertDialog(
title: Text("警告"),
content: Text("すでにロゲの参加を行いゴールをしています。ロゲは1日1回に制限されています。ご了承ください。"),
actions: <Widget>[
TextButton(
child: Text("OK"),
onPressed: () {
Get.back(); // Close the dialog
Get.back(); // Close the bottom sheet
},
),
],
),
);
return;
}
destinationController.isInRog.value = true;
@ -259,6 +318,8 @@ class BottomSheetNew extends GetView<BottomSheetController> {
(destination.cp == 0 || destination.cp == -2|| destination.cp == -1) &&
DestinationController.ready_for_goal == true
? () async {
destinationController.isAtGoal.value = true;
destinationController.photos.clear();
await showModalBottomSheet(
@ -331,6 +392,62 @@ class BottomSheetNew extends GetView<BottomSheetController> {
return Container();
}
// Add these new methods to check event date and completion status
Future<bool> checkIfEventIsToday() async {
try {
final now = tz.TZDateTime.now(tz.getLocation('Asia/Tokyo'));
// Null チェックと安全な操作を追加
final userEventDate = indexController.currentUser.isNotEmpty &&
indexController.currentUser[0] != null &&
indexController.currentUser[0]["user"] != null
? indexController.currentUser[0]["user"]["event_date"]
: null;
if (userEventDate == null || userEventDate.toString().isEmpty) {
print('Event date is null or empty');
return false; // イベント日付が設定されていない場合は false を返す
}
final eventDate = tz.TZDateTime.from(
DateTime.parse(userEventDate.toString()),
tz.getLocation('Asia/Tokyo')
);
return eventDate.year == now.year &&
eventDate.month == now.month &&
eventDate.day == now.day;
} catch (e) {
print('Error in checkIfEventIsToday: $e');
// エラーが発生した場合はダイアログを表示
Get.dialog(
AlertDialog(
title: Text('エラー'),
content: Text('イベント日付の確認中にエラーが発生しました。\nアプリを再起動するか、管理者にお問い合わせください。'),
actions: <Widget>[
TextButton(
child: Text('OK'),
onPressed: () => Get.back(),
),
],
),
);
return false; // エラーが発生した場合は false を返す
}
}
Future<bool> checkIfCompletedToday() async {
final IndexController indexController = Get.find<IndexController>();
final lastGoalTime = await indexController.getLastGoalTime();
if (lastGoalTime == null) return false;
final now = DateTime.now();
return lastGoalTime.year == now.year &&
lastGoalTime.month == now.month &&
lastGoalTime.day == now.day;
}
// 継承元のbuild をオーバーライドし、detailsSheetメソッドを呼び出して、目的地の詳細情報を表示します。
@override
Widget build(BuildContext context) {