Line data Source code
1 : import 'package:app_pym/core/network/network_info.dart';
2 : import 'package:app_pym/data/datasources/map_pym_local_data_source.dart';
3 : import 'package:app_pym/data/datasources/map_pym_remote_data_source.dart';
4 : import 'package:app_pym/data/models/map_pym/batiment_model.dart';
5 : import 'package:app_pym/domain/entities/map_pym/batiment.dart';
6 : import 'package:app_pym/domain/repositories/map_pym/batiment_repository.dart';
7 : import 'package:connectivity/connectivity.dart';
8 : import 'package:flutter/foundation.dart';
9 : import 'package:injectable/injectable.dart';
10 :
11 : @prod
12 : @LazySingleton(as: BatimentRepository)
13 : class BatimentRepositoryImpl implements BatimentRepository {
14 : final MapPymLocalDataSource localDataSource;
15 : final MapPymRemoteDataSource remoteDataSource;
16 : final NetworkInfo networkInfo;
17 :
18 1 : const BatimentRepositoryImpl({
19 : @required this.localDataSource,
20 : @required this.remoteDataSource,
21 : @required this.networkInfo,
22 : });
23 :
24 : @override
25 1 : Future<Batiment> fetch(int id) async {
26 4 : if (await networkInfo.result != ConnectivityResult.none) {
27 3 : final data = await remoteDataSource.fetchBatiment(id);
28 3 : await localDataSource.cacheBatiment(data);
29 1 : return data?.toEntity();
30 : } else {
31 2 : final data = localDataSource.fetchBatiment(id);
32 1 : return data?.toEntity();
33 : }
34 : }
35 :
36 : @override
37 1 : Future<List<Batiment>> fetchAll() async {
38 4 : if (await networkInfo.result != ConnectivityResult.none) {
39 4 : final data = await remoteDataSource.fetchAllBatiment() ?? [];
40 3 : await localDataSource.cacheAllBatiment(data);
41 4 : return data.map((e) => e?.toEntity()).toList();
42 : } else {
43 2 : final data = localDataSource.fetchAllBatiment();
44 4 : return data.map((e) => e?.toEntity()).toList();
45 : }
46 : }
47 : }
|