Skip to content

Commit a835883

Browse files
committed
fix uncache method
1 parent 85d8abf commit a835883

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

lib/stores/habr_storage.dart

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies:
2121
html: "0.14.0+3"
2222
share: ^2.0.1
2323
flutter_highlight: "0.6.0"
24-
either_dart: ^0.1.1
24+
either_dart: ^0.1.3
2525
flutter_svg: "^0.22.0"
2626
url_launcher: "^5.6.0"
2727
hive: ^1.4.4

0 commit comments

Comments
 (0)