Skip to content

Commit

Permalink
Fix login callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes committed Sep 28, 2023
1 parent 25fa4b2 commit 172d85e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions uni/lib/model/providers/startup/session_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SessionProvider extends StateProviderNotifier {
);
} catch (e) {
throw InternetStatusException(
Provider.of<LocaleNotifier>(context).getLocale(),
Provider.of<LocaleNotifier>(context, listen: false).getLocale(),
);
}

Expand All @@ -93,7 +93,7 @@ class SessionProvider extends StateProviderNotifier {
throw ExpiredCredentialsException();
} else {
throw WrongCredentialsException(
Provider.of<LocaleNotifier>(context).getLocale(),
Provider.of<LocaleNotifier>(context, listen: false).getLocale(),
);
}
}
Expand Down
1 change: 0 additions & 1 deletion uni/lib/model/providers/state_provider_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ abstract class StateProviderNotifier extends ChangeNotifier {
_initializedFromRemote = false;
_status = _initialStatus;
_lastUpdateTime = null;
notifyListeners();
}

void _updateStatus(RequestStatus status) {
Expand Down
32 changes: 16 additions & 16 deletions uni/lib/view/login/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:uni/generated/l10n.dart';
import 'package:uni/model/entities/login_exceptions.dart';
import 'package:uni/model/providers/startup/session_provider.dart';
import 'package:uni/model/providers/state_providers.dart';
import 'package:uni/model/request_status.dart';
import 'package:uni/utils/drawer_items.dart';
import 'package:uni/view/common_widgets/toast_message.dart';
import 'package:uni/view/home/widgets/exit_app_dialog.dart';
Expand Down Expand Up @@ -40,16 +39,19 @@ class LoginPageViewState extends State<LoginPageView> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
bool _keepSignedIn = true;
bool _obscurePasswordInput = true;
bool _loggingIn = false;

Future<void> _login(BuildContext context) async {
final stateProviders = StateProviders.fromContext(context);
final sessionProvider = stateProviders.sessionProvider;
if (sessionProvider.status != RequestStatus.busy &&
_formKey.currentState!.validate()) {
if (!_loggingIn && _formKey.currentState!.validate()) {
final user = usernameController.text.trim();
final pass = passwordController.text.trim();

try {
setState(() {
_loggingIn = true;
});
await sessionProvider.postAuthentication(
context,
user,
Expand All @@ -58,9 +60,18 @@ class LoginPageViewState extends State<LoginPageView> {
persistentSession: _keepSignedIn,
);
if (context.mounted) {
handleLogin(sessionProvider.status, context);
await Navigator.pushReplacementNamed(
context,
'/${DrawerItem.navPersonalArea.title}',
);
setState(() {
_loggingIn = false;
});
}
} catch (error) {
setState(() {
_loggingIn = false;
});
if (error is ExpiredCredentialsException) {
updatePasswordDialog();
} else if (error is InternetStatusException) {
Expand Down Expand Up @@ -250,7 +261,7 @@ class LoginPageViewState extends State<LoginPageView> {
Widget createStatusWidget(BuildContext context) {
return Consumer<SessionProvider>(
builder: (context, sessionProvider, _) {
if (sessionProvider.status == RequestStatus.busy) {
if (_loggingIn) {
return const SizedBox(
height: 60,
child:
Expand All @@ -262,17 +273,6 @@ class LoginPageViewState extends State<LoginPageView> {
);
}

void handleLogin(RequestStatus? status, BuildContext context) {
if (status == RequestStatus.successful) {
Navigator.pushReplacementNamed(
context,
'/${DrawerItem.navPersonalArea.title}',
);
} else if (status == RequestStatus.failed) {
ToastMessage.error(context, S.of(context).failed_login);
}
}

void updatePasswordDialog() {
showDialog<void>(
context: context,
Expand Down

0 comments on commit 172d85e

Please sign in to comment.