Skip to content

Commit

Permalink
Implement fetchUser() gRPC method in user_handler.dart and update…
Browse files Browse the repository at this point in the history
… `user_viewmodel.dart`
  • Loading branch information
carlobortolan committed Nov 25, 2023
1 parent a2b3cb1 commit 8435810
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
25 changes: 13 additions & 12 deletions lib/base/networking/api/user_handler.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import 'package:gocast_mobile/models/user/user_model.dart';
import 'package:gocast_mobile/base/helpers/model_generator.dart';
import 'package:gocast_mobile/base/networking/api/grpc_handler.dart';
import 'package:gocast_mobile/base/networking/api/gocast/api_v2.pbgrpc.dart';
import 'package:gocast_mobile/models/user/user_model.dart' as model;

/// Class responsible for fetching and posting user-related data (e.g., fetch user details, update usersettings, etc.)
class UserHandler {
// Generate user mock for testing the views until API/v2 is implemented
final GrpcHandler _grpcHandler;

/// Performs SSO authentication.
///
/// This method opens the SSO login page in a web view. After the user logs in,
/// it saves the JWT token and redirects back to the app.
///
/// Throws an [AppError] if a network error occurs or if no JWT-cookie is set.
UserHandler(this._grpcHandler);

static Future<User> fetchUser() async {
// TODO: Fetch user information from API (preferably over gRPC) - see issues
return ModelGenerator.generateRandomUser();
Future<model.User> fetchUser() async {
return await _grpcHandler.callGrpcMethod(
(client) async {
final response = await client.getUser(GetUserRequest());
// Deserialize the gRPC response into a User instance
return model.User.fromProto(response.user);
},
);
}
}
22 changes: 13 additions & 9 deletions lib/view_models/user_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'package:flutter/material.dart';
import 'package:gocast_mobile/base/networking/api/auth_handler.dart';
import 'package:gocast_mobile/base/networking/api/grpc_handler.dart';
import 'package:gocast_mobile/base/networking/api/user_handler.dart';
import 'package:gocast_mobile/models/user/user_model.dart';
import 'package:gocast_mobile/models/user/user_state_model.dart';
Expand All @@ -12,27 +13,30 @@ class UserViewModel {
BehaviorSubject<UserState> current =
BehaviorSubject.seeded(UserState.defaultConstructor());

// Sign in user and store cookie
final GrpcHandler _grpcHandler;

UserViewModel(this._grpcHandler);
Future<void> basicAuth(String email, String password) async {
await AuthHandler.basicAuth(email, password).then(
(value) => UserHandler.fetchUser().then(
(value) => current.value.setUser(value),
onError: (error) => current.addError(error),
),
(value) => _fetchUser(),
onError: (error) => current.addError(error),
);
}

Future<void> ssoAuth(BuildContext context) async {
await AuthHandler.ssoAuth(context).then(
(value) => UserHandler.fetchUser().then(
(value) => current.value.setUser(value),
onError: (error) => current.addError(error),
),
(value) => _fetchUser(),
onError: (error) => current.addError(error),
);
}

Future<void> _fetchUser() async {
await UserHandler(_grpcHandler).fetchUser().then(
(value) => current.value.setUser(value as User),
onError: (error) => current.addError(error),
);
}

Future<void> logout() async {
current.value.removeUser();
current.value = UserState.defaultConstructor();
Expand Down
4 changes: 3 additions & 1 deletion lib/views/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
leading: const CircleAvatar(
backgroundImage: AssetImage('path_to_your_profile_image'),
),
title: const Text('Max Mustermann'),
title: Text(
ref.read(userViewModel).current.value.user?.name ?? 'Guest',
),
onTap: () {
// Navigate to profile edit screen
},
Expand Down

0 comments on commit 8435810

Please sign in to comment.