Skip to content

Commit

Permalink
Move Logout button (#99)
Browse files Browse the repository at this point in the history
* Move logout to settings

Co-authored-by: Alisa Vynohradova <[email protected]>

* Style NavigationRail in Settings

Co-authored-by: Alisa Vynohradova <[email protected]>

* Add settings for regular users

Co-authored-by: Alisa Vynohradova <[email protected]>

* Fix logout for mobile

Co-authored-by: Alisa Vynohradova <[email protected]>

* Extract logout button

Co-authored-by: Alisa Vynohradova <[email protected]>

* Fix tests

---------

Co-authored-by: Alisa Vynohradova <[email protected]>
  • Loading branch information
ANDREYDEN and alisondraV authored Mar 20, 2024
1 parent cef1450 commit 6a1edfa
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 64 deletions.
34 changes: 10 additions & 24 deletions lib/pages/home_page/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:spare_parts/pages/home_page/settings_view/settings_view.dart';
Expand All @@ -22,35 +21,29 @@ class _HomePageState extends State<HomePage> {
final PageController pageController = PageController();
late final TabFactory tabFactory;

bool get isAdmin => context.read<UserRole>() == UserRole.admin;

@override
void initState() {
_pageTitle = isAdmin ? 'Inventory' : 'My Items';
tabFactory = TabFactory(isAdmin: isAdmin, isDesktop: false);
super.initState();
}

void _handleSignOut() {
final auth = context.read<FirebaseAuth>();
auth.signOut();
}

void _handleSettings() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Scaffold(
builder: (_) => Scaffold(
appBar: AppBar(
title: const Text('Settings'),
actions: [
TextButton.icon(
label: Text('Logout'),
onPressed: _handleSignOut,
icon: const Icon(Icons.logout),
style: TextButton.styleFrom(foregroundColor: Colors.white),
),
],
),
body: Center(child: SettingsView()),
body: Center(
child: Provider.value(
value: context.read<UserRole>(),
child: SettingsView(),
),
),
),
),
);
Expand Down Expand Up @@ -90,8 +83,6 @@ class _HomePageState extends State<HomePage> {
});
}

bool get isAdmin => context.read<UserRole>() == UserRole.admin;

@override
Widget build(BuildContext context) {
return CustomLayoutBuilder(
Expand All @@ -111,17 +102,12 @@ class _HomePageState extends State<HomePage> {
onPressed: _handleScan,
color: Theme.of(context).colorScheme.primary,
),
if (isDesktop && isAdmin)
if (isDesktop)
TextButton.icon(
label: Text('Settings'),
onPressed: _handleSettings,
icon: Icon(Icons.settings),
),
TextButton.icon(
label: Text('Logout'),
onPressed: _handleSignOut,
icon: const Icon(Icons.logout),
),
],
),
floatingActionButton: isAdmin ? AddInventoryItemButton() : null,
Expand Down
118 changes: 78 additions & 40 deletions lib/pages/home_page/settings_view/settings_view.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:spare_parts/pages/home_page/settings_view/borrowing_rules_setting/borrowing_rules_setting.dart';
import 'package:spare_parts/pages/home_page/settings_view/set_admins_button.dart';
import 'package:spare_parts/pages/home_page/settings_view/users_setting/users_setting.dart';
import 'package:spare_parts/utilities/constants.dart';
import 'package:spare_parts/widgets/buttons/logout_button.dart';
import 'package:spare_parts/widgets/custom_layout_builder.dart';

