2024-06-09 17:06:10 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2024-06-11 17:32:25 +00:00
|
|
|
import 'package:calodiary/food_entry_bloc.dart';
|
2024-06-09 17:06:10 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2024-06-12 12:42:29 +00:00
|
|
|
import 'package:universal_platform/universal_platform.dart';
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-12 12:42:29 +00:00
|
|
|
class AppStorage {
|
|
|
|
static late AppStorage _instance;
|
2024-06-09 17:06:10 +00:00
|
|
|
late String path;
|
2024-06-12 12:42:29 +00:00
|
|
|
AppStorage._create();
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-12 12:42:29 +00:00
|
|
|
static Future<AppStorage> create() async {
|
|
|
|
var storage = AppStorage._create();
|
|
|
|
|
|
|
|
Directory dir = Directory('');
|
|
|
|
|
|
|
|
if (UniversalPlatform.isDesktop) {
|
|
|
|
dir = await getApplicationCacheDirectory();
|
|
|
|
} else if (UniversalPlatform.isAndroid) {
|
|
|
|
dir = await getApplicationDocumentsDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.path = dir.path;
|
2024-06-11 17:05:42 +00:00
|
|
|
_instance = storage;
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
return _instance;
|
2024-06-09 17:06:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-12 12:42:29 +00:00
|
|
|
static AppStorage getInstance() => _instance;
|
2024-06-11 17:05:42 +00:00
|
|
|
|
2024-06-09 17:06:10 +00:00
|
|
|
Future<List<FoodEntry>> getEntriesForDate(DateTime date) async {
|
|
|
|
List<FoodEntry> entries = [];
|
|
|
|
var filePath = '$path/${date.year}/${date.month}/${date.day}';
|
|
|
|
|
|
|
|
var file = File(filePath);
|
|
|
|
var exists = await file.exists();
|
|
|
|
|
|
|
|
if (!exists) return [];
|
|
|
|
|
|
|
|
var lines = await file.readAsLines();
|
|
|
|
|
|
|
|
for (var line in lines) {
|
|
|
|
var fields = line.split(',');
|
|
|
|
var entry = FoodEntry(
|
|
|
|
name: fields[1],
|
|
|
|
mass: double.parse(fields[2]),
|
|
|
|
kcalPerMass: double.parse(fields[3]));
|
|
|
|
entries.add(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> writeEntriesForDate(
|
|
|
|
DateTime date, List<FoodEntry> foodEntries) async {
|
|
|
|
var filePath = '$path/${date.year}/${date.month}/${date.day}';
|
|
|
|
var file = File(filePath);
|
|
|
|
|
|
|
|
var exists = await file.exists();
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
await file.create(recursive: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
String fullString = '';
|
|
|
|
for (var entry in foodEntries) {
|
|
|
|
fullString += '${entry.toString()}\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
await file.writeAsString(fullString);
|
|
|
|
}
|
2024-06-10 01:06:56 +00:00
|
|
|
|
|
|
|
Future<void> updateLimit(double limit) async {
|
|
|
|
var filePath = '$path/limit';
|
|
|
|
var file = File(filePath);
|
|
|
|
|
|
|
|
var exists = await file.exists();
|
|
|
|
if (!exists) {
|
|
|
|
await file.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
await file.writeAsString(limit.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<double> readLimit() async {
|
|
|
|
var filePath = '$path/limit';
|
|
|
|
var file = File(filePath);
|
|
|
|
var exists = await file.exists();
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
return 2000;
|
|
|
|
}
|
|
|
|
|
|
|
|
var line = await file.readAsLines();
|
|
|
|
|
|
|
|
double limit;
|
|
|
|
try {
|
|
|
|
limit = double.parse(line[0]);
|
|
|
|
} catch (e) {
|
|
|
|
limit = 2000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
}
|
2024-06-12 12:42:29 +00:00
|
|
|
|
|
|
|
Future<String> readBrightness() async {
|
|
|
|
var filePath = '$path/brightness';
|
|
|
|
var file = File(filePath);
|
|
|
|
var exists = await file.exists();
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
return 'dark';
|
|
|
|
}
|
|
|
|
|
|
|
|
var line = await file.readAsLines();
|
|
|
|
|
|
|
|
if (line.isEmpty || (line[0] != 'dark' && line[0] != 'light')) {
|
|
|
|
return 'dark';
|
|
|
|
}
|
|
|
|
|
|
|
|
return line[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> writeBrightness(String brightness) async {
|
|
|
|
var filePath = '$path/brightness';
|
|
|
|
var file = File(filePath);
|
|
|
|
var exists = await file.exists();
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
file.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
await file.writeAsString(brightness);
|
|
|
|
}
|
2024-06-09 17:06:10 +00:00
|
|
|
}
|