From 53f9697805514e025280f8437ca57c71efe9d9f1 Mon Sep 17 00:00:00 2001 From: Developer Date: Wed, 8 Apr 2026 01:32:27 +0800 Subject: [PATCH] feat: add ProfileController for user state management --- lib/src/controllers/profile_controller.dart | 95 +++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 lib/src/controllers/profile_controller.dart diff --git a/lib/src/controllers/profile_controller.dart b/lib/src/controllers/profile_controller.dart new file mode 100644 index 0000000..0253f91 --- /dev/null +++ b/lib/src/controllers/profile_controller.dart @@ -0,0 +1,95 @@ +import 'package:get/get.dart'; +import 'package:mom_kitchen/src/controllers/base/base_controller.dart'; +import 'package:mom_kitchen/src/services/app_service.dart'; + +class UserModel { + final String id; + final String name; + final String email; + final String? avatar; + + UserModel({ + required this.id, + required this.name, + required this.email, + this.avatar, + }); + + factory UserModel.empty() { + return UserModel(id: '', name: '', email: ''); + } + + bool get isEmpty => id.isEmpty; + bool get isNotEmpty => id.isNotEmpty; +} + +class ProfileController extends BaseController { + final user = Rx(null); + final isLoggedIn = false.obs; + + @override + void onInit() { + super.onInit(); + checkLoginStatus(); + } + + Future checkLoginStatus() async { + await runWithLoading(() async { + final storage = AppService.instance.storage; + final userId = await storage.getString('user_id'); + if (userId != null && userId.isNotEmpty) { + final name = await storage.getString('user_name') ?? 'User'; + final email = await storage.getString('user_email') ?? ''; + user.value = UserModel(id: userId, name: name, email: email); + isLoggedIn.value = true; + } + }); + } + + Future login(String email, String password) async { + await runWithLoading(() async { + await Future.delayed(const Duration(seconds: 1)); + + final storage = AppService.instance.storage; + await storage.setString('user_id', 'user_123'); + await storage.setString('user_name', 'Test User'); + await storage.setString('user_email', email); + + user.value = UserModel(id: 'user_123', name: 'Test User', email: email); + isLoggedIn.value = true; + }); + } + + Future logout() async { + await runWithLoading(() async { + final storage = AppService.instance.storage; + await storage.remove('user_id'); + await storage.remove('user_name'); + await storage.remove('user_email'); + + user.value = null; + isLoggedIn.value = false; + }); + } + + Future updateProfile({String? name, String? avatar}) async { + if (user.value == null) return; + + await runWithLoading(() async { + await Future.delayed(const Duration(milliseconds: 500)); + + final storage = AppService.instance.storage; + + if (name != null) { + await storage.setString('user_name', name); + } + + user.value = UserModel( + id: user.value!.id, + name: name ?? user.value!.name, + email: user.value!.email, + avatar: avatar ?? user.value!.avatar, + ); + }); + } +}