-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp_widget_service.dart
82 lines (70 loc) · 2.74 KB
/
app_widget_service.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
// Flutter imports:
import 'package:flutter/services.dart';
// Package imports:
import 'package:home_widget/home_widget.dart';
// Project imports:
import 'package:notredame/core/constants/widget_helper.dart';
import 'package:notredame/core/models/widget_models.dart';
import 'package:notredame/core/services/analytics_service.dart';
import 'package:notredame/locator.dart';
// MODEL
// CONSTANTS
// MANAGER / SERVICE
// OTHER
/// Manage the app widget function to update data and visual.
class AppWidgetService {
static const String tag = "AppWidgetService";
final AnalyticsService _analyticsService = locator<AnalyticsService>();
Future<bool?> init() async {
return HomeWidget.setAppGroupId('group.ca.etsmtl.applets.ETSMobile');
}
/// Update session progress widget with provided data
Future<bool?> sendProgressData(ProgressWidgetData progressWidgetData) async {
try {
await HomeWidget.saveWidgetData<double>(
'${ProgressWidgetData.keyPrefix}progress',
progressWidgetData.progress);
await HomeWidget.saveWidgetData<int>(
'${ProgressWidgetData.keyPrefix}elapsedDays',
progressWidgetData.elapsedDays);
await HomeWidget.saveWidgetData<int>(
'${ProgressWidgetData.keyPrefix}totalDays',
progressWidgetData.totalDays);
await HomeWidget.saveWidgetData<String>(
'${ProgressWidgetData.keyPrefix}suffix', progressWidgetData.suffix);
return await HomeWidget.saveWidgetData<String>(
'${ProgressWidgetData.keyPrefix}title', progressWidgetData.title);
} on PlatformException {
_analyticsService.logError(
tag, 'Error sending data to session progress widget.');
rethrow;
}
}
/// Update grades widget with provided data
Future<bool?> sendGradesData(GradesWidgetData gradeWidgetData) async {
try {
await HomeWidget.saveWidgetData<List<String>>(
'${GradesWidgetData.keyPrefix}courseAcronyms',
gradeWidgetData.courseAcronyms);
await HomeWidget.saveWidgetData<List<String>>(
'${GradesWidgetData.keyPrefix}grades', gradeWidgetData.grades);
return await HomeWidget.saveWidgetData<String>(
'${GradesWidgetData.keyPrefix}title', gradeWidgetData.title);
} on PlatformException {
_analyticsService.logError(tag, 'Error sending data to grades widget.');
rethrow;
}
}
/// Tell the system to update the given widget type
Future<bool?> updateWidget(WidgetType type) async {
try {
return HomeWidget.updateWidget(
name: type.androidName,
androidName: type.androidName,
iOSName: type.iOSname);
} on PlatformException {
_analyticsService.logError(tag, 'Error updating widget ${type.iOSname}.');
return false;
}
}
}