feat: add ProfileController for user state management
This commit is contained in:
95
lib/src/controllers/profile_controller.dart
Normal file
95
lib/src/controllers/profile_controller.dart
Normal file
@@ -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<UserModel?>(null);
|
||||
final isLoggedIn = false.obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
checkLoginStatus();
|
||||
}
|
||||
|
||||
Future<void> 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<void> 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<void> 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<void> 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,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user