diff --git a/prediqo_mobile/ios/Runner/Info.plist b/prediqo_mobile/ios/Runner/Info.plist
index 4393e9d3..88c581ba 100644
--- a/prediqo_mobile/ios/Runner/Info.plist
+++ b/prediqo_mobile/ios/Runner/Info.plist
@@ -45,6 +45,10 @@
UIApplicationSupportsIndirectInputEvents
+ NSPhotoLibraryUsageDescription
+ Tloto uses your photo library to send images to support.
+ NSCameraUsageDescription
+ Tloto uses the camera to send photos to support.
UIStatusBarHidden
diff --git a/prediqo_mobile/lib/app.dart b/prediqo_mobile/lib/app.dart
index 59d2138c..0ae18665 100644
--- a/prediqo_mobile/lib/app.dart
+++ b/prediqo_mobile/lib/app.dart
@@ -13,8 +13,11 @@ import 'domain/repositories/health_repository.dart';
import 'domain/repositories/prediction_repository.dart';
import 'domain/repositories/referral_repository.dart';
import 'domain/repositories/repositories.dart';
+import 'data/repositories/support_repository.dart';
import 'features/auth/bloc/auth_bloc.dart';
import 'features/prediction/bloc/prediction_bloc.dart';
+import 'features/support/bloc/support_bloc.dart';
+import 'features/support/presentation/support_widget.dart';
import 'features/wallet/bloc/wallet_bloc.dart';
import 'l10n/app_locale.dart';
import 'l10n/locale_controller.dart';
@@ -28,6 +31,7 @@ class PrediqoApp extends StatefulWidget {
class _PrediqoAppState extends State {
late final AuthBloc _authBloc;
+ late final SupportBloc _supportBloc;
late final GoRouter _router;
late final LocaleController _localeController;
@@ -36,6 +40,7 @@ class _PrediqoAppState extends State {
super.initState();
_localeController = sl();
_authBloc = sl();
+ _supportBloc = sl();
sl().onSessionExpired = () {
if (!_authBloc.isClosed) {
_authBloc.add(const AuthSessionExpired());
@@ -47,6 +52,7 @@ class _PrediqoAppState extends State {
@override
void dispose() {
sl().onSessionExpired = null;
+ _supportBloc.close();
_authBloc.close();
super.dispose();
}
@@ -63,10 +69,12 @@ class _PrediqoAppState extends State {
RepositoryProvider.value(value: sl()),
RepositoryProvider.value(value: sl()),
RepositoryProvider.value(value: sl()),
+ RepositoryProvider.value(value: sl()),
],
child: MultiBlocProvider(
providers: [
BlocProvider.value(value: _authBloc),
+ BlocProvider.value(value: _supportBloc),
BlocProvider(create: (_) => sl()),
BlocProvider(create: (_) => sl()),
],
@@ -87,6 +95,9 @@ class _PrediqoAppState extends State {
GlobalCupertinoLocalizations.delegate,
],
routerConfig: _router,
+ builder: (context, child) {
+ return _SupportRouteLayer(router: _router, child: child);
+ },
);
},
),
@@ -94,3 +105,47 @@ class _PrediqoAppState extends State {
);
}
}
+
+/// Rebuilds the support overlay only when the route path actually changes.
+class _SupportRouteLayer extends StatefulWidget {
+ const _SupportRouteLayer({required this.router, this.child});
+
+ final GoRouter router;
+ final Widget? child;
+
+ @override
+ State<_SupportRouteLayer> createState() => _SupportRouteLayerState();
+}
+
+class _SupportRouteLayerState extends State<_SupportRouteLayer> {
+ late String _path;
+
+ @override
+ void initState() {
+ super.initState();
+ _path = widget.router.routerDelegate.currentConfiguration.uri.path;
+ widget.router.routerDelegate.addListener(_onRoute);
+ }
+
+ @override
+ void dispose() {
+ widget.router.routerDelegate.removeListener(_onRoute);
+ super.dispose();
+ }
+
+ void _onRoute() {
+ final path = widget.router.routerDelegate.currentConfiguration.uri.path;
+ if (path == _path) return;
+ setState(() => _path = path);
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Stack(
+ children: [
+ widget.child ?? const SizedBox.shrink(),
+ SupportOverlay(currentPath: _path),
+ ],
+ );
+ }
+}
diff --git a/prediqo_mobile/lib/core/config/app_config.dart b/prediqo_mobile/lib/core/config/app_config.dart
index 3a4a677d..85df5fa2 100644
--- a/prediqo_mobile/lib/core/config/app_config.dart
+++ b/prediqo_mobile/lib/core/config/app_config.dart
@@ -27,6 +27,31 @@ class AppConfig {
/// Defaults to the same host/port as [apiBaseUrl] with path `/ws/...`
/// (Docker/nginx proxies `/ws/` on 9080). Override with `--dart-define=WS_URL=...`
/// or `--dart-define=WS_PORT=4001` for a dedicated WS port.
+ /// WebSocket URL for player support chat (`/ws/v1/support`).
+ String get resolvedSupportWsUrl {
+ if (wsUrl != null && wsUrl!.isNotEmpty) {
+ final game = Uri.parse(wsUrl!);
+ return game.replace(path: '/ws/v1/support', query: '').toString();
+ }
+ final api = Uri.parse(apiBaseUrl);
+ final wsScheme = api.scheme == 'https' ? 'wss' : 'ws';
+ const wsPortOverride = String.fromEnvironment('WS_PORT', defaultValue: '');
+ final int? port;
+ if (wsPortOverride.isNotEmpty) {
+ port = int.tryParse(wsPortOverride);
+ } else if (api.hasPort) {
+ port = api.port;
+ } else {
+ port = null;
+ }
+ return Uri(
+ scheme: wsScheme,
+ host: api.host,
+ port: port,
+ path: '/ws/v1/support',
+ ).toString();
+ }
+
String get resolvedWsUrl {
if (wsUrl != null && wsUrl!.isNotEmpty) return wsUrl!;
final api = Uri.parse(apiBaseUrl);
diff --git a/prediqo_mobile/lib/core/di/injection.dart b/prediqo_mobile/lib/core/di/injection.dart
index 06cbb4f6..30179572 100644
--- a/prediqo_mobile/lib/core/di/injection.dart
+++ b/prediqo_mobile/lib/core/di/injection.dart
@@ -21,9 +21,12 @@ import '../../domain/repositories/repositories.dart';
import '../../features/auth/bloc/auth_bloc.dart';
import '../../features/prediction/bloc/prediction_bloc.dart';
import '../../features/wallet/bloc/wallet_bloc.dart';
+import '../../data/repositories/support_repository.dart';
+import '../../features/support/bloc/support_bloc.dart';
import '../config/app_config.dart';
import '../network/api_client.dart';
import '../realtime/game_socket_service.dart';
+import '../realtime/support_socket_service.dart';
import '../../l10n/locale_controller.dart';
final sl = GetIt.instance;
@@ -45,6 +48,14 @@ Future configureDependencies({AppConfig? config}) async {
if (!sl.isRegistered()) {
sl.registerLazySingleton(() => GameSocketService(sl(), sl()));
}
+ if (!sl.isRegistered()) {
+ sl.registerLazySingleton(() => SupportRepository(sl()));
+ }
+ if (!sl.isRegistered()) {
+ sl.registerLazySingleton(
+ () => SupportSocketService(sl(), sl(), sl()),
+ );
+ }
if (!sl.isRegistered()) {
sl.registerLazySingleton(() => AuthRepositoryImpl(sl()));
}
@@ -93,4 +104,7 @@ Future configureDependencies({AppConfig? config}) async {
if (!sl.isRegistered()) {
sl.registerFactory(() => PredictionBloc(sl(), sl(), sl()));
}
+ if (!sl.isRegistered()) {
+ sl.registerFactory(() => SupportBloc(sl(), sl()));
+ }
}
diff --git a/prediqo_mobile/lib/core/network/api_client.dart b/prediqo_mobile/lib/core/network/api_client.dart
index fe9c0727..f1bae2ca 100644
--- a/prediqo_mobile/lib/core/network/api_client.dart
+++ b/prediqo_mobile/lib/core/network/api_client.dart
@@ -121,6 +121,11 @@ class _AuthInterceptor extends Interceptor {
return handler.next(err);
}
}
+ final refreshToken = await _client.readRefreshToken();
+ final isSupport = path.contains('/support/') || uriPath.contains('/support/');
+ if (isSupport && (refreshToken == null || refreshToken.isEmpty)) {
+ return handler.next(err);
+ }
await _expireSession();
}
handler.next(err);
diff --git a/prediqo_mobile/lib/core/realtime/support_socket_service.dart b/prediqo_mobile/lib/core/realtime/support_socket_service.dart
new file mode 100644
index 00000000..680a9aeb
--- /dev/null
+++ b/prediqo_mobile/lib/core/realtime/support_socket_service.dart
@@ -0,0 +1,137 @@
+import 'dart:async';
+import 'dart:convert';
+import 'package:web_socket_channel/web_socket_channel.dart';
+import '../../data/repositories/support_repository.dart';
+import '../config/app_config.dart';
+import '../network/api_client.dart';
+
+typedef SupportSocketListener = void Function(Map message);
+
+/// WebSocket client for `/ws/v1/support` (mirrors web SupportSocketClient).
+class SupportSocketService {
+ SupportSocketService(this._config, this._api, this._repo);
+
+ final AppConfig _config;
+ final ApiClient _api;
+ final SupportRepository _repo;
+
+ WebSocketChannel? _channel;
+ StreamSubscription? _sub;
+ Timer? _heartbeat;
+ Timer? _reconnectTimer;
+ final _listeners = {};
+ String? _subscribedConversationId;
+ bool _connecting = false;
+ int _reconnectAttempt = 0;
+
+ bool get isConnected => _channel != null;
+ bool get isEnabled => _config.wsEnabled;
+
+ void addListener(SupportSocketListener listener) => _listeners.add(listener);
+ void removeListener(SupportSocketListener listener) => _listeners.remove(listener);
+
+ Future connect() async {
+ if (!isEnabled || _channel != null || _connecting) return;
+ _connecting = true;
+ try {
+ final token = await _api.readAccessToken();
+ final visitor = await _repo.visitorToken();
+ final uri = Uri.parse(_config.resolvedSupportWsUrl);
+ final url = uri.replace(queryParameters: {
+ ...uri.queryParameters,
+ if (token != null && token.isNotEmpty) 'token': token,
+ 'visitorToken': visitor,
+ }).toString();
+ final channel = WebSocketChannel.connect(Uri.parse(url));
+ await channel.ready.timeout(const Duration(seconds: 8));
+ _channel = channel;
+ _sub = channel.stream.listen(
+ _onMessage,
+ onError: (_) => _scheduleReconnect(),
+ onDone: _scheduleReconnect,
+ cancelOnError: true,
+ );
+ _reconnectAttempt = 0;
+ _heartbeat?.cancel();
+ _heartbeat = Timer.periodic(const Duration(seconds: 30), (_) {
+ _send({'type': 'ping', 'ts': DateTime.now().millisecondsSinceEpoch});
+ });
+ final conv = _subscribedConversationId;
+ if (conv != null) subscribe(conv);
+ } catch (_) {
+ _disposeChannel();
+ _scheduleReconnect();
+ } finally {
+ _connecting = false;
+ }
+ }
+
+ void subscribe(String conversationId) {
+ _subscribedConversationId = conversationId;
+ _send({'type': 'subscribe', 'conversationId': conversationId});
+ }
+
+ void unsubscribe() {
+ final id = _subscribedConversationId;
+ if (id != null) {
+ _send({'type': 'unsubscribe', 'conversationId': id});
+ }
+ _subscribedConversationId = null;
+ }
+
+ void _onMessage(dynamic raw) {
+ try {
+ final text = raw is String
+ ? raw
+ : raw is List
+ ? utf8.decode(raw)
+ : null;
+ if (text == null) return;
+ final decoded = jsonDecode(text);
+ if (decoded is! Map) return;
+ final map = Map.from(decoded);
+ for (final l in _listeners) {
+ l(map);
+ }
+ } catch (_) {}
+ }
+
+ void _send(Map msg) {
+ final ch = _channel;
+ if (ch == null) return;
+ try {
+ ch.sink.add(jsonEncode(msg));
+ } catch (_) {}
+ }
+
+ void _scheduleReconnect() {
+ _disposeChannel();
+ if (!isEnabled) return;
+ _reconnectTimer?.cancel();
+ final delay = Duration(
+ milliseconds: (1000 * (1 << _reconnectAttempt.clamp(0, 5))).clamp(1000, 15000),
+ );
+ _reconnectAttempt++;
+ _reconnectTimer = Timer(delay, () async {
+ await connect();
+ });
+ }
+
+ void _disposeChannel() {
+ _heartbeat?.cancel();
+ _heartbeat = null;
+ _sub?.cancel();
+ _sub = null;
+ try {
+ _channel?.sink.close();
+ } catch (_) {}
+ _channel = null;
+ }
+
+ Future dispose() async {
+ _reconnectTimer?.cancel();
+ unsubscribe();
+ _disposeChannel();
+ _listeners.clear();
+ }
+}
diff --git a/prediqo_mobile/lib/core/router/app_router.dart b/prediqo_mobile/lib/core/router/app_router.dart
index 3fa13d42..ce256a3e 100644
--- a/prediqo_mobile/lib/core/router/app_router.dart
+++ b/prediqo_mobile/lib/core/router/app_router.dart
@@ -33,11 +33,11 @@ import '../../features/wallet/presentation/withdraw_screen.dart';
import '../../features/welcome/presentation/welcome_screen.dart';
import '../../shared/layout/app_shell.dart';
-final _rootNavigatorKey = GlobalKey();
+final rootNavigatorKey = GlobalKey();
GoRouter createAppRouter(AuthBloc authBloc) {
return GoRouter(
- navigatorKey: _rootNavigatorKey,
+ navigatorKey: rootNavigatorKey,
initialLocation: '/',
refreshListenable: GoRouterRefreshStream(authBloc.stream),
redirect: (context, state) {
diff --git a/prediqo_mobile/lib/data/models/support_models.dart b/prediqo_mobile/lib/data/models/support_models.dart
new file mode 100644
index 00000000..e7b16804
--- /dev/null
+++ b/prediqo_mobile/lib/data/models/support_models.dart
@@ -0,0 +1,88 @@
+class SupportConversationModel {
+ const SupportConversationModel({
+ required this.id,
+ required this.status,
+ this.unreadUser = 0,
+ });
+
+ final String id;
+ final String status;
+ final int unreadUser;
+
+ factory SupportConversationModel.fromJson(Map json) {
+ return SupportConversationModel(
+ id: json['id'] as String,
+ status: (json['status'] ?? 'open').toString(),
+ unreadUser: (json['unreadUser'] as num?)?.toInt() ?? 0,
+ );
+ }
+}
+
+class SupportMessageModel {
+ const SupportMessageModel({
+ required this.id,
+ required this.conversationId,
+ required this.senderType,
+ required this.createdAt,
+ this.senderDisplayName,
+ this.body,
+ this.attachmentUrl,
+ this.attachmentName,
+ this.attachmentMime,
+ this.readAt,
+ });
+
+ final String id;
+ final String conversationId;
+ final String senderType;
+ final String createdAt;
+ final String? senderDisplayName;
+ final String? body;
+ final String? attachmentUrl;
+ final String? attachmentName;
+ final String? attachmentMime;
+ final String? readAt;
+
+ bool get isMine => senderType == 'user' || senderType == 'visitor';
+ bool get isImage => attachmentMime?.startsWith('image/') == true;
+
+ SupportMessageModel copyWith({String? readAt}) {
+ return SupportMessageModel(
+ id: id,
+ conversationId: conversationId,
+ senderType: senderType,
+ createdAt: createdAt,
+ senderDisplayName: senderDisplayName,
+ body: body,
+ attachmentUrl: attachmentUrl,
+ attachmentName: attachmentName,
+ attachmentMime: attachmentMime,
+ readAt: readAt ?? this.readAt,
+ );
+ }
+
+ factory SupportMessageModel.fromJson(Map json) {
+ return SupportMessageModel(
+ id: json['id'] as String,
+ conversationId: (json['conversationId'] ?? '').toString(),
+ senderType: (json['senderType'] ?? 'staff').toString(),
+ createdAt: (json['createdAt'] ?? '').toString(),
+ senderDisplayName: json['senderDisplayName'] as String?,
+ body: json['body'] as String?,
+ attachmentUrl: json['attachmentUrl'] as String?,
+ attachmentName: json['attachmentName'] as String?,
+ attachmentMime: json['attachmentMime'] as String?,
+ readAt: json['readAt'] as String?,
+ );
+ }
+}
+
+class SupportMessagesPage {
+ const SupportMessagesPage({
+ required this.messages,
+ required this.staffTyping,
+ });
+
+ final List messages;
+ final bool staffTyping;
+}
diff --git a/prediqo_mobile/lib/data/repositories/support_repository.dart b/prediqo_mobile/lib/data/repositories/support_repository.dart
new file mode 100644
index 00000000..d09c0f23
--- /dev/null
+++ b/prediqo_mobile/lib/data/repositories/support_repository.dart
@@ -0,0 +1,129 @@
+import 'package:dio/dio.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+import 'package:uuid/uuid.dart';
+import '../../core/errors/app_exception.dart';
+import '../../core/network/api_client.dart';
+import '../models/support_models.dart';
+
+const _visitorTokenKey = 'prediqo_support_visitor_token';
+
+class SupportRepository {
+ SupportRepository(this._api);
+
+ final ApiClient _api;
+ String? _visitorToken;
+
+ Future visitorToken() async {
+ if (_visitorToken != null && _visitorToken!.length >= 16) {
+ return _visitorToken!;
+ }
+ final prefs = await SharedPreferences.getInstance();
+ var token = prefs.getString(_visitorTokenKey);
+ if (token == null || token.length < 16) {
+ token = const Uuid().v4().replaceAll('-', '');
+ await prefs.setString(_visitorTokenKey, token);
+ }
+ _visitorToken = token;
+ return token;
+ }
+
+ Future _options() async {
+ return Options(headers: {
+ 'X-Support-Visitor-Token': await visitorToken(),
+ });
+ }
+
+ Future getOrCreateConversation() async {
+ try {
+ final res = await _api.dio.post