LCOV - code coverage report
Current view: top level - src/storage - io.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 39 40 97.5 %
Date: 2020-06-20 14:47:21 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           3 :     subject.value.clear();
      18           1 :     return _file.deleteSync();
      19             :   }
      20             : 
      21             :   // Future<bool> _exists() async {
      22             :   //   File _file = await _getFile();
      23             :   //   return _file.existsSync();
      24             :   // }
      25             : 
      26           1 :   Future<void> flush() async {
      27           3 :     final serialized = json.encode(subject.value);
      28           2 :     File _file = await _getFile();
      29           2 :     await _file.writeAsString(serialized, flush: true);
      30             :     return;
      31             :   }
      32             : 
      33           1 :   T read<T>(String key) {
      34           3 :     return subject.value[key] as T;
      35             :   }
      36             : 
      37           1 :   Future<void> init([Map<String, dynamic> initialData]) async {
      38           3 :     subject.value = initialData ?? <String, dynamic>{};
      39           2 :     File _file = await _getFile();
      40           1 :     if (_file.existsSync()) {
      41           1 :       return _readFile();
      42             :     } else {
      43           0 :       return _writeFile(subject.value);
      44             :     }
      45             :   }
      46             : 
      47           1 :   Future<void> remove(String key) async {
      48           1 :     subject
      49           2 :       ..value.remove(key)
      50           1 :       ..update();
      51           4 :     await _writeFile(subject.value);
      52             :   }
      53             : 
      54           1 :   Future<void> write(String key, dynamic value) async {
      55           1 :     subject
      56           2 :       ..value[key] = value
      57           1 :       ..update();
      58           4 :     await _writeFile(subject.value);
      59             :   }
      60             : 
      61           1 :   Future<void> _writeFile(Map<String, dynamic> data) async {
      62           2 :     File _file = await _getFile();
      63           2 :     _file.writeAsString(json.encode(data), flush: true);
      64             :   }
      65             : 
      66           1 :   Future<void> _readFile() async {
      67           2 :     File _file = await _getFile();
      68           2 :     final content = await _file.readAsString();
      69           3 :     subject.value = json?.decode(content) as Map<String, dynamic>;
      70             :   }
      71             : 
      72           1 :   Future<File> _getFile() async {
      73           2 :     final dir = await _getDocumentDir();
      74           2 :     final _path = path ?? dir.path;
      75           3 :     final _file = File('$_path/$fileName.gs');
      76             :     return _file;
      77             :   }
      78             : 
      79           1 :   Future<Directory> _getDocumentDir() async {
      80             :     try {
      81           2 :       return await getApplicationDocumentsDirectory();
      82             :     } catch (err) {
      83             :       throw err;
      84             :     }
      85             :   }
      86             : }

Generated by: LCOV version 1.14