import 'package:flutter/material.dart'; import '../data/instruction_repository.dart'; import '../models/instruction.dart'; class InstructionProvider extends ChangeNotifier { final InstructionRepository _repository; InstructionProvider(this._repository); bool _isLoading = false; List _instructions = []; Set _selectedTags = {}; bool get isLoading => _isLoading; List get instructions => _instructions; List get filteredInstructions { if (_selectedTags.isEmpty) { return _instructions; } return _instructions .where((instr) => _selectedTags.every((tag) => instr.tags.contains(tag))) .toList(); } Set get allAvailableTags { return _instructions.fold>( {}, (prev, element) => prev..addAll(element.tags)); } Set get selectedTags => _selectedTags; Future loadInstructions() async { _isLoading = true; notifyListeners(); _instructions = await _repository.getInstructions(); _isLoading = false; notifyListeners(); } Future addInstruction(String title, String content, List tags) async { final newInstruction = Instruction( title: title, content: content, tags: tags, createdAt: DateTime.now(), ); await _repository.addInstruction(newInstruction); await loadInstructions(); } Future updateInstruction(Instruction instruction) async { await _repository.updateInstruction(instruction); await loadInstructions(); } Future deleteInstruction(String id) async { await _repository.deleteInstruction(id); _instructions.removeWhere((instr) => instr.id == id); notifyListeners(); } void toggleTagFilter(String tag) { if (_selectedTags.contains(tag)) { _selectedTags.remove(tag); } else { _selectedTags.add(tag); } notifyListeners(); } }