-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnews_provider.dart
192 lines (170 loc) · 5.85 KB
/
news_provider.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// ignore_for_file: flutter_style_todos
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import '../../../authentication/service/auth_provider.dart';
import '../../../generated/l10n.dart';
import '../../../widgets/toast.dart';
import '../model/news_feed_item.dart';
class NewsProvider with ChangeNotifier {
AuthProvider _authProvider;
// ignore: prefer_final_parameters, use_setters_to_change_properties
void updateAuth(AuthProvider authProvider) {
_authProvider = authProvider;
}
List<String> getBookmarkedNews() =>
_authProvider.currentUserFromCache?.bookmarkedNews;
Future<List<NewsFeedItem>> fetchNewsFeedItems({final int limit}) async {
try {
final CollectionReference news =
FirebaseFirestore.instance.collection('news');
final QuerySnapshot<Map<String, dynamic>> qSnapshot =
limit == null ? await news.get() : await news.limit(limit).get();
return qSnapshot.docs.map(DatabaseNews.fromSnap).toList();
} catch (e) {
print(e);
AppToast.show(S.current.errorSomethingWentWrong);
return null;
}
}
Future<List<NewsFeedItem>> fetchFavoriteNewsFeedItems() async {
try {
final bookmarkedNews = getBookmarkedNews();
if (bookmarkedNews == null || bookmarkedNews.isEmpty) {
return [];
}
final CollectionReference news =
FirebaseFirestore.instance.collection('news');
final QuerySnapshot<Map<String, dynamic>> qSnapshot = await news
.where(FieldPath.documentId, whereIn: getBookmarkedNews())
.get();
return qSnapshot.docs.map(DatabaseNews.fromSnap).toList();
} catch (e) {
print(e);
AppToast.show(S.current.errorSomethingWentWrong);
return null;
}
}
Future<List<NewsFeedItem>> fetchPersonalNewsFeedItem() async {
try {
return [];
} catch (e) {
print(e);
AppToast.show(S.current.errorSomethingWentWrong);
return null;
}
}
Future<NewsFeedItem> fetchNewsItemDetails(final String newsId) async {
try {
final DocumentSnapshot doc =
await FirebaseFirestore.instance.collection('news').doc(newsId).get();
return DatabaseNews.fromSnap(doc);
} catch (e) {
print(e);
AppToast.show(S.current.errorSomethingWentWrong);
return null;
}
}
bool isNewsItemBookmarked(final String newsItemGuid) {
final _currentUser = _authProvider.currentUserFromCache;
if (_currentUser.bookmarkedNews == null ||
_currentUser.bookmarkedNews.isEmpty) {
return false;
}
return _currentUser.bookmarkedNews.contains(newsItemGuid);
}
Future<bool> bookmarkNewsItem(final String newsItemGuid) async {
try {
print('bookmarking news item $newsItemGuid');
final _currentUser = _authProvider.currentUserFromCache;
if (_currentUser.bookmarkedNews.contains(newsItemGuid)) {
throw Exception('News item already bookmarked');
}
_currentUser.bookmarkedNews.add(newsItemGuid);
await FirebaseFirestore.instance
.collection('users')
.doc(_currentUser.uid)
.update(_currentUser.toData());
notifyListeners();
return true;
} catch (e) {
_errorHandler(e);
return false;
}
}
Future<bool> unbookmarkNewsItem(final String newsItemGuid) async {
try {
print('un-bookmarking news item $newsItemGuid');
final _currentUser = _authProvider.currentUserFromCache;
_currentUser.bookmarkedNews
.removeWhere((final item) => item == newsItemGuid);
await FirebaseFirestore.instance
.collection('users')
.doc(_currentUser.uid)
.update(_currentUser.toData());
notifyListeners();
return true;
} catch (e) {
_errorHandler(e);
return false;
}
}
void _errorHandler(final dynamic e, {bool showToast = true}) {
try {
print('${e.message} code: ${e.code}');
if (showToast) {
switch (e.code) {
case 'invalid-email':
case 'invalid-credential':
AppToast.show(S.current.errorInvalidEmail);
break;
case 'wrong-password':
AppToast.show(S.current.errorIncorrectPassword);
break;
case 'user-not-found':
AppToast.show(S.current.errorEmailNotFound);
break;
case 'user-disabled':
AppToast.show(S.current.errorAccountDisabled);
break;
case 'too-many-requests':
AppToast.show(
'${S.current.errorTooManyRequests} ${S.current.warningTryAgainLater}');
break;
case 'email-already-in-use':
AppToast.show(S.current.errorEmailInUse);
break;
default:
AppToast.show(e.message);
}
}
} catch (_) {
// Unknown exception
print(e);
AppToast.show(S.current.errorSomethingWentWrong);
}
}
}
extension DatabaseNews on NewsFeedItem {
static NewsFeedItem fromSnap(
final DocumentSnapshot<Map<String, dynamic>> snap) {
final data = snap.data();
final String itemGuid = snap.id;
final String title = data['title'];
final String body = data['body'];
final String source = data['source'];
final String type = data['type'];
final String createdAt = data['createdAt'].toDate().toString();
final String sourceLink = data['sourceLink'];
final String relevance = data['relevance'];
return NewsFeedItem(
itemGuid: itemGuid,
title: title,
body: body,
source: source,
createdAt: createdAt,
type: type,
relevance: relevance,
sourceTags: null,
sourceLink: sourceLink);
}
}