class SettingsView extends StatefulWidget {
Expand All @@ -14,6 +17,8 @@ class SettingsView extends StatefulWidget {
class _SettingsViewState extends State<SettingsView> {
int _navigationIndex = 0;

bool get isAdmin => context.read<UserRole>() == UserRole.admin;

double _getPadding(BuildContext context, LayoutType layout) {
switch (layout) {
case LayoutType.mobile:
Expand All @@ -40,47 +45,80 @@ class _SettingsViewState extends State<SettingsView> {

@override
Widget build(BuildContext context) {
return CustomLayoutBuilder(builder: (context, layout) {
return Row(
children: [
NavigationRail(
extended:
layout == LayoutType.desktop || layout == LayoutType.tablet,
destinations: [
NavigationRailDestination(
icon: Icon(Icons.person_outlined),
selectedIcon: Icon(Icons.person),
label: Text('General'),
),
NavigationRailDestination(
icon: Icon(Icons.waving_hand_outlined),
selectedIcon: Icon(Icons.waving_hand),
label: Text('Borrowing Rules'),
),
NavigationRailDestination(
icon: Icon(Icons.manage_accounts_outlined),
selectedIcon: Icon(Icons.manage_accounts),
label: Text('Users'),
),
],
selectedIndex: _navigationIndex,
onDestinationSelected: (index) {
setState(() {
_navigationIndex = index;
});
},
),
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 10,
horizontal: _getPadding(context, layout),
return CustomLayoutBuilder(
builder: (context, layout) {
if (!isAdmin) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'More settings coming soon ;)',
style: TextStyle(fontSize: 16),
),
SizedBox(height: 10),
LogoutButton(popOnSuccess: layout == LayoutType.desktop)
],
),
);
}

return Row(
children: [
NavigationRail(
extended:
layout == LayoutType.desktop || layout == LayoutType.tablet,
elevation: 5,
destinations: [
NavigationRailDestination(
icon: Icon(Icons.person_outlined),
selectedIcon: Icon(Icons.person),
label: Text('General'),
),
NavigationRailDestination(
icon: Icon(Icons.waving_hand_outlined),
selectedIcon: Icon(Icons.waving_hand),
label: Text('Borrowing Rules'),
),
NavigationRailDestination(
icon: Icon(Icons.manage_accounts_outlined),
selectedIcon: Icon(Icons.manage_accounts),
label: Text('Users'),
),
],
selectedIndex: _navigationIndex,
onDestinationSelected: (index) {
if (index == 3) {}

setState(() {
_navigationIndex = index;
});
},
trailing: Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 16),
child: LogoutButton(
iconOnly: layout == LayoutType.mobile,
popOnSuccess: layout == LayoutType.desktop,
),
),
),
),
child: _buildContent(),
),
)
],
);
});
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 10,
horizontal: _getPadding(context, layout),
),
child: _buildContent(),
),
)
],
);
},
);
}
}
33 changes: 33 additions & 0 deletions lib/widgets/buttons/logout_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LogoutButton extends StatelessWidget {
final bool popOnSuccess;
final bool iconOnly;
const LogoutButton({super.key, this.popOnSuccess = false, this.iconOnly = false});

void _handleSignOut(BuildContext context) async {
final auth = context.read<FirebaseAuth>();
await auth.signOut();
if (popOnSuccess) {
Navigator.pop(context);
}
}

@override
Widget build(BuildContext context) {
if (iconOnly) {
return IconButton(
onPressed: () => _handleSignOut(context),
icon: const Icon(Icons.logout),
);
}

return ElevatedButton.icon(
label: Text('Logout'),
onPressed: () => _handleSignOut(context),
icon: const Icon(Icons.logout),
);
}
}
15 changes: 15 additions & 0 deletions test/pages/home_page/settings_view/settings_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ void main() {
.thenAnswer((_) async => [user1, user2]);
});

testWidgets('is not visible for regular users', (tester) async {
await pumpPage(
Scaffold(body: SettingsView()),
tester,
callableService: mockCallableService,
userRole: UserRole.user,
);

expect(find.text('Set Admins'), findsNothing);
});

group('displays a list of users', () {
testWidgets(
'excluding the current user',
Expand All @@ -31,6 +42,7 @@ void main() {
tester,
callableService: mockCallableService,
auth: mockFirebaseAuth,
userRole: UserRole.admin,
);

await tester.tap(find.text('Set Admins'));
Expand All @@ -48,6 +60,7 @@ void main() {
Scaffold(body: SettingsView()),
tester,
callableService: mockCallableService,
userRole: UserRole.admin,
);

await tester.tap(find.text('Set Admins'));
Expand Down Expand Up @@ -75,6 +88,7 @@ void main() {
Scaffold(body: SettingsView()),
tester,
callableService: mockCallableService,
userRole: UserRole.admin,
);

await tester.tap(find.text('Set Admins'));
Expand All @@ -97,6 +111,7 @@ void main() {
Scaffold(body: SettingsView()),
tester,
callableService: mockCallableService,
userRole: UserRole.admin,
);

await tester.tap(find.text('Set Admins'));
Expand Down

0 comments on commit 6a1edfa

Please sign in to comment.