Skip to content

Commit

Permalink
fix(login): Fixed some issues with the raffle & logging in. Where the…
Browse files Browse the repository at this point in the history
… user info was empty
  • Loading branch information
vanlooverenkoen committed Jan 23, 2024
1 parent ef72898 commit 8bc1559
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/model/error/invalid_userid_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class InvalidUserIdError extends Error {}
1 change: 1 addition & 0 deletions lib/model/error/invalid_username_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class InvalidUserNameError extends Error {}
1 change: 1 addition & 0 deletions lib/model/error/not_loggedin_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class NotLoggedInError extends Error {}
4 changes: 4 additions & 0 deletions lib/navigation/main_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class MainNavigator with BaseNavigator {
List<NavigatorObserver> get navigatorObservers => _navigatorObservers;

void showError(String title, {Object? error, StackTrace? trace}) {
final context = navigatorKey.currentContext;
if (context == null) return;
Flushbar(
title: title,
message: error.toString(),
Expand All @@ -40,6 +42,8 @@ class MainNavigator with BaseNavigator {
}

void showMessage(String message) {
final context = navigatorKey.currentContext;
if (context == null) return;
Flushbar(
message: message.toString(),
icon: const Icon(
Expand Down
27 changes: 9 additions & 18 deletions lib/navigation/main_navigator.navigator.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/repo/login_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class _LoginRepository implements LoginRepository {
authProvider = GoogleAuthProvider();
break;
}
await _firebaseAuth.signInWithPopup(authProvider);
final userCredentials = await _firebaseAuth.signInWithPopup(authProvider);
_user = userCredentials.user;
}

@override
Expand Down
11 changes: 9 additions & 2 deletions lib/viewmodel/login_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_belgium/model/data/login/login_type.dart';
import 'package:injectable/injectable.dart';
import 'package:flutter_belgium/navigation/main_navigator.dart';
Expand All @@ -18,7 +18,14 @@ class LoginViewModel with ChangeNotifier {
this._mainNavigator,
);

void init() {}
void init() {
if (_loginRepository.isLoggedIn) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_mainNavigator.goToRaffleScreen();
});
return;
}
}

Future<void> onLoginTapped(LoginType loginType) async {
try {
Expand Down
26 changes: 19 additions & 7 deletions lib/viewmodel/raffle_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:confetti/confetti.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_belgium/model/data/raffle/raffle.dart';
import 'package:flutter_belgium/repo/raffle_repo.dart';
import 'package:flutter_belgium/style/theme_duration.dart';
Expand Down Expand Up @@ -48,7 +48,15 @@ class RaffleViewModel with ChangeNotifier {
this._mainNavigator,
);

void init() => _getData();
void init() {
if (!_loginRepository.isLoggedIn) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_mainNavigator.goToLoginScreen();
});
return;
}
_getData();
}

@override
void dispose() {
Expand Down Expand Up @@ -101,12 +109,16 @@ class RaffleViewModel with ChangeNotifier {
}

Future<void> onEnterRaffleTapped() async {
final raffle = _raffle;
if (raffle == null) {
_mainNavigator.showError('No raffle found');
return;
try {
final raffle = _raffle;
if (raffle == null) {
_mainNavigator.showError('No raffle found');
return;
}
await _raffleRepository.enterRaffle(raffle.id);
} catch (e) {
_mainNavigator.showError('Failed to enter raffle', error: e);
}
await _raffleRepository.enterRaffle(raffle.id);
}

void onStartFortuneWheel() => _mainNavigator.goToRaffleWinnerPickerScreen();
Expand Down
8 changes: 7 additions & 1 deletion lib/widget/general/button.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_belgium/di/injectable.dart';
import 'package:flutter_belgium/navigation/main_navigator.dart';
import 'package:flutter_belgium/style/theme.dart';
import 'package:flutter_belgium/widget/general/loading.dart';

Expand Down Expand Up @@ -86,7 +88,11 @@ class _ButtonState extends State<Button> {
Future<void> _onTap() async {
if (_isLoading) return;
setState(() => _isLoading = true);
await widget.onTap();
try {
await widget.onTap();
} catch (e) {
getIt<MainNavigator>().showError('Failed to complete action', error: e);
}
if (!mounted) return;
setState(() => _isLoading = false);
}
Expand Down

0 comments on commit 8bc1559

Please sign in to comment.