153 lines
4.3 KiB
Dart
153 lines
4.3 KiB
Dart
// 🧑🤝🧑 백품타 친구 목록/신청 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
|||
|
|
import 'dart:convert';
|
||
|
|
import 'package:flutter/foundation.dart';
|
||
|
|
import 'package:http/http.dart' as http;
|
||
|
|
import '../config.dart';
|
||
|
|
|
||
|
|
class FriendController extends ChangeNotifier {
|
||
|
|
final String studentId;
|
||
|
|
final String studentName;
|
||
|
|
|
||
|
|
FriendController({required this.studentId, required this.studentName});
|
||
|
|
|
||
|
|
bool _isLoading = true;
|
||
|
|
bool _isBusy = false;
|
||
|
|
List<dynamic> _friends = [];
|
||
|
|
List<dynamic> _requests = [];
|
||
|
|
bool _disposed = false;
|
||
|
|
|
||
|
|
bool get isLoading => _isLoading;
|
||
|
|
bool get isBusy => _isBusy;
|
||
|
|
List<dynamic> get friends => _friends;
|
||
|
|
List<dynamic> get requests => _requests;
|
||
|
|
|
||
|
|
void _safeNotify() {
|
||
|
|
if (!_disposed) notifyListeners();
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void dispose() {
|
||
|
|
_disposed = true;
|
||
|
|
super.dispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> init() async {
|
||
|
|
await refresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> refresh() async {
|
||
|
|
_isLoading = true;
|
||
|
|
_safeNotify();
|
||
|
|
await Future.wait([_fetchFriends(), _fetchRequests()]);
|
||
|
|
_isLoading = false;
|
||
|
|
_safeNotify();
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _fetchFriends() async {
|
||
|
|
try {
|
||
|
|
final response = await http.get(
|
||
|
|
Uri.parse('$baseUrl/api/friends/list?studentId=$studentId'),
|
||
|
|
);
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||
|
|
_friends = data['friends'] ?? [];
|
||
|
|
}
|
||
|
|
} catch (_) {
|
||
|
|
// 조용히 무시
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _fetchRequests() async {
|
||
|
|
try {
|
||
|
|
final response = await http.get(
|
||
|
|
Uri.parse('$baseUrl/api/friends/requests?studentId=$studentId'),
|
||
|
|
);
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||
|
|
_requests = data['requests'] ?? [];
|
||
|
|
}
|
||
|
|
} catch (_) {
|
||
|
|
// 조용히 무시
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<(bool success, String message)> sendRequest(String friendId) async {
|
||
|
|
_isBusy = true;
|
||
|
|
_safeNotify();
|
||
|
|
try {
|
||
|
|
final response = await http.post(
|
||
|
|
Uri.parse('$baseUrl/api/friends/request'),
|
||
|
|
headers: {"Content-Type": "application/json"},
|
||
|
|
body: jsonEncode({
|
||
|
|
"studentId": studentId,
|
||
|
|
"studentName": studentName,
|
||
|
|
"friendId": friendId.trim(),
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
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)> respondRequest(
|
||
|
|
int requestId,
|
||
|
|
bool accept,
|
||
|
|
) async {
|
||
|
|
_isBusy = true;
|
||
|
|
_safeNotify();
|
||
|
|
try {
|
||
|
|
final response = await http.post(
|
||
|
|
Uri.parse('$baseUrl/api/friends/respond'),
|
||
|
|
headers: {"Content-Type": "application/json"},
|
||
|
|
body: jsonEncode({
|
||
|
|
"requestId": requestId,
|
||
|
|
"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)> removeFriend(String friendId) async {
|
||
|
|
_isBusy = true;
|
||
|
|
_safeNotify();
|
||
|
|
try {
|
||
|
|
final response = await http.delete(
|
||
|
|
Uri.parse('$baseUrl/api/friends'),
|
||
|
|
headers: {"Content-Type": "application/json"},
|
||
|
|
body: jsonEncode({"studentId": studentId, "friendId": friendId}),
|
||
|
|
);
|
||
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
||
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
||
|
|
await _fetchFriends();
|
||
|
|
return (true, '${result['message'] ?? '삭제했습니다.'}');
|
||
|
|
}
|
||
|
|
return (false, '${result['message'] ?? '삭제 실패'}');
|
||
|
|
} catch (e) {
|
||
|
|
return (false, '네트워크 에러: $e');
|
||
|
|
} finally {
|
||
|
|
_isBusy = false;
|
||
|
|
_safeNotify();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|