63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'dart:async';
 | |
| import 'package:get/get.dart';
 | |
| import 'package:marco/model/dynamicMenu/dynamic_menu_model.dart';
 | |
| import 'package:marco/helpers/services/api_service.dart';
 | |
| import 'package:marco/helpers/services/app_logger.dart';
 | |
| 
 | |
| class DynamicMenuController extends GetxController {
 | |
|   // UI reactive states
 | |
|   final RxBool isLoading = false.obs;
 | |
|   final RxBool hasError = false.obs;
 | |
|   final RxString errorMessage = ''.obs;
 | |
|   final RxList<MenuItem> menuItems = <MenuItem>[].obs;
 | |
| 
 | |
|   @override
 | |
|   void onInit() {
 | |
|     super.onInit();
 | |
|     // Fetch menus directly from API at startup
 | |
|     fetchMenu();
 | |
|   }
 | |
| 
 | |
|   /// Fetch dynamic menu from API (no local cache)
 | |
|   Future<void> fetchMenu() async {
 | |
|     isLoading.value = true;
 | |
|     hasError.value = false;
 | |
|     errorMessage.value = '';
 | |
| 
 | |
|     try {
 | |
|       final responseData = await ApiService.getMenuApi();
 | |
|       if (responseData != null) {
 | |
|         final menuResponse = MenuResponse.fromJson(responseData);
 | |
|         menuItems.assignAll(menuResponse.data);
 | |
| 
 | |
|         logSafe("✅ Menu loaded from API with ${menuItems.length} items");
 | |
|       } else {
 | |
|         _handleApiFailure("Menu API returned null response");
 | |
|       }
 | |
|     } catch (e) {
 | |
|       _handleApiFailure("Menu fetch exception: $e");
 | |
|     } finally {
 | |
|       isLoading.value = false;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   void _handleApiFailure(String logMessage) {
 | |
|     logSafe(logMessage, level: LogLevel.error);
 | |
| 
 | |
|     // No cache available, show error state
 | |
|     hasError.value = true;
 | |
|     errorMessage.value = "❌ Unable to load menus. Please try again later.";
 | |
|     menuItems.clear();
 | |
|   }
 | |
| 
 | |
|   bool isMenuAllowed(String menuName) {
 | |
|     final menu = menuItems.firstWhereOrNull((m) => m.name == menuName);
 | |
|     return menu?.available ?? false;
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onClose() {
 | |
|     super.onClose();
 | |
|   }
 | |
| }
 |