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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
package com.vauth.foxyvpn.ui.screens
import com.vauth.foxyvpn.ui.l10n.s
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.BrightnessAuto
import androidx.compose.material.icons.filled.Contrast
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.Download
import androidx.compose.material.icons.filled.LightMode
import androidx.compose.material.icons.filled.Public
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Upload
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearWavyProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.vauth.foxyvpn.FoxyVpnApp
import com.vauth.foxyvpn.data.SettingsStore
import com.vauth.foxyvpn.data.formatBytesPerSecond
import com.vauth.foxyvpn.data.model.AccountLiveStats
import com.vauth.foxyvpn.data.model.ConnectionState
import com.vauth.foxyvpn.data.model.ProxyCandidate
import com.vauth.foxyvpn.data.model.VpnAccount
import com.vauth.foxyvpn.data.model.emojiFlag
import com.vauth.foxyvpn.ui.theme.AppearanceController
import com.vauth.foxyvpn.ui.theme.CardCornerStyle
import com.vauth.foxyvpn.ui.theme.ListDensity
import com.vauth.foxyvpn.ui.theme.LocalFoxyStatusColors
import com.vauth.foxyvpn.ui.theme.Personalization
import com.vauth.foxyvpn.ui.theme.ThemeMode
import com.vauth.foxyvpn.vpn.FoxyVpnService
import com.vauth.foxyvpn.vpn.PingUtil
import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun HomeScreen(
app: FoxyVpnApp,
appearance: AppearanceController,
onRequestConnect: () -> Unit,
onDisconnect: () -> Unit,
onOpenServers: () -> Unit,
onOpenSettings: () -> Unit,
onOpenAccounts: () -> Unit,
) {
val state by FoxyVpnService.state.collectAsState()
val lastError by FoxyVpnService.lastError.collectAsState()
// NOTE (v1.3 lag fix): multiAccountStats emits every 2s while connected —
// it is collected locally inside SpeedRow/DetailsPanel, never here.
val connectedCount by FoxyVpnService.connectedAccountCount.collectAsState()
val accounts by app.tokenStore.accountsFlow.collectAsState()
val selectedProxy by app.proxyStateStore.selectedProxyFlow.collectAsState()
val haptics = LocalHapticFeedback.current
var detailsOpen by remember { mutableStateOf(false) }
var pingMs by remember { mutableIntStateOf(-1) }
val scope = rememberCoroutineScope()
LaunchedEffect(detailsOpen, state) {
if (detailsOpen && state == ConnectionState.CONNECTED) {
val proxy = selectedProxy
pingMs = -1
if (proxy != null) {
pingMs = PingUtil.measureTcpLatencyMs(proxy.host, proxy.port) ?: -2
}
}
}
// NOTE (v1.3 lag fix): the elapsed-seconds ticker used to live here and
// recomposed the ENTIRE home screen every second while connected. It now
// lives inside the connect core (rememberElapsedSeconds) so only the
// elapsed Text recomposes.
fun navigate(dest: FoxyDestination) {
when (dest) {
FoxyDestination.Home -> Unit
FoxyDestination.Locations -> onOpenServers()
FoxyDestination.Accounts -> onOpenAccounts()
FoxyDestination.Settings -> onOpenSettings()
}
}
Scaffold(
topBar = {
TopAppBar(
title = { Text("FoxyVPN", style = MaterialTheme.typography.titleLargeEmphasized) },
actions = {
IconButton(
onClick = {
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
val next = when (appearance.themeMode) {
ThemeMode.LIGHT -> ThemeMode.DARK
ThemeMode.DARK -> ThemeMode.AMOLED
ThemeMode.AMOLED -> ThemeMode.LIGHT
ThemeMode.SYSTEM -> ThemeMode.DARK
}
appearance.updateThemeMode(next)
},
) {
Icon(
imageVector = when (appearance.themeMode) {
ThemeMode.SYSTEM -> Icons.Filled.BrightnessAuto
ThemeMode.LIGHT -> Icons.Filled.LightMode
ThemeMode.DARK -> Icons.Filled.DarkMode
ThemeMode.AMOLED -> Icons.Filled.Contrast
},
contentDescription = "Theme (tap to cycle)",
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
IconButton(onClick = onOpenSettings) {
Icon(
imageVector = Icons.Filled.Settings,
contentDescription = "Settings",
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
},
)
},
bottomBar = {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
) {
// One shared footer for every home design (Pixel / Portal /
// Classic): they all render inside this Scaffold, so the
// badge below shows whichever design is chosen.
MadeByBadge()
FoxyBottomBar(
style = appearance.navBarStyle,
current = FoxyDestination.Home,
onNavigate = ::navigate,
)
}
},
) { padding ->
val data = HomeContentData(
state = state,
lastError = lastError,
connectedCount = connectedCount,
accounts = accounts,
selectedProxy = selectedProxy,
pingMs = pingMs,
detailsOpen = detailsOpen,
appearance = appearance,
)
val callbacks = HomeCallbacks(
onToggle = {
if (state == ConnectionState.DISCONNECTED) onRequestConnect() else onDisconnect()
},
onToggleDetails = { detailsOpen = !detailsOpen },
onRefreshPing = {
val proxy = selectedProxy
scope.launch {
pingMs = -1
pingMs = proxy?.let { PingUtil.measureTcpLatencyMs(it.host, it.port) } ?: -2
}
},
onOpenServers = onOpenServers,
onOpenAccounts = onOpenAccounts,
)
when (appearance.appDesign) {
SettingsStore.AppDesign.PIXEL -> PixelHomeBody(padding, data, callbacks)
SettingsStore.AppDesign.PORTAL -> PortalHomeBody(padding, data, callbacks)
SettingsStore.AppDesign.OLD -> OldHomeBody(padding, data, callbacks)
}
}
}
/**
* The makers' badge: a small gradient pill centered at the bottom of the
* Home screen, above the navigation bar. Pure decoration — it holds no
* state and touches no connection logic.
*/
@Composable
private fun MadeByBadge() {
val cs = MaterialTheme.colorScheme
val gradient = Brush.horizontalGradient(
listOf(cs.primary, cs.tertiary, cs.secondary, cs.primary),
)
Box(
modifier = Modifier
.padding(top = 6.dp, bottom = 2.dp)
.clip(RoundedCornerShape(50))
.border(1.dp, gradient, RoundedCornerShape(50))
.background(cs.surfaceContainer.copy(alpha = 0.65f))
.padding(horizontal = 14.dp, vertical = 6.dp),
contentAlignment = Alignment.Center,
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = "✦",
style = MaterialTheme.typography.labelMedium,
color = cs.tertiary,
)
Spacer(Modifier.width(6.dp))
Text(
text = "Made by arblant & Muse",
style = MaterialTheme.typography.labelLarge.copy(
brush = gradient,
fontWeight = FontWeight.SemiBold,
),
maxLines = 1,
)
Spacer(Modifier.width(6.dp))
Text(
text = "✦",
style = MaterialTheme.typography.labelMedium,
color = cs.tertiary,
)
}
}
}
private data class HomeContentData(
val state: ConnectionState,
val lastError: String?,
// NOTE (v1.3 lag fix): multiStats is NOT collected here anymore. The pool
// emits fresh stats every 2s while connected; collecting at HomeScreen
// level recomposed the whole tree on every tick. Consumers (SpeedRow,
// DetailsPanel) collect it locally so only they recompose.
val connectedCount: Int,
val accounts: List<VpnAccount>,
val selectedProxy: ProxyCandidate?,
val pingMs: Int,
val detailsOpen: Boolean,
val appearance: AppearanceController,
)
private class HomeCallbacks(
val onToggle: () -> Unit,
val onToggleDetails: () -> Unit,
val onRefreshPing: () -> Unit,
val onOpenServers: () -> Unit,
val onOpenAccounts: () -> Unit,
)
private val HomeContentData.corner: Dp
get() = appearance.personalization.cornerStyle.cardDp.dp
private val HomeContentData.compact: Boolean
get() = appearance.personalization.density == ListDensity.Compact
private fun HomeContentData.cardShape() = RoundedCornerShape(corner)
@Composable
private fun HomeConnectCore(data: HomeContentData, callbacks: HomeCallbacks) {
val a = data.appearance
ConnectCore(
state = data.state,
style = a.connectButtonStyle,
reduceMotion = a.reduceMotion,
hapticEnabled = a.hapticFeedback,
connectColor = a.personalization.connectColor,
connectedColor = a.personalization.connectedColor,
onToggle = callbacks.onToggle,
)
}
/**
* Scoped 1-second elapsed ticker (v1.3 lag fix): only this Text recomposes
* each second while connected — never the parent screen.
*/
@Composable
private fun ElapsedText(
state: ConnectionState,
style: androidx.compose.ui.text.TextStyle = MaterialTheme.typography.bodyMedium,
color: Color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier: Modifier = Modifier,
prefix: String = "",
) {
var elapsed by remember { mutableIntStateOf(0) }
LaunchedEffect(state) {
if (state == ConnectionState.CONNECTED) {
val start = System.currentTimeMillis()
while (true) {
kotlinx.coroutines.delay(1_000L)
elapsed = ((System.currentTimeMillis() - start) / 1_000L).toInt()
}
} else {
elapsed = 0
}
}
if (state == ConnectionState.CONNECTED) {
Text(
text = "$prefix${formatElapsed(elapsed)}",
style = style,
color = color,
modifier = modifier,
)
}
}
@Composable
private fun PixelHomeBody(
padding: PaddingValues,
data: HomeContentData,
callbacks: HomeCallbacks,
) {
val state = data.state
val vGap = if (data.compact) 6.dp else 10.dp
Column(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.verticalScroll(rememberScrollState())
.padding(horizontal = 16.dp)
.widthIn(max = 560.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Spacer(Modifier.height(8.dp))
HeroCard(data = data, callbacks = callbacks)
Spacer(Modifier.height(vGap))
FactChipsRow(
state = state,
placeLabel = data.selectedProxy?.let { it.countryName.ifBlank { it.countryCode } } ?: "Recommended",
flag = data.selectedProxy?.emojiFlag.orEmpty(),
pingMs = data.pingMs,
connectedCount = data.connectedCount,
totalAccounts = data.accounts.size,
onOpenDetails = callbacks.onToggleDetails,
)
AnimatedVisibility(visible = data.detailsOpen && state == ConnectionState.CONNECTED) {
DetailsPanel(
accounts = data.accounts,
pingMs = data.pingMs,
personalization = data.appearance.personalization,
placeLabel = data.selectedProxy?.let {
listOfNotNull(it.countryName.ifBlank { null }, it.host).joinToString(" · ")
} ?: "Recommended",
onRefreshPing = callbacks.onRefreshPing,
)
}
val error = data.lastError
if (error != null && state != ConnectionState.CONNECTING) {
Spacer(Modifier.height(vGap))
ErrorCard(error = error, corner = data.corner)
}
Spacer(Modifier.height(vGap))
ServerRow(
flag = data.selectedProxy?.emojiFlag.orEmpty(),
name = data.selectedProxy?.let { it.countryName.ifBlank { it.countryCode } } ?: "Recommended",
detail = data.selectedProxy?.let { "${it.host}:${it.port}" } ?: "Tap to choose a location",
personalization = data.appearance.personalization,
corner = data.corner,
onClick = callbacks.onOpenServers,
)
Spacer(Modifier.height(vGap))
AccountsRow(
accounts = data.accounts,
connectedCount = data.connectedCount,
state = state,
personalization = data.appearance.personalization,
corner = data.corner,
onClick = callbacks.onOpenAccounts,
)
Spacer(Modifier.height(vGap))
SpeedRow(live = state == ConnectionState.CONNECTED, corner = data.corner)
Spacer(Modifier.height(16.dp))
}
}
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
private fun HeroCard(
data: HomeContentData,
callbacks: HomeCallbacks,
) {
val cs = MaterialTheme.colorScheme
val state = data.state
val live = state == ConnectionState.CONNECTED
val busy = state == ConnectionState.CONNECTING
// 1:1 from ZedSecure PixelHeroCard: slowEffectsSpec colors, 44.dp card.
val effects = MaterialTheme.motionScheme.slowEffectsSpec<Color>()
val container by animateColorAsState(
if (live) cs.primaryContainer else cs.surfaceContainer,
effects,
label = "pixel-card",
)
val ink by animateColorAsState(
if (live) cs.onPrimaryContainer else cs.onSurface,
effects,
label = "pixel-ink",
)
val sub by animateColorAsState(
if (live) cs.onPrimaryContainer.copy(alpha = 0.78f) else cs.onSurfaceVariant,
effects,
label = "pixel-sub",
)
val rise = MaterialTheme.motionScheme.defaultSpatialSpec<androidx.compose.ui.unit.IntOffset>()
Surface(
shape = RoundedCornerShape(44.dp),
color = container,
modifier = Modifier.fillMaxWidth(),
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 26.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
HomeConnectCore(data = data, callbacks = callbacks)
Spacer(Modifier.height(16.dp))
// 1:1: fadeIn(280) + slideInVertically(rise){it/3} togetherWith fadeOut(160).
AnimatedContent(
targetState = state,
transitionSpec = {
(fadeIn(tween(280)) + slideInVertically(rise) { it / 3 }) togetherWith fadeOut(tween(160))
},
label = "pixel-state",
) { s ->
Text(
text = when (s) {
ConnectionState.CONNECTED -> "Connected"
ConnectionState.CONNECTING -> "Connecting…"
ConnectionState.DISCONNECTED -> "Disconnected"
},
style = MaterialTheme.typography.headlineSmallEmphasized,
color = ink,
textAlign = TextAlign.Center,
)
}
if (busy) {
Spacer(Modifier.height(10.dp))
// 1:1: LinearWavyProgressIndicator, 156.dp.
LinearWavyProgressIndicator(
modifier = Modifier.width(156.dp),
color = cs.primary,
trackColor = cs.primary.copy(alpha = 0.18f),
)
Spacer(Modifier.height(6.dp))
Text(s("Tap the core to cancel"), style = MaterialTheme.typography.bodySmall, color = sub)
} else if (live) {
Spacer(Modifier.height(4.dp))
Text(
"All traffic is protected",
style = MaterialTheme.typography.bodySmall,
color = sub,
)
} else {
Spacer(Modifier.height(4.dp))
Text(
"Tap the core to connect",
style = MaterialTheme.typography.bodySmall,
color = sub,
)
}
}
}
}
/**
* PORTAL design: immersive single column. The connect core glows at the
* center; facts live in one unified card below.
*/
@Composable
private fun PortalHomeBody(
padding: PaddingValues,
data: HomeContentData,
callbacks: HomeCallbacks,
) {
val cs = MaterialTheme.colorScheme
val state = data.state
val live = state == ConnectionState.CONNECTED
val busy = state == ConnectionState.CONNECTING
val selectedProxy = data.selectedProxy
val vGap = if (data.compact) 8.dp else 12.dp
val ring by animateColorAsState(
targetValue = when {
live -> cs.primary
busy -> cs.tertiary
else -> cs.surfaceContainerHighest
},
animationSpec = tween(500),
label = "portal-ring",
)
Column(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.verticalScroll(rememberScrollState())
.padding(horizontal = 20.dp)
.widthIn(max = 560.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Spacer(Modifier.height(12.dp))
AnimatedContent(
targetState = state,
transitionSpec = { fadeIn(tween(250)) togetherWith fadeOut(tween(150)) },
label = "portal-title",
) { s ->
Text(
text = when (s) {
ConnectionState.CONNECTED -> "Connected"
ConnectionState.CONNECTING -> "Connecting…"
ConnectionState.DISCONNECTED -> "Disconnected"
},
style = MaterialTheme.typography.headlineSmallEmphasized,
color = if (live) cs.primary else cs.onSurface,
)
}
if (live) {
ElapsedText(
state = state,
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(top = 4.dp),
)
}
Spacer(Modifier.height(20.dp))
Box(contentAlignment = Alignment.Center) {
Box(
modifier = Modifier
.size(236.dp)
.clip(CircleShape)
.background(ring.copy(alpha = 0.14f)),
)
Box(
modifier = Modifier
.size(208.dp)
.clip(CircleShape)
.background(ring.copy(alpha = 0.20f)),
)
HomeConnectCore(data = data, callbacks = callbacks)
}
Spacer(Modifier.height(20.dp))
Surface(
shape = RoundedCornerShape(data.corner),
color = cs.surfaceContainer,
modifier = Modifier.fillMaxWidth(),
) {
Column(Modifier.padding(vertical = 6.dp)) {
PortalInfoRow(
label = "Location",
value = selectedProxy?.let { it.countryName.ifBlank { it.countryCode } } ?: "Recommended",
flag = selectedProxy?.emojiFlag.orEmpty(),
)
HorizontalDivider(Modifier.padding(horizontal = 20.dp), color = cs.outlineVariant.copy(alpha = 0.5f))
PortalInfoRow(
label = "Ping",
value = when {
data.pingMs < 0 -> "—"
else -> "${data.pingMs} ms"
},
flag = "",
)
HorizontalDivider(Modifier.padding(horizontal = 20.dp), color = cs.outlineVariant.copy(alpha = 0.5f))
PortalInfoRow(
label = "Accounts",
value = "${data.connectedCount}/${data.accounts.size} active",
flag = "",
)
}
}
val error = data.lastError
if (error != null && state != ConnectionState.CONNECTING) {
Spacer(Modifier.height(vGap))
ErrorCard(error = error, corner = data.corner)
}
Spacer(Modifier.height(vGap))
ServerRow(
flag = selectedProxy?.emojiFlag.orEmpty(),
name = selectedProxy?.let { it.countryName.ifBlank { it.countryCode } } ?: "Recommended",
detail = selectedProxy?.let { "${it.host}:${it.port}" } ?: "Tap to choose a location",
personalization = data.appearance.personalization,
corner = data.corner,
onClick = callbacks.onOpenServers,
)
Spacer(Modifier.height(vGap))
AccountsRow(
accounts = data.accounts,
connectedCount = data.connectedCount,
state = state,
personalization = data.appearance.personalization,
corner = data.corner,
onClick = callbacks.onOpenAccounts,
)
Spacer(Modifier.height(vGap))
SpeedRow(live = live, corner = data.corner)
Spacer(Modifier.height(16.dp))
}
}
@Composable
private fun PortalInfoRow(label: String, value: String, flag: String) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 12.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
text = label,
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.weight(1f),
)
if (flag.isNotBlank()) {
Text(text = flag, modifier = Modifier.padding(end = 6.dp))
}
Text(
text = value,
style = MaterialTheme.typography.titleSmallEmphasized,
color = MaterialTheme.colorScheme.onSurface,
)
}
}
/**
* CLASSIC design: traditional VPN-app layout — big button, status line,
* plain divided rows.
*/
@Composable
private fun OldHomeBody(
padding: PaddingValues,
data: HomeContentData,
callbacks: HomeCallbacks,
) {
val cs = MaterialTheme.colorScheme
val state = data.state
val live = state == ConnectionState.CONNECTED
val selectedProxy = data.selectedProxy
Column(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.verticalScroll(rememberScrollState())
.padding(horizontal = 24.dp)
.widthIn(max = 560.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Spacer(Modifier.height(24.dp))
AnimatedContent(
targetState = state,
transitionSpec = { fadeIn(tween(250)) togetherWith fadeOut(tween(150)) },
label = "classic-title",
) { s ->
Text(
text = when (s) {
ConnectionState.CONNECTED -> "VPN is ON"
ConnectionState.CONNECTING -> "Connecting…"
ConnectionState.DISCONNECTED -> "VPN is OFF"
},
style = MaterialTheme.typography.headlineMedium,
color = if (live) cs.primary else cs.onSurfaceVariant,
)
}
Spacer(Modifier.height(18.dp))
HomeConnectCore(data = data, callbacks = callbacks)
if (live) {
Spacer(Modifier.height(10.dp))
ElapsedText(
state = state,
style = MaterialTheme.typography.bodyMedium,
prefix = "Connected for ",
)
}
Spacer(Modifier.height(24.dp))
val error = data.lastError
if (error != null && state != ConnectionState.CONNECTING) {
ErrorCard(error = error, corner = data.corner)
Spacer(Modifier.height(12.dp))
}
ClassicRow(
title = "Server",
subtitle = selectedProxy?.let {
"${it.countryName.ifBlank { it.countryCode }} · ${it.host}:${it.port}"
} ?: "Recommended — tap to choose",
monospace = data.appearance.personalization.monospaceAddress,
onClick = callbacks.onOpenServers,
)
HorizontalDivider(color = cs.outlineVariant.copy(alpha = 0.6f))
ClassicRow(
title = s("Accounts"),
subtitle = "${data.connectedCount} of ${data.accounts.size} connected",
monospace = false,
onClick = callbacks.onOpenAccounts,
)
HorizontalDivider(color = cs.outlineVariant.copy(alpha = 0.6f))
ClassicRow(
title = "Speed",
subtitle = if (live) "Live" else "Idle",
monospace = false,
onClick = {},
)
Spacer(Modifier.height(16.dp))
}
}
@Composable
private fun ClassicRow(title: String, subtitle: String, monospace: Boolean, onClick: () -> Unit) {
Row(
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = onClick)
.padding(vertical = 14.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Column(Modifier.weight(1f)) {
Text(text = title, style = MaterialTheme.typography.titleMedium)
Text(
text = subtitle,
style = MaterialTheme.typography.bodySmall.copy(
fontFamily = if (monospace) FontFamily.Monospace else FontFamily.Default,
),
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
Icon(
imageVector = Icons.AutoMirrored.Filled.KeyboardArrowRight,
contentDescription = null,
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
fun formatElapsed(totalSeconds: Int): String {
val h = totalSeconds / 3600
val m = (totalSeconds % 3600) / 60
val s = totalSeconds % 60
return if (h > 0) "%d:%02d:%02d".format(h, m, s) else "%02d:%02d".format(m, s)
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun FactChipsRow(
state: ConnectionState,
placeLabel: String,
flag: String,
pingMs: Int,
connectedCount: Int,
totalAccounts: Int,
onOpenDetails: () -> Unit,
) {
val live = state == ConnectionState.CONNECTED
FlowRow(
horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterHorizontally),
modifier = Modifier.fillMaxWidth(),
) {
FactChip(onClick = onOpenDetails) {
if (flag.isNotBlank()) {
Text(flag, style = MaterialTheme.typography.bodyMedium)
Spacer(Modifier.width(6.dp))
} else {
Icon(Icons.Filled.Public, contentDescription = null, modifier = Modifier.size(16.dp))
Spacer(Modifier.width(6.dp))
}
Text(placeLabel, maxLines = 1, overflow = TextOverflow.Ellipsis)
}
if (live) {
FactChip(onClick = onOpenDetails) {
Text(
when (pingMs) {
-1 -> "ping …"
-2 -> "ping n/a"
else -> "$pingMs ms"
},
maxLines = 1,
)
}
FactChip(onClick = onOpenDetails) {
Icon(Icons.Filled.AccountCircle, contentDescription = null, modifier = Modifier.size(16.dp))
Spacer(Modifier.width(6.dp))
Text("$connectedCount/$totalAccounts", maxLines = 1)
}
}
}
}
@Composable
private fun FactChip(onClick: () -> Unit, content: @Composable () -> Unit) {
val cs = MaterialTheme.colorScheme
Surface(
onClick = onClick,
shape = CircleShape,
color = cs.surfaceContainerHigh,
contentColor = cs.onSurfaceVariant,
) {
Row(
modifier = Modifier.padding(horizontal = 14.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
androidx.compose.runtime.CompositionLocalProvider(
androidx.compose.material3.LocalTextStyle provides MaterialTheme.typography.labelLarge,
) {
content()
}
}
}
}
@Composable
private fun DetailsPanel(
accounts: List<VpnAccount>,
pingMs: Int,
personalization: Personalization,
placeLabel: String,
onRefreshPing: () -> Unit,
) {
val cs = MaterialTheme.colorScheme
// v1.3 lag fix: collected here so stats ticks recompose only this panel.
val stats by FoxyVpnService.multiAccountStats.collectAsState()
val byId = remember(stats) { stats.associateBy { it.accountId } }
Surface(
shape = RoundedCornerShape(personalization.cornerStyle.cardDp.dp),
color = personalization.configCardColor ?: cs.surfaceContainerHigh,
modifier = Modifier.fillMaxWidth(),
) {
val contentColor = personalization.configCardTextColor
Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
"Connection details",
style = MaterialTheme.typography.titleSmallEmphasized,
color = contentColor ?: cs.onSurface,
modifier = Modifier.weight(1f),
)
IconButton(onClick = onRefreshPing, modifier = Modifier.size(32.dp)) {
Icon(
Icons.Filled.Refresh,
contentDescription = "Refresh ping",
modifier = Modifier.size(18.dp),
tint = contentColor ?: cs.onSurfaceVariant,
)
}
}
DetailRow("Location", placeLabel, contentColor)
DetailRow(
"Ping",
when (pingMs) {
-1 -> "measuring…"
-2 -> "unavailable"
else -> "$pingMs ms"
},
contentColor,
)
if (accounts.isEmpty()) {
Text(
"No accounts yet — add some from the Accounts screen.",
style = MaterialTheme.typography.bodySmall,
color = contentColor?.copy(alpha = 0.75f) ?: cs.onSurfaceVariant,
)
} else {
accounts.forEach { account ->
val s = byId[account.id]
DetailRow(
label = account.email.ifBlank { "Account" },
value = when {
s == null || !account.isEnabled -> "idle"
s.isExhausted -> "quota exhausted"
!s.isConnected -> "dialing…"
else -> "↓ ${formatBytesPerSecond(s.speedDownBytesPerSec)} · ↑ ${formatBytesPerSecond(s.speedUpBytesPerSec)}"
},
contentColor = contentColor,
)
}
}
}
}
}
@Composable
private fun DetailRow(label: String, value: String, contentColor: Color?) {
val cs = MaterialTheme.colorScheme
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
Text(
label,
style = MaterialTheme.typography.bodySmall,
color = contentColor?.copy(alpha = 0.75f) ?: cs.onSurfaceVariant,
modifier = Modifier.weight(1f),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Text(
value,
style = MaterialTheme.typography.labelLarge,
color = contentColor ?: cs.onSurface,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
}
@Composable
private fun ErrorCard(error: String, corner: Dp) {
val cs = MaterialTheme.colorScheme
Surface(
shape = RoundedCornerShape(corner),
color = cs.errorContainer,
contentColor = cs.onErrorContainer,
modifier = Modifier.fillMaxWidth(),
) {
Text(
error,
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
modifier = Modifier.padding(horizontal = 20.dp, vertical = 14.dp),
)
}
}
@Composable
private fun ServerRow(
flag: String,
name: String,
detail: String,
personalization: Personalization,
corner: Dp,
onClick: () -> Unit,
) {
val cs = MaterialTheme.colorScheme
Surface(
onClick = onClick,
shape = RoundedCornerShape(corner),
color = personalization.serverCardColor ?: cs.surfaceContainerHigh,
modifier = Modifier.fillMaxWidth(),
) {
Row(
Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 14.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Box(
modifier = Modifier
.size(44.dp)
.clip(RoundedCornerShape(16.dp))
.background(cs.primary),
contentAlignment = Alignment.Center,
) {
if (flag.isNotBlank()) {
Text(flag, style = MaterialTheme.typography.titleLarge)
} else {
Icon(Icons.Filled.Public, contentDescription = null, tint = cs.onPrimary)
}
}
Spacer(Modifier.width(14.dp))
Column(Modifier.weight(1f)) {
Text(name, style = MaterialTheme.typography.titleMediumEmphasized, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(
detail,
style = MaterialTheme.typography.bodySmall.copy(
fontFamily = if (personalization.monospaceAddress) FontFamily.Monospace else FontFamily.Default,
),
color = cs.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null, tint = cs.onSurfaceVariant)
}
}
}
@Composable
private fun AccountsRow(
accounts: List<VpnAccount>,
connectedCount: Int,
state: ConnectionState,
personalization: Personalization,
corner: Dp,
onClick: () -> Unit,
) {
val cs = MaterialTheme.colorScheme
val statusColors = LocalFoxyStatusColors.current
Surface(
onClick = onClick,
shape = RoundedCornerShape(corner),
color = personalization.serverActiveColor ?: cs.surfaceContainerHigh,
modifier = Modifier.fillMaxWidth(),
) {
Row(
Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 14.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Box(
modifier = Modifier
.size(44.dp)
.clip(RoundedCornerShape(16.dp))
.background(cs.tertiaryContainer),
contentAlignment = Alignment.Center,
) {
Icon(Icons.Filled.AccountCircle, contentDescription = null, tint = cs.onTertiaryContainer)
}
Spacer(Modifier.width(14.dp))
Column(Modifier.weight(1f)) {
Text(s("Accounts"), style = MaterialTheme.typography.titleMediumEmphasized)
Text(
if (accounts.isEmpty()) "Tap to add your first account"
else "${accounts.count { it.isEnabled }} enabled · $connectedCount connected",
style = MaterialTheme.typography.bodySmall,
color = cs.onSurfaceVariant,
)
}
Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
accounts.take(8).forEach { account ->
val dot = when {
state == ConnectionState.CONNECTED && account.isEnabled -> statusColors.connected
!account.isEnabled -> cs.outlineVariant
else -> cs.outline
}
val animated by animateColorAsState(dot, tween(300), label = "acct-dot")
Box(Modifier.size(10.dp).clip(CircleShape).background(animated))
}
}
Spacer(Modifier.width(8.dp))
Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null, tint = cs.onSurfaceVariant)
}
}
}
/**
* v1.3 lag fix: collects pool stats locally so the 2-second stats tick
* recomposes ONLY this row, never the whole home screen.
*/
@Composable
private fun SpeedRow(live: Boolean, corner: Dp) {
val cs = MaterialTheme.colorScheme
val stats by FoxyVpnService.multiAccountStats.collectAsState()
val down = stats.sumOf { it.speedDownBytesPerSec }
val up = stats.sumOf { it.speedUpBytesPerSec }
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(6.dp),
) {
SpeedTile(
icon = Icons.Filled.Download,
label = "Download",
value = if (live) formatBytesPerSecond(down) else "—",
badge = cs.tertiaryContainer,
onBadge = cs.onTertiaryContainer,
corner = corner,
modifier = Modifier.weight(1f),
)
SpeedTile(
icon = Icons.Filled.Upload,
label = "Upload",
value = if (live) formatBytesPerSecond(up) else "—",
badge = cs.primaryContainer,
onBadge = cs.onPrimaryContainer,
corner = corner,
modifier = Modifier.weight(1f),
)
}
}
@Composable
private fun SpeedTile(
icon: androidx.compose.ui.graphics.vector.ImageVector,
label: String,
value: String,
badge: Color,
onBadge: Color,
corner: Dp,
modifier: Modifier = Modifier,
) {
val cs = MaterialTheme.colorScheme
Surface(shape = RoundedCornerShape(corner), color = cs.surfaceContainerHigh, modifier = modifier) {
Row(
Modifier.padding(horizontal = 14.dp, vertical = 12.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Box(
modifier = Modifier
.size(36.dp)
.clip(RoundedCornerShape(14.dp))
.background(badge),
contentAlignment = Alignment.Center,
) {
Icon(icon, contentDescription = null, tint = onBadge, modifier = Modifier.size(18.dp))
}
Spacer(Modifier.width(10.dp))
Column {
Text(label, style = MaterialTheme.typography.labelMedium, color = cs.onSurfaceVariant)
Text(value, style = MaterialTheme.typography.titleLargeEmphasized, maxLines = 1)
}
}
}
}