@@ -10,17 +10,15 @@ class _SettingsController with ShareMixin, AppLogger {
1010
1111 String appVersion = '' ;
1212
13- final organizations = ValueNotifier <ApiResponse <List <Organization >>?>(null );
14-
15- bool get hasMultiOrgs => (organizations.value? .data? .length ?? 0 ) > 1 ;
13+ final directories = ValueNotifier <ApiResponse <List <UserTenant >>?>(null );
1614
1715 Future <void > init () async {
1816 final info = await PackageInfo .fromPlatform ();
1917 appVersion = info.version;
2018
21- final orgs = await api.getOrganizations ();
22- // copyWith is needed to make page visible even if getOrganizations returns 401
23- organizations .value = orgs.copyWith (isError: false , data: []);
19+ final orgs = await api.getDirectories ();
20+ // copyWith is needed to make page visible even if getDirectories returns 401
21+ directories .value = orgs.copyWith (isError: false , data: orgs.data ?? []);
2422 }
2523
2624 void shareApp () {
@@ -78,44 +76,43 @@ class _SettingsController with ShareMixin, AppLogger {
7876 InAppReview .instance.openStoreListing (appStoreId: '1666994628' );
7977 }
8078
81- Future <void > switchOrganization () async {
82- final selectedOrg = await _selectOrganization (organizations.value! .data! );
83- if (selectedOrg == null ) return ;
79+ Future <void > chooseDirectory () async {
80+ await OverlayService .bottomsheet (
81+ title: 'Switch directory' ,
82+ isScrollControlled: true ,
83+ heightPercentage: .6 ,
84+ builder: (context) => _SwitchDirectoryWidget (
85+ directories: directories.value? .data ?? [],
86+ onSwitch: _switchOrganization,
87+ ),
88+ );
89+ }
90+
91+ Future <void > _switchOrganization (UserTenant tenant) async {
92+ final token = await MsalService ().login (authority: 'https://login.microsoftonline.com/${tenant .id }' );
8493
85- api.switchOrganization (selectedOrg.accountName! );
86- unawaited (AppRouter .goToSplash ());
94+ storage.setTenantId (tenant.id);
95+
96+ if (token != null ) unawaited (_loginAndNavigate (token));
8797 }
8898
89- Future <Organization ?> _selectOrganization ( List < Organization > organizations ) async {
90- final currentOrg = storage.getOrganization ( );
99+ Future <void > _loginAndNavigate ( String token ) async {
100+ storage.setOrganization ( '' );
91101
92- Organization ? selectedOrg ;
102+ final isLogged = await api. login (token) ;
93103
94- await OverlayService .bottomsheet (
95- title: 'Select your organization' ,
96- isScrollControlled: true ,
97- heightPercentage: .7 ,
98- builder: (context) => ListView (
99- children: organizations
100- .map (
101- (org) => Padding (
102- padding: const EdgeInsets .symmetric (vertical: 10 ),
103- child: LoadingButton (
104- onPressed: () {
105- selectedOrg = org;
106- AppRouter .popRoute ();
107- },
108- text: org.accountName == currentOrg ? '${org .accountName !} (current)' : org.accountName! ,
109- ),
110- ),
111- )
112- .toList (),
113- ),
114- );
104+ final isFailed = [LoginStatus .failed, LoginStatus .unauthorized].contains (isLogged);
105+
106+ logAnalytics ('switch_directory_${isFailed ? 'failed' : 'success' }' , {});
115107
116- if (selectedOrg? .accountName == currentOrg) return null ;
108+ if (isLogged == LoginStatus .failed) {
109+ return OverlayService .error (
110+ 'Login error' ,
111+ description: 'Check that you have access to the organization you are trying to switch to.' ,
112+ );
113+ }
117114
118- return selectedOrg ;
115+ await AppRouter . goToChooseProjects () ;
119116 }
120117
121118 Future <void > showChangelog () async {
0 commit comments