After correcting the chat issue5

This commit is contained in:
Your Name 2026-09-24 17:14:05 +02:00
parent 6512d4cf6f
commit cec19ac03d
4 changed files with 162 additions and 63 deletions

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import '../../../core/di/injection.dart';
import '../../../core/errors/app_exception.dart';
@ -20,6 +21,7 @@ import '../../../shared/widgets/language_toggle_button.dart';
import '../../../shared/widgets/pred_card.dart';
import '../../../shared/widgets/prediqo_logo.dart';
import '../utils/home_navigation.dart';
import '../../prediction/bloc/prediction_bloc.dart';
import '../../prediction/utils/game_catalog_ui.dart';
import 'widgets/home_ad_carousel.dart';
@ -45,6 +47,10 @@ class _HomeScreenState extends State<HomeScreen> {
super.initState();
_load();
_checkAppUpdate();
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
context.read<PredictionBloc>().add(const PredictionLoadRequested(ifStale: true));
});
}
Future<void> _checkAppUpdate() async {

View File

@ -178,7 +178,23 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
Future<void> _onResumed(PredictionResumed event, Emitter<PredictionState> emit) async {
_screenActive = true;
add(const PredictionLoadRequested());
add(const PredictionLoadRequested(ifStale: true));
}
static const _freshSyncMs = 5000;
void _ensureRealtime([int? intervalMinutes]) {
if (!_socket.isEnabled) return;
unawaited(_socket.connect());
_socket.subscribe(intervalMinutes ?? state.selectedIntervalMinutes);
}
Future<T?> _soft<T>(Future<T> future) async {
try {
return await future;
} catch (_) {
return null;
}
}
/// True root fix for "countdown stuck until I leave and reopen the page":
@ -312,80 +328,83 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
}
Future<void> _onLoad(PredictionLoadRequested event, Emitter<PredictionState> emit) async {
emit(state.copyWith(status: PredictionStatus.loading, clearError: true));
final nowMs = DateTime.now().millisecondsSinceEpoch;
if (event.ifStale &&
state.gameState != null &&
state.candles.isNotEmpty &&
nowMs - _lastFullStateSyncMs < _freshSyncMs) {
_ensureRealtime(state.selectedIntervalMinutes);
_startPolling();
return;
}
final keepExistingUi = state.gameState != null;
if (!keepExistingUi) {
emit(state.copyWith(status: PredictionStatus.loading, clearError: true));
} else {
emit(state.copyWith(clearError: true));
}
_ensureRealtime(state.selectedIntervalMinutes);
try {
if (_socket.isEnabled) {
await _socket.connect();
}
final interval = state.selectedIntervalMinutes;
final gameState = await _repo.getState(intervalMinutes: interval);
final stateFuture = _repo.getState(intervalMinutes: interval);
final historyFuture = _soft(_fetchHistoryPage(1));
final balancesFuture = _soft(_loadBalances(state.gameState?.config.walletTypeCode));
final gameState = await stateFuture;
_ensureRealtime(gameState.selectedIntervalMinutes);
if (state.gameState == null) {
emit(_readyFromGameState(
gameState,
candles: state.candles,
isLoadingCandles: state.candles.isEmpty,
));
}
final pageSize = gameState.interval.chartCandleLimit.clamp(20, 200);
final candlesPage = await _repo.getCandlesPage(
final candlesFuture = _repo.getCandlesPage(
intervalMinutes: gameState.selectedIntervalMinutes,
limit: pageSize,
);
final history = await _fetchHistoryPage(1);
final labels = await _loadBalances(gameState.config.walletTypeCode);
if (_socket.isEnabled) {
await _socket.connect();
if (_socket.isConnected) {
_socket.subscribe(gameState.selectedIntervalMinutes);
}
}
final historyItems = gameState.userMultiBet != null
? _historyWithMultiProgress(history.items, gameState.userMultiBet!)
: history.items;
// Prefer wallet of active multi; else auto wallet while enabled; else main.
final auto = gameState.autoSettings;
final multi = gameState.userMultiBet;
final stakeKind = multi != null
? _parseStakeKind(multi.stakeAccountKind)
: (auto?.enabled == true)
? _parseStakeKind(auto!.stakeAccountKind)
: StakeAccountKind.main;
final nextStake = state.status == PredictionStatus.initial
? (auto?.enabled == true && auto!.stakeDisplay > 0
? auto.stakeDisplay
: _defaultStake(gameState.config))
: state.selectedStake;
emit(state.copyWith(
status: PredictionStatus.ready,
gameState: gameState,
final candlesPage = await candlesFuture;
emit(_readyFromGameState(
gameState,
candles: candlesPage.items,
hasMoreOlderCandles: candlesPage.hasMore,
isLoadingOlderCandles: false,
history: historyItems,
historyPage: history.page,
historyTotalPages: history.totalPages,
selectedIntervalMinutes: gameState.selectedIntervalMinutes,
selectedStake: nextStake,
multiSequence: _initialMultiSequence(gameState),
selectedMultiLevel: gameState.multiLevels.isNotEmpty
? gameState.multiLevels.first
: state.selectedMultiLevel,
playMode: gameState.userMultiBet != null
? PredictionPlayMode.multi
: (auto?.enabled == true ? PredictionPlayMode.auto : state.playMode),
stakeAccountKind: stakeKind,
mainBalanceLabel: labels.mainLabel,
bonusBalanceLabel: labels.bonusLabel,
testBalanceLabel: labels.testLabel,
mainAvailable: labels.main,
bonusAvailable: labels.bonus,
testAvailable: labels.test,
wsConnected: _socket.isConnected,
realtimeStatus: _socket.isConnected
? PredictionRealtimeStatus.live
: (_socket.isEnabled ? PredictionRealtimeStatus.syncing : PredictionRealtimeStatus.polling),
hasMoreOlder: candlesPage.hasMore,
isLoadingCandles: false,
));
final history = await historyFuture;
final labels = await balancesFuture;
final historyItems = history == null
? state.history
: (gameState.userMultiBet != null
? _historyWithMultiProgress(history.items, gameState.userMultiBet!)
: history.items);
emit(_readyFromGameState(
gameState,
candles: candlesPage.items,
hasMoreOlder: candlesPage.hasMore,
isLoadingCandles: false,
history: historyItems,
historyPage: history?.page,
historyTotalPages: history?.totalPages,
labels: labels,
));
_lastFullStateSyncMs = DateTime.now().millisecondsSinceEpoch;
_healthyPollSkips = 0;
await _fillHistoryUntilFilterMatches(emit);
_startPolling();
} catch (e) {
emit(state.copyWith(
status: PredictionStatus.error,
errorMessage: e.toString(),
status: keepExistingUi ? PredictionStatus.ready : PredictionStatus.error,
errorMessage: keepExistingUi ? state.errorMessage : e.toString(),
wsConnected: _socket.isConnected,
realtimeStatus: _socket.isEnabled
? PredictionRealtimeStatus.polling
@ -394,6 +413,67 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
}
}
PredictionState _readyFromGameState(
GameStateModel gameState, {
required List<OhlcCandleModel> candles,
bool hasMoreOlder = true,
bool isLoadingCandles = false,
List<PredictionModel>? history,
int? historyPage,
int? historyTotalPages,
({
String mainLabel,
String bonusLabel,
String testLabel,
double main,
double bonus,
double test,
})? labels,
}) {
final auto = gameState.autoSettings;
final multi = gameState.userMultiBet;
final stakeKind = multi != null
? _parseStakeKind(multi.stakeAccountKind)
: (auto?.enabled == true)
? _parseStakeKind(auto!.stakeAccountKind)
: state.stakeAccountKind;
final nextStake = state.status == PredictionStatus.initial
? (auto?.enabled == true && auto!.stakeDisplay > 0
? auto.stakeDisplay
: _defaultStake(gameState.config))
: state.selectedStake;
return state.copyWith(
status: PredictionStatus.ready,
gameState: gameState,
candles: candles,
hasMoreOlderCandles: hasMoreOlder,
isLoadingOlderCandles: isLoadingCandles,
history: history,
historyPage: historyPage,
historyTotalPages: historyTotalPages,
selectedIntervalMinutes: gameState.selectedIntervalMinutes,
selectedStake: nextStake,
multiSequence: _initialMultiSequence(gameState),
selectedMultiLevel: gameState.multiLevels.isNotEmpty
? gameState.multiLevels.first
: state.selectedMultiLevel,
playMode: gameState.userMultiBet != null
? PredictionPlayMode.multi
: (auto?.enabled == true ? PredictionPlayMode.auto : state.playMode),
stakeAccountKind: stakeKind,
mainBalanceLabel: labels?.mainLabel,
bonusBalanceLabel: labels?.bonusLabel,
testBalanceLabel: labels?.testLabel,
mainAvailable: labels?.main,
bonusAvailable: labels?.bonus,
testAvailable: labels?.test,
wsConnected: _socket.isConnected,
realtimeStatus: _socket.isConnected
? PredictionRealtimeStatus.live
: (_socket.isEnabled ? PredictionRealtimeStatus.syncing : PredictionRealtimeStatus.polling),
);
}
Future<void> _onIntervalChanged(PredictionIntervalChanged event, Emitter<PredictionState> emit) async {
final prevInterval = state.selectedIntervalMinutes;
final gs = state.gameState;
@ -401,7 +481,7 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
selectedIntervalMinutes: event.minutes,
candles: const [],
hasMoreOlderCandles: true,
isLoadingOlderCandles: false,
isLoadingOlderCandles: true,
gameState: gs?.copyWith(
selectedIntervalMinutes: event.minutes,
clearRound: true,

View File

@ -7,7 +7,14 @@ abstract class PredictionEvent extends Equatable {
}
class PredictionLoadRequested extends PredictionEvent {
const PredictionLoadRequested();
const PredictionLoadRequested({this.ifStale = false});
/// Skip the network round-trip when a full sync finished a few seconds ago.
/// Used for home prefetch + opening the screen right after.
final bool ifStale;
@override
List<Object?> get props => [ifStale];
}
class PredictionIntervalChanged extends PredictionEvent {

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import '../../../core/di/injection.dart';
import '../../../core/theme/app_colors.dart';
@ -9,6 +10,7 @@ import '../../../l10n/app_strings.dart';
import '../../../shared/widgets/app_snackbar.dart';
import '../../../shared/widgets/pred_card.dart';
import '../../home/utils/home_navigation.dart';
import '../bloc/prediction_bloc.dart';
import '../utils/game_catalog_ui.dart';
/// Catalog of prediction games — entry point for the Predict tab.
@ -28,6 +30,10 @@ class _PredictionGamesScreenState extends State<PredictionGamesScreen> {
void initState() {
super.initState();
_load();
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
context.read<PredictionBloc>().add(const PredictionLoadRequested(ifStale: true));
});
}
Future<void> _load() async {