그룹을 공개로 만들면 "공개 그룹" 목록에 뜨고, 학생이 참여 신청을 보내면 그룹장이 허용/거절할 수 있다. 그룹 상세 화면에 그룹장 전용 공개/비공개 전환 버튼과 대기 중인 참여 신청 목록을 추가. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
176 lines
5.2 KiB
Dart
176 lines
5.2 KiB
Dart
// 👥 백품타 그룹 상세(멤버/오늘 출석/초대/나가기) 화면의 기능 담당 컨트롤러.
|
|
import 'dart:convert';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import '../config.dart';
|
|
|
|
class GroupDetailController extends ChangeNotifier {
|
|
final int groupId;
|
|
final String studentId;
|
|
|
|
GroupDetailController({required this.groupId, required this.studentId});
|
|
|
|
bool _isLoading = true;
|
|
bool _isBusy = false;
|
|
List<dynamic> _members = [];
|
|
List<dynamic> _joinRequests = [];
|
|
bool _isPublic = false;
|
|
bool _disposed = false;
|
|
|
|
bool get isLoading => _isLoading;
|
|
bool get isBusy => _isBusy;
|
|
List<dynamic> get members => _members;
|
|
List<dynamic> get joinRequests => _joinRequests;
|
|
bool get isPublic => _isPublic;
|
|
bool get isOwner =>
|
|
_members.any((m) => m['studentId'] == studentId && m['role'] == 'owner');
|
|
|
|
void _safeNotify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> init() => refresh();
|
|
|
|
Future<void> refresh() async {
|
|
_isLoading = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('$baseUrl/api/groups/$groupId/members'),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
|
_members = data['members'] ?? [];
|
|
_isPublic = data['isPublic'] == true;
|
|
}
|
|
} catch (_) {
|
|
// 조용히 무시
|
|
}
|
|
if (isOwner) await _fetchJoinRequests();
|
|
_isLoading = false;
|
|
_safeNotify();
|
|
}
|
|
|
|
Future<void> _fetchJoinRequests() async {
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse(
|
|
'$baseUrl/api/groups/$groupId/join-requests?ownerId=$studentId',
|
|
),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
|
_joinRequests = data['requests'] ?? [];
|
|
}
|
|
} catch (_) {
|
|
// 조용히 무시
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> respondJoinRequest(
|
|
int requestId,
|
|
bool accept,
|
|
) async {
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/groups/join-requests/$requestId/respond'),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"responderId": studentId, "accept": accept}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
await refresh();
|
|
return (true, '${result['message'] ?? '처리했습니다.'}');
|
|
}
|
|
return (false, '${result['message'] ?? '처리 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
} finally {
|
|
_isBusy = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> setVisibility(bool isPublic) async {
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/groups/$groupId/visibility'),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"ownerId": studentId, "isPublic": isPublic}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
await refresh();
|
|
return (true, '${result['message'] ?? '변경했습니다.'}');
|
|
}
|
|
return (false, '${result['message'] ?? '변경 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
} finally {
|
|
_isBusy = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> invite(
|
|
String inviteeId,
|
|
String inviteeName,
|
|
) async {
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/groups/$groupId/invite'),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({
|
|
"inviterId": studentId,
|
|
"inviteeId": inviteeId,
|
|
"inviteeName": inviteeName,
|
|
}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
return (true, '${result['message'] ?? '초대를 보냈습니다.'}');
|
|
}
|
|
return (false, '${result['message'] ?? '초대 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
} finally {
|
|
_isBusy = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> leave() async {
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/groups/$groupId/leave'),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"studentId": studentId}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
return (true, '${result['message'] ?? '그룹에서 나갔습니다.'}');
|
|
}
|
|
return (false, '${result['message'] ?? '처리 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
} finally {
|
|
_isBusy = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
}
|