This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
news_page.dart
49 lines (45 loc) · 1.72 KB
/
news_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'package:app/foundation/extension/async_snapshot.dart';
import 'package:app/ui/component/loading/container_with_loading.dart';
import 'package:app/ui/hook/use_l10n.dart';
import 'package:app/ui/loading_state_view_model.dart';
import 'package:app/ui/news/article_item.dart';
import 'package:app/ui/news/news_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
class NewsPage extends HookConsumerWidget {
const NewsPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final l10n = useL10n();
final homeViewModel = ref.read(newsViewModelProvider);
final news = ref.watch(newsViewModelProvider.select((value) => value.news));
final snapshot = useFuture(
useMemoized(() {
return ref
.read(loadingStateProvider)
.whileLoading(homeViewModel.fetchNews);
}, [news?.toString()]),
);
return ContainerWithLoading(
child: snapshot.isWaiting || news == null
? const SizedBox()
: news.when(success: (data) {
if (data.articles.isEmpty) {
return Center(child: Text(l10n.noResult));
}
return RefreshIndicator(
onRefresh: () async => homeViewModel.fetchNews(),
child: ListView.builder(
itemCount: data.articles.length,
itemBuilder: (_, index) {
return ArticleItem(article: data.articles[index]);
},
),
);
}, failure: (e) {
return Center(child: Text(l10n.fetchFailed));
}),
);
}
}