import '../models/instruction.dart'; import 'package:uuid/uuid.dart'; abstract class InstructionRepository { Future> getInstructions(); Future addInstruction(Instruction instruction); Future updateInstruction(Instruction instruction); Future deleteInstruction(String id); } // ЛОКАЛЬНАЯ РЕАЛИЗАЦИЯ, имитирующая работу с базой данных. class LocalInstructionRepository implements InstructionRepository { final List _instructions = [ Instruction( id: '1', title: 'Инструктаж по пожарной безопасности', content: '1. Не курить в помещении. 2. В случае пожара звонить 101. 3. Использовать огнетушитель, расположенный у выхода.', tags: ['безопасность', 'офис'], createdAt: DateTime.now().subtract(const Duration(days: 2)), ), Instruction( id: '2', title: 'Работа с новым ПО "Феникс"', content: 'Подробное описание как работать с новым программным обеспечением "Феникс".', tags: ['it', 'софт'], createdAt: DateTime.now().subtract(const Duration(days: 1)), ), Instruction( id: '3', title: 'Правила поведения на новогоднем корпоративе', content: 'Веселиться, но в меру. Помнить о субординации. Не злоупотреблять напитками.', tags: ['офис', 'развлечения', 'hr'], createdAt: DateTime.now(), ), ]; final _uuid = const Uuid(); @override Future> getInstructions() async { // Имитация задержки сети await Future.delayed(const Duration(milliseconds: 400)); _instructions.sort((a, b) => b.createdAt.compareTo(a.createdAt)); return List.of(_instructions); } @override Future addInstruction(Instruction instruction) async { await Future.delayed(const Duration(milliseconds: 300)); final newInstruction = instruction.copyWith( id: _uuid.v4(), createdAt: DateTime.now(), ); _instructions.add(newInstruction); return newInstruction; } @override Future updateInstruction(Instruction instruction) async { await Future.delayed(const Duration(milliseconds: 300)); final index = _instructions.indexWhere((i) => i.id == instruction.id); if (index != -1) { _instructions[index] = instruction; } else { throw Exception('Instruction not found'); } } @override Future deleteInstruction(String id) async { await Future.delayed(const Duration(milliseconds: 200)); _instructions.removeWhere((i) => i.id == id); } }