diff --git a/lib/app/bindings/initial_bindings.dart b/lib/app/bindings/initial_bindings.dart index d9631c9..189bb2e 100644 --- a/lib/app/bindings/initial_bindings.dart +++ b/lib/app/bindings/initial_bindings.dart @@ -1,4 +1,5 @@ import 'package:get/get.dart'; +import 'package:quiz_app/data/controllers/user_controller.dart'; import 'package:quiz_app/data/providers/dio_client.dart'; import 'package:quiz_app/data/services/user_storage_service.dart'; @@ -7,5 +8,6 @@ class InitialBindings extends Bindings { void dependencies() { Get.put(UserStorageService()); Get.putAsync(() => ApiClient().init()); + Get.put(UserController(Get.find())); } } diff --git a/lib/data/controllers/user_controller.dart b/lib/data/controllers/user_controller.dart new file mode 100644 index 0000000..28a5ef4 --- /dev/null +++ b/lib/data/controllers/user_controller.dart @@ -0,0 +1,28 @@ +import 'package:get/get.dart'; +import 'package:quiz_app/data/services/user_storage_service.dart'; + +class UserController extends GetxController { + final UserStorageService _userStorageService; + + UserController(this._userStorageService); + + Rx userName = "".obs; + Rx userImage = Rx(null); + Rx email = "".obs; + + @override + void onInit() { + loadUser(); + super.onInit(); + } + + Future loadUser() async { + final data = await _userStorageService.loadUser(); + if (data != null) { + userName.value = data.name; + userImage.value = data.picUrl; + email.value = data.email; + print("Loaded user: ${data.toJson()}"); + } + } +} diff --git a/lib/feature/home/binding/home_binding.dart b/lib/feature/home/binding/home_binding.dart index d2028f7..78eabad 100644 --- a/lib/feature/home/binding/home_binding.dart +++ b/lib/feature/home/binding/home_binding.dart @@ -5,6 +5,6 @@ import 'package:quiz_app/feature/home/controller/home_controller.dart'; class HomeBinding extends Bindings { @override void dependencies() { - Get.lazyPut(() => HomeController(Get.find())); + Get.lazyPut(() => HomeController()); } } diff --git a/lib/feature/home/controller/home_controller.dart b/lib/feature/home/controller/home_controller.dart index aef1787..a388524 100644 --- a/lib/feature/home/controller/home_controller.dart +++ b/lib/feature/home/controller/home_controller.dart @@ -1,26 +1,9 @@ import 'package:get/get.dart'; -import 'package:quiz_app/data/models/login/login_response_model.dart'; -import 'package:quiz_app/data/services/user_storage_service.dart'; +import 'package:quiz_app/data/controllers/user_controller.dart'; class HomeController extends GetxController { - final UserStorageService _userStorageService; + final UserController _userController = Get.find(); - HomeController(this._userStorageService); - - Rx userName = "Dani".obs; - String? userImage; - - @override - void onInit() { - getUserData(); - super.onInit(); - } - - Future getUserData() async { - LoginResponseModel? data = await _userStorageService.loadUser(); - if (data == null) return; - print("User data: ${data.toJson()}"); - userName.value = data.name; - userImage = data.picUrl; - } + Rx get userName => _userController.userName; + Rx get userImage => _userController.userImage; } diff --git a/lib/feature/home/view/home_page.dart b/lib/feature/home/view/home_page.dart index 5a8fd41..5ac1288 100644 --- a/lib/feature/home/view/home_page.dart +++ b/lib/feature/home/view/home_page.dart @@ -23,7 +23,7 @@ class HomeView extends GetView { Obx( () => UserGretingsComponent( userName: controller.userName.value, - userImage: controller.userImage, + userImage: controller.userImage.value, ), ), const SizedBox(height: 20), diff --git a/lib/feature/profile/controller/profile_controller.dart b/lib/feature/profile/controller/profile_controller.dart index 0e0df07..a52dffe 100644 --- a/lib/feature/profile/controller/profile_controller.dart +++ b/lib/feature/profile/controller/profile_controller.dart @@ -1,8 +1,12 @@ import 'package:get/get.dart'; +import 'package:quiz_app/data/controllers/user_controller.dart'; class ProfileController extends GetxController { - final userName = 'Alhidan Robbani'.obs; - final email = 'alhidan@example.com'.obs; + final UserController _userController = Get.find(); + + Rx get userName => _userController.userName; + Rx get email => _userController.email; + Rx get userImage => _userController.userImage; final totalQuizzes = 12.obs; final avgScore = 85.obs; diff --git a/lib/feature/profile/view/profile_view.dart b/lib/feature/profile/view/profile_view.dart index bf8c68a..cfc0cd0 100644 --- a/lib/feature/profile/view/profile_view.dart +++ b/lib/feature/profile/view/profile_view.dart @@ -43,11 +43,19 @@ class ProfileView extends GetView { } Widget _buildAvatar() { - return const CircleAvatar( - radius: 45, - backgroundColor: Colors.blueAccent, - child: Icon(Icons.person, size: 50, color: Colors.white), - ); + if (controller.userImage.value != null) { + return CircleAvatar( + radius: 45, + backgroundColor: Colors.blueAccent, + backgroundImage: NetworkImage(controller.userImage.value!), + ); + } else { + return const CircleAvatar( + radius: 45, + backgroundColor: Colors.blueAccent, + child: Icon(Icons.person, size: 50, color: Colors.white), + ); + } } Widget _buildStats() {