Files
GymLink_Flutter/lib/tools/history.dart
2024-06-18 15:14:37 +03:00

109 lines
4.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
import 'dart:math';
import 'package:gymlink_module_web/interfaces/items.dart';
import 'package:shared_preferences/shared_preferences.dart';
Future<List<GymHistoryItem>> getHistory() async {
final prefs = await SharedPreferences.getInstance();
String historyString = prefs.getString('history') ?? "[]";
List<GymHistoryItem> history = [];
for (var historyItem in jsonDecode(historyString) as List<dynamic>) {
history.add(GymHistoryItem.fromJson(historyItem));
}
history.sort((a, b) => b.id.compareTo(a.id));
history = history.reversed.toList();
return history;
}
Future<void> addToHistory(GymHistoryItem item) async {
final prefs = await SharedPreferences.getInstance();
String historyString = prefs.getString('history') ?? "[]";
List<GymHistoryItem> history = [];
for (var historyItem in jsonDecode(historyString) as List<dynamic>) {
history.add(GymHistoryItem.fromJson(historyItem));
}
item.id = Random().nextInt(100000).toString();
String detailHistoryString = prefs.getString('detail_history') ?? "[]";
List<GymHistoryItemDetail> detailHistory = [];
for (var historyItem in jsonDecode(detailHistoryString) as List<dynamic>) {
detailHistory.add(GymHistoryItemDetail.fromJson(historyItem));
}
var json = {
"id": item.id,
"date": DateTime.now()
.toLocal()
.toLocal()
.toString()
.split(' ')[0]
.replaceAll('-', '.')
.split('.')
.reversed
.join('.'),
"sum": Random().nextInt(100000).toString(),
"pay_url": [null, "https://example.org"][Random().nextInt(2)],
"receiver": "Иванов Иван Иванович ${Random().nextInt(100000).toString()}",
"email": "a${Random().nextInt(100000).toString()}@a.ru",
"address":
"г. ${['Москва', 'Петербург', 'Новгород'][Random().nextInt(3)]}, ул. ${[
'Пушкина',
'Ленина',
'Лермонтова'
][Random().nextInt(3)]}, д. ${Random().nextInt(100).toString()}",
"providers": [
{
"id": Random().nextInt(100000).toString(),
"name": "Поставщик ${Random().nextInt(100000).toString()}",
"status": ["Доставлен", "Доставляется", "Ожидает"][Random().nextInt(3)],
"items": [
{
"photo": "url${Random().nextInt(100000).toString()}",
"id": Random().nextInt(100000).toString(),
"count": Random().nextInt(100),
"price": Random().nextInt(100000).toString()
},
{
"photo": "url${Random().nextInt(100000).toString()}",
"id": Random().nextInt(100000).toString(),
"count": Random().nextInt(100),
"price": Random().nextInt(100000).toString()
}
]
},
{
"id": Random().nextInt(100000).toString(),
"name": "Поставщик ${Random().nextInt(100000).toString()}",
"status": ["Доставлен", "Доставляется", "Ожидает"][Random().nextInt(3)],
"items": [
{
"photo": "url${Random().nextInt(100000).toString()}",
"id": Random().nextInt(100000).toString(),
"count": Random().nextInt(100),
"price": Random().nextInt(100000).toString()
}
]
}
]
};
final detailHistoryItem = GymHistoryItemDetail.fromJson(json);
detailHistory.add(detailHistoryItem);
history.add(GymHistoryItem(
date: detailHistoryItem.date,
id: detailHistoryItem.id,
photo: 'product.png',
sum: detailHistoryItem.sum));
prefs.setString('history', jsonEncode(history));
prefs.setString('detail_history', jsonEncode(detailHistory));
}
Future<GymHistoryItemDetail?> getHistoryDetail(String id) async {
final prefs = await SharedPreferences.getInstance();
String historyString = prefs.getString('detail_history') ?? "[]";
for (var historyItem in jsonDecode(historyString) as List<dynamic>) {
if (GymHistoryItemDetail.fromJson(historyItem).id == id) {
return GymHistoryItemDetail.fromJson(historyItem);
}
}
return null;
}