チーム編集時の文字化け修正済み
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
// lib/controllers/team_controller.dart
|
||||
|
||||
import 'dart:convert';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:rogapp/model/team.dart';
|
||||
import 'package:rogapp/model/category.dart';
|
||||
@ -9,44 +9,78 @@ import 'package:rogapp/services/api_service.dart';
|
||||
class TeamController extends GetxController {
|
||||
late final ApiService _apiService;
|
||||
|
||||
|
||||
final teams = <Team>[].obs;
|
||||
final categories = <NewCategory>[].obs;
|
||||
final teamMembers = <User>[].obs;
|
||||
|
||||
|
||||
final selectedCategory = Rx<NewCategory?>(null);
|
||||
final selectedTeam = Rx<Team?>(null);
|
||||
final currentUser = Rx<User?>(null);
|
||||
|
||||
final isLoading = true.obs;
|
||||
final teamName = ''.obs;
|
||||
final isLoading = false.obs;
|
||||
final error = RxString('');
|
||||
|
||||
@override
|
||||
void onInit() async{
|
||||
super.onInit();
|
||||
try {
|
||||
//await Get.putAsync(() => ApiService().init());
|
||||
_apiService = Get.find<ApiService>();
|
||||
|
||||
await Future.wait([
|
||||
await fetchCategories();
|
||||
await Future.wait([
|
||||
fetchTeams(),
|
||||
fetchCategories(),
|
||||
getCurrentUser(),
|
||||
]);
|
||||
|
||||
print("selectedCategory=$selectedCategory.value");
|
||||
// カテゴリが取得できたら、最初のカテゴリを選択状態にする
|
||||
if (categories.isNotEmpty && selectedCategory.value == null) {
|
||||
selectedCategory.value = categories.first;
|
||||
}
|
||||
|
||||
}catch(e){
|
||||
print("Team Controller error: $e");
|
||||
error.value = e.toString();
|
||||
}finally{
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setSelectedTeam(Team team) {
|
||||
selectedTeam.value = team;
|
||||
teamName.value = team.teamName;
|
||||
if (categories.isNotEmpty) {
|
||||
selectedCategory.value = categories.firstWhere(
|
||||
(category) => category.id == team.category.id,
|
||||
orElse: () => categories.first,
|
||||
);
|
||||
} else {
|
||||
// カテゴリリストが空の場合、teamのカテゴリをそのまま使用
|
||||
selectedCategory.value = team.category;
|
||||
}
|
||||
fetchTeamMembers(team.id);
|
||||
}
|
||||
|
||||
void resetForm() {
|
||||
selectedTeam.value = null;
|
||||
teamName.value = '';
|
||||
selectedCategory.value = categories.isNotEmpty ? categories.first : null;
|
||||
teamMembers.clear();
|
||||
}
|
||||
|
||||
Future<void> fetchTeams() async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
final fetchedTeams = await _apiService.getTeams();
|
||||
teams.assignAll(fetchedTeams);
|
||||
} catch (e) {
|
||||
error.value = 'チームの取得に失敗しました: $e';
|
||||
print('Error fetching teams: $e');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,6 +88,8 @@ class TeamController extends GetxController {
|
||||
try {
|
||||
final fetchedCategories = await _apiService.getCategories();
|
||||
categories.assignAll(fetchedCategories);
|
||||
print("Fetched categories: ${categories.length}"); // デバッグ用
|
||||
|
||||
} catch (e) {
|
||||
print('Error fetching categories: $e');
|
||||
}
|
||||
@ -68,25 +104,15 @@ class TeamController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> createTeam(String teamName, int categoryId) async {
|
||||
try {
|
||||
final newTeam = await _apiService.createTeam(teamName, categoryId);
|
||||
teams.add(newTeam);
|
||||
} catch (e) {
|
||||
print('Error creating team: $e');
|
||||
}
|
||||
Future<Team> createTeam(String teamName, int categoryId) async {
|
||||
final newTeam = await _apiService.createTeam(teamName, categoryId);
|
||||
return newTeam;
|
||||
}
|
||||
|
||||
Future<void> updateTeam(int teamId, String teamName, int categoryId) async {
|
||||
try {
|
||||
final updatedTeam = await _apiService.updateTeam(teamId, teamName, categoryId);
|
||||
final index = teams.indexWhere((team) => team.id == teamId);
|
||||
if (index != -1) {
|
||||
teams[index] = updatedTeam;
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error updating team: $e');
|
||||
}
|
||||
Future<Team> updateTeam(int teamId, String teamName, int categoryId) async {
|
||||
// APIサービスを使用してチームを更新
|
||||
final updatedTeam = await _apiService.updateTeam(teamId, teamName, categoryId);
|
||||
return updatedTeam;
|
||||
}
|
||||
|
||||
Future<void> deleteTeam(int teamId) async {
|
||||
@ -108,10 +134,56 @@ class TeamController extends GetxController {
|
||||
}
|
||||
|
||||
void updateTeamName(String value) {
|
||||
// Update local state
|
||||
teamName.value = value;
|
||||
}
|
||||
|
||||
void updateCategory(NewCategory? value) {
|
||||
selectedCategory.value = value;
|
||||
if (value != null) {
|
||||
selectedCategory.value = categories.firstWhere(
|
||||
(category) => category.id == value.id,
|
||||
orElse: () => value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveTeam() async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
if (selectedCategory.value == null) {
|
||||
throw Exception('カテゴリを選択してください');
|
||||
}
|
||||
|
||||
if (selectedTeam.value == null) {
|
||||
await createTeam(teamName.value, selectedCategory.value!.id);
|
||||
} else {
|
||||
await updateTeam(selectedTeam.value!.id, teamName.value, selectedCategory.value!.id);
|
||||
}
|
||||
|
||||
// サーバーから最新のデータを再取得
|
||||
await fetchTeams();
|
||||
|
||||
// 選択中のチームを更新
|
||||
if (selectedTeam.value != null) {
|
||||
selectedTeam.value = teams.firstWhere((t) => t.id == selectedTeam.value!.id);
|
||||
teamName.value = selectedTeam.value!.teamName;
|
||||
selectedCategory.value = selectedTeam.value!.category;
|
||||
}
|
||||
|
||||
update(); // UIを強制的に更新
|
||||
} catch (e) {
|
||||
error.value = 'チームの保存に失敗しました: $e';
|
||||
print("Team save error: $e");
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<void> deleteSelectedTeam() async {
|
||||
if (selectedTeam.value != null) {
|
||||
await deleteTeam(selectedTeam.value!.id);
|
||||
selectedTeam.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user