// πŸ“ μ„ μƒλ‹˜ 호좜(ν•™μƒμš©) ν™”λ©΄μ˜ κΈ°λŠ₯(μ„œλ²„ 톡신/μƒνƒœ) λ‹΄λ‹Ή 컨트둀러. import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import '../config.dart'; import '../data/teacher_call_schedule.dart'; class TeacherCallController extends ChangeNotifier { final String studentId; final String studentName; TeacherCallController({required this.studentId, required this.studentName}); bool _isLoading = true; bool _isCalling = false; List _teachers = []; List _myCalls = []; List _ranking = []; bool _disposed = false; bool get isLoading => _isLoading; bool get isCalling => _isCalling; List get teachers => _teachers; List get myCalls => _myCalls; List get ranking => _ranking; void _safeNotify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; super.dispose(); } bool isInClassNow(String teacherName) { final now = DateTime.now(); final hhmm = '${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}'; return teachersInClassNow(now.weekday, hhmm).contains(teacherName); } Future init() async { await Future.wait([_fetchTeachers(), _fetchMyCalls(), _fetchRanking()]); _isLoading = false; _safeNotify(); } Future refresh() => init(); Future _fetchTeachers() async { try { final response = await http.get( Uri.parse('$baseUrl/api/teacher-call/teachers'), ); if (response.statusCode == 200) { final data = jsonDecode(utf8.decode(response.bodyBytes)); _teachers = data['teachers'] ?? []; } } catch (_) { // 쑰용히 λ¬΄μ‹œ - λ§ˆμ§€λ§‰μœΌλ‘œ λ°›μ•„μ˜¨ λͺ©λ‘μ„ μœ μ§€ν•œλ‹€. } } Future _fetchMyCalls() async { try { final response = await http.get( Uri.parse('$baseUrl/api/teacher-call/my-calls?studentId=$studentId'), ); if (response.statusCode == 200) { final data = jsonDecode(utf8.decode(response.bodyBytes)); _myCalls = data['calls'] ?? []; } } catch (_) { // 쑰용히 λ¬΄μ‹œ } } Future _fetchRanking() async { try { final response = await http.get( Uri.parse('$baseUrl/api/teacher-call/ranking'), ); if (response.statusCode == 200) { final data = jsonDecode(utf8.decode(response.bodyBytes)); _ranking = data['ranking'] ?? []; } } catch (_) { // 쑰용히 λ¬΄μ‹œ } } Future<(bool success, String message)> callTeacher({ required String teacherId, required String purpose, }) async { _isCalling = true; _safeNotify(); try { final response = await http.post( Uri.parse('$baseUrl/api/teacher-call/call'), headers: {"Content-Type": "application/json"}, body: jsonEncode({ "studentId": studentId, "studentName": studentName, "teacherId": teacherId, "purpose": purpose, }), ); final result = jsonDecode(utf8.decode(response.bodyBytes)); if (response.statusCode == 200 && result['status'] == 'success') { await Future.wait([_fetchMyCalls(), _fetchRanking()]); return (true, '${result['message'] ?? 'ν˜ΈμΆœν–ˆμŠ΅λ‹ˆλ‹€.'}'); } return (false, '${result['message'] ?? '호좜 μ‹€νŒ¨'}'); } catch (e) { return (false, 'λ„€νŠΈμ›Œν¬ μ—λŸ¬: $e'); } finally { _isCalling = false; _safeNotify(); } } }