LCOV - code coverage report
Current view: top level - lib/data/datasources - metropole_local_data_source.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 51 57 89.5 %
Date: 2020-06-26 11:36:11 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:io';
       2             : 
       3             : import 'package:app_pym/core/directory_manager/directory_manager.dart';
       4             : import 'package:app_pym/core/error/exceptions.dart';
       5             : import 'package:app_pym/core/utils/gtfs_utils.dart';
       6             : import 'package:app_pym/data/datasources/gtfs_type_local_data_source.dart';
       7             : import 'package:app_pym/data/models/mobility/calendar_model.dart';
       8             : import 'package:app_pym/data/models/mobility/route_model.dart';
       9             : import 'package:app_pym/data/models/mobility/stop_model.dart';
      10             : import 'package:app_pym/data/models/mobility/stop_time_model.dart';
      11             : import 'package:app_pym/data/models/mobility/trip_model.dart';
      12             : import 'package:archive/archive.dart';
      13             : import 'package:flutter/foundation.dart';
      14             : import 'package:injectable/injectable.dart';
      15             : 
      16             : abstract class MetropoleLocalDataSource extends GTFSTypeLocalDataSource {
      17             :   Future<DateTime> get timestamp;
      18             :   Future<void> setTimestamp(DateTime timestamp);
      19             : }
      20             : 
      21             : @prod
      22             : @LazySingleton(as: MetropoleLocalDataSource)
      23             : class MetropoleLocalDataSourceImpl implements MetropoleLocalDataSource {
      24             :   final DirectoryManager directoryManager;
      25             :   final ZipDecoder zipDecoder;
      26             : 
      27           1 :   const MetropoleLocalDataSourceImpl({
      28             :     @required this.directoryManager,
      29             :     @required this.zipDecoder,
      30             :   });
      31             : 
      32             :   @override
      33           0 :   Future<bool> get fileExists async {
      34           0 :     return File('${await directoryManager.metropole}/CPA.zip').existsSync();
      35             :   }
      36             : 
      37             :   @override
      38           1 :   Future<DateTime> get timestamp async {
      39           5 :     final file = File('${await directoryManager.metropole}/timestamp.txt');
      40           1 :     if (file.existsSync()) {
      41           3 :       return DateTime.parse(file.readAsLinesSync().first);
      42             :     }
      43           1 :     return DateTime.parse("1970-01-01 12:00:00");
      44             :   }
      45             : 
      46             :   @override
      47           1 :   Future<List<CalendarModel>> fetchCalendars() async {
      48           5 :     final file = File('${await directoryManager.metropole}/calendar.txt');
      49           1 :     if (file.existsSync()) {
      50           1 :       return file.parseCalendars();
      51             :     } else {
      52           1 :       throw CacheException('Calendars not found.');
      53             :     }
      54             :   }
      55             : 
      56             :   @override
      57           1 :   Future<List<RouteModel>> fetchRoutes() async {
      58           5 :     final file = File('${await directoryManager.metropole}/routes.txt');
      59           1 :     if (file.existsSync()) {
      60           1 :       return file.parseRoutes();
      61             :     } else {
      62           1 :       throw CacheException('Routes not found.');
      63             :     }
      64             :   }
      65             : 
      66             :   @override
      67           1 :   Future<List<StopModel>> fetchStops() async {
      68           5 :     final file = File('${await directoryManager.metropole}/stops.txt');
      69           1 :     if (file.existsSync()) {
      70           1 :       return file.parseStops();
      71             :     } else {
      72           1 :       throw CacheException('Stops not found.');
      73             :     }
      74             :   }
      75             : 
      76             :   @override
      77           1 :   Future<List<StopTimeModel>> fetchStopTimes() async {
      78           5 :     final file = File('${await directoryManager.metropole}/stop_times.txt');
      79           1 :     if (file.existsSync()) {
      80           1 :       return file.parseStopTimes();
      81             :     } else {
      82           1 :       throw CacheException('StopTimes not found.');
      83             :     }
      84             :   }
      85             : 
      86             :   @override
      87           1 :   Future<List<TripModel>> fetchTrips() async {
      88           5 :     final file = File('${await directoryManager.metropole}/trips.txt');
      89           1 :     if (file.existsSync()) {
      90           1 :       return file.parseTrips();
      91             :     } else {
      92           1 :       throw CacheException('Trips not found.');
      93             :     }
      94             :   }
      95             : 
      96             :   @override
      97           1 :   Future<void> setTimestamp(DateTime timestamp) async {
      98           3 :     final path = await directoryManager.metropole;
      99           2 :     Directory(path).createSync(recursive: true);
     100           2 :     final File file = File('${path}/timestamp.txt');
     101           3 :     await file.writeAsString(timestamp.toIso8601String());
     102             :   }
     103             : 
     104             :   @override
     105           0 :   Stream<List<int>> useAsset() async* {
     106           0 :     final bytedata = await directoryManager.cpaZip;
     107           0 :     final buffer = bytedata.buffer;
     108           0 :     yield buffer.asUint8List(bytedata.offsetInBytes, bytedata.lengthInBytes);
     109             :   }
     110             : 
     111             :   @override
     112           1 :   Future<void> writeFile(Stream<List<int>> bytes) async {
     113             :     // Write the Zip file
     114           5 :     final File file = File('${await directoryManager.metropole}/CPA.zip');
     115           1 :     final IOSink sink = file.openWrite();
     116           3 :     await bytes.forEach(sink.add);
     117           2 :     await sink.flush();
     118           2 :     await sink.close();
     119           1 :     return _unzip(file);
     120             :   }
     121             : 
     122           1 :   Future<void> _unzip(File file) async {
     123             :     // Decode the Zip file
     124           3 :     final Archive archive = zipDecoder.decodeBytes(file.readAsBytesSync());
     125             : 
     126             :     // Extract the contents of the Zip archive to disk.
     127           2 :     for (final ArchiveFile archiveFile in archive) {
     128           1 :       if (archiveFile.isFile) {
     129           1 :         final List<int> data = archiveFile.content as List<int>;
     130             :         final openFile =
     131           6 :             File('${await directoryManager.metropole}/' + archiveFile.name)
     132           1 :               ..createSync(recursive: true);
     133           1 :         openFile.writeAsBytesSync(data);
     134             :       }
     135             :     }
     136             :   }
     137             : }

Generated by: LCOV version 1.13