이벤트 가져오기 위젯 테스트 추가 및 테스트 훅 도입
This commit is contained in:
@@ -13,6 +13,7 @@ class ClubService with ChangeNotifier {
|
||||
bool _isLoading = false;
|
||||
String? _token;
|
||||
ApiService _apiService;
|
||||
bool _disposed = false;
|
||||
|
||||
// 기본 생성자
|
||||
ClubService() : _apiService = ApiService();
|
||||
@@ -26,6 +27,7 @@ class ClubService with ChangeNotifier {
|
||||
|
||||
// 초기화 함수
|
||||
Future<void> initialize(String token) async {
|
||||
if (_disposed) return;
|
||||
_token = token;
|
||||
_apiService.setToken(token);
|
||||
|
||||
@@ -34,16 +36,17 @@ class ClubService with ChangeNotifier {
|
||||
final clubId = prefs.getString(ApiConfig.clubIdKey);
|
||||
|
||||
if (clubId != null) {
|
||||
if (_disposed) return;
|
||||
await fetchClubById(clubId);
|
||||
}
|
||||
}
|
||||
|
||||
// 사용자의 모든 클럽 가져오기
|
||||
Future<void> fetchUserClubs() async {
|
||||
if (_token == null) return;
|
||||
if (_disposed || _token == null) return;
|
||||
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
|
||||
try {
|
||||
final data = await _apiService.post('${ApiConfig.clubs}/user');
|
||||
@@ -53,24 +56,24 @@ class ClubService with ChangeNotifier {
|
||||
_clubs = clubsData.map((clubData) => Club.fromJson(clubData)).toList();
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
throw Exception('클럽 목록을 불러오는데 실패했습니다: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ID로 클럽 정보 가져오기
|
||||
Future<void> fetchClubById(String clubId) async {
|
||||
if (_token == null) return;
|
||||
if (_disposed || _token == null) return;
|
||||
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
|
||||
try {
|
||||
final data = await _apiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
ApiConfig.clubs,
|
||||
data: {'clubId': clubId},
|
||||
);
|
||||
|
||||
@@ -82,10 +85,10 @@ class ClubService with ChangeNotifier {
|
||||
await prefs.setString(ApiConfig.clubIdKey, clubId);
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
throw Exception('클럽 정보를 불러오는데 실패했습니다: $e');
|
||||
}
|
||||
}
|
||||
@@ -97,10 +100,10 @@ class ClubService with ChangeNotifier {
|
||||
|
||||
// 백엔드에 클럽 선택 요청 (세션에 clubId 저장)
|
||||
Future<void> selectClub(String clubId) async {
|
||||
if (_token == null) return;
|
||||
if (_disposed || _token == null) return;
|
||||
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
|
||||
try {
|
||||
await _apiService.post(
|
||||
@@ -109,6 +112,7 @@ class ClubService with ChangeNotifier {
|
||||
);
|
||||
|
||||
// 클럽 선택 성공 후 클럽 정보 가져오기
|
||||
if (_disposed) return;
|
||||
await fetchClubById(clubId);
|
||||
|
||||
// 클럽 ID를 로컬에 저장
|
||||
@@ -121,13 +125,15 @@ class ClubService with ChangeNotifier {
|
||||
}
|
||||
|
||||
// 클럽 변경 이벤트 발생 - 다른 서비스들에게 알림
|
||||
EventBus().fire(ClubChangedEvent(clubId));
|
||||
if (!_disposed) {
|
||||
EventBus().fire(ClubChangedEvent(clubId));
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
throw Exception('클럽 선택에 실패했습니다: $e');
|
||||
}
|
||||
}
|
||||
@@ -139,11 +145,11 @@ class ClubService with ChangeNotifier {
|
||||
}
|
||||
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
|
||||
try {
|
||||
final data = await _apiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
ApiConfig.clubs,
|
||||
data: club.toJson(),
|
||||
);
|
||||
|
||||
@@ -157,24 +163,24 @@ class ClubService with ChangeNotifier {
|
||||
await prefs.setString(ApiConfig.clubIdKey, newClub.id);
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
|
||||
return newClub;
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
throw Exception('클럽 생성에 실패했습니다: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 클럽 정보 업데이트
|
||||
Future<Club> updateClub(String clubId, Map<String, dynamic> updates) async {
|
||||
if (_token == null) {
|
||||
if (_disposed || _token == null) {
|
||||
throw Exception('인증이 필요합니다');
|
||||
}
|
||||
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
|
||||
try {
|
||||
final data = await _apiService.put(
|
||||
@@ -202,19 +208,19 @@ class ClubService with ChangeNotifier {
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
|
||||
return updatedClub;
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
if (!_disposed) notifyListeners();
|
||||
throw Exception('클럽 정보 업데이트에 실패했습니다: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 클럽 정보 가져오기 (ID로)
|
||||
Future<Club> fetchClub(String clubId) async {
|
||||
if (_token == null) {
|
||||
if (_disposed || _token == null) {
|
||||
throw Exception('인증이 필요합니다');
|
||||
}
|
||||
|
||||
@@ -225,7 +231,7 @@ class ClubService with ChangeNotifier {
|
||||
}
|
||||
|
||||
final data = await _apiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
ApiConfig.clubs,
|
||||
data: {'clubId': clubId},
|
||||
);
|
||||
|
||||
@@ -235,4 +241,10 @@ class ClubService with ChangeNotifier {
|
||||
throw Exception('클럽 정보를 불러오는데 실패했습니다: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user