feat: Implement recent tenant selection logic and enhance tenant loading process
This commit is contained in:
parent
98612db7b5
commit
781a8dabaf
@ -3,6 +3,7 @@ import 'package:marco/helpers/services/app_logger.dart';
|
||||
import 'package:marco/helpers/services/tenant_service.dart';
|
||||
import 'package:marco/model/tenant/tenant_list_model.dart';
|
||||
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
||||
import 'package:marco/helpers/services/storage/local_storage.dart';
|
||||
|
||||
class TenantSelectionController extends GetxController {
|
||||
final TenantService _tenantService = TenantService();
|
||||
@ -17,14 +18,28 @@ class TenantSelectionController extends GetxController {
|
||||
}
|
||||
|
||||
/// Load tenants from API
|
||||
Future<void> loadTenants() async {
|
||||
Future<void> loadTenants({bool fromTenantSelectionScreen = false}) async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
final data = await _tenantService.getTenants();
|
||||
if (data != null) {
|
||||
tenants.value = data.map((e) => Tenant.fromJson(e)).toList();
|
||||
|
||||
// ✅ Automatically select if only one tenant
|
||||
final recentTenantId = LocalStorage.getRecentTenantId();
|
||||
|
||||
// ✅ If user came from TenantSelectionScreen & recent tenant exists, auto-select
|
||||
if (fromTenantSelectionScreen && recentTenantId != null) {
|
||||
final tenantExists = tenants.any((t) => t.id == recentTenantId);
|
||||
if (tenantExists) {
|
||||
await onTenantSelected(recentTenantId);
|
||||
return;
|
||||
} else {
|
||||
// if tenant is no longer valid, clear recentTenant
|
||||
await LocalStorage.removeRecentTenantId();
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ Auto-select if only one tenant
|
||||
if (tenants.length == 1) {
|
||||
await onTenantSelected(tenants.first.id);
|
||||
}
|
||||
@ -49,13 +64,15 @@ class TenantSelectionController extends GetxController {
|
||||
logSafe("✅ Tenant selection successful: $tenantId");
|
||||
|
||||
// Store selected tenant in memory
|
||||
TenantService.setSelectedTenant(
|
||||
tenants.firstWhere((t) => t.id == tenantId));
|
||||
final selectedTenant = tenants.firstWhere((t) => t.id == tenantId);
|
||||
TenantService.setSelectedTenant(selectedTenant);
|
||||
|
||||
// Navigate to dashboard/home
|
||||
// 🔥 Save in LocalStorage
|
||||
await LocalStorage.setRecentTenantId(tenantId);
|
||||
|
||||
// Navigate to dashboard
|
||||
Get.offAllNamed('/dashboard');
|
||||
|
||||
// Optional: show success snackbar
|
||||
showAppSnackbar(
|
||||
title: "Success",
|
||||
message: "Organization selected successfully.",
|
||||
|
@ -22,6 +22,17 @@ class LocalStorage {
|
||||
static const String _isMpinKey = "isMpin";
|
||||
static const String _fcmTokenKey = "fcm_token";
|
||||
static const String _menuStorageKey = "dynamic_menus";
|
||||
// In LocalStorage
|
||||
static const String _recentTenantKey = "recent_tenant_id";
|
||||
|
||||
static Future<bool> setRecentTenantId(String tenantId) =>
|
||||
preferences.setString(_recentTenantKey, tenantId);
|
||||
|
||||
static String? getRecentTenantId() =>
|
||||
_initialized ? preferences.getString(_recentTenantKey) : null;
|
||||
|
||||
static Future<bool> removeRecentTenantId() =>
|
||||
preferences.remove(_recentTenantKey);
|
||||
|
||||
static SharedPreferences? _preferencesInstance;
|
||||
static bool _initialized = false;
|
||||
@ -76,7 +87,8 @@ class LocalStorage {
|
||||
static Future<bool> removeMenus() => preferences.remove(_menuStorageKey);
|
||||
|
||||
// ================== User Permissions ==================
|
||||
static Future<bool> setUserPermissions(List<UserPermission> permissions) async {
|
||||
static Future<bool> setUserPermissions(
|
||||
List<UserPermission> permissions) async {
|
||||
final jsonList = permissions.map((e) => e.toJson()).toList();
|
||||
return preferences.setString(_userPermissionsKey, jsonEncode(jsonList));
|
||||
}
|
||||
@ -94,8 +106,8 @@ class LocalStorage {
|
||||
preferences.remove(_userPermissionsKey);
|
||||
|
||||
// ================== Employee Info ==================
|
||||
static Future<bool> setEmployeeInfo(EmployeeInfo employeeInfo) =>
|
||||
preferences.setString(_employeeInfoKey, jsonEncode(employeeInfo.toJson()));
|
||||
static Future<bool> setEmployeeInfo(EmployeeInfo employeeInfo) => preferences
|
||||
.setString(_employeeInfoKey, jsonEncode(employeeInfo.toJson()));
|
||||
|
||||
static EmployeeInfo? getEmployeeInfo() {
|
||||
if (!_initialized) return null;
|
||||
@ -135,6 +147,7 @@ class LocalStorage {
|
||||
await removeMpinToken();
|
||||
await removeIsMpin();
|
||||
await removeMenus();
|
||||
await removeRecentTenantId();
|
||||
await preferences.remove("mpin_verified");
|
||||
await preferences.remove(_languageKey);
|
||||
await preferences.remove(_themeCustomizerKey);
|
||||
|
@ -35,7 +35,9 @@ class _TenantSelectionScreenState extends State<TenantSelectionScreen>
|
||||
curve: Curves.easeOutBack,
|
||||
);
|
||||
_logoAnimController.forward();
|
||||
_controller.loadTenants();
|
||||
|
||||
// 🔥 Tell controller this is tenant selection screen
|
||||
_controller.loadTenants(fromTenantSelectionScreen: true);
|
||||
}
|
||||
|
||||
@override
|
||||
|
Loading…
x
Reference in New Issue
Block a user