refactor(app): remove unsupported runtime and UI state
This commit is contained in:
1 parent
9ac236a981
commit
0ed7bf27b5
19 files changed
+173
-381
No files matched your search
@@ -5,13 +5,9 @@ import 'package:revoltvpn/logic/app_colors.dart';
|
||||
import 'package:revoltvpn/screens/settings/in_settings/lightning.dart';
|
||||
|
||||
/// 2D lightning bolt — a jagged yellow line from top to bottom with glow.
|
||||
|
||||
class LightningEffect extends StatefulWidget {
|
||||
|
||||
final int minIntervalSec;
|
||||
|
||||
final int maxIntervalSec;
|
||||
|
||||
final double doubleChance;
|
||||
|
||||
const LightningEffect({
|
||||
@@ -26,26 +22,49 @@ class LightningEffect extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _LightningEffectState extends State<LightningEffect>
|
||||
with SingleTickerProviderStateMixin {
|
||||
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
|
||||
late final AnimationController _ctrl;
|
||||
Timer? _timer;
|
||||
final _random = Random();
|
||||
bool _appActive = true;
|
||||
|
||||
LightningBolt? _bolt;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_ctrl = AnimationController(vsync: this, duration: const Duration(milliseconds: 400));
|
||||
_ctrl = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 400),
|
||||
);
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
final lifecycle = WidgetsBinding.instance.lifecycleState;
|
||||
_appActive = lifecycle == null || lifecycle == AppLifecycleState.resumed;
|
||||
lightningEnabled.addListener(_onEnabledChanged);
|
||||
if (_appActive && lightningEnabled.value) _scheduleNext();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
_appActive = state == AppLifecycleState.resumed;
|
||||
if (!_appActive) {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
_ctrl.stop();
|
||||
_bolt = null;
|
||||
if (mounted) setState(() {});
|
||||
return;
|
||||
}
|
||||
|
||||
if (lightningEnabled.value) _scheduleNext();
|
||||
}
|
||||
|
||||
void _onEnabledChanged() {
|
||||
if (lightningEnabled.value) {
|
||||
if (lightningEnabled.value && _appActive) {
|
||||
_scheduleNext();
|
||||
} else {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
_ctrl.stop();
|
||||
_bolt = null;
|
||||
}
|
||||
@@ -54,6 +73,7 @@ class _LightningEffectState extends State<LightningEffect>
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
lightningEnabled.removeListener(_onEnabledChanged);
|
||||
_timer?.cancel();
|
||||
_ctrl.dispose();
|
||||
@@ -61,19 +81,23 @@ class _LightningEffectState extends State<LightningEffect>
|
||||
}
|
||||
|
||||
void _scheduleNext() {
|
||||
_timer?.cancel();
|
||||
if (!mounted || !_appActive || !lightningEnabled.value) return;
|
||||
|
||||
final secs = widget.minIntervalSec +
|
||||
_random.nextInt(widget.maxIntervalSec - widget.minIntervalSec + 1);
|
||||
_timer = Timer(Duration(seconds: secs), _strike);
|
||||
}
|
||||
|
||||
void _strike() {
|
||||
if (!mounted || !lightningEnabled.value) return;
|
||||
_timer = null;
|
||||
if (!mounted || !_appActive || !lightningEnabled.value) return;
|
||||
_bolt = LightningBolt.random(_random);
|
||||
_ctrl.forward(from: 0.0);
|
||||
|
||||
if (_random.nextDouble() < widget.doubleChance) {
|
||||
Future.delayed(const Duration(milliseconds: 120), () {
|
||||
if (mounted && lightningEnabled.value) {
|
||||
if (mounted && _appActive && lightningEnabled.value) {
|
||||
_bolt = LightningBolt.random(_random);
|
||||
_ctrl.forward(from: 0.0);
|
||||
}
|
||||
@@ -83,10 +107,10 @@ class _LightningEffectState extends State<LightningEffect>
|
||||
_scheduleNext();
|
||||
}
|
||||
|
||||
/// Sharp flash curve: quick on, quick off. No slow fade.
|
||||
/// Sharp flash curve: quick on, quick off. No slow fade.
|
||||
double _opacity(double t) {
|
||||
if (t < 0.08) return t / 0.08; // fast ramp
|
||||
return 1.0 - ((t - 0.08) / 0.92); // linear decay
|
||||
if (t < 0.08) return t / 0.08;
|
||||
return 1.0 - ((t - 0.08) / 0.92);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -117,37 +141,31 @@ class _LightningEffectState extends State<LightningEffect>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// A single bolt defined by a list of 2D points (0.0–1.0 in width×height space).
|
||||
class LightningBolt {
|
||||
final List<Offset> mainPath;
|
||||
final List<List<Offset>> branches; // 0–2 forks off the main path
|
||||
final List<List<Offset>> branches;
|
||||
|
||||
const LightningBolt({required this.mainPath, this.branches = const []});
|
||||
|
||||
/// Generate a random bolt. The path always starts near the top-center
|
||||
/// Generate a random bolt. The path always starts near the top-center
|
||||
/// and ends near the bottom, zigzagging left/right along the way.
|
||||
factory LightningBolt.random(Random rng) {
|
||||
final points = <Offset>[];
|
||||
|
||||
// Start: top, slight horizontal offset
|
||||
double x = 0.5 + (rng.nextDouble() - 0.5) * 0.15;
|
||||
points.add(Offset(x, 0.0));
|
||||
|
||||
// Zigzag down — 6–10 segments
|
||||
final segments = 6 + rng.nextInt(5);
|
||||
for (int i = 1; i < segments; i++) {
|
||||
final y = i / segments;
|
||||
// Random horizontal offset — each segment adds jitter
|
||||
x += (rng.nextDouble() - 0.5) * 0.25;
|
||||
x = x.clamp(0.05, 0.95); // stay on screen
|
||||
x = x.clamp(0.05, 0.95);
|
||||
points.add(Offset(x, y));
|
||||
}
|
||||
|
||||
// End: bottom
|
||||
points.add(Offset(x.clamp(0.1, 0.9), 1.0));
|
||||
|
||||
// Branches — 0–2 forks from random points along the main path
|
||||
final branches = <List<Offset>>[];
|
||||
final branchCount = rng.nextInt(3);
|
||||
for (int b = 0; b < branchCount; b++) {
|
||||
@@ -160,7 +178,7 @@ class LightningBolt {
|
||||
double by = start.dy;
|
||||
final branchLen = 2 + rng.nextInt(3);
|
||||
for (int j = 0; j < branchLen; j++) {
|
||||
by += (0.05 + rng.nextDouble() * 0.12);
|
||||
by += 0.05 + rng.nextDouble() * 0.12;
|
||||
bx += (rng.nextDouble() - 0.5) * 0.3;
|
||||
bx = bx.clamp(0.02, 0.98);
|
||||
by = by.clamp(0.0, 1.0);
|
||||
@@ -173,7 +191,6 @@ class LightningBolt {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class _LightningPainter extends CustomPainter {
|
||||
final LightningBolt bolt;
|
||||
final double opacity;
|
||||
@@ -182,7 +199,8 @@ class _LightningPainter extends CustomPainter {
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final toScreen = (Offset p) => Offset(p.dx * size.width, p.dy * size.height);
|
||||
Offset toScreen(Offset p) => Offset(p.dx * size.width, p.dy * size.height);
|
||||
|
||||
final scaledMain = bolt.mainPath.map(toScreen).toList();
|
||||
final scaledBranches = bolt.branches
|
||||
.map((b) => b.map(toScreen).toList())
|
||||
@@ -191,7 +209,6 @@ class _LightningPainter extends CustomPainter {
|
||||
final baseColor = AppColors.accent.withAlpha((opacity * 255).round());
|
||||
final glowColor = AppColors.accent.withAlpha((opacity * 0.35 * 255).round());
|
||||
|
||||
// Glow
|
||||
final glowPaint = Paint()
|
||||
..color = glowColor
|
||||
..strokeWidth = 18
|
||||
@@ -202,7 +219,6 @@ class _LightningPainter extends CustomPainter {
|
||||
|
||||
_drawPath(canvas, scaledMain, scaledBranches, glowPaint);
|
||||
|
||||
// Core
|
||||
final corePaint = Paint()
|
||||
..color = baseColor
|
||||
..strokeWidth = 5
|
||||
@@ -212,7 +228,6 @@ class _LightningPainter extends CustomPainter {
|
||||
|
||||
_drawPath(canvas, scaledMain, scaledBranches, corePaint);
|
||||
|
||||
// Hot center
|
||||
if (opacity > 0.6) {
|
||||
final hotPaint = Paint()
|
||||
..color = Colors.white.withAlpha((opacity * 180).round())
|
||||
@@ -225,8 +240,12 @@ class _LightningPainter extends CustomPainter {
|
||||
}
|
||||
}
|
||||
|
||||
void _drawPath(Canvas canvas, List<Offset> main,
|
||||
List<List<Offset>> branches, Paint paint) {
|
||||
void _drawPath(
|
||||
Canvas canvas,
|
||||
List<Offset> main,
|
||||
List<List<Offset>> branches,
|
||||
Paint paint,
|
||||
) {
|
||||
final mainPath = Path()..moveTo(main.first.dx, main.first.dy);
|
||||
for (int i = 1; i < main.length; i++) {
|
||||
mainPath.lineTo(main[i].dx, main[i].dy);
|
||||
|
||||
Reference in new issue
Block a user