Created
February 1, 2022 16:37
-
-
Save AbedElazizShe/49ee26b1323b632e53dcee3914d87611 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:cache/src/db_provider.dart'; | |
import 'package:cache/src/mapper/articles_entity_mapper.dart'; | |
import 'package:cache/src/models/article_model.dart'; | |
import 'package:data/data.dart'; | |
import 'package:injectable/injectable.dart'; | |
@Injectable(as: CacheStorageRepository) | |
class CacheStorageRepositoryImpl implements CacheStorageRepository { | |
CacheStorageRepositoryImpl( | |
{required this.dbProvider, required this.articlesEntityMapper}); | |
final DBProvider dbProvider; | |
final ArticlesEntityMapper articlesEntityMapper; | |
@override | |
Future<List<ArticlesEntity>> getArticles(String type) async { | |
final String? contents = await dbProvider.fetchArticles(type); | |
if (contents == null) return <ArticlesEntity>[]; | |
return (json.decode(contents) as List) | |
.map((i) => articlesEntityMapper.mapFromModel(ArticleModel.fromJson(i))) | |
.toList(); | |
} | |
@override | |
Future<int> saveArticles(String contents, String type) async => | |
dbProvider.saveArticles(contents, type); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment