LCOV - code coverage report
Current view: top level - src/storage - io.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 44 45 97.8 %
Date: 2020-06-20 22:24:18 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:async';
       2             : import 'dart:convert';
       3             : import 'dart:io';
       4             : import 'package:path_provider/path_provider.dart';
       5             : import '../value.dart';
       6             : 
       7             : class StorageImpl {
       8           1 :   StorageImpl(this.fileName, [this.path]);
       9             : 
      10             :   final String path, fileName;
      11             : 
      12             :   final Value<Map<String, dynamic>> subject =
      13             :       Value<Map<String, dynamic>>(<String, dynamic>{});
      14             : 
      15           1 :   Future<void> clear() async {
      16           2 :     File _file = await _getFile();
      17           1 :     subject
      18           2 :       ..value.clear()
      19           1 :       ..update("", 2, null);
      20           3 :     subject.value.clear();
      21           1 :     return _file.deleteSync();
      22             :   }
      23             : 
      24             :   // Future<bool> _exists() async {
      25             :   //   File _file = await _getFile();
      26             :   //   return _file.existsSync();
      27             :   // }
      28             : 
      29           1 :   Future<void> flush() async {
      30           3 :     final serialized = json.encode(subject.value);
      31           2 :     File _file = await _getFile();
      32           2 :     await _file.writeAsString(serialized, flush: true);
      33             :     return;
      34             :   }
      35             : 
      36           1 :   T read<T>(String key) {
      37           3 :     return subject.value[key] as T;
      38             :   }
      39             : 
      40           1 :   Future<void> init([Map<String, dynamic> initialData]) async {
      41           3 :     subject.value = initialData ?? <String, dynamic>{};
      42           2 :     File _file = await _getFile();
      43           1 :     if (_file.existsSync()) {
      44           1 :       return _readFile();
      45             :     } else {
      46           0 :       return _writeFile(subject.value);
      47             :     }
      48             :   }
      49             : 
      50           1 :   Future<void> remove(String key) async {
      51           1 :     subject
      52           2 :       ..value.remove(key)
      53           1 :       ..update(key, 1, null);
      54           4 :     await _writeFile(subject.value);
      55             :   }
      56             : 
      57           1 :   Future<void> write(String key, dynamic value) async {
      58           1 :     subject
      59           2 :       ..value[key] = value
      60           1 :       ..update(key, 0, value);
      61           4 :     await _writeFile(subject.value);
      62             :   }
      63             : 
      64           1 :   Future<void> _writeFile(Map<String, dynamic> data) async {
      65           2 :     File _file = await _getFile();
      66           2 :     _file.writeAsString(json.encode(data), flush: true);
      67             :   }
      68             : 
      69           1 :   Future<void> _readFile() async {
      70           2 :     File _file = await _getFile();
      71           2 :     final content = await _file.readAsString()
      72           1 :       ..trim();
      73           2 :     subject.value =
      74           2 :         json?.decode(content == "" ? {} : content) as Map<String, dynamic>;
      75             :   }
      76             : 
      77           1 :   Future<File> _getFile() async {
      78           2 :     final dir = await _getDocumentDir();
      79           2 :     final _path = path ?? dir.path;
      80           3 :     final _file = File('$_path/$fileName.gs');
      81             :     return _file;
      82             :   }
      83             : 
      84           1 :   Future<Directory> _getDocumentDir() async {
      85             :     try {
      86           2 :       return await getApplicationDocumentsDirectory();
      87             :     } catch (err) {
      88             :       throw err;
      89             :     }
      90             :   }
      91             : }

Generated by: LCOV version 1.14