72 lines
1.9 KiB
Dart
72 lines
1.9 KiB
Dart
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<Instruction> _instructions = [];
|
|
Set<String> _selectedTags = {};
|
|
|
|
bool get isLoading => _isLoading;
|
|
List<Instruction> get instructions => _instructions;
|
|
|
|
List<Instruction> get filteredInstructions {
|
|
if (_selectedTags.isEmpty) {
|
|
return _instructions;
|
|
}
|
|
return _instructions
|
|
.where((instr) => _selectedTags.every((tag) => instr.tags.contains(tag)))
|
|
.toList();
|
|
}
|
|
|
|
Set<String> get allAvailableTags {
|
|
return _instructions.fold<Set<String>>(
|
|
<String>{}, (prev, element) => prev..addAll(element.tags));
|
|
}
|
|
|
|
Set<String> get selectedTags => _selectedTags;
|
|
|
|
|
|
Future<void> loadInstructions() async {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
_instructions = await _repository.getInstructions();
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> addInstruction(String title, String content, List<String> tags) async {
|
|
final newInstruction = Instruction(
|
|
title: title,
|
|
content: content,
|
|
tags: tags,
|
|
createdAt: DateTime.now(),
|
|
);
|
|
await _repository.addInstruction(newInstruction);
|
|
await loadInstructions();
|
|
}
|
|
|
|
Future<void> updateInstruction(Instruction instruction) async {
|
|
await _repository.updateInstruction(instruction);
|
|
await loadInstructions();
|
|
}
|
|
|
|
Future<void> 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();
|
|
}
|
|
} |