import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/custom_kanji_item.dart'; class CustomDeckRepository { static const _key = 'custom_deck'; Future> getCustomDeck() async { final prefs = await SharedPreferences.getInstance(); final jsonString = prefs.getString(_key); if (jsonString != null) { final List jsonList = json.decode(jsonString); return jsonList.map((json) => CustomKanjiItem.fromJson(json)).toList(); } return []; } Future addCard(CustomKanjiItem item) async { final deck = await getCustomDeck(); deck.add(item); await saveDeck(deck); } Future updateCard(CustomKanjiItem item) async { await updateCards([item]); } Future updateCards(List itemsToUpdate) async { final deck = await getCustomDeck(); for (var item in itemsToUpdate) { final index = deck.indexWhere((element) => element.characters == item.characters); if (index != -1) { deck[index] = item; } } await saveDeck(deck); } Future deleteCard(CustomKanjiItem item) async { final deck = await getCustomDeck(); deck.removeWhere((element) => element.characters == item.characters); await saveDeck(deck); } Future saveDeck(List deck) async { final prefs = await SharedPreferences.getInstance(); final jsonList = deck.map((item) => item.toJson()).toList(); await prefs.setString(_key, json.encode(jsonList)); } }