82 lines
2.0 KiB
Dart
82 lines
2.0 KiB
Dart
import 'package:path_provider/path_provider.dart';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
class FileHandler {
|
|
|
|
static Future<String> get localPath async {
|
|
final directory = await getApplicationDocumentsDirectory();
|
|
return directory.path;
|
|
}
|
|
|
|
static Future<File> get localFile async {
|
|
final path = await localPath;
|
|
return File("$path/todos.json");
|
|
}
|
|
|
|
static Future<dynamic> get fileContent async {
|
|
await existCheck();
|
|
File f = await localFile;
|
|
String content = await f.readAsString();
|
|
final js = await json.decode(content);
|
|
return js;
|
|
}
|
|
|
|
static Future<void> saveNotified(String listName, String todoName, String notifyType, bool value) async {
|
|
Map content = await fileContent;
|
|
content[listName][todoName][notifyType] = value;
|
|
|
|
await saveFile(content);
|
|
}
|
|
|
|
static Future<void> saveFile(js) async {
|
|
File f = await localFile;
|
|
String content = json.encode(js);
|
|
f.writeAsString(content);
|
|
}
|
|
|
|
static Future<void> saveList(js, String listName) async {
|
|
final content = await fileContent;
|
|
content[listName] = js;
|
|
await saveFile(content);
|
|
}
|
|
|
|
static Future<void> renameList(String oldListName, String newListName) async {
|
|
var newJs = {};
|
|
|
|
final content = await fileContent;
|
|
for (String key in content.keys) {
|
|
if (key != oldListName) {
|
|
newJs[key] = content[key];
|
|
} else {
|
|
newJs[newListName] = content[key];
|
|
}
|
|
}
|
|
await saveFile(newJs);
|
|
}
|
|
|
|
static Future<void> addList(String listName) async {
|
|
var js = await fileContent;
|
|
js[listName] = {};
|
|
await saveFile(js);
|
|
}
|
|
|
|
static Future<void> removeList(String listName) async {
|
|
final js = await fileContent;
|
|
var newJs = {};
|
|
for (String key in js.keys) {
|
|
if (key != listName) {
|
|
newJs[key] = js[key];
|
|
}
|
|
}
|
|
await saveFile(newJs);
|
|
}
|
|
|
|
static Future<void> existCheck() async {
|
|
File f = await localFile;
|
|
bool exists = await f.exists();
|
|
|
|
if (!exists) {
|
|
await saveFile({"Personal": {}});
|
|
}
|
|
}
|
|
} |