-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfeedback_viewmodel.dart
107 lines (85 loc) · 3.56 KB
/
feedback_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Dart imports:
import 'dart:io';
import 'dart:typed_data';
// Package imports:
import 'package:feedback/feedback.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:github/github.dart';
import 'package:image/image.dart' as image;
import 'package:stacked/stacked.dart';
// Project imports:
import 'package:notredame/core/constants/feedback_type.dart';
import 'package:notredame/core/constants/preferences_flags.dart';
import 'package:notredame/core/models/feedback_issue.dart';
import 'package:notredame/core/services/github_api.dart';
import 'package:notredame/core/services/preferences_service.dart';
import 'package:notredame/locator.dart';
class FeedbackViewModel extends FutureViewModel {
/// Used to access Github functionalities
final GithubApi _githubApi = locator<GithubApi>();
/// Use to get the value associated to each settings key
final PreferencesService _preferencesService = locator<PreferencesService>();
final AppIntl _appIntl;
final int _screenshotImageWidth = 307;
List<FeedbackIssue> _myIssues = [];
// get the list of issues
List<FeedbackIssue> get myIssues => _myIssues;
FeedbackViewModel({required AppIntl intl}) : _appIntl = intl;
/// Create a Github issue with [UserFeedback] and the screenshot associated.
Future<void> sendFeedback(
UserFeedback feedback, FeedbackType reportType) async {
//Generate info to pass to github
final File file = await _githubApi.localFile;
await file.writeAsBytes(encodeScreenshotForGithub(feedback.screenshot));
final String fileName = file.path.split('/').last;
// Upload the file and create the issue
_githubApi.uploadFileToGithub(filePath: fileName, file: file);
final Issue issue = await _githubApi.createGithubIssue(
feedbackText: feedback.text,
fileName: fileName,
feedbackType: reportType.name,
email: feedback.extra != null && feedback.extra!.containsKey('email')
? feedback.extra!['email'].toString()
: null);
setBusy(true);
_myIssues.add(FeedbackIssue(issue));
// Sort by state open first and by number descending
_myIssues.sort(
(a, b) => b.state.compareTo(a.state) * 100000 + b.number - a.number);
setBusy(false);
// Save the issue number in the preferences
_preferencesService.setString(
PreferencesFlag.ghIssues, _myIssues.map((e) => e.number).join(','));
file.deleteSync();
Fluttertoast.showToast(
msg: _appIntl.thank_you_for_the_feedback,
gravity: ToastGravity.CENTER,
);
}
List<int> encodeScreenshotForGithub(Uint8List screenshot) {
return image.encodePng(image.copyResize(
image.decodeImage(screenshot) ?? image.Image(0, 0),
width: _screenshotImageWidth));
}
// @override
@override
Future<int> futureToRun() async {
// Get the issues number from the preferences
final String? issuesString =
await _preferencesService.getString(PreferencesFlag.ghIssues);
// If there is no issues, return 0
if (issuesString == null || issuesString.isEmpty) {
return 0;
}
// Split the issues number
final List<String> issuesNumberList = issuesString.split(',');
// To integers instead of String and fetch it
_myIssues = await _githubApi.fetchIssuesByNumbers(
issuesNumberList.map((e) => int.parse(e)).toList(), _appIntl);
// Sort by state open first and by number descending
_myIssues.sort(
(a, b) => b.state.compareTo(a.state) * 100000 + b.number - a.number);
return _myIssues.length;
}
}