Team,Member, Entryの登録まで完了
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
// lib/entry/entry_controller.dart
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:rogapp/model/entry.dart';
|
||||
import 'package:rogapp/model/event.dart';
|
||||
import 'package:rogapp/model/team.dart';
|
||||
@ -8,7 +9,7 @@ import 'package:rogapp/model/category.dart';
|
||||
import 'package:rogapp/services/api_service.dart';
|
||||
|
||||
class EntryController extends GetxController {
|
||||
late final ApiService _apiService;
|
||||
late ApiService _apiService;
|
||||
|
||||
final entries = <Entry>[].obs;
|
||||
final events = <Event>[].obs;
|
||||
@ -21,14 +22,27 @@ class EntryController extends GetxController {
|
||||
final selectedDate = Rx<DateTime?>(null);
|
||||
|
||||
final currentEntry = Rx<Entry?>(null);
|
||||
final isLoading = true.obs;
|
||||
|
||||
@override
|
||||
void onInit() async{
|
||||
void onInit() async {
|
||||
super.onInit();
|
||||
await Get.putAsync(() => ApiService().init());
|
||||
_apiService = Get.find<ApiService>();
|
||||
await initializeApiService();
|
||||
await loadInitialData();
|
||||
}
|
||||
|
||||
Future<void> initializeApiService() async {
|
||||
try {
|
||||
_apiService = await Get.putAsync(() => ApiService().init());
|
||||
} catch (e) {
|
||||
print('Error initializing ApiService: $e');
|
||||
Get.snackbar('Error', 'Failed to initialize API service');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadInitialData() async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
await Future.wait([
|
||||
fetchEntries(),
|
||||
fetchEvents(),
|
||||
@ -37,14 +51,53 @@ class EntryController extends GetxController {
|
||||
]);
|
||||
if (Get.arguments != null && Get.arguments['entry'] != null) {
|
||||
currentEntry.value = Get.arguments['entry'];
|
||||
_initializeEntryData();
|
||||
initializeEditMode(currentEntry.value!);
|
||||
} else {
|
||||
// 新規作成モードの場合、最初のイベントを選択
|
||||
if (events.isNotEmpty) {
|
||||
selectedEvent.value = events.first;
|
||||
selectedDate.value = events.first.startDatetime;
|
||||
}
|
||||
}
|
||||
Get.putAsync(() => ApiService().init());
|
||||
}catch(e){
|
||||
} catch(e) {
|
||||
print('Error initializing data: $e');
|
||||
Get.snackbar('Error', 'Failed to load initial data');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void initializeEditMode(Entry entry) {
|
||||
selectedEvent.value = entry.event;
|
||||
selectedTeam.value = entry.team;
|
||||
selectedCategory.value = entry.category;
|
||||
selectedDate.value = entry.date;
|
||||
}
|
||||
|
||||
void updateEvent(Event? value) {
|
||||
selectedEvent.value = value;
|
||||
if (value != null) {
|
||||
// イベント変更時に日付を調整
|
||||
if (selectedDate.value == null ||
|
||||
selectedDate.value!.isBefore(value.startDatetime) ||
|
||||
selectedDate.value!.isAfter(value.endDatetime)) {
|
||||
selectedDate.value = value.startDatetime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateTeam(Team? value) => selectedTeam.value = value;
|
||||
void updateCategory(NewCategory? value) => selectedCategory.value = value;
|
||||
void updateDate(DateTime value) => selectedDate.value = value;
|
||||
|
||||
/*
|
||||
void updateDate(DateTime value){
|
||||
selectedDate.value = DateFormat('yyyy-MM-dd').format(value!) as DateTime?;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
void _initializeEntryData() {
|
||||
if (currentEntry.value != null) {
|
||||
selectedEvent.value = currentEntry.value!.event;
|
||||
@ -60,6 +113,7 @@ class EntryController extends GetxController {
|
||||
entries.assignAll(fetchedEntries);
|
||||
} catch (e) {
|
||||
print('Error fetching entries: $e');
|
||||
Get.snackbar('Error', 'Failed to fetch entries');
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,6 +123,7 @@ class EntryController extends GetxController {
|
||||
events.assignAll(fetchedEvents);
|
||||
} catch (e) {
|
||||
print('Error fetching events: $e');
|
||||
Get.snackbar('Error', 'Failed to fetch events');
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,6 +133,7 @@ class EntryController extends GetxController {
|
||||
teams.assignAll(fetchedTeams);
|
||||
} catch (e) {
|
||||
print('Error fetching teams: $e');
|
||||
Get.snackbar('Error', 'Failed to fetch team');
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,6 +143,7 @@ class EntryController extends GetxController {
|
||||
categories.assignAll(fetchedCategories);
|
||||
} catch (e) {
|
||||
print('Error fetching categories: $e');
|
||||
Get.snackbar('Error', 'Failed to fetch categories');
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,10 +200,6 @@ class EntryController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
void updateEvent(Event? value) => selectedEvent.value = value;
|
||||
void updateTeam(Team? value) => selectedTeam.value = value;
|
||||
void updateCategory(NewCategory? value) => selectedCategory.value = value;
|
||||
void updateDate(DateTime value) => selectedDate.value = value;
|
||||
|
||||
bool isOwner() {
|
||||
// Implement logic to check if the current user is the owner of the entry
|
||||
|
||||
Reference in New Issue
Block a user