// πŸ“ μ„ μƒλ‹˜ μœ„μΉ˜ 등둝(κ΅μ‚¬μš©) ν™”λ©΄μ˜ κΈ°λŠ₯(μ„œλ²„ 톡신/μƒνƒœ) λ‹΄λ‹Ή 컨트둀러. import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import '../config.dart'; class TeacherLocationController extends ChangeNotifier { final String teacherId; TeacherLocationController({required this.teacherId}); bool _isLoading = true; bool _isSaving = false; String _currentLocation = "ꡐ무싀"; List _receivedCalls = []; bool _disposed = false; bool get isLoading => _isLoading; bool get isSaving => _isSaving; String get currentLocation => _currentLocation; List get receivedCalls => _receivedCalls; void _safeNotify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; super.dispose(); } Future init() async { await Future.wait([_fetchMyLocation(), fetchReceivedCalls()]); _isLoading = false; _safeNotify(); } Future _fetchMyLocation() 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)); final List list = data['teachers'] ?? []; final mine = list.firstWhere( (t) => t['teacherId'] == teacherId, orElse: () => null, ); if (mine != null) _currentLocation = mine['location'] ?? 'ꡐ무싀'; } } catch (_) { // 쑰용히 λ¬΄μ‹œ } } Future fetchReceivedCalls() async { try { final response = await http.get( Uri.parse( '$baseUrl/api/teacher-call/received-calls?teacherId=$teacherId', ), ); if (response.statusCode == 200) { final data = jsonDecode(utf8.decode(response.bodyBytes)); _receivedCalls = data['calls'] ?? []; _safeNotify(); } } catch (_) { // 쑰용히 λ¬΄μ‹œ } } Future<(bool success, String message)> updateLocation(String location) async { _isSaving = true; _safeNotify(); try { final response = await http.post( Uri.parse('$baseUrl/api/teacher-call/location'), headers: {"Content-Type": "application/json"}, body: jsonEncode({"teacherId": teacherId, "location": location}), ); final result = jsonDecode(utf8.decode(response.bodyBytes)); if (response.statusCode == 200 && result['status'] == 'success') { _currentLocation = location; return (true, '${result['message'] ?? 'μœ„μΉ˜κ°€ μ—…λ°μ΄νŠΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€.'}'); } return (false, '${result['message'] ?? 'μ—…λ°μ΄νŠΈ μ‹€νŒ¨'}'); } catch (e) { return (false, 'λ„€νŠΈμ›Œν¬ μ—λŸ¬: $e'); } finally { _isSaving = false; _safeNotify(); } } }