forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_review_service.dart
More file actions
205 lines (182 loc) · 8.24 KB
/
Copy pathapp_review_service.dart
File metadata and controls
205 lines (182 loc) · 8.24 KB
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
import 'dart:io';
import 'package:omi/utils/platform/platform_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:omi/utils/logger.dart';
class AppReviewService {
static final AppReviewService _instance = AppReviewService._internal();
factory AppReviewService() => _instance;
AppReviewService._internal();
static final Uri _appStoreReviewUrl = Uri.parse('https://apps.apple.com/app/id6502156163?action=write-review');
static final Uri _playStoreUrl = Uri.parse('https://play.google.com/store/apps/details?id=com.friend.ios');
static const String _hasCompletedFirstActionItemKey = 'has_completed_first_action_item';
static const String _hasShownReviewPromptKey = 'has_shown_review_prompt';
static const String _hasFirstConversationKey = 'has_first_conversation';
static const String _hasShownReviewForConversationKey = 'has_shown_review_for_conversation';
static const String _hasShownReviewForActionItemKey = 'has_shown_review_for_action_item';
// Checks if the user has completed their first action item
Future<bool> hasCompletedFirstActionItem() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_hasCompletedFirstActionItemKey) ?? false;
}
// Marks that the user has completed their first action item
Future<void> markFirstActionItemCompleted() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_hasCompletedFirstActionItemKey, true);
}
// Checks if the review prompt has already been shown
Future<bool> hasShownReviewPrompt() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_hasShownReviewPromptKey) ?? false;
}
// Marks that the review prompt has been shown
Future<void> markReviewPromptShown() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_hasShownReviewPromptKey, true);
}
// Checks if this is the user's first conversation
Future<bool> isFirstConversation() async {
final prefs = await SharedPreferences.getInstance();
return !(prefs.getBool(_hasFirstConversationKey) ?? false);
}
// Marks that the user has had their first conversation
Future<void> markFirstConversation() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_hasFirstConversationKey, true);
}
// Checks if review prompt has been shown for conversation
Future<bool> hasShownReviewForConversation() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_hasShownReviewForConversationKey) ?? false;
}
// Marks that review prompt has been shown for conversation
Future<void> markReviewShownForConversation() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_hasShownReviewForConversationKey, true);
}
// Checks if review prompt has been shown for action item
Future<bool> hasShownReviewForActionItem() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_hasShownReviewForActionItemKey) ?? false;
}
// Marks that review prompt has been shown for action item
Future<void> markReviewShownForActionItem() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_hasShownReviewForActionItemKey, true);
}
// Shows the review prompt if conditions are met
Future<bool> showReviewPromptIfNeeded(BuildContext context, {bool isProcessingFirstConversation = false}) async {
final hasCompleted = await hasCompletedFirstActionItem();
final isFirst = await isFirstConversation();
bool shouldShow = false;
if (isProcessingFirstConversation && isFirst) {
final hasShownForConversation = await hasShownReviewForConversation();
if (!hasShownForConversation) {
shouldShow = true;
await markFirstConversation();
await markReviewShownForConversation();
}
} else if (hasCompleted) {
final hasShownForActionItem = await hasShownReviewForActionItem();
if (!hasShownForActionItem) {
shouldShow = true;
await markReviewShownForActionItem();
}
}
if (shouldShow) {
await markReviewPromptShown();
if (context.mounted) {
_showReviewDialog(context);
}
return true;
}
return false;
}
// Shows a dialog asking the user to review the app
Future<void> _showReviewDialog(BuildContext context) async {
HapticFeedback.mediumImpact();
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Dialog(
backgroundColor: Colors.transparent,
elevation: 0,
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(24),
border: Border.all(color: Colors.grey.shade800, width: 1),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Loving Omi?',
style: TextStyle(color: Colors.white, fontSize: 24, fontWeight: FontWeight.bold, height: 1.2),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
Text(
'Help us reach more people by leaving a review in the ${Platform.isIOS ? 'App Store' : 'Google Play Store'}. Your feedback means the world to us!',
style: const TextStyle(color: Colors.grey, fontSize: 16, height: 1.4),
textAlign: TextAlign.center,
),
const SizedBox(height: 32),
Column(
children: [
ElevatedButton(
onPressed: () async {
HapticFeedback.mediumImpact();
Navigator.of(context).pop();
final Uri reviewUrl = Platform.isIOS ? _appStoreReviewUrl : _playStoreUrl;
if (await canLaunchUrl(reviewUrl)) {
await launchUrl(reviewUrl, mode: LaunchMode.externalApplication);
PlatformManager.instance.analytics.track('App Review Opened');
await Future.delayed(const Duration(milliseconds: 500));
} else {
Logger.debug('Could not launch review URL');
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.deepPurple,
foregroundColor: Colors.white,
minimumSize: const Size(double.infinity, 48),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(Platform.isIOS ? FontAwesomeIcons.appStoreIos : FontAwesomeIcons.googlePlay, size: 20),
const SizedBox(width: 12),
Text(
'Rate on ${Platform.isIOS ? 'App Store' : 'Google Play'}',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
),
],
),
),
const SizedBox(height: 8),
TextButton(
onPressed: () {
HapticFeedback.lightImpact();
PlatformManager.instance.analytics.track('App Review Skipped');
Navigator.of(context).pop();
},
child: const Text('Maybe later', style: TextStyle(color: Colors.grey, fontSize: 16)),
),
],
),
],
),
),
);
},
);
}
}