Files
superport/lib/screens/license/controllers/license_form_controller.dart
JiWoong Sul 8384423cf2 feat: 라이선스 및 창고 관리 API 연동 구현
- 라이선스 관리 API 연동 완료
  - LicenseRemoteDataSource, LicenseService 구현
  - LicenseListController, LicenseFormController API 연동
  - 페이지네이션, 검색, 필터링 기능 추가
  - 라이선스 할당/해제 기능 구현

- 창고 관리 API 연동 완료
  - WarehouseRemoteDataSource, WarehouseService 구현
  - WarehouseLocationListController, WarehouseLocationFormController API 연동
  - 창고별 장비 조회 및 용량 관리 기능 추가

- DI 컨테이너에 새로운 서비스 등록
- API 통합 문서 업데이트 (전체 진행률 100% 달성)
2025-07-25 00:18:49 +09:00

187 lines
4.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/models/license_model.dart';
import 'package:superport/services/license_service.dart';
import 'package:superport/services/mock_data_service.dart';
// 라이센스 폼의 상태 및 비즈니스 로직을 담당하는 컨트롤러
class LicenseFormController extends ChangeNotifier {
final bool useApi;
final MockDataService? mockDataService;
late final LicenseService _licenseService;
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
bool _isEditMode = false;
int? _licenseId;
License? _originalLicense;
bool _isLoading = false;
String? _error;
bool _isSaving = false;
// 폼 필드 값
String _name = '';
int _companyId = 1;
int _durationMonths = 12; // 기본값: 12개월
String _visitCycle = '미방문'; // 기본값: 미방문
LicenseFormController({
this.useApi = true,
this.mockDataService,
int? licenseId,
}) {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
_licenseService = GetIt.instance<LicenseService>();
}
if (licenseId != null) {
_licenseId = licenseId;
_isEditMode = true;
loadLicense();
}
}
// Getters
bool get isEditMode => _isEditMode;
int? get licenseId => _licenseId;
License? get originalLicense => _originalLicense;
bool get isLoading => _isLoading;
String? get error => _error;
bool get isSaving => _isSaving;
String get name => _name;
int get companyId => _companyId;
int get durationMonths => _durationMonths;
String get visitCycle => _visitCycle;
// Setters
void setName(String value) {
_name = value;
notifyListeners();
}
void setCompanyId(int value) {
_companyId = value;
notifyListeners();
}
void setDurationMonths(int value) {
_durationMonths = value;
notifyListeners();
}
void setVisitCycle(String value) {
_visitCycle = value;
notifyListeners();
}
// 라이센스 정보 로드 (수정 모드)
Future<void> loadLicense() async {
if (_licenseId == null) return;
_isLoading = true;
_error = null;
notifyListeners();
try {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
_originalLicense = await _licenseService.getLicenseById(_licenseId!);
} else {
_originalLicense = mockDataService?.getLicenseById(_licenseId!);
}
if (_originalLicense != null) {
_name = _originalLicense!.name;
_companyId = _originalLicense!.companyId;
_durationMonths = _originalLicense!.durationMonths;
_visitCycle = _originalLicense!.visitCycle;
}
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
// 라이센스 저장
Future<bool> saveLicense() async {
if (formKey.currentState?.validate() != true) return false;
formKey.currentState?.save();
_isSaving = true;
_error = null;
notifyListeners();
try {
final license = License(
id: _isEditMode ? _licenseId : null,
companyId: _companyId,
name: _name,
durationMonths: _durationMonths,
visitCycle: _visitCycle,
);
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
if (_isEditMode) {
await _licenseService.updateLicense(license);
} else {
await _licenseService.createLicense(license);
}
} else {
if (_isEditMode) {
mockDataService?.updateLicense(license);
} else {
mockDataService?.addLicense(license);
}
}
return true;
} catch (e) {
_error = e.toString();
notifyListeners();
return false;
} finally {
_isSaving = false;
notifyListeners();
}
}
// 폼 초기화
void resetForm() {
_name = '';
_companyId = 1;
_durationMonths = 12;
_visitCycle = '미방문';
_error = null;
formKey.currentState?.reset();
notifyListeners();
}
// 유효성 검사
String? validateName(String? value) {
if (value == null || value.isEmpty) {
return '라이선스명을 입력해주세요';
}
if (value.length < 2) {
return '라이선스명은 2자 이상이어야 합니다';
}
return null;
}
String? validateDuration(String? value) {
if (value == null || value.isEmpty) {
return '계약 기간을 입력해주세요';
}
final duration = int.tryParse(value);
if (duration == null || duration < 1) {
return '유효한 계약 기간을 입력해주세요';
}
return null;
}
@override
void dispose() {
super.dispose();
}
}