After persist the live activity data for welcome10

This commit is contained in:
Your Name 2026-10-24 12:03:14 +02:00
parent f48492c27c
commit 95762e4204
4 changed files with 174 additions and 117 deletions

View File

@ -1,12 +1,17 @@
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
/// App-wide connectivity signal for offline banner + reconnect refresh.
class ConnectivityController extends ChangeNotifier {
///
/// Uses [connectivity_plus] (interface type: wifi/mobile/none). Empty result
/// lists are treated as offline. Re-checks when the app resumes so the banner
/// updates after airplane-mode toggles that some platforms miss mid-session.
class ConnectivityController extends ChangeNotifier with WidgetsBindingObserver {
ConnectivityController({Connectivity? connectivity})
: _connectivity = connectivity ?? Connectivity() {
WidgetsBinding.instance.addObserver(this);
_init();
}
@ -18,26 +23,35 @@ class ConnectivityController extends ChangeNotifier {
bool get isOnline => _online;
bool get isOffline => !_online;
static bool onlineFromResults(List<ConnectivityResult> results) {
if (results.isEmpty) return false;
return results.any((r) => r != ConnectivityResult.none);
}
Future<void> _init() async {
if (_started) return;
_started = true;
try {
final current = await _connectivity.checkConnectivity();
_apply(current);
_apply(current, forceNotify: true);
} catch (_) {
// Keep optimistic online; stream/resume will correct.
_online = true;
notifyListeners();
}
_sub = _connectivity.onConnectivityChanged.listen(_apply);
_sub = _connectivity.onConnectivityChanged.listen((results) {
_apply(results);
});
}
void _apply(List<ConnectivityResult> results) {
final next = results.any((r) => r != ConnectivityResult.none);
if (next == _online) return;
void _apply(List<ConnectivityResult> results, {bool forceNotify = false}) {
final next = onlineFromResults(results);
if (!forceNotify && next == _online) return;
_online = next;
notifyListeners();
}
/// Force a one-shot re-check (e.g. after a failed Dio call).
/// Force a one-shot re-check (e.g. after app resume or a failed Dio call).
Future<void> refresh() async {
try {
final current = await _connectivity.checkConnectivity();
@ -45,8 +59,16 @@ class ConnectivityController extends ChangeNotifier {
} catch (_) {}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
unawaited(refresh());
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_sub?.cancel();
super.dispose();
}

View File

@ -58,7 +58,6 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
bool _gamesLoading = true;
bool _activityLoading = true;
bool _withdrawalsLoading = true;
bool _adsRetrying = false;
bool _gamesRetrying = false;
bool _activityRetrying = false;
bool _withdrawalsRetrying = false;
@ -181,7 +180,6 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
setState(() {
_adsLoading = true;
_adsError = null;
_adsRetrying = force;
});
}
try {
@ -194,13 +192,11 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
_heroAds = ads;
_adsLoading = false;
_adsError = null;
_adsRetrying = false;
});
} catch (_) {
if (!mounted) return;
setState(() {
_adsLoading = false;
_adsRetrying = false;
if (_heroAds.isEmpty) {
_adsError = AppException.connectionFailedMessage;
}
@ -369,123 +365,119 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
final isDark = Theme.of(context).brightness == Brightness.dark;
final bottomInset = MediaQuery.paddingOf(context).bottom;
// SafeArea keeps OfflineBanner below the status bar (same pattern as AppShell).
return Scaffold(
key: const Key('welcome_screen'),
body: Column(
children: [
OfflineBanner(controller: sl<ConnectivityController>()),
Expanded(
child: RefreshIndicator(
onRefresh: () => _load(force: true),
child: CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
slivers: [
SliverAppBar(
pinned: true,
floating: false,
centerTitle: false,
expandedHeight: 0,
backgroundColor: Colors.transparent,
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF059669),
Color(0xFF047857),
Color(0xFF0F172A),
],
),
),
),
title: Row(
children: [
const PrediqoLogo(size: 28, showWordmark: false),
const SizedBox(width: 10),
Text(
s.appName,
style: const TextStyle(
fontWeight: FontWeight.w900,
letterSpacing: 0.4,
color: Colors.white,
body: SafeArea(
bottom: false,
child: Column(
children: [
OfflineBanner(controller: sl<ConnectivityController>()),
Expanded(
child: RefreshIndicator(
onRefresh: () => _load(force: true),
child: CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
slivers: [
SliverAppBar(
pinned: true,
floating: false,
centerTitle: false,
expandedHeight: 0,
backgroundColor: Colors.transparent,
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF059669),
Color(0xFF047857),
Color(0xFF0F172A),
],
),
),
),
title: Row(
children: [
const PrediqoLogo(size: 28, showWordmark: false),
const SizedBox(width: 10),
Text(
s.appName,
style: const TextStyle(
fontWeight: FontWeight.w900,
letterSpacing: 0.4,
color: Colors.white,
),
),
],
),
actions: [
const LanguageToggleButton(),
IconButton(
key: const Key('welcome_app_update'),
tooltip: s.appUpdate,
onPressed: () => handleAppUpdateTap(
context,
updateAvailable: _updateAvailable,
),
icon: Badge(
isLabelVisible: _updateAvailable,
smallSize: 8,
backgroundColor: AppColors.warning,
child: const Icon(
Icons.system_update_alt_rounded,
color: Colors.white,
),
),
),
const SizedBox(width: 4),
],
),
actions: [
const LanguageToggleButton(),
IconButton(
key: const Key('welcome_app_update'),
tooltip: s.appUpdate,
onPressed: () => handleAppUpdateTap(
context,
updateAvailable: _updateAvailable,
),
icon: Badge(
isLabelVisible: _updateAvailable,
smallSize: 8,
backgroundColor: AppColors.warning,
child: const Icon(
Icons.system_update_alt_rounded,
color: Colors.white,
),
),
),
const SizedBox(width: 4),
],
),
SliverPadding(
padding: EdgeInsets.fromLTRB(20, 12, 20, 24 + bottomInset),
sliver: SliverList(
delegate: SliverChildListDelegate([
// Hero
SliverPadding(
padding: EdgeInsets.fromLTRB(20, 12, 20, 24 + bottomInset),
sliver: SliverList(
delegate: SliverChildListDelegate([
// Get Started hero — logo only in AppBar (no duplicate).
Container(
key: const Key('welcome_get_started_card'),
width: double.infinity,
padding: const EdgeInsets.all(22),
padding: const EdgeInsets.fromLTRB(22, 24, 22, 22),
decoration: BoxDecoration(
gradient: AppColors.heroGradient(isDark),
borderRadius: BorderRadius.circular(24),
borderRadius: BorderRadius.circular(22),
border: Border.all(
color: AppColors.accentGold.withValues(alpha: 0.35),
color: isDark
? AppColors.primary.withValues(alpha: 0.28)
: AppColors.primary.withValues(alpha: 0.22),
),
boxShadow: [
BoxShadow(
color: AppColors.primary.withValues(alpha: 0.18),
blurRadius: 24,
offset: const Offset(0, 10),
color: AppColors.primary.withValues(
alpha: isDark ? 0.16 : 0.10,
),
blurRadius: 28,
offset: const Offset(0, 12),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const PrediqoLogo(size: 44, showWordmark: false),
const SizedBox(height: 10),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 10,
vertical: 4,
),
decoration: BoxDecoration(
color: AppColors.accentGold.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: AppColors.accentGold.withValues(alpha: 0.5),
),
),
child: Text(
s.tagline,
style: const TextStyle(
color: AppColors.accentGold,
fontSize: 11,
fontWeight: FontWeight.w700,
),
Text(
s.tagline.toUpperCase(),
style: TextStyle(
color: isDark
? AppColors.accentGold
: AppColors.primaryDark,
fontSize: 11,
fontWeight: FontWeight.w800,
letterSpacing: 1.1,
),
),
const SizedBox(height: 16),
const SizedBox(height: 14),
Text(
s.welcomeHeroTitle,
style: Theme.of(context)
@ -493,18 +485,27 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
.headlineSmall
?.copyWith(
fontWeight: FontWeight.w900,
height: 1.15,
height: 1.18,
letterSpacing: -0.3,
color: isDark
? AppColors.textPrimary
: const Color(0xFF0F172A),
),
),
const SizedBox(height: 10),
const SizedBox(height: 12),
Text(
s.welcomeHeroSubtitle,
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(color: AppColors.textSecondary),
?.copyWith(
height: 1.45,
color: isDark
? AppColors.textSecondary
: const Color(0xFF475569),
),
),
const SizedBox(height: 20),
const SizedBox(height: 24),
PredPrimaryButton(
key: const Key('welcome_get_started'),
label: s.welcomeGetStarted,
@ -671,6 +672,7 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
),
),
],
),
),
);
}

View File

@ -3,7 +3,8 @@ import '../../core/network/connectivity_controller.dart';
import '../../core/theme/app_colors.dart';
import '../../l10n/app_strings.dart';
/// Compact strip shown under the shell chrome when the device is offline.
/// Compact strip shown under chrome when the device reports no connectivity.
/// Stays visible for the whole offline period; collapses when back online.
class OfflineBanner extends StatelessWidget {
const OfflineBanner({super.key, required this.controller});
@ -17,19 +18,21 @@ class OfflineBanner extends StatelessWidget {
if (controller.isOnline) return const SizedBox.shrink();
final s = context.strings;
return Material(
color: AppColors.warning.withValues(alpha: 0.18),
key: const Key('offline_banner'),
color: AppColors.warning.withValues(alpha: 0.22),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
child: Row(
children: [
const Icon(Icons.cloud_off_rounded, size: 18, color: AppColors.warning),
const SizedBox(width: 8),
const Icon(Icons.cloud_off_rounded, size: 20, color: AppColors.warning),
const SizedBox(width: 10),
Expanded(
child: Text(
s.offlineBanner,
style: Theme.of(context).textTheme.labelMedium?.copyWith(
color: AppColors.warning,
fontWeight: FontWeight.w700,
fontWeight: FontWeight.w800,
height: 1.25,
),
),
),

View File

@ -0,0 +1,30 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:prediqo_mobile/core/network/connectivity_controller.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
void main() {
test('onlineFromResults treats empty as offline', () {
expect(ConnectivityController.onlineFromResults(const []), isFalse);
});
test('onlineFromResults treats none as offline', () {
expect(
ConnectivityController.onlineFromResults(const [ConnectivityResult.none]),
isFalse,
);
});
test('onlineFromResults treats wifi/mobile as online', () {
expect(
ConnectivityController.onlineFromResults(const [ConnectivityResult.wifi]),
isTrue,
);
expect(
ConnectivityController.onlineFromResults(const [
ConnectivityResult.none,
ConnectivityResult.mobile,
]),
isTrue,
);
});
}