diff --git a/lib/modules/auth/edit/sc_edit_profile_page.dart b/lib/modules/auth/edit/sc_edit_profile_page.dart index fe942c5..e71d2ca 100644 --- a/lib/modules/auth/edit/sc_edit_profile_page.dart +++ b/lib/modules/auth/edit/sc_edit_profile_page.dart @@ -15,8 +15,10 @@ import 'package:yumi/app/constants/sc_screen.dart'; import 'package:yumi/app/routes/sc_routes.dart'; import 'package:yumi/app/routes/sc_fluro_navigator.dart'; import 'package:yumi/shared/tools/sc_date_utils.dart'; +import 'package:yumi/shared/tools/sc_google_auth_utils.dart'; import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart'; import 'package:yumi/shared/data_sources/sources/local/user_manager.dart'; +import 'package:yumi/shared/data_sources/models/enum/sc_auth_type.dart'; import 'package:yumi/shared/business_logic/models/res/login_res.dart'; import 'package:yumi/services/general/sc_app_general_manager.dart'; import 'package:yumi/services/auth/user_profile_manager.dart'; @@ -612,16 +614,42 @@ class _SCEditProfilePageState extends State { userProvider?.updateBornDay(birthdayDate.day); num age = DateTime.now().year - birthdayDate.year; userProvider?.updateAge(age.abs()); - String authType = - Provider.of( - context, - listen: false, - ).authType; - String idToken = - Provider.of( - context, - listen: false, - ).uid; + final authProvider = Provider.of( + context, + listen: false, + ); + String authType = authProvider.authType.trim(); + String idToken = authProvider.uid.trim(); + if (authType.isEmpty || idToken.isEmpty) { + final cachedAuthType = DataPersistence.getPendingChannelAuthType().trim(); + final cachedOpenId = DataPersistence.getPendingChannelAuthOpenId().trim(); + if (authType.isEmpty) { + authType = cachedAuthType; + } + if (idToken.isEmpty) { + idToken = cachedOpenId; + } + } + if (authType.isEmpty || idToken.isEmpty) { + try { + final googleUser = + SCGoogleAuthService.currentUser ?? + await SCGoogleAuthService.signInSilently(); + final googleUid = googleUser?.uid.trim() ?? ""; + if (googleUid.isNotEmpty) { + authType = SCAuthType.GOOGLE.name; + idToken = googleUid; + } + } catch (e) { + debugPrint('resolve register auth failed: $e'); + } + } + if (authType.isEmpty || idToken.isEmpty) { + SCLoadingManager.hide(); + SCTts.show("Please sign in again."); + return; + } + await DataPersistence.setPendingChannelAuth(authType, idToken); final invitationCode = invitationCodeController.text.trim(); SocialChatLoginRes user = await SCAccountRepository().regist( authType, @@ -683,6 +711,7 @@ class _SCEditProfilePageState extends State { return; } AccountStorage().setCurrentUser(user); + await DataPersistence.clearPendingChannelAuth(); userProvider?.syncCurrentUserProfile(user.userProfile); await DataPersistence.clearPendingRegisterRewardDialog(); await DataPersistence.setAwaitRegisterRewardSocket(true); diff --git a/lib/modules/country/country_page.dart b/lib/modules/country/country_page.dart index 933a9e5..4952415 100644 --- a/lib/modules/country/country_page.dart +++ b/lib/modules/country/country_page.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; @@ -11,7 +10,6 @@ import 'package:provider/provider.dart'; import 'package:yumi/ui_kit/components/appbar/socialchat_appbar.dart'; import 'package:yumi/app/routes/sc_fluro_navigator.dart'; import 'package:yumi/ui_kit/components/custom_cached_image.dart'; -import 'package:yumi/ui_kit/theme/socialchat_theme.dart'; import 'package:yumi/shared/business_logic/models/res/country_res.dart'; import 'package:yumi/services/general/sc_app_general_manager.dart'; import 'package:yumi/services/auth/user_profile_manager.dart'; @@ -21,19 +19,17 @@ import '../../app/config/business_logic_strategy.dart'; ///国家 class CountryPage extends StatefulWidget { - bool isDialog = false; + final bool isDialog; - CountryPage({this.isDialog = false}); + const CountryPage({super.key, this.isDialog = false}); @override - _CountryPageState createState() => _CountryPageState(isDialog); + State createState() => _CountryPageState(); } class _CountryPageState extends State { - bool isDialog = false; Country? selectCountryInfo; - - _CountryPageState(this.isDialog); + bool _didConfirmSelection = false; @override void initState() { @@ -43,10 +39,12 @@ class _CountryPageState extends State { @override void dispose() { - Provider.of( - context, - listen: false, - ).clearCountrySelection(); + if (!_didConfirmSelection) { + Provider.of( + context, + listen: false, + ).clearCountrySelection(); + } super.dispose(); } @@ -98,6 +96,7 @@ class _CountryPageState extends State { provider.updateCurrentCountry( selectCountryInfo, ); + _didConfirmSelection = true; SCNavigatorUtils.goBack(context); } }, @@ -159,6 +158,7 @@ class _CountryPageState extends State { provider.updateCurrentCountry( selectCountryInfo, ); + _didConfirmSelection = true; SmartDialog.dismiss(tag: "showCountryPage"); } }, diff --git a/lib/modules/splash/splash_page.dart b/lib/modules/splash/splash_page.dart index 060d74f..d85043b 100644 --- a/lib/modules/splash/splash_page.dart +++ b/lib/modules/splash/splash_page.dart @@ -9,6 +9,7 @@ import 'package:yumi/app_localizations.dart'; import 'package:yumi/shared/tools/sc_version_utils.dart'; import 'package:yumi/shared/tools/sc_google_auth_utils.dart'; import 'package:yumi/shared/data_sources/models/enum/sc_auth_type.dart'; +import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart'; import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart'; import 'package:yumi/shared/data_sources/sources/local/user_manager.dart'; import 'package:yumi/shared/business_logic/models/res/login_res.dart'; @@ -485,11 +486,13 @@ class _SplashPageState extends State { if (uid.isEmpty) { return null; } + await DataPersistence.setPendingChannelAuth(SCAuthType.GOOGLE.name, uid); final user = await SCAccountRepository().loginForChannel( SCAuthType.GOOGLE.name, uid, ); AccountStorage().setCurrentUser(user); + await DataPersistence.clearPendingChannelAuth(); return user; } catch (e) { debugPrint('restore login session failed: $e'); diff --git a/lib/services/auth/authentication_manager.dart b/lib/services/auth/authentication_manager.dart index 0d6aa28..a473469 100644 --- a/lib/services/auth/authentication_manager.dart +++ b/lib/services/auth/authentication_manager.dart @@ -1,13 +1,13 @@ import 'package:firebase_auth/firebase_auth.dart'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; -import 'package:flutter/foundation.dart'; import 'package:sign_in_with_apple/sign_in_with_apple.dart'; import 'package:yumi/app/routes/sc_routes.dart'; import 'package:yumi/app/routes/sc_fluro_navigator.dart'; import 'package:yumi/shared/tools/sc_google_auth_utils.dart'; import 'package:yumi/shared/tools/sc_loading_manager.dart'; import 'package:yumi/shared/tools/sc_version_utils.dart'; +import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart'; import 'package:yumi/shared/data_sources/sources/local/user_manager.dart'; import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart'; import 'package:yumi/shared/business_logic/models/res/login_res.dart'; @@ -39,6 +39,11 @@ class SocialChatAuthenticationManager extends ChangeNotifier { Future _verifyExistingSession() async { final user = await SCGoogleAuthService.signInSilently(); _user = user; + if (user != null) { + authType = SCAuthType.GOOGLE.name; + uid = user.uid; + await DataPersistence.setPendingChannelAuth(authType, uid); + } notifyListeners(); } @@ -53,6 +58,7 @@ class SocialChatAuthenticationManager extends ChangeNotifier { uid = _user!.uid; SCLoadingManager.show(); if (uid.isNotEmpty) { + await DataPersistence.setPendingChannelAuth(authType, uid); final res = await Future.wait([ SCAccountRepository().loginForChannel(authType, uid), SCVersionUtils.checkReview(), @@ -74,6 +80,10 @@ class SocialChatAuthenticationManager extends ChangeNotifier { return; } AccountStorage().setCurrentUser(user); + await DataPersistence.clearPendingChannelAuth(); + if (!context.mounted) { + return; + } SCLoadingManager.hide(); notifyListeners(); SCNavigatorUtils.push(context, SCRoutes.home, replace: true); @@ -93,6 +103,7 @@ class SocialChatAuthenticationManager extends ChangeNotifier { uid = appleCredential.userIdentifier ?? ""; SCLoadingManager.show(); if (uid.isNotEmpty) { + await DataPersistence.setPendingChannelAuth(authType, uid); final res = await Future.wait([ SCAccountRepository().loginForChannel(authType, uid), SCVersionUtils.checkReview(), @@ -114,6 +125,10 @@ class SocialChatAuthenticationManager extends ChangeNotifier { return; } AccountStorage().setCurrentUser(user); + await DataPersistence.clearPendingChannelAuth(); + if (!context.mounted) { + return; + } SCLoadingManager.hide(); notifyListeners(); SCNavigatorUtils.push(context, SCRoutes.home, replace: true); @@ -127,6 +142,7 @@ class SocialChatAuthenticationManager extends ChangeNotifier { Future logoutUser() async { await SCGoogleAuthService.signOut(); + await DataPersistence.clearPendingChannelAuth(); _user = null; notifyListeners(); } diff --git a/lib/shared/data_sources/sources/local/data_persistence.dart b/lib/shared/data_sources/sources/local/data_persistence.dart index 1c66b18..cc12f5f 100644 --- a/lib/shared/data_sources/sources/local/data_persistence.dart +++ b/lib/shared/data_sources/sources/local/data_persistence.dart @@ -10,6 +10,9 @@ class DataPersistence { "await_register_reward_socket"; static const String _pendingRegisterRewardDialogKey = "pending_register_reward_dialog"; + static const String _pendingChannelAuthTypeKey = "pending_channel_auth_type"; + static const String _pendingChannelAuthOpenIdKey = + "pending_channel_auth_open_id"; static SharedPreferences? _prefs; static bool _isInitialized = false; static Completer? _initializationCompleter; @@ -215,6 +218,31 @@ class DataPersistence { return await remove(_awaitRegisterRewardSocketKey); } + static Future setPendingChannelAuth( + String authType, + String openId, + ) async { + await Future.wait([ + setString(_pendingChannelAuthTypeKey, authType), + setString(_pendingChannelAuthOpenIdKey, openId), + ]); + } + + static String getPendingChannelAuthType() { + return getString(_pendingChannelAuthTypeKey); + } + + static String getPendingChannelAuthOpenId() { + return getString(_pendingChannelAuthOpenIdKey); + } + + static Future clearPendingChannelAuth() async { + await Future.wait([ + remove(_pendingChannelAuthTypeKey), + remove(_pendingChannelAuthOpenIdKey), + ]); + } + ///获取用户已经添加的房间音乐 static String getUserRoomMusic() { final currentUser = AccountStorage().getCurrentUser()?.userProfile?.account; diff --git a/lib/shared/data_sources/sources/local/user_manager.dart b/lib/shared/data_sources/sources/local/user_manager.dart index b02420e..dc08151 100644 --- a/lib/shared/data_sources/sources/local/user_manager.dart +++ b/lib/shared/data_sources/sources/local/user_manager.dart @@ -161,6 +161,7 @@ class AccountStorage { _currentUser = null; DataPersistence.setToken(""); DataPersistence.setCurrentUser(""); + unawaited(DataPersistence.clearPendingChannelAuth()); unawaited(DurableAuthStorage.clearSession()); }