import 'package:flutter/material.dart'; import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/utilities/logger.dart'; class NotesService extends ChangeNotifier { final String walletId; NotesService({required this.walletId}); Map get notesSync { final notes = DB.instance.get(boxName: walletId, key: 'notes') as Map?; return notes == null ? {} : Map.from(notes); } /// Holds transaction notes /// map of contact /// txid is used as key due to uniqueness Future>? _notes; Future> get notes => _notes ??= _fetchNotes(); // fetch notes map Future> _fetchNotes() async { final notes = DB.instance.get(boxName: walletId, key: 'notes') as Map?; return notes == null ? {} : Map.from(notes); } /// search notes //TODO optimize notes search? Future> search(String text) async { if (text.isEmpty) return notes; var results = Map.from(await notes); results.removeWhere( (key, value) => (!key.contains(text) && !value.contains(text))); return results; } /// fetch note given a transaction ID Future getNoteFor({required String txid}) async { final note = (await notes)[txid]; return note ?? ""; } /// edit or add new note for the given [txid] Future editOrAddNote( {required String txid, required String note}) async { final _notes = await notes; _notes[txid] = note; await DB.instance .put(boxName: walletId, key: 'notes', value: _notes); //todo: check if this is needed Logging.instance.log("editOrAddNote: tx note saved", level: LogLevel.Info); await _refreshNotes(); } /// Remove note from db Future deleteNote({required String txid}) async { final entries = DB.instance.get(boxName: walletId, key: 'notes') as Map; entries.remove(txid); await DB.instance .put(boxName: walletId, key: 'notes', value: entries); Logging.instance.log("tx note removed", level: LogLevel.Info); await _refreshNotes(); } Future _refreshNotes() async { final newNotes = await _fetchNotes(); _notes = Future(() => newNotes); notifyListeners(); } }