add custom srs
This commit is contained in:
30
lib/src/services/custom_deck_repository.dart
Normal file
30
lib/src/services/custom_deck_repository.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/custom_kanji_item.dart';
|
||||
|
||||
class CustomDeckRepository {
|
||||
static const _key = 'custom_deck';
|
||||
|
||||
Future<List<CustomKanjiItem>> getCustomDeck() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final jsonString = prefs.getString(_key);
|
||||
if (jsonString != null) {
|
||||
final List<dynamic> jsonList = json.decode(jsonString);
|
||||
return jsonList.map((json) => CustomKanjiItem.fromJson(json)).toList();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
Future<void> addCard(CustomKanjiItem item) async {
|
||||
final deck = await getCustomDeck();
|
||||
deck.add(item);
|
||||
await _saveDeck(deck);
|
||||
}
|
||||
|
||||
Future<void> _saveDeck(List<CustomKanjiItem> deck) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final jsonList = deck.map((item) => item.toJson()).toList();
|
||||
await prefs.setString(_key, json.encode(jsonList));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user