-
Notifications
You must be signed in to change notification settings - Fork 12
/
author_viewmodel.dart
83 lines (70 loc) · 2.45 KB
/
author_viewmodel.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
// Package imports:
import 'package:ets_api_clients/models.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:stacked/stacked.dart';
// Project imports:
import 'package:notredame/core/managers/author_repository.dart';
import 'package:notredame/core/managers/news_repository.dart';
import 'package:notredame/locator.dart';
class AuthorViewModel extends BaseViewModel implements Initialisable {
final AuthorRepository _authorRepository = locator<AuthorRepository>();
final NewsRepository _newsRepository = locator<NewsRepository>();
/// Localization class of the application.
final AppIntl appIntl;
final String authorId;
/// Author
Organizer? _author;
/// Return the author
Organizer? get author => _author;
final PagingController<int, News> pagingController =
PagingController(firstPageKey: 1);
AuthorViewModel({required this.authorId, required this.appIntl});
bool isLoadingEvents = false;
bool isNotified = false;
@override
Future<void> initialise() async {
// This will be called when init state cycle runs
pagingController.addPageRequestListener((pageKey) {
fetchPage(pageKey);
});
}
Future<void> fetchPage(int pageNumber) async {
try {
final pagination = await _newsRepository.getNews(
pageNumber: pageNumber, organizerId: authorId);
final isLastPage = pagination?.totalPages == pageNumber;
if (isLastPage) {
pagingController.appendLastPage(pagination?.news ?? []);
} else {
final nextPageKey = pageNumber + 1;
pagingController.appendPage(pagination?.news ?? [], nextPageKey);
}
} catch (error) {
pagingController.error = error;
}
}
void notifyMe() {
// TODO activate/deactivate notifications
isNotified = !isNotified;
if (isNotified) {
Fluttertoast.showToast(
msg: appIntl.news_author_notified_for(author?.organization ?? ""),
toastLength: Toast.LENGTH_LONG);
} else {
Fluttertoast.showToast(
msg: appIntl.news_author_not_notified_for(author?.organization ?? ""),
toastLength: Toast.LENGTH_LONG);
}
}
Future<void> fetchAuthorData() async {
setBusy(true);
try {
_author = await _authorRepository.getOrganizer(authorId);
notifyListeners();
} finally {
setBusy(false);
}
}
}