manager-app/lib/Helpers/SessionHelper.dart
2021-07-28 18:11:45 +02:00

39 lines
876 B
Dart

import 'dart:io';
import 'package:manager_app/Models/session.dart';
import 'package:path_provider/path_provider.dart';
class SessionHelper {
// TODO instance LOAD FILE..
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/session.json');
}
Future<File> writeSession(Session session) async {
final file = await _localFile;
// Write the file
return file.writeAsString(session.toString());
}
Future<int> readSession() async {
try {
final file = await _localFile;
// Read the file
final contents = await file.readAsString();
return int.parse(contents);
} catch (e) {
// If encountering an error, return 0
return 0;
}
}
}