From 46588259dd2b60707547e82f40c25dcbacadc6cc Mon Sep 17 00:00:00 2001 From: grtsinry43 Date: Tue, 7 Oct 2025 00:06:37 +0800 Subject: [PATCH] feat: Implement user profile screen and enhance authentication This commit introduces a dedicated user profile screen (`UserScreen`) and significantly enhances the authentication system by adding registration, auto-login, and robust form validation. **Key Changes:** * **feat(UserScreen):** * Added a new `UserScreen` to display the logged-in user's profile. * Displays user details including avatar, nickname, ID, bio, location, and email in a styled card. * Includes a menu section for actions like "Post Management," "Settings," and "About." * Shows a "not logged in" state to prompt users to log in. * **feat(Auth):** * **Registration:** Implemented a registration flow alongside the login flow on the `LoginScreen`. Users can now switch between Login and Register modes. * **Auto-Login:** The app now automatically logs in the user on startup if a valid token or saved credentials exist, improving user experience. * **Token Management:** Enhanced `TokenManager` to handle JWT parsing to extract `userId`, check token validity (expiration), and securely store credentials. Added `java-jwt` dependency for this. * **Logout:** Improved the logout function to clear all stored tokens and user credentials from `SharedPreferences`. * **feat(Validation):** * Introduced `ValidationUtils` to provide real-time validation for phone number, password, and nickname fields on the `LoginScreen`. * Input fields now display specific error messages to guide the user (e.g., "Password must contain a letter," "Invalid phone number format"). * **refactor(LoginScreen):** * Redesigned the `LoginScreen` to support both login and registration (`AuthMode`) with animated transitions between modes and form fields. * Form submission buttons are dynamically enabled based on validation results. * After successful registration, the user is now automatically logged in. * **refactor(UserViewModel):** * Integrated auto-login logic (`checkAutoLogin`) that runs on initialization. * Refactored `login` and `register` flows to handle token storage and fetch user profile data immediately after authentication. * Added `isLoggedIn()` to provide a reliable check of the current authentication state. * **refactor(TopBar):** * The top bar avatar now correctly reflects the user's login state. It displays the user's avatar when logged in and a generic app icon when logged out. * Clicking the avatar/icon now navigates to the `UserScreen` if logged in, or the `LoginScreen` if not. --- .idea/deploymentTargetSelector.xml | 2 +- app/build.gradle.kts | 3 + .../main/java/com/qingshuige/tangyuan/App.kt | 53 +- .../tangyuan/network/TokenManager.kt | 123 +++- .../tangyuan/ui/components/TopBar.kt | 29 +- .../tangyuan/ui/screens/LoginScreen.kt | 663 +++++++++++++----- .../tangyuan/ui/screens/UserScreen.kt | 540 ++++++++++++++ .../tangyuan/utils/ValidationUtils.kt | 94 +++ .../tangyuan/viewmodel/UserViewModel.kt | 146 +++- gradle/libs.versions.toml | 2 + 10 files changed, 1448 insertions(+), 207 deletions(-) create mode 100644 app/src/main/java/com/qingshuige/tangyuan/ui/screens/UserScreen.kt create mode 100644 app/src/main/java/com/qingshuige/tangyuan/utils/ValidationUtils.kt diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml index a9a15e9..d912518 100644 --- a/.idea/deploymentTargetSelector.xml +++ b/.idea/deploymentTargetSelector.xml @@ -4,7 +4,7 @@