Files
school-attendance/lib/function/device_checkout_ledger_controller.dart
T
sihooandClaude Sonnet 5 2b3227f268 스마트기기 반출: "기기 반납 확인" 버튼 추가
자동 감지 하드웨어가 아직 없어서, 승인된(APPROVED) 요청에 선생님이
기기를 실제로 돌려받았을 때 누르는 "기기 반납 확인" 버튼을 추가.
누르면 상태가 반납완료로 바뀌어 10분 후 미반납 알림이 더 이상
가지 않는다 (백엔드는 별도 저장소에서 이미 배포 완료).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-01 14:05:19 +09:00

101 lines
3.0 KiB
Dart

// 📋 선생님용 "스마트기기 반출 대장" 화면의 기능(서버 통신/상태) 담당 컨트롤러.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import '../config.dart';
class DeviceCheckoutLedgerController extends ChangeNotifier {
List<dynamic> _requests = [];
bool _isLoading = true;
bool _isWorking = false;
Timer? _timer;
bool _disposed = false;
List<dynamic> get requests => _requests;
bool get isLoading => _isLoading;
bool get isWorking => _isWorking;
void _safeNotify() {
if (!_disposed) notifyListeners();
}
void init() {
fetchAll();
_timer = Timer.periodic(const Duration(seconds: 5), (_) => fetchAll());
}
@override
void dispose() {
_disposed = true;
_timer?.cancel();
super.dispose();
}
Future<void> fetchAll() async {
try {
final response = await http.get(
Uri.parse('$baseUrl/api/device-checkout/list'),
);
if (response.statusCode == 200) {
final data = jsonDecode(utf8.decode(response.bodyBytes));
_requests = data['requests'] ?? [];
}
} catch (_) {
// 새로고침 실패는 조용히 무시하고 마지막으로 받아온 목록을 유지한다.
} finally {
_isLoading = false;
_safeNotify();
}
}
// ✅ 승인 시 승인한 선생님 계정을 함께 보낸다 (대장에 "OOO 선생님이 허용했습니다" 표시 +
// 미반납 알림을 그 선생님에게만 보내기 위함).
Future<(bool success, String message)> approve(
int requestId, {
required String teacherId,
required String teacherName,
}) async {
return _postAction('/api/device-checkout/approve', {
"requestId": requestId,
"teacherId": teacherId,
"teacherName": teacherName,
});
}
Future<(bool success, String message)> reject(int requestId) async {
return _postAction('/api/device-checkout/reject', {"requestId": requestId});
}
// 🖐️ [하드웨어 자동 감지 전까지 임시] 선생님이 기기를 실제로 돌려받았을 때 누르는 버튼.
Future<(bool success, String message)> confirmReturn(int requestId) async {
return _postAction('/api/device-checkout/return', {"requestId": requestId});
}
Future<(bool success, String message)> _postAction(
String path,
Map<String, dynamic> body,
) async {
_isWorking = true;
_safeNotify();
try {
final response = await http.post(
Uri.parse('$baseUrl$path'),
headers: {"Content-Type": "application/json"},
body: jsonEncode(body),
);
final result = jsonDecode(utf8.decode(response.bodyBytes));
if (response.statusCode == 200 && result['status'] == 'success') {
await fetchAll();
return (true, '✅ ${result['message'] ?? '처리되었습니다.'}');
}
return (false, '❌ ${result['message'] ?? '처리 실패'}');
} catch (e) {
return (false, '🚨 네트워크 에러: $e');
} finally {
_isWorking = false;
_safeNotify();
}
}
}