Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsreichardt committed Mar 1, 2024
1 parent 7c55c3b commit f288cb3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/lib/main/sharezone_bloc_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import 'package:sharezone/settings/src/subpages/imprint/bloc/imprint_bloc_factor
import 'package:sharezone/settings/src/subpages/imprint/gateway/imprint_gateway.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_type_of_user/change_type_of_user_analytics.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_type_of_user/change_type_of_user_controller.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_type_of_user/change_type_of_user_repository.dart';
import 'package:sharezone/settings/src/subpages/timetable/bloc/timetable_settings_bloc_factory.dart';
import 'package:sharezone/settings/src/subpages/timetable/time_picker_settings_cache.dart';
import 'package:sharezone/sharezone_plus/page/sharezone_plus_page_controller.dart';
Expand Down Expand Up @@ -365,6 +366,9 @@ class _SharezoneBlocProvidersState extends State<SharezoneBlocProviders> {
),
ChangeNotifierProvider(
create: (context) => ChangeTypeOfUserController(
repository: ChangeTypeOfUserRepository(
functions: widget.blocDependencies.functions,
),
typeOfUserStream: typeOfUserStream,
analytics: ChangeTypeOfUserAnalytics(analytics),
userId: UserId(api.uID),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'dart:async';

import 'package:cloud_functions/cloud_functions.dart';
import 'package:common_domain_models/common_domain_models.dart';
import 'package:flutter/material.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_type_of_user/change_type_of_user_analytics.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_type_of_user/change_type_of_user_repository.dart';
import 'package:user/user.dart';

class ChangeTypeOfUserController extends ChangeNotifier {
/// The user ID of the user whose type of user should be changed.
final UserId userId;
final ChangeTypeOfUserAnalytics analytics;
final ChangeTypeOfUserRepository repository;
StreamSubscription<TypeOfUser?>? _typeOfUserSubscription;

late ChangeTypeOfUserState state;
Expand All @@ -22,6 +24,7 @@ class ChangeTypeOfUserController extends ChangeNotifier {
ChangeTypeOfUserController({
required this.userId,
required this.analytics,
required this.repository,
required Stream<TypeOfUser?> typeOfUserStream,
}) {
state = const ChangeTypeOfUserInitial();
Expand All @@ -38,29 +41,27 @@ class ChangeTypeOfUserController extends ChangeNotifier {
Future<void> changeTypeOfUser() async {
final typeOfUser = selectedTypeOfUser;
if (typeOfUser == null) {
state = const NoTypeOfUserSelectedException();
notifyListeners();
return;
throw const NoTypeOfUserSelectedException();
}

if (typeOfUser == initialTypeOfUser) {
state = const TypeUserOfUserHasNotChangedException();
notifyListeners();
return;
throw const TypeUserOfUserHasNotChangedException();
}

try {
// Call backend...
await Future.delayed(const Duration(seconds: 1));
await repository.changeTypeOfUser(typeOfUser);

analytics.logChangedOrder(
from: initialTypeOfUser,
to: typeOfUser,
);
initialTypeOfUser = typeOfUser;
state = const ChangedTypeOfUserSuccessfully();
} on FirebaseFunctionsException catch (e) {
throw ChangeTypeOfUserUnknownException(
'[${e.plugin}/${e.code}] ${e.message}');
} catch (e) {
state = ChangeTypeOfUserUnknownException(e);
throw ChangeTypeOfUserUnknownException(e);
} finally {
notifyListeners();
}
Expand Down Expand Up @@ -90,7 +91,7 @@ class ChangedTypeOfUserSuccessfully extends ChangeTypeOfUserState {
const ChangedTypeOfUserSuccessfully();
}

sealed class ChangeTypeOfUserFailed extends ChangeTypeOfUserState {
sealed class ChangeTypeOfUserFailed {
const ChangeTypeOfUserFailed();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class _SaveFab extends StatelessWidget {
TypeUserOfUserHasNotChangedException() =>
'Der Account-Typ hat sich nicht geändert.',
},
context: context,
);
}

Expand All @@ -60,6 +61,7 @@ class _SaveFab extends StatelessWidget {
await controller.changeTypeOfUser();

if (context.mounted) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
_showRestartDialog(context);
}
} on ChangeTypeOfUserFailed catch (e) {
Expand Down Expand Up @@ -112,7 +114,8 @@ class _ChangeTypeOfUser extends StatelessWidget {
@override
Widget build(BuildContext context) {
final controller = context.watch<ChangeTypeOfUserController>();
final selectedTypeOfUser = controller.selectedTypeOfUser;
final selectedTypeOfUser =
controller.selectedTypeOfUser ?? controller.initialTypeOfUser;
return Column(
children: [
for (final typeOfUser in [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:cloud_functions/cloud_functions.dart';
import 'package:user/user.dart';

class ChangeTypeOfUserRepository {
final FirebaseFunctions _functions;

const ChangeTypeOfUserRepository({
required FirebaseFunctions functions,
}) : _functions = functions;

Future<void> changeTypeOfUser(TypeOfUser typeOfUser) async {
final callable = _functions.httpsCallable('changeTypeOfUser');
await callable.call({
'type': typeOfUser.name,
});
}
}

0 comments on commit f288cb3

Please sign in to comment.