forked from student-hub/acs-upb-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite_provider.dart
398 lines (353 loc) · 12.5 KB
/
website_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import 'dart:async';
import 'package:acs_upb_mobile/authentication/model/user.dart';
import 'package:acs_upb_mobile/generated/l10n.dart';
import 'package:acs_upb_mobile/pages/filter/model/filter.dart';
import 'package:acs_upb_mobile/pages/portal/model/website.dart';
import 'package:acs_upb_mobile/resources/utils.dart';
import 'package:acs_upb_mobile/widgets/toast.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:preferences/preference_service.dart';
extension UserExtension on User {
/// Check if there is at least one website that the [User] has permission to edit
Future<bool> get hasEditableWebsites async {
// We assume there is at least one public website in the database
if (canEditPublicInfo) return true;
return hasPrivateWebsites;
}
/// Check if user has at least one private website
Future<bool> get hasPrivateWebsites async {
final CollectionReference ref = FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('websites');
return (await ref.get()).docs.isNotEmpty;
}
}
extension WebsiteCategoryExtension on WebsiteCategory {
static WebsiteCategory fromString(String category) {
switch (category) {
case 'learning':
return WebsiteCategory.learning;
case 'administrative':
return WebsiteCategory.administrative;
case 'association':
return WebsiteCategory.association;
case 'resource':
return WebsiteCategory.resource;
default:
return WebsiteCategory.other;
}
}
}
extension WebsiteExtension on Website {
// [ownerUid] should be provided if the website is user-private
static Website fromSnap(DocumentSnapshot snap, {String ownerUid}) {
final data = snap.data();
return Website(
ownerUid: ownerUid ?? data['addedBy'],
id: snap.id,
isPrivate: ownerUid != null,
editedBy: List<String>.from(data['editedBy'] ?? []),
category: WebsiteCategoryExtension.fromString(data['category']),
label: data['label'] ?? 'Website',
link: data['link'] ?? '',
infoByLocale: data['info'] == null
? {}
: {
'en': data['info']['en'],
'ro': data['info']['ro'],
},
degree: data['degree'],
relevance: data['relevance'] == null
? null
: List<String>.from(data['relevance']),
);
}
Map<String, dynamic> toData() {
final data = <String, dynamic>{};
if (!isPrivate) {
if (ownerUid != null) data['addedBy'] = ownerUid;
data['editedBy'] = editedBy;
data['relevance'] = relevance;
if (degree != null) data['degree'] = degree;
}
if (label != null) data['label'] = label;
if (category != null) {
data['category'] = category.toShortString();
}
if (link != null) data['link'] = link;
if (infoByLocale != null) data['info'] = infoByLocale;
return data;
}
}
class WebsiteProvider with ChangeNotifier {
final FirebaseFirestore _db = FirebaseFirestore.instance;
void _errorHandler(dynamic e, {bool showToast = true}) {
print(e.message);
if (showToast) {
if (e.message.contains('PERMISSION_DENIED')) {
AppToast.show(S.current.errorPermissionDenied);
} else {
AppToast.show(S.current.errorSomethingWentWrong);
}
}
}
/// Initializes the number of visits of websites with the value stored from Firebase.
/// If no [uid] is provided, store the data locally instead.
Future<bool> _initializeNumberOfVisits(
List<Website> websites, String uid) async {
if (uid == null) {
return _initializeNumberOfVisitsLocally(websites);
}
try {
final DocumentReference userDoc = _db.collection('users').doc(uid);
final userData = (await userDoc.get()).data();
if (userData != null) {
final websiteVisits =
Map<String, dynamic>.from(userData['websiteVisits'] ?? {});
for (final website in websites) {
website.numberOfVisits = websiteVisits[website.id] ?? 0;
}
return true;
} else {
print('User not found.');
return false;
}
} catch (e) {
print(e);
return false;
}
}
/// Initializes the number of visits of websites with the value stored locally.
///
/// Because [PrefService] doesn't support storing maps, the
/// data is stored in 2 lists: the list of website IDs (`websiteIds`) and the list
/// with the number of visits (`websiteVisits`), where `websiteVisits[i]` is the
/// number of times the user accessed website with ID `websiteIds[i]`.
Future<bool> _initializeNumberOfVisitsLocally(List<Website> websites) async {
try {
final List<String> websiteIds =
PrefService.sharedPreferences.getStringList('websiteIds') ?? [];
final List<String> websiteVisits =
PrefService.sharedPreferences.getStringList('websiteVisits') ?? [];
final visitsByWebsiteId = Map<String, int>.from(websiteIds.asMap().map(
(index, key) =>
MapEntry(key, int.tryParse(websiteVisits[index] ?? 0))));
for (final Website website in websites) {
website.numberOfVisits = visitsByWebsiteId[website.id] ?? 0;
}
return true;
} catch (e) {
print(e);
return false;
}
}
/// Increments the number of visits of [website], both in-memory and on Firebase.
/// If no [uid] is provided, update data in the local storage.
Future<bool> incrementNumberOfVisits(Website website, {String uid}) async {
try {
website.numberOfVisits++;
if (uid == null) {
return await incrementNumberOfVisitsLocally(website);
}
final DocumentReference userDoc = _db.collection('users').doc(uid);
final userData = (await userDoc.get()).data();
if (userData != null) {
final websiteVisits =
Map<String, dynamic>.from(userData['websiteVisits'] ?? {});
websiteVisits[website.id] = website.numberOfVisits++;
await userDoc.update({'websiteVisits': websiteVisits});
notifyListeners();
return true;
} else {
print('User not found.');
return false;
}
} catch (e) {
print(e);
return false;
}
}
/// Increments the number of visits of [website], both in-memory and on the local storage.
///
/// Because [PrefService] doesn't support storing maps, the
/// data is stored in 2 lists: the list of website IDs (`websiteIds`) and the list
/// with the number of visits `websiteVisits`, where `websiteVisits[i]` is the
/// number of times the user accessed website with ID `websiteIds[i]`.
Future<bool> incrementNumberOfVisitsLocally(Website website) async {
try {
website.numberOfVisits++;
final List<String> websiteIds =
PrefService.sharedPreferences.getStringList('websiteIds') ?? [];
final List<String> websiteVisits =
PrefService.sharedPreferences.getStringList('websiteVisits') ?? [];
if (websiteIds.contains(website.id)) {
final int index = websiteIds.indexOf(website.id);
websiteVisits.insert(index, website.numberOfVisits.toString());
} else {
websiteIds.add(website.id);
websiteVisits.add(website.numberOfVisits.toString());
await PrefService.sharedPreferences
.setStringList('websiteIds', websiteIds);
}
await PrefService.sharedPreferences
.setStringList('websiteVisits', websiteVisits);
notifyListeners();
return true;
} catch (e) {
print(e);
return false;
}
}
Future<List<Website>> fetchWebsites(Filter filter,
{bool userOnly = false, String uid}) async {
try {
final websites = <Website>[];
if (!userOnly) {
List<DocumentSnapshot> documents = [];
if (filter == null) {
final QuerySnapshot qSnapshot =
await _db.collection('websites').get();
documents.addAll(qSnapshot.docs);
} else {
// Documents without a 'relevance' field are relevant for everyone
final query =
_db.collection('websites').where('relevance', isNull: true);
final QuerySnapshot qSnapshot = await query.get();
documents.addAll(qSnapshot.docs);
for (final string in filter.relevantNodes) {
// selected nodes
final query = _db
.collection('websites')
.where('degree', isEqualTo: filter.baseNode)
.where('relevance', arrayContains: string);
final QuerySnapshot qSnapshot = await query.get();
documents.addAll(qSnapshot.docs);
}
}
// Remove duplicates
// (a document may result out of more than one query)
final seenDocumentIds = <String>{};
documents =
documents.where((doc) => seenDocumentIds.add(doc.id)).toList();
websites.addAll(documents.map(WebsiteExtension.fromSnap));
}
// Get user-added websites
if (uid != null) {
final DocumentReference ref =
FirebaseFirestore.instance.collection('users').doc(uid);
final QuerySnapshot qSnapshot = await ref.collection('websites').get();
websites.addAll(qSnapshot.docs
.map((doc) => WebsiteExtension.fromSnap(doc, ownerUid: uid)));
}
final bool initializeReturnSuccess =
await _initializeNumberOfVisits(websites, uid);
if (!initializeReturnSuccess) {
AppToast.show(S.current.warningFavouriteWebsitesInitializationFailed);
}
websites.sort((website1, website2) =>
website2.numberOfVisits.compareTo(website1.numberOfVisits));
return websites;
} catch (e) {
_errorHandler(e, showToast: false);
return null;
}
}
Future<List<Website>> fetchFavouriteWebsites(String uid,
{int limit = 3}) async {
final favouriteWebsites = (await fetchWebsites(null, uid: uid))
.where((website) => website.numberOfVisits > 0)
.take(limit)
.toList();
if (favouriteWebsites.isEmpty) {
return null;
}
return favouriteWebsites;
}
Future<bool> addWebsite(Website website) async {
assert(website.label != null);
try {
DocumentReference ref;
if (!website.isPrivate) {
ref = _db.collection('websites').doc(website.id);
} else {
ref = _db
.collection('users')
.doc(website.ownerUid)
.collection('websites')
.doc(website.id);
}
if ((await ref.get()).data() != null) {
// TODO(IoanaAlexandru): Properly check if a website with a similar name/link already exists
print('A website with id ${website.id} already exists');
AppToast.show(S.current.warningWebsiteNameExists);
return false;
}
final data = website.toData();
await ref.set(data);
notifyListeners();
return true;
} catch (e) {
_errorHandler(e);
return false;
}
}
Future<bool> updateWebsite(Website website) async {
assert(website.label != null);
try {
final DocumentReference publicRef =
_db.collection('websites').doc(website.id);
final DocumentReference privateRef = _db
.collection('users')
.doc(website.ownerUid)
.collection('websites')
.doc(website.id);
DocumentReference previousRef;
bool wasPrivate;
if ((await publicRef.get()).data() != null) {
wasPrivate = false;
previousRef = publicRef;
} else if ((await privateRef.get()).data() != null) {
wasPrivate = true;
previousRef = privateRef;
} else {
print('Website not found.');
return false;
}
if (wasPrivate == website.isPrivate) {
// No privacy change
await previousRef.update(website.toData());
} else {
// Privacy changed
await previousRef.delete();
await (wasPrivate ? publicRef : privateRef).set(website.toData());
}
notifyListeners();
return true;
} catch (e) {
_errorHandler(e);
return false;
}
}
Future<bool> deleteWebsite(Website website) async {
try {
DocumentReference ref;
if (!website.isPrivate) {
ref = _db.collection('websites').doc(website.id);
} else {
ref = _db
.collection('users')
.doc(website.ownerUid)
.collection('websites')
.doc(website.id);
}
await ref.delete();
notifyListeners();
return true;
} catch (e) {
_errorHandler(e);
return false;
}
}
}