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/helpers/services/tenant_service.dart';
|
||||||
import 'package:marco/model/tenant/tenant_list_model.dart';
|
import 'package:marco/model/tenant/tenant_list_model.dart';
|
||||||
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
||||||
|
import 'package:marco/helpers/services/storage/local_storage.dart';
|
||||||
|
|
||||||
class TenantSelectionController extends GetxController {
|
class TenantSelectionController extends GetxController {
|
||||||
final TenantService _tenantService = TenantService();
|
final TenantService _tenantService = TenantService();
|
||||||
@ -17,14 +18,28 @@ class TenantSelectionController extends GetxController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Load tenants from API
|
/// Load tenants from API
|
||||||
Future<void> loadTenants() async {
|
Future<void> loadTenants({bool fromTenantSelectionScreen = false}) async {
|
||||||
try {
|
try {
|
||||||
isLoading.value = true;
|
isLoading.value = true;
|
||||||
final data = await _tenantService.getTenants();
|
final data = await _tenantService.getTenants();
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
tenants.value = data.map((e) => Tenant.fromJson(e)).toList();
|
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) {
|
if (tenants.length == 1) {
|
||||||
await onTenantSelected(tenants.first.id);
|
await onTenantSelected(tenants.first.id);
|
||||||
}
|
}
|
||||||
@ -49,13 +64,15 @@ class TenantSelectionController extends GetxController {
|
|||||||
logSafe("✅ Tenant selection successful: $tenantId");
|
logSafe("✅ Tenant selection successful: $tenantId");
|
||||||
|
|
||||||
// Store selected tenant in memory
|
// Store selected tenant in memory
|
||||||
TenantService.setSelectedTenant(
|
final selectedTenant = tenants.firstWhere((t) => t.id == tenantId);
|
||||||
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');
|
Get.offAllNamed('/dashboard');
|
||||||
|
|
||||||
// Optional: show success snackbar
|
|
||||||
showAppSnackbar(
|
showAppSnackbar(
|
||||||
title: "Success",
|
title: "Success",
|
||||||
message: "Organization selected successfully.",
|
message: "Organization selected successfully.",
|
||||||
|
@ -22,6 +22,17 @@ class LocalStorage {
|
|||||||
static const String _isMpinKey = "isMpin";
|
static const String _isMpinKey = "isMpin";
|
||||||
static const String _fcmTokenKey = "fcm_token";
|
static const String _fcmTokenKey = "fcm_token";
|
||||||
static const String _menuStorageKey = "dynamic_menus";
|
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 SharedPreferences? _preferencesInstance;
|
||||||
static bool _initialized = false;
|
static bool _initialized = false;
|
||||||
@ -76,7 +87,8 @@ class LocalStorage {
|
|||||||
static Future<bool> removeMenus() => preferences.remove(_menuStorageKey);
|
static Future<bool> removeMenus() => preferences.remove(_menuStorageKey);
|
||||||
|
|
||||||
// ================== User Permissions ==================
|
// ================== 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();
|
final jsonList = permissions.map((e) => e.toJson()).toList();
|
||||||
return preferences.setString(_userPermissionsKey, jsonEncode(jsonList));
|
return preferences.setString(_userPermissionsKey, jsonEncode(jsonList));
|
||||||
}
|
}
|
||||||
@ -94,8 +106,8 @@ class LocalStorage {
|
|||||||
preferences.remove(_userPermissionsKey);
|
preferences.remove(_userPermissionsKey);
|
||||||
|
|
||||||
// ================== Employee Info ==================
|
// ================== Employee Info ==================
|
||||||
static Future<bool> setEmployeeInfo(EmployeeInfo employeeInfo) =>
|
static Future<bool> setEmployeeInfo(EmployeeInfo employeeInfo) => preferences
|
||||||
preferences.setString(_employeeInfoKey, jsonEncode(employeeInfo.toJson()));
|
.setString(_employeeInfoKey, jsonEncode(employeeInfo.toJson()));
|
||||||
|
|
||||||
static EmployeeInfo? getEmployeeInfo() {
|
static EmployeeInfo? getEmployeeInfo() {
|
||||||
if (!_initialized) return null;
|
if (!_initialized) return null;
|
||||||
@ -135,6 +147,7 @@ class LocalStorage {
|
|||||||
await removeMpinToken();
|
await removeMpinToken();
|
||||||
await removeIsMpin();
|
await removeIsMpin();
|
||||||
await removeMenus();
|
await removeMenus();
|
||||||
|
await removeRecentTenantId();
|
||||||
await preferences.remove("mpin_verified");
|
await preferences.remove("mpin_verified");
|
||||||
await preferences.remove(_languageKey);
|
await preferences.remove(_languageKey);
|
||||||
await preferences.remove(_themeCustomizerKey);
|
await preferences.remove(_themeCustomizerKey);
|
||||||
|
@ -35,7 +35,9 @@ class _TenantSelectionScreenState extends State<TenantSelectionScreen>
|
|||||||
curve: Curves.easeOutBack,
|
curve: Curves.easeOutBack,
|
||||||
);
|
);
|
||||||
_logoAnimController.forward();
|
_logoAnimController.forward();
|
||||||
_controller.loadTenants();
|
|
||||||
|
// 🔥 Tell controller this is tenant selection screen
|
||||||
|
_controller.loadTenants(fromTenantSelectionScreen: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user