Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete account bug #1118

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ awsconfiguration.json
lib/api_prod.dart
/ios/Flutter/.last_build_id
/ios/Runner/GoogleService-Info.plist
Podfile.lock
118 changes: 0 additions & 118 deletions ios/Podfile.lock

This file was deleted.

58 changes: 39 additions & 19 deletions lib/app/material_app_with_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ import 'package:junto_beta_mobile/screens/welcome/bloc/bloc.dart';
import 'package:junto_beta_mobile/screens/welcome/sign_up_agreement.dart';
import 'package:junto_beta_mobile/screens/welcome/welcome.dart';
import 'package:junto_beta_mobile/widgets/background/background_theme.dart';
import 'package:junto_beta_mobile/widgets/fade_route.dart';
import 'package:junto_beta_mobile/widgets/progress_indicator.dart';
import 'package:provider/provider.dart';

class MaterialAppWithTheme extends StatelessWidget {
final GlobalKey<NavigatorState> _navKey = GlobalKey();

@override
Widget build(BuildContext context) {
return Consumer<JuntoThemesProvider>(
builder: (context, theme, _) {
return MaterialApp(
home: HomePage(),
navigatorKey: _navKey,
home: HomePage(navKey: _navKey),
title: 'JUNTO Alpha',
debugShowCheckedModeBanner: false,
theme: theme.currentTheme,
Expand All @@ -47,43 +51,58 @@ class MaterialAppWithTheme extends StatelessWidget {
}
}

class HomePage extends StatelessWidget {
const HomePage({Key key}) : super(key: key);
class HomePage extends StatefulWidget {
const HomePage({Key key, @required this.navKey}) : super(key: key);

final GlobalKey<NavigatorState> navKey;

@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
context.bloc<AuthBloc>().listen(_buildChildFor);
}

Widget _buildChildFor({@required AuthState state}) {
void _buildChildFor(AuthState state) {
if (state is AuthLoading) {
return HomeLoadingPage();
widget.navKey?.currentState
?.pushReplacement(FadeRoute(child: HomeLoadingPage()));
} else if (state is AuthAgreementsRequired) {
return SignUpAgreements();
widget.navKey?.currentState
?.pushReplacement(FadeRoute(child: SignUpAgreements()));
} else if (state is AuthAuthenticated) {
return HomePageContent();
} else if (state is AuthUnauthenticated) {
return Welcome();
widget.navKey?.currentState
?.pushReplacement(FadeRoute(child: HomePageContent()));
} else {
return Welcome();
widget.navKey?.currentState?.pushReplacement(FadeRoute(child: Welcome()));
}
}

@override
Widget build(BuildContext context) {
return BlocBuilder<AuthBloc, AuthState>(
builder: (context, state) {
return Stack(
children: <Widget>[
BackgroundTheme(),
AnimatedSwitcher(
duration: kThemeAnimationDuration,
child: _buildChildFor(state: state),
),
],
);
if (state is AuthLoading) {
return HomeLoadingPage();
} else if (state is AuthAgreementsRequired) {
return SignUpAgreements();
} else if (state is AuthAuthenticated) {
return HomePageContent();
} else {
return Welcome();
}
},
);
}
}

class HomePageContent extends StatefulWidget {
const HomePageContent({Key key}) : super(key: key);

@override
State<StatefulWidget> createState() {
return HomePageContentState();
Expand Down Expand Up @@ -141,6 +160,7 @@ class HomePageContentState extends State<HomePageContent>

class HomeLoadingPage extends StatelessWidget {
const HomeLoadingPage({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
Expand Down
22 changes: 21 additions & 1 deletion lib/screens/welcome/bloc/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
if (event is RefreshUser) {
yield* _mapRefreshUser(event);
}
if (event is DeleteUser) {
yield* _mapDeleteUser(event);
}
}

Stream<AuthState> _mapDeleteUser(DeleteUser event) async* {
yield AuthState.loading();
try {
await userRepo.deleteUserAccount(event.userAddress);
await await authRepo.logoutUser();
yield AuthState.unauthenticated();
} on JuntoException catch (e) {
logger.logError('Error during delete user: ${e.message}');
await authRepo.logoutUser();
yield AuthState.unauthenticated(error: true, errorMessage: e.message);
} catch (error) {
logger.logException(error);
yield AuthState.unauthenticated(error: true, errorMessage: error.message);
}
}

Stream<AuthState> _mapSignUpEventState(SignUpEvent event) async* {
Expand All @@ -83,6 +102,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
await userDataProvider.updateUser(userData);
await userDataProvider.initialize();
await onBoardingRepo.loadTutorialState();
logger.logDebug('Loading new state agreementsRequired');
yield AuthState.agreementsRequired(userData);
} on JuntoException catch (e) {
logger.logError('Error during sign up: ${e.message}');
Expand All @@ -91,7 +111,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
yield AuthState.unauthenticated();
yield AuthState.unauthenticated(error: true, errorMessage: e.message);
} catch (error) {
logger.logException(error);
logger.logException(error, null, "Sign up event");
yield AuthState.unauthenticated();
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/screens/welcome/bloc/auth_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ class LoggedInEvent extends AuthEvent {}
class LogoutEvent extends AuthEvent {}

class RefreshUser extends AuthEvent {}

class DeleteUser extends AuthEvent {
DeleteUser(this.userAddress);
final String userAddress;
}
20 changes: 7 additions & 13 deletions lib/widgets/dialogs/delete_account_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:junto_beta_mobile/backend/backend.dart';
import 'package:provider/provider.dart';
import 'package:junto_beta_mobile/widgets/dialogs/single_action_dialog.dart';
import 'package:junto_beta_mobile/utils/junto_overlay.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:junto_beta_mobile/app/logger/logger.dart';
import 'package:junto_beta_mobile/backend/backend.dart';
import 'package:junto_beta_mobile/screens/welcome/bloc/auth_bloc.dart';
import 'package:junto_beta_mobile/screens/welcome/bloc/auth_event.dart';
import 'package:junto_beta_mobile/widgets/fade_route.dart';
import 'package:junto_beta_mobile/screens/welcome/welcome.dart';
import 'package:junto_beta_mobile/utils/junto_overlay.dart';
import 'package:junto_beta_mobile/widgets/dialogs/single_action_dialog.dart';
import 'package:junto_beta_mobile/widgets/fade_route.dart';

class DeleteAccountDialog extends StatelessWidget {
const DeleteAccountDialog({this.buildContext, this.user});
Expand All @@ -20,17 +19,12 @@ class DeleteAccountDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
final deleteController = TextEditingController();
final bloc = context.bloc<AuthBloc>();
Future<void> _deleteAccount() async {
JuntoLoader.showLoader(context);
try {
// Delete user account
await Provider.of<UserRepo>(context, listen: false)
.deleteUserAccount(user.userAddress);
// Hide Junto Loader
JuntoLoader.showLoader(context);
bloc.add(DeleteUser(user.userAddress));
JuntoLoader.hide();
// Log user out
await context.bloc<AuthBloc>().add(LogoutEvent());
// Bring user back to Welcome screen after logging out
Navigator.of(context).pushAndRemoveUntil(
FadeRoute(child: Welcome()),
ModalRoute.withName('/'),
Expand Down