친구 신청·수락, 그룹 생성·초대(친구 목록에서 선택)·오늘 출석 현황 공유, 개인 연속 출석(스트릭) 달력, 관리자가 날짜별로 선물/이벤트를 등록하는 달력 관리 화면을 추가. 대시보드에 학생용 3개(친구/그룹 스터디/출석 달력) + 교사·관리자용 1개(출석 달력 관리) 타일을 배치. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
125 lines
3.5 KiB
Dart
125 lines
3.5 KiB
Dart
// 👥 백품타 그룹 스터디(목록/생성/초대) 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
|
import 'dart:convert';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import '../config.dart';
|
|
|
|
class GroupController extends ChangeNotifier {
|
|
final String studentId;
|
|
final String studentName;
|
|
|
|
GroupController({required this.studentId, required this.studentName});
|
|
|
|
bool _isLoading = true;
|
|
bool _isBusy = false;
|
|
List<dynamic> _groups = [];
|
|
List<dynamic> _invites = [];
|
|
bool _disposed = false;
|
|
|
|
bool get isLoading => _isLoading;
|
|
bool get isBusy => _isBusy;
|
|
List<dynamic> get groups => _groups;
|
|
List<dynamic> get invites => _invites;
|
|
|
|
void _safeNotify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> init() => refresh();
|
|
|
|
Future<void> refresh() async {
|
|
_isLoading = true;
|
|
_safeNotify();
|
|
await Future.wait([_fetchGroups(), _fetchInvites()]);
|
|
_isLoading = false;
|
|
_safeNotify();
|
|
}
|
|
|
|
Future<void> _fetchGroups() async {
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('$baseUrl/api/groups/mine?studentId=$studentId'),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
|
_groups = data['groups'] ?? [];
|
|
}
|
|
} catch (_) {
|
|
// 조용히 무시
|
|
}
|
|
}
|
|
|
|
Future<void> _fetchInvites() async {
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('$baseUrl/api/groups/invites?studentId=$studentId'),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
|
_invites = data['invites'] ?? [];
|
|
}
|
|
} catch (_) {
|
|
// 조용히 무시
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> createGroup(String name) async {
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/groups'),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({
|
|
"ownerId": studentId,
|
|
"ownerName": studentName,
|
|
"name": name.trim(),
|
|
}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
await _fetchGroups();
|
|
return (true, '${result['message'] ?? '그룹을 만들었습니다.'}');
|
|
}
|
|
return (false, '${result['message'] ?? '그룹 생성 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
} finally {
|
|
_isBusy = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> respondInvite(
|
|
int inviteId,
|
|
bool accept,
|
|
) async {
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/groups/invites/$inviteId/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();
|
|
}
|
|
}
|
|
}
|