feat: add quiz controller
This commit is contained in:
parent
dbb3bd8c90
commit
39ab35b2a8
|
@ -1,4 +1,5 @@
|
||||||
import 'package:get/get.dart';
|
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/providers/dio_client.dart';
|
||||||
import 'package:quiz_app/data/services/user_storage_service.dart';
|
import 'package:quiz_app/data/services/user_storage_service.dart';
|
||||||
|
|
||||||
|
@ -7,5 +8,6 @@ class InitialBindings extends Bindings {
|
||||||
void dependencies() {
|
void dependencies() {
|
||||||
Get.put<UserStorageService>(UserStorageService());
|
Get.put<UserStorageService>(UserStorageService());
|
||||||
Get.putAsync(() => ApiClient().init());
|
Get.putAsync(() => ApiClient().init());
|
||||||
|
Get.put<UserController>(UserController(Get.find<UserStorageService>()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<String> userName = "".obs;
|
||||||
|
Rx<String?> userImage = Rx<String?>(null);
|
||||||
|
Rx<String> email = "".obs;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onInit() {
|
||||||
|
loadUser();
|
||||||
|
super.onInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> 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()}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,6 @@ import 'package:quiz_app/feature/home/controller/home_controller.dart';
|
||||||
class HomeBinding extends Bindings {
|
class HomeBinding extends Bindings {
|
||||||
@override
|
@override
|
||||||
void dependencies() {
|
void dependencies() {
|
||||||
Get.lazyPut<HomeController>(() => HomeController(Get.find<UserStorageService>()));
|
Get.lazyPut<HomeController>(() => HomeController());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,9 @@
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:quiz_app/data/models/login/login_response_model.dart';
|
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||||
import 'package:quiz_app/data/services/user_storage_service.dart';
|
|
||||||
|
|
||||||
class HomeController extends GetxController {
|
class HomeController extends GetxController {
|
||||||
final UserStorageService _userStorageService;
|
final UserController _userController = Get.find<UserController>();
|
||||||
|
|
||||||
HomeController(this._userStorageService);
|
Rx<String> get userName => _userController.userName;
|
||||||
|
Rx<String?> get userImage => _userController.userImage;
|
||||||
Rx<String> userName = "Dani".obs;
|
|
||||||
String? userImage;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void onInit() {
|
|
||||||
getUserData();
|
|
||||||
super.onInit();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> getUserData() async {
|
|
||||||
LoginResponseModel? data = await _userStorageService.loadUser();
|
|
||||||
if (data == null) return;
|
|
||||||
print("User data: ${data.toJson()}");
|
|
||||||
userName.value = data.name;
|
|
||||||
userImage = data.picUrl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ class HomeView extends GetView<HomeController> {
|
||||||
Obx(
|
Obx(
|
||||||
() => UserGretingsComponent(
|
() => UserGretingsComponent(
|
||||||
userName: controller.userName.value,
|
userName: controller.userName.value,
|
||||||
userImage: controller.userImage,
|
userImage: controller.userImage.value,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||||
|
|
||||||
class ProfileController extends GetxController {
|
class ProfileController extends GetxController {
|
||||||
final userName = 'Alhidan Robbani'.obs;
|
final UserController _userController = Get.find<UserController>();
|
||||||
final email = 'alhidan@example.com'.obs;
|
|
||||||
|
Rx<String> get userName => _userController.userName;
|
||||||
|
Rx<String> get email => _userController.email;
|
||||||
|
Rx<String?> get userImage => _userController.userImage;
|
||||||
|
|
||||||
final totalQuizzes = 12.obs;
|
final totalQuizzes = 12.obs;
|
||||||
final avgScore = 85.obs;
|
final avgScore = 85.obs;
|
||||||
|
|
|
@ -43,12 +43,20 @@ class ProfileView extends GetView<ProfileController> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildAvatar() {
|
Widget _buildAvatar() {
|
||||||
|
if (controller.userImage.value != null) {
|
||||||
|
return CircleAvatar(
|
||||||
|
radius: 45,
|
||||||
|
backgroundColor: Colors.blueAccent,
|
||||||
|
backgroundImage: NetworkImage(controller.userImage.value!),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
return const CircleAvatar(
|
return const CircleAvatar(
|
||||||
radius: 45,
|
radius: 45,
|
||||||
backgroundColor: Colors.blueAccent,
|
backgroundColor: Colors.blueAccent,
|
||||||
child: Icon(Icons.person, size: 50, color: Colors.white),
|
child: Icon(Icons.person, size: 50, color: Colors.white),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Widget _buildStats() {
|
Widget _buildStats() {
|
||||||
return Container(
|
return Container(
|
||||||
|
|
Loading…
Reference in New Issue