6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit 05dd386a

AuthorEdmir Suljic<esu@dwarf.dk>
Date2025-05-28 13:24:21 +0200
Profile page: fixed bugs

Changed files

.../lib/profile/cubit/profile_cubit.dart           | 29 +++++++++++++---
 comwell_key_app/lib/profile/profile_page.dart      | 39 +++++++++++++++++++---
 .../lib/profile/profile_repository.dart            |  8 +++++
 .../cubit/profile_settings_cubit.dart              |  1 +
 comwell_key_app/lib/routing/app_router.dart        |  2 +-
 5 files changed, 69 insertions(+), 10 deletions(-)

Diff

diff --git a/comwell_key_app/lib/profile/cubit/profile_cubit.dart b/comwell_key_app/lib/profile/cubit/profile_cubit.dart
index 3267d150..5af635e6 100644
--- a/comwell_key_app/lib/profile/cubit/profile_cubit.dart
+++ b/comwell_key_app/lib/profile/cubit/profile_cubit.dart
@@ -28,25 +28,46 @@ class ProfileCubit extends Cubit<ProfileState> {
);
}
-
- Future<String> getVersion() async {
+ Future<String> getVersion() async {
final packageInfo = await PackageInfo.fromPlatform();
return packageInfo.version;
}
void logOutPressed() {
authenticationRepository.logOut();
- emit(const ProfileState(isLoading: false, user: null, error: null, isToSAccepted: false, isNewsletterAccepted: false));
+ emit(const ProfileState(
+ isLoading: false,
+ user: null,
+ error: null,
+ isToSAccepted: false,
+ isNewsletterAccepted: false));
}
void deleteAllInfoOnLogOut() async {
- emit(const ProfileState(isLoading: false, user: null, error: null, isToSAccepted: false, isNewsletterAccepted: false));
+ emit(const ProfileState(
+ isLoading: false,
+ user: null,
+ error: null,
+ isToSAccepted: false,
+ isNewsletterAccepted: false));
}
void init() async {
emit(const ProfileState(isLoading: true, user: null, error: null));
try {
final user = await profileRepository.fetchProfileSettings();
+ sendPageViewEvent();
+ emit(ProfileState(user: user, isLoading: false));
+ } catch (e) {
+ emit(state.setError(Error()));
+ }
+ }
+
+ void fetchRemoteProfile() async {
+ emit(const ProfileState(isLoading: true, user: null, error: null));
+ try {
+ final user = await profileRepository.fetchRemoteProfile();
+
emit(ProfileState(user: user, isLoading: false));
} catch (e) {
emit(state.setError(Error()));
diff --git a/comwell_key_app/lib/profile/profile_page.dart b/comwell_key_app/lib/profile/profile_page.dart
index 88ecfa17..7a6f76b3 100644
--- a/comwell_key_app/lib/profile/profile_page.dart
+++ b/comwell_key_app/lib/profile/profile_page.dart
@@ -6,15 +6,44 @@ import 'package:comwell_key_app/utils/secure_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
-class ProfilePage extends StatelessWidget {
+class ProfilePage extends StatefulWidget {
+ const ProfilePage({super.key});
+
+ @override
+ State<ProfilePage> createState() => _ProfilePageState();
+}
+
+class _ProfilePageState extends State<ProfilePage> with WidgetsBindingObserver {
final SecureStorage secureStorage = SecureStorage();
+ late final ProfileCubit _cubit = context.read<ProfileCubit>();
- ProfilePage({super.key});
+ @override
+ void initState() {
+ WidgetsBinding.instance.addObserver(this);
+ super.initState();
+ }
+
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ _cubit.fetchRemoteProfile();
+ }
+
+ @override
+ void dispose() {
+ WidgetsBinding.instance.removeObserver(this);
+ super.dispose();
+ }
+
+ @override
+ void didChangeAppLifecycleState(AppLifecycleState state) {
+ if (state == AppLifecycleState.resumed && mounted) {
+ _cubit.fetchRemoteProfile();
+ }
+ }
@override
Widget build(BuildContext context) {
- final cubit = context.read<ProfileCubit>();
- cubit.sendPageViewEvent();
return Scaffold(
backgroundColor: sandColor[20],
@@ -25,7 +54,7 @@ class ProfilePage extends StatelessWidget {
} else if (state.error != null) {
return const ErrorPageWidget();
} else {
- return ProfilePageWidget(cubit: cubit);
+ return ProfilePageWidget(cubit: _cubit);
}
},
),
diff --git a/comwell_key_app/lib/profile/profile_repository.dart b/comwell_key_app/lib/profile/profile_repository.dart
index 08870f25..4fd3c557 100644
--- a/comwell_key_app/lib/profile/profile_repository.dart
+++ b/comwell_key_app/lib/profile/profile_repository.dart
@@ -37,6 +37,14 @@ class ProfileRepository {
return updatedUser;
}
+ Future<User> fetchRemoteProfile() async {
+ final response = await api.fetchProfileSettings();
+ final data = response.data as Json;
+ final userDto = UserDto.fromJson(data);
+ final user = userDto.toUser();
+ return user;
+ }
+
Future<User> fetchProfileSettings() async {
try {
final user = await db.userDAO.getUser();
diff --git a/comwell_key_app/lib/profile_settings/cubit/profile_settings_cubit.dart b/comwell_key_app/lib/profile_settings/cubit/profile_settings_cubit.dart
index 52a8f0c3..b87937b9 100644
--- a/comwell_key_app/lib/profile_settings/cubit/profile_settings_cubit.dart
+++ b/comwell_key_app/lib/profile_settings/cubit/profile_settings_cubit.dart
@@ -35,6 +35,7 @@ class ProfileSettingsCubit extends Cubit<ProfileSettingsState> {
firstNameController.text = user.firstName;
lastNameController.text = user.lastName;
phoneNumberController.text = user.phoneNumber;
+
emit(ProfileSettingsState(isLoading: false, user: user, error: null));
} catch (e) {
emit(ProfileSettingsState(isLoading: false, user: null, error: Error()));
diff --git a/comwell_key_app/lib/routing/app_router.dart b/comwell_key_app/lib/routing/app_router.dart
index 05a5616f..67019452 100644
--- a/comwell_key_app/lib/routing/app_router.dart
+++ b/comwell_key_app/lib/routing/app_router.dart
@@ -211,7 +211,7 @@ GoRouter goRouter() {
authenticationRepository:
locator<AuthenticationRepository>())
..init(),
- child: ProfilePage(), //mobileKey: key);
+ child: const ProfilePage(), //mobileKey: key);
);
},
routes: [