diff --git a/lib/main.dart b/lib/main.dart index 9aec6c2..3948322 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -131,10 +131,25 @@ class _DidvanState extends State with WidgetsBindingObserver { void _navigateTo(Uri uri) { if (mounted) { String path = uri.path; - if (uri.fragment.isNotEmpty) { - path = "/${uri.fragment}"; + print("path: $path, uri: $uri"); + + if (path.contains("news")) { + final regex = RegExp(r'^/news/(\d+)$'); + final match = regex.firstMatch(path); + + if (match != null) { + final id = match.group(1); // This will be '1234' as a string + print('Extracted ID: $id'); + navigatorKey.currentState?.pushNamed(Routes.newsDetails, arguments: { 'id': int.parse(id!) ,'hasUnmarkConfirmation':true, 'isForward': true }); + } else { + print('No match found'); + } + + + } else { + print("no fragments identified. opening home path, uri: $uri"); + navigatorKey.currentState?.pushNamed(path); } - navigatorKey.currentState?.pushNamed(path); } } diff --git a/lib/views/news/news_details/news_details_state.dart b/lib/views/news/news_details/news_details_state.dart index de02437..83300bc 100644 --- a/lib/views/news/news_details/news_details_state.dart +++ b/lib/views/news/news_details/news_details_state.dart @@ -25,16 +25,20 @@ class NewsDetailsState extends CoreProvier { NewsDetailsData get currentNews => news[_currentIndex]!; Future getNewsDetails(int id, {bool? isForward}) async { - if (isForward == null) { - appState = AppState.busy; - } else { + + print("isForward: $isForward"); + // if (isForward == null) { + // appState = AppState.busy; + // } else { isFetchingNewItem = true; notifyListeners(); - } + // } final service = RequestService(RequestHelper.newsDetails(id, args)); await service.httpGet(); + print("making request to news endpoint $id"); _handleTracking(sendRequest: isForward != null); if (service.isSuccess) { + print("success in receiving news detail for id $id"); final result = service.result; final newsItem = NewsDetailsData.fromJson(result['news']); if (args.page == 0) { @@ -79,9 +83,10 @@ class NewsDetailsState extends CoreProvier { appState = AppState.idle; return; } - if (isForward == null) { - appState = AppState.failed; - } + print("failed to receive news detail for id $id"); + // if (isForward == null) { + // appState = AppState.failed; + // } } bool exists(NewsDetailsData? newsItem) => diff --git a/lib/views/splash/splash.dart b/lib/views/splash/splash.dart index d6e7aa8..8a45489 100644 --- a/lib/views/splash/splash.dart +++ b/lib/views/splash/splash.dart @@ -87,7 +87,9 @@ class _SplashState extends State { Future _initialize(ThemeProvider themeProvider, UserProvider userProvider, MediaProvider mediaProvider) async { try { + print("checking if is in browser"); if (kIsWeb) { + print("detected browser"); html.window.onBeforeUnload.listen((event) { StorageService.webStorage .removeWhere((key, value) => key == 'image-cache'); @@ -103,20 +105,25 @@ class _SplashState extends State { await AppInitializer.setupServices(navigatorKey.currentContext!); final String? token = await userProvider.setAndGetToken(); + print("detected token as $token"); if (token != null) { + print("detected token"); if (!kIsWeb) { await mediaProvider.getDownloadsList(); } RequestService.token = token; + print("fetching user info..."); final result = await userProvider.getUserInfo(); if (!result) { + print("no results were returned for user info"); try { StorageService.delete(key: 'token'); } catch (e) { + print("error in case of no user info result: $e"); // catch } @@ -127,29 +134,53 @@ class _SplashState extends State { return; } - + print("got results for user info: $result"); await ServerDataProvider.getData(); } - - // --- بخش اصلاح شده --- - // ابتدا بررسی می‌کنیم که کاربر باید به کدام صفحه اصلی برود - final String destinationRoute = token == null ? Routes.authenticaion : Routes.home; - final dynamic routeArguments = token == null ? false : {'showDialogs': true}; - // اگر لینک ورودی وجود داشت، آن را به عنوان آرگومان به صفحه Home می‌فرستیم - if (destinationRoute == Routes.home && initialURI != null) { - (routeArguments as Map)['deepLinkUri'] = initialURI; - initialURI = null; // لینک را مصرف می‌کنیم تا دوباره استفاده نشود + // --- بخش اصلاح شده --- + try { + // Determine destination and arguments based on authentication status + if (token == null) { + // User is not authenticated, go to the authentication screen + print("User not authenticated. Navigating to authentication."); + await navigatorKey.currentState!.pushReplacementNamed( + Routes.authenticaion, + arguments: false, // Pass the expected boolean argument + ); + } else { + // User is authenticated, go to the home screen + print("User authenticated. Navigating to home."); + + // Prepare arguments for the home screen + final Map routeArguments = {'showDialogs': true}; + + // If a deep link exists, add it to the arguments + if (initialURI != null) { + print("Deep link found: $initialURI. Adding to arguments."); + routeArguments['deepLinkUri'] = initialURI; + initialURI = null; // Consume the link so it's not used again + } + + // Navigate to the home screen with the correct arguments + await navigatorKey.currentState!.pushReplacementNamed( + Routes.home, + arguments: routeArguments, + ); + } + // --- پایان بخش اصلاح شده --- + + } catch (e) { + print("An error occurred during initialization: $e"); + if (mounted) { + setState(() { + _errorOccured = true; + }); + } } - // در نهایت به مسیر تعیین شده می‌رویم - await navigatorKey.currentState!.pushReplacementNamed( - destinationRoute, - arguments: routeArguments, - ); - // --- پایان بخش اصلاح شده --- - } catch (e) { + print("error in splash screen: $e"); setState(() { _errorOccured = true; });