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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import 'dart:async';
import 'package:flutter/material.dart';
import '../app_settings.dart';
import '../core/core_controller.dart';
import '../core/models.dart';
import '../l10n/app_localizations.dart';
import 'format.dart';
import 'signin_dialog.dart';
import 'widgets.dart';
/// The Foxy home: FoxyVPN's phone look on every screen — one big connect
/// button, the state under it, the account in use with its data bar, and
/// where the traffic goes. The classic home stays a design choice away
/// (Settings, Design). Every action is the core's, through the same
/// controller calls the classic home makes; nothing here decides anything
/// about the VPN itself.
class FoxyOverviewPage extends StatelessWidget {
const FoxyOverviewPage({
super.key,
required this.core,
required this.settings,
this.onOpenServers,
this.onOpenAccounts,
this.onOpenConnection,
});
final CoreController core;
final AppSettings settings;
/// Opens the server list and the accounts (a page on the phone, a tab
/// on the computer).
final VoidCallback? onOpenServers;
final VoidCallback? onOpenAccounts;
/// Opens the connection settings, when Mozilla's servers are blocked.
final VoidCallback? onOpenConnection;
/// The button, as on the classic phone home: with a system proxy the
/// switch is the proxy mode; elsewhere the proxy's power.
Future<void> _toggle(CoreStatus status, bool on) async {
if (status.systemProxySupported) {
await core.setSystemProxy(on ? 'clear' : 'set');
} else {
await core.setOn(!on);
}
if (!on) unawaited(core.runTest().catchError((_) {}));
}
@override
Widget build(BuildContext context) {
final l = AppLocalizations.of(context);
final theme = Theme.of(context);
final scheme = theme.colorScheme;
final status = core.status!;
final canSwitch = status.systemProxySupported;
final on = canSwitch
? status.systemProxySet || status.tunMode
: status.state.phase != Phase.off;
final running = status.state.phase == Phase.running;
final connected = running && (status.computerUsesVpn || !canSwitch);
final look = PhaseLook.of(l, status);
final accent = !on
? scheme.primary
: switch (status.state.phase) {
Phase.running => StatusColors.good,
Phase.error => StatusColors.bad,
_ => StatusColors.busy,
};
final place = homeLocation(l, status, connected: connected);
final mismatch = exitMismatchText(l, status);
final ip = !settings.showPublicIp
? ''
: status.exit?.ipv4.isNotEmpty == true
? status.exit!.ipv4
: status.exit?.ipv6 ?? '';
final top = MediaQuery.paddingOf(context).top;
return ListView(
padding: pagePadding(context) + EdgeInsets.only(top: top),
children: [
Row(
children: [
Text(
l.appTitle,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.w700,
),
),
],
),
const SizedBox(height: 36),
Center(
child: _PowerButton(
on: on,
accent: accent,
busy: core.switchingProxy,
tooltip: on ? l.tapToDisconnectPhone : l.tapToConnectPhone,
onPressed: () => status.totalAccounts == 0
? _openAccounts(context)
: runShowingErrors(context, () => _toggle(status, on)),
),
),
const SizedBox(height: 20),
Text(
look.label,
textAlign: TextAlign.center,
style: theme.textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.w800,
color: on ? accent : null,
),
),
const SizedBox(height: 6),
Text(
look.hint.isNotEmpty
? look.hint
: on
? l.tapToDisconnectPhone
: l.tapToConnectPhone,
textAlign: TextAlign.center,
style: theme.textTheme.bodyMedium?.copyWith(
color: scheme.onSurfaceVariant,
),
),
if (connected && ip.isNotEmpty) ...[
const SizedBox(height: 6),
Text(
'${l.publicIp} ${ltr(ip)}',
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
style: theme.textTheme.bodyMedium?.copyWith(
color: scheme.onSurfaceVariant,
),
),
],
if (mismatch != null) ...[
const SizedBox(height: 10),
Text(
mismatch,
textAlign: TextAlign.center,
style: theme.textTheme.bodyMedium?.copyWith(
color: StatusColors.busy,
),
),
],
const SizedBox(height: 28),
_ActiveAccountCard(
core: core,
status: status,
onOpenAccounts: () => _openAccounts(context),
),
const SizedBox(height: 12),
Panel(
title: place.caption,
icon: Icons.public_rounded,
child: InkWell(
onTap: onOpenServers,
borderRadius: BorderRadius.circular(12),
child: Row(
children: [
CountryFlag(code: place.flag, width: 40),
const SizedBox(width: 14),
Expanded(
child: Text(
place.name,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
),
const Icon(Icons.chevron_right_rounded),
],
),
),
),
if (status.traffic case final traffic?) ...[
const SizedBox(height: 12),
Panel(
child: Row(
children: [
Icon(
Icons.arrow_upward_rounded,
size: 18,
color: StatusColors.good,
),
const SizedBox(width: 6),
Text(formatRate(traffic.upRate)),
const SizedBox(width: 18),
Icon(
Icons.arrow_downward_rounded,
size: 18,
color: scheme.primary,
),
const SizedBox(width: 6),
Text(formatRate(traffic.downRate)),
const Spacer(),
Text(
'${formatBytes(traffic.up)} ↑ · ${formatBytes(traffic.down)} ↓',
style: theme.textTheme.bodySmall?.copyWith(
color: scheme.onSurfaceVariant,
),
),
],
),
),
],
if (status.totalAccounts == 0) ...[
const SizedBox(height: 20),
FilledButton.icon(
onPressed: () => _openAccounts(context),
icon: const Icon(Icons.add_rounded),
label: Text(l.addAccount),
),
],
],
);
}
/// The accounts page where there is one; the sign-in form otherwise.
void _openAccounts(BuildContext context) {
if (onOpenAccounts != null) {
onOpenAccounts!();
} else {
showSignInDialog(context, core);
}
}
}
/// The account carrying the traffic now, with its data bar; a tap opens
/// the accounts page.
class _ActiveAccountCard extends StatelessWidget {
const _ActiveAccountCard({
required this.core,
required this.status,
required this.onOpenAccounts,
});
final CoreController core;
final CoreStatus status;
final VoidCallback onOpenAccounts;
@override
Widget build(BuildContext context) {
final l = AppLocalizations.of(context);
final theme = Theme.of(context);
final muted = theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
);
final a = status.activeAccount;
final usage = a?.usage;
return Panel(
title: l.activeAccount,
icon: Icons.person_rounded,
child: InkWell(
onTap: onOpenAccounts,
borderRadius: BorderRadius.circular(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
a == null ? l.noActiveAccount : a.email,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
textDirection: a == null ? null : TextDirection.ltr,
overflow: TextOverflow.ellipsis,
),
),
const Icon(Icons.chevron_right_rounded),
],
),
const SizedBox(height: 12),
if (a == null)
Text(l.waitingForAccount, style: muted)
else if (usage == null)
Text(l.quotaNotRead, style: muted)
else if (usage.unlimited)
Text(l.unlimitedQuota, style: muted)
else ...[
QuotaBar(
percent: a.usedPercent,
threshold: status.thresholdPercent,
),
const SizedBox(height: 6),
Text(
[
l.usedOf(
formatBytes(usage.usedBytes),
formatBytes(usage.limitBytes),
),
if (usage.resetAt != null)
l.resets(formatDate(l, usage.resetAt!)),
].join(' · '),
style: muted,
),
],
],
),
),
);
}
}
/// One big round connect button: a soft ring, a filled disc, the power
/// sign. A spinner's ring while the switch is being changed.
class _PowerButton extends StatelessWidget {
const _PowerButton({
required this.on,
required this.accent,
required this.busy,
required this.tooltip,
required this.onPressed,
});
final bool on;
final Color accent;
final bool busy;
final String tooltip;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
const size = 184.0;
return Tooltip(
message: tooltip,
child: SizedBox.square(
dimension: size,
child: Stack(
alignment: Alignment.center,
children: [
Container(
width: size,
height: size,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: RadialGradient(
colors: [
accent.withValues(alpha: on ? 0.26 : 0.10),
accent.withValues(alpha: 0),
],
),
),
),
Container(
width: size - 16,
height: size - 16,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: accent.withValues(alpha: on ? 0.55 : 0.28),
width: 1.5,
),
),
),
if (busy)
SizedBox.square(
dimension: size - 16,
child: CircularProgressIndicator(
strokeWidth: 2.5,
color: accent,
),
),
AnimatedContainer(
duration: const Duration(milliseconds: 350),
width: size - 44,
height: size - 44,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: on
? [Color.lerp(accent, Colors.white, 0.28)!, accent]
: [
scheme.surfaceContainerHighest,
scheme.surfaceContainerHigh,
],
),
border: Border.all(
color: on
? Colors.white.withValues(alpha: 0.4)
: scheme.outlineVariant,
width: 1.5,
),
boxShadow: [
BoxShadow(
color: accent.withValues(alpha: on ? 0.45 : 0.12),
blurRadius: on ? 34 : 22,
offset: const Offset(0, 10),
),
],
),
child: Material(
type: MaterialType.transparency,
shape: const CircleBorder(),
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: busy ? null : onPressed,
child: Icon(
Icons.power_settings_new_rounded,
size: 62,
color: on ? Colors.white : scheme.primary,
),
),
),
),
],
),
),
);
}
}