games_app/prediqo_mobile/lib/features/home/utils/home_navigation.dart
2026-07-25 22:48:54 +02:00

82 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:url_launcher/url_launcher.dart';
/// Maps advertisement / catalog deep links (web or mobile) to in-app routes or external URLs.
Future<void> openHomeLink(BuildContext context, String? linkUrl) async {
if (linkUrl == null || linkUrl.trim().isEmpty) return;
final link = linkUrl.trim();
if (link.startsWith('http://') || link.startsWith('https://')) {
final uri = Uri.tryParse(link);
if (uri == null) return;
await launchUrl(uri, mode: LaunchMode.externalApplication);
return;
}
final mapped = mapWebPathToMobileRoute(link);
if (mapped == null) {
if (context.mounted) context.go('/home');
return;
}
if (mapped.useGo) {
context.go(mapped.path);
} else {
context.push(mapped.path);
}
}
/// Resolves a catalog/web path to a known mobile route.
({String path, bool useGo})? mapWebPathToMobileRoute(String raw) {
var path = raw.trim();
if (!path.startsWith('/')) path = '/$path';
// Strip query for matching; re-attach below when needed.
final uri = Uri.parse(path);
final p = uri.path;
if (p.contains('candle-predict') || p.endsWith('/candle-predict')) {
return (path: '/predict/candle-predict', useGo: true);
}
if (p.startsWith('/games/') || p == '/predict') {
return (path: '/predict', useGo: true);
}
if (p == '/wallet' || p.startsWith('/wallet/')) {
return (path: '/wallet', useGo: true);
}
if (p.startsWith('/deposit')) {
return (path: p, useGo: false);
}
// Web uses /profile; mobile account hub is /account.
if (p == '/profile' || p.startsWith('/profile/')) {
return (path: '/account', useGo: true);
}
if (p == '/account' || p.startsWith('/account/')) {
return (path: p == '/account' ? '/account' : p, useGo: p == '/account');
}
if (p.startsWith('/referral')) {
return (path: p, useGo: false);
}
if (p.startsWith('/faq')) {
final q = uri.hasQuery ? '?${uri.query}' : '';
return (path: '/faq$q', useGo: false);
}
if (p.startsWith('/transfer') || p.startsWith('/withdraw') || p == '/notifications') {
return (path: p, useGo: false);
}
if (p == '/home' || p == '/') {
return (path: '/home', useGo: true);
}
return null;
}
Color parseHexColor(String? raw, {Color fallback = const Color(0xFF10B981)}) {
if (raw == null || raw.isEmpty) return fallback;
var hex = raw.trim();
if (hex.startsWith('#')) hex = hex.substring(1);
if (hex.length == 6) hex = 'FF$hex';
final value = int.tryParse(hex, radix: 16);
if (value == null) return fallback;
return Color(value);
}