@@ -31,27 +31,26 @@ class HabrStorage {
3131 }
3232
3333 Future <Either <AppError , Post >> article (String id) async {
34- final articleOrError = await api.article (id);
35- if (articleOrError.isLeft) {
36- final cachedPost = await articles.get (id);
37- final cachedAuthor =
38- cachedPost != null ? await authors.get (cachedPost.authorId) : null ;
39-
40- return Either .condLazy (
41- cachedPost != null && cachedAuthor != null ,
42- () => AppError (
43- errCode: ErrorType .NotFound ,
44- message: "Article not found in local storage" ),
45- () => Post (
46- id: cachedPost.id,
47- title: cachedPost.title,
48- body: cachedPost.body,
49- publishDate: cachedPost.publishDate,
50- author: cachedAuthor,
51- ),
52- );
53- }
54- return articleOrError;
34+ return cachedArticle (id).thenLeft ((_) => api.article (id));
35+ }
36+
37+ Future <Either <AppError , Post >> cachedArticle (String id) async {
38+ final cachedPost = await articles.get (id);
39+ final cachedAuthor =
40+ cachedPost != null ? await authors.get (cachedPost.authorId) : null ;
41+ return Either .condLazy (
42+ cachedPost != null && cachedAuthor != null ,
43+ () => AppError (
44+ errCode: ErrorType .NotFound ,
45+ message: "Article not found in local storage" ),
46+ () => Post (
47+ id: cachedPost.id,
48+ title: cachedPost.title,
49+ body: cachedPost.body,
50+ publishDate: cachedPost.publishDate,
51+ author: cachedAuthor,
52+ ),
53+ );
5554 }
5655
5756 Future <bool > addArticleInCache (String id) {
@@ -125,18 +124,15 @@ class HabrStorage {
125124 }
126125
127126 Future _cacheAuthor (Author author) async {
128- String avatarUrl;
129-
130127 if (author.avatar.isNotDefault) {
131- final maybeSavedImage = await imgStore.saveImage (author.avatar.url);
132- avatarUrl = maybeSavedImage.fold ((left) => null , (right) => right);
128+ await imgStore.saveImage (author.avatar.url);
133129 }
134130
135131 await authors.put (author.id, author);
136132 }
137133
138134 Future _uncacheArticle (String articleId) async {
139- final eitherPost = await article (articleId);
135+ final eitherPost = await cachedArticle (articleId);
140136 if (eitherPost.isLeft) return ;
141137 final post = eitherPost.right;
142138 await articles.delete (articleId);
0 commit comments