56 lines
1.7 KiB
Dart
56 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:todo/buisness/ToDoItem.dart';
|
|
import 'package:todo/database/DatabaseInterface.dart';
|
|
|
|
class PathProvider implements DatabaseInterface {
|
|
Future<String> get _localPath async {
|
|
final directory = await getApplicationDocumentsDirectory();
|
|
return directory.path;
|
|
}
|
|
Future<File> get _localFile async {
|
|
final path = await _localPath;
|
|
File file = File('$path/dataSeq.txt');
|
|
if (!file.existsSync()) {
|
|
file = await file.create();
|
|
await file.writeAsString('[]');
|
|
}
|
|
return file;
|
|
}
|
|
|
|
@override
|
|
Future<List<ToDoItem>> getToDoItems() async {
|
|
final file = await _localFile;
|
|
final contents = await file.readAsString();
|
|
List<dynamic> contentJson = jsonDecode(contents);
|
|
|
|
List<ToDoItem> toDoList = contentJson.map((item) => ToDoItem.fromMap(item)).toList();
|
|
return toDoList;
|
|
}
|
|
|
|
@override
|
|
Future<File> insertToDoItem(ToDoItem item) async {
|
|
item.id = DateTime.now().millisecondsSinceEpoch;
|
|
List<ToDoItem> l = await getToDoItems();
|
|
l.add(item);
|
|
final file = await _localFile;
|
|
return file.writeAsString(jsonEncode(l.map((item) => item.toMap()).toList()));
|
|
}
|
|
|
|
@override
|
|
Future<File> updateToDoItem(ToDoItem item) async {
|
|
final file = await _localFile;
|
|
return file.writeAsString(item.toMap() as String);
|
|
}
|
|
|
|
@override
|
|
Future<File> deleteToDoItem(int id) async {
|
|
final file = await _localFile;
|
|
List<ToDoItem> l = await getToDoItems();
|
|
l.remove(l.where((item) => item.id == id).first);
|
|
return file.writeAsString(jsonEncode(l.map((item) => item.toMap()).toList()));
|
|
}
|
|
|
|
} |