69 lines
1.9 KiB
Dart
69 lines
1.9 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import '../models/instruction_log.dart';
|
|
import '../data/interface/IRepository.dart';
|
|
|
|
class InstructionLogProvider extends ChangeNotifier {
|
|
final Repository<InstructionLog> _repository;
|
|
List<InstructionLog> _logs = [];
|
|
bool _isLoading = false;
|
|
|
|
InstructionLogProvider(this._repository);
|
|
|
|
List<InstructionLog> get logs => _logs;
|
|
bool get isLoading => _isLoading;
|
|
|
|
Future<void> loadLogs() async {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
try {
|
|
_logs = await _repository.load();
|
|
} catch (e) {
|
|
debugPrint('Ошибка загрузки журнала инструктажей: $e');
|
|
} finally {
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> addLog(InstructionLog log) async {
|
|
try {
|
|
final newLog = await _repository.add(log);
|
|
_logs.add(newLog);
|
|
notifyListeners();
|
|
} catch (e) {
|
|
debugPrint('Ошибка добавления записи в журнал: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> updateLog(InstructionLog log) async {
|
|
try {
|
|
await _repository.update(log);
|
|
final index = _logs.indexWhere((l) => l.id == log.id);
|
|
if (index != -1) {
|
|
_logs[index] = log;
|
|
notifyListeners();
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Ошибка обновления записи в журнале: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> deleteLog(String? id) async {
|
|
try {
|
|
await _repository.delete(id);
|
|
_logs.removeWhere((log) => log.id == id);
|
|
notifyListeners();
|
|
} catch (e) {
|
|
debugPrint('Ошибка удаления записи из журнала: $e');
|
|
}
|
|
}
|
|
|
|
List<InstructionLog> getLogsByWorker(String workerId) {
|
|
return _logs.where((log) => log.workerId == workerId).toList();
|
|
}
|
|
|
|
List<InstructionLog> getLogsByInstruction(String instructionId) {
|
|
return _logs.where((log) => log.instructionId == instructionId).toList();
|
|
}
|
|
} |