libretranslatr
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/history_item.dart';
|
||||
|
||||
class HistoryService {
|
||||
static const _key = 'translation_history';
|
||||
static const _maxItems = 200;
|
||||
|
||||
Future<List<HistoryItem>> getAll() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getStringList(_key) ?? [];
|
||||
return raw
|
||||
.map((s) => HistoryItem.fromJson(jsonDecode(s) as Map<String, dynamic>))
|
||||
.toList()
|
||||
.reversed
|
||||
.toList(); // en yeni en üstte
|
||||
}
|
||||
|
||||
Future<void> add(HistoryItem item) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getStringList(_key) ?? [];
|
||||
raw.add(jsonEncode(item.toJson()));
|
||||
final trimmed = raw.length > _maxItems
|
||||
? raw.sublist(raw.length - _maxItems)
|
||||
: raw;
|
||||
await prefs.setStringList(_key, trimmed);
|
||||
}
|
||||
|
||||
Future<void> clear() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user