Initial commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
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> 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": {}});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Todo {
|
||||
Todo({required this.title, required this.widget, required this.checked});
|
||||
|
||||
String title;
|
||||
Widget widget;
|
||||
bool checked;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:todoapp/elems/todo.dart';
|
||||
|
||||
class TodoList {
|
||||
TodoList({required this.title, required this.widget, required this.todos});
|
||||
|
||||
String title;
|
||||
Widget widget;
|
||||
List<Todo> todos;
|
||||
}
|
||||
Reference in New Issue
Block a user