- Added TaskListModel for managing daily tasks with JSON parsing. - Introduced WorkStatusResponseModel and WorkStatus for handling work status data. - Created MenuResponse and MenuItem models for dynamic menu management. - Updated routes to reflect correct naming conventions for task planning screens. - Enhanced DashboardScreen to include dynamic menu functionality and improved task statistics display. - Developed DailyProgressReportScreen for displaying daily progress reports with filtering options. - Implemented DailyTaskPlanningScreen for planning daily tasks with detailed views and actions. - Refactored left navigation bar to align with updated task planning routes.
87 lines
2.6 KiB
Dart
87 lines
2.6 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';
|
|
import 'package:marco/helpers/services/storage/local_storage.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;
|
|
|
|
Timer? _autoRefreshTimer;
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
fetchMenu();
|
|
|
|
/// Auto refresh every 5 minutes (adjust as needed)
|
|
_autoRefreshTimer = Timer.periodic(
|
|
const Duration(minutes: 1),
|
|
(_) => fetchMenu(),
|
|
);
|
|
}
|
|
|
|
/// Fetch dynamic menu from API with error and local storage support
|
|
Future<void> fetchMenu() async {
|
|
isLoading.value = true;
|
|
hasError.value = false;
|
|
errorMessage.value = '';
|
|
|
|
try {
|
|
final responseData = await ApiService.getMenuApi();
|
|
if (responseData != null) {
|
|
// Directly parse full JSON into MenuResponse
|
|
final menuResponse = MenuResponse.fromJson(responseData);
|
|
|
|
menuItems.assignAll(menuResponse.data);
|
|
|
|
// Save menus for offline use
|
|
await LocalStorage.setMenus(menuItems);
|
|
|
|
logSafe("Menu loaded from API with ${menuItems.length} items");
|
|
} else {
|
|
// If API fails, load from cache
|
|
final cachedMenus = LocalStorage.getMenus();
|
|
if (cachedMenus.isNotEmpty) {
|
|
menuItems.assignAll(cachedMenus);
|
|
logSafe("Loaded menus from cache: ${menuItems.length} items");
|
|
} else {
|
|
hasError.value = true;
|
|
errorMessage.value = "Failed to fetch menu";
|
|
menuItems.clear();
|
|
}
|
|
}
|
|
} catch (e) {
|
|
logSafe("Menu fetch exception: $e", level: LogLevel.error);
|
|
|
|
// On error, load cached menus
|
|
final cachedMenus = LocalStorage.getMenus();
|
|
if (cachedMenus.isNotEmpty) {
|
|
menuItems.assignAll(cachedMenus);
|
|
logSafe("Loaded menus from cache after error: ${menuItems.length}");
|
|
} else {
|
|
hasError.value = true;
|
|
errorMessage.value = e.toString();
|
|
menuItems.clear();
|
|
}
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
bool isMenuAllowed(String menuName) {
|
|
final menu = menuItems.firstWhereOrNull((m) => m.name == menuName);
|
|
return menu?.available ?? false; // default false if not found
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
_autoRefreshTimer?.cancel(); // clean up timer
|
|
super.onClose();
|
|
}
|
|
}
|