6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit 4198eeeb

AuthorEdmir Suljic<esu@dwarf.dk>
Date2025-04-23 14:05:32 +0200
1515: Resolved PR comments

Changed files

.gitignore                                         |   3 +
 .../models/notification_permission.g.dart          |   4 +
 .../lib/.generated/overview/models/booking.g.dart  |   2 +-
 .../lib/database/daos/notifications_dao.dart       |  10 +-
 .../components/communications_list.dart            |  52 ++++
 .../notifications/cubit/notifications_cubit.dart   |  93 ++++++--
 .../notifications/cubit/notifications_state.dart   |  35 ++-
 .../models/notification_permission.dart            |  41 +++-
 .../lib/notifications/notifications_page.dart      | 264 ++++++---------------
 .../notifications/notifications_repository.dart    |  49 ++--
 .../lib/profile/profile_repository.dart            |  20 --
 .../repostiory/profile_settings_repository.dart    |   4 +-
 comwell_key_app/lib/services/api.dart              |   8 +-
 .../profile_settings_cubit_test.dart               |   2 +-
 14 files changed, 300 insertions(+), 287 deletions(-)

Diff

diff --git a/.gitignore b/.gitignore
index e69de29b..d16d260a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -0,0 +1,3 @@
+comwell_key_app/ios/Runner/Runner.entitlements
+comwell_key_app/ios/Runner.xcodeproj/project.pbxproj
+comwell_key_app/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
diff --git a/comwell_key_app/lib/.generated/notifications/models/notification_permission.g.dart b/comwell_key_app/lib/.generated/notifications/models/notification_permission.g.dart
index 959a65f7..c4aafa0e 100644
--- a/comwell_key_app/lib/.generated/notifications/models/notification_permission.g.dart
+++ b/comwell_key_app/lib/.generated/notifications/models/notification_permission.g.dart
@@ -10,6 +10,8 @@ NotificationPermission _$NotificationPermissionFromJson(Map json) =>
NotificationPermission(
id: (json['id'] as num).toInt(),
name: json['name'] as String,
+ notificationPermissionDescription:
+ json['notificationPermissionDescription'] as String?,
allowed: json['allowed'] as bool,
);
@@ -18,5 +20,7 @@ Map<String, dynamic> _$NotificationPermissionToJson(
<String, dynamic>{
'id': instance.id,
'name': instance.name,
+ 'notificationPermissionDescription':
+ instance.notificationPermissionDescription,
'allowed': instance.allowed,
};
diff --git a/comwell_key_app/lib/.generated/overview/models/booking.g.dart b/comwell_key_app/lib/.generated/overview/models/booking.g.dart
index 105a7e31..21cba377 100644
--- a/comwell_key_app/lib/.generated/overview/models/booking.g.dart
+++ b/comwell_key_app/lib/.generated/overview/models/booking.g.dart
@@ -46,8 +46,8 @@ Map<String, dynamic> _$BookingToJson(Booking instance) => <String, dynamic>{
'bookingDate': instance.bookingDate.toIso8601String(),
'paymentDetails': instance.paymentDetails.toJson(),
'confirmationId': instance.confirmationId,
- 'guests': instance.guests.map((e) => e.toJson()).toList(),
'digitalCard': instance.digitalCard,
+ 'guests': instance.guests.map((e) => e.toJson()).toList(),
};
const _$BookingStatusEnumMap = {
diff --git a/comwell_key_app/lib/database/daos/notifications_dao.dart b/comwell_key_app/lib/database/daos/notifications_dao.dart
index d9d0f697..51f26bcc 100644
--- a/comwell_key_app/lib/database/daos/notifications_dao.dart
+++ b/comwell_key_app/lib/database/daos/notifications_dao.dart
@@ -16,16 +16,10 @@ class NotificationPermissionDAO extends DatabaseAccessor<ComwellDatabase>
Future<void> saveNotificationPermission(
Iterable<NotificationPermission> permissions) async {
final permissionsList = permissions.toList();
- final json = jsonEncode(
- permissionsList.map((permission) => permission.toJson()).toList());
- final insertOp = NotificationPermissionEntityCompanion.insert(
- id: permissionsList.first.id, json: json);
- await insert(permissionsList, insertOp);
- await getNotificationPermissions();
+ await insert(permissionsList);
}
- Future<void> insert(Iterable<NotificationPermission> permissions,
- NotificationPermissionEntityCompanion insertOp) async {
+ Future<void> insert(Iterable<NotificationPermission> permissions) async {
final entities = permissions.map((permission) {
final json = jsonEncode(permission.toJson());
return NotificationPermissionEntityCompanion.insert(
diff --git a/comwell_key_app/lib/notifications/components/communications_list.dart b/comwell_key_app/lib/notifications/components/communications_list.dart
new file mode 100644
index 00000000..e8359985
--- /dev/null
+++ b/comwell_key_app/lib/notifications/components/communications_list.dart
@@ -0,0 +1,52 @@
+import 'package:comwell_key_app/notifications/models/notification_permission.dart';
+import 'package:comwell_key_app/themes/light_theme.dart';
+import 'package:flutter/material.dart';
+
+class CommunicationsList extends StatelessWidget {
+ final Iterable<NotificationPermission> notificationPermissions;
+ final void Function(String) valueChanged;
+
+ const CommunicationsList({
+ super.key,
+ required this.notificationPermissions,
+ required this.valueChanged,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ return SingleChildScrollView(
+ child: ListView.builder(
+ shrinkWrap: true,
+ itemCount: notificationPermissions.length,
+ itemBuilder: (context, index) {
+ return SwitchListTile(
+ activeColor: sandColor,
+ inactiveTrackColor: Colors.grey[200],
+ inactiveThumbColor: Colors.white,
+ trackOutlineColor: const WidgetStatePropertyAll(Colors.white),
+ title: Text(
+ notificationPermissions.elementAt(index).name,
+ style: const TextStyle(fontSize: 16),
+ ),
+ subtitle: Text(
+ notificationPermissions
+ .elementAt(index)
+ .notificationPermissionDescription ??
+ '',
+ style: const TextStyle(
+ fontSize: 12,
+ color: Colors.grey,
+ fontWeight: FontWeight.w400,
+ ),
+ ),
+ value: notificationPermissions.elementAt(index).allowed,
+ onChanged: (bool value) {
+ valueChanged(notificationPermissions.elementAt(index).name);
+ },
+ contentPadding: EdgeInsets.zero,
+ );
+ },
+ ),
+ );
+ }
+}
diff --git a/comwell_key_app/lib/notifications/cubit/notifications_cubit.dart b/comwell_key_app/lib/notifications/cubit/notifications_cubit.dart
index 8aadee87..44c4475a 100644
--- a/comwell_key_app/lib/notifications/cubit/notifications_cubit.dart
+++ b/comwell_key_app/lib/notifications/cubit/notifications_cubit.dart
@@ -7,46 +7,87 @@ import 'package:flutter_bloc/flutter_bloc.dart';
class NotificationsCubit extends Cubit<NotificationsState> {
final NotificationsRepository notificationsRepository;
NotificationsCubit(this.notificationsRepository)
- : super(const NotificationsState(allNotifications: []));
+ : super(const NotificationsState(
+ allNotifications: [],
+ isLoading: false,
+ error: null,
+ ));
Iterable<NotificationPermission> notificationPermissions = [];
- User? user;
+ late User user;
+
+ // List of permission types that should be shown in the UI
+ final List<NotificationPermissionType> _visiblePermissionTypes = [
+ NotificationPermissionType.b2bNewsletter,
+ NotificationPermissionType.ccSms,
+ NotificationPermissionType.ccDigital,
+ NotificationPermissionType.companyNotifications,
+ ];
+
+ Iterable<NotificationPermission> _filterVisiblePermissions(
+ Iterable<NotificationPermission> permissions) {
+ return permissions.where((permission) => _visiblePermissionTypes
+ .any((type) => type.notificationPermissionId == permission.id));
+ }
void init() async {
- emit(NotificationsLoading(allNotifications: notificationPermissions));
- user = await notificationsRepository.fetchUser();
- notificationPermissions =
- await notificationsRepository.getNotificationPermissionsFromDb();
+ emit(NotificationsState(
+ allNotifications: notificationPermissions,
+ isLoading: true,
+ error: null,
+ ));
+ try {
+ user = await notificationsRepository.fetchUser();
+ notificationPermissions =
+ await notificationsRepository.fetchNotificationPermissions(user.id);
- if (notificationPermissions.isEmpty) {
- try {
- fetchNotificationPermissions(user!.id);
- } catch (e) {
- emit(NotificationsError(allNotifications: notificationPermissions));
- }
- } else if (user != null) {
- emit(NotificationsLoaded(allNotifications: notificationPermissions));
- } else {
- emit(NotificationsError(allNotifications: notificationPermissions));
+ notificationPermissions =
+ _filterVisiblePermissions(notificationPermissions);
+ emit(NotificationsState(
+ allNotifications: notificationPermissions,
+ isLoading: false,
+ error: null,
+ ));
+ } catch (e) {
+ emit(NotificationsState(
+ allNotifications: notificationPermissions,
+ isLoading: false,
+ error: Error(),
+ ));
}
}
void onNotificationPermissionClicked(String name) {
notificationPermissions = notificationPermissions
.map((permission) => permission.name == name
- ? permission.copyWith(allowed: !permission.allowed)
+ ? permission.copyWith(
+ allowed: !permission.allowed,
+ notificationPermissionDescription:
+ permission.notificationPermissionDescription)
: permission)
.toList();
- emit(NotificationsLoaded(allNotifications: notificationPermissions));
+ emit(NotificationsState(
+ allNotifications: notificationPermissions,
+ isLoading: false,
+ error: null,
+ ));
}
void updateAllPermissionsUI(bool value) {
notificationPermissions = notificationPermissions
- .map((permission) => permission.copyWith(allowed: value))
+ .map((permission) => permission.copyWith(
+ allowed: value,
+ notificationPermissionDescription:
+ permission.notificationPermissionDescription,
+ ))
.toList();
- emit(NotificationsLoaded(allNotifications: notificationPermissions));
+ emit(NotificationsState(
+ allNotifications: notificationPermissions,
+ isLoading: false,
+ error: null,
+ ));
}
void updatePreferences(
@@ -55,13 +96,21 @@ class NotificationsCubit extends Cubit<NotificationsState> {
await notificationsRepository.updateNotificationPreferences(
guestId, notificationPermissions);
- emit(NotificationsLoaded(allNotifications: notificationPermissions));
+ emit(NotificationsState(
+ allNotifications: notificationPermissions,
+ isLoading: false,
+ error: null,
+ ));
}
void fetchNotificationPermissions(int guestId) async {
notificationPermissions =
await notificationsRepository.fetchNotificationPermissions(guestId);
- emit(NotificationsState(allNotifications: notificationPermissions));
+ emit(NotificationsState(
+ allNotifications: notificationPermissions,
+ isLoading: false,
+ error: null,
+ ));
}
}
diff --git a/comwell_key_app/lib/notifications/cubit/notifications_state.dart b/comwell_key_app/lib/notifications/cubit/notifications_state.dart
index 1b4828a0..8f2d3882 100644
--- a/comwell_key_app/lib/notifications/cubit/notifications_state.dart
+++ b/comwell_key_app/lib/notifications/cubit/notifications_state.dart
@@ -2,11 +2,20 @@ import 'package:comwell_key_app/notifications/models/notification_permission.dar
import 'package:equatable/equatable.dart';
class NotificationsState extends Equatable {
+ final Error? error;
+ final bool isLoading;
final Iterable<NotificationPermission> allNotifications;
- const NotificationsState({required this.allNotifications});
+ const NotificationsState({
+ required this.allNotifications,
+ this.error,
+ required this.isLoading,
+ });
- NotificationsState.initial() : allNotifications = [];
+ NotificationsState.initial()
+ : allNotifications = [],
+ error = null,
+ isLoading = false;
NotificationsState notificationSelected({
required NotificationPermission notificationPermission,
@@ -15,30 +24,16 @@ class NotificationsState extends Equatable {
NotificationsState _copyWith({
Iterable<NotificationPermission>? allNotifications,
+ bool? isLoading,
+ Error? error,
}) {
return NotificationsState(
allNotifications: allNotifications ?? this.allNotifications,
+ isLoading: isLoading ?? this.isLoading,
+ error: error ?? this.error,
);
}
@override
List<Object> get props => [allNotifications];
}
-
-final class NotificationsLoading extends NotificationsState {
- const NotificationsLoading({required super.allNotifications});
-
- @override
- List<Object> get props => [allNotifications];
-}
-
-final class NotificationsLoaded extends NotificationsState {
- const NotificationsLoaded({required super.allNotifications});
-
- @override
- List<Object> get props => [allNotifications];
-}
-
-final class NotificationsError extends NotificationsState {
- const NotificationsError({required super.allNotifications});
-}
diff --git a/comwell_key_app/lib/notifications/models/notification_permission.dart b/comwell_key_app/lib/notifications/models/notification_permission.dart
index af3246a0..73725466 100644
--- a/comwell_key_app/lib/notifications/models/notification_permission.dart
+++ b/comwell_key_app/lib/notifications/models/notification_permission.dart
@@ -1,4 +1,5 @@
import 'package:comwell_key_app/utils/json.dart';
+import 'package:easy_localization/easy_localization.dart';
import 'package:json_annotation/json_annotation.dart';
part '../../.generated/notifications/models/notification_permission.g.dart';
@@ -40,10 +41,11 @@ enum NotificationPermissionType {
}
}
+ //TODO: Change displayname to correct name
String get displayName {
switch (this) {
case NotificationPermissionType.b2bNewsletter:
- return 'B2B Newsletter Permission';
+ return 'B2B Newsletter';
case NotificationPermissionType.ccEmail:
return 'CC Email Permission';
case NotificationPermissionType.ccDigital:
@@ -64,17 +66,45 @@ enum NotificationPermissionType {
return 'Similar products permissions';
}
}
+
+// Not all permissions are used so some of the descriptions are not implemented
+ String get notificationPermissionDescription {
+ switch (this) {
+ case NotificationPermissionType.b2bNewsletter:
+ return 'company_deal_subtitle'.tr();
+ case NotificationPermissionType.ccEmail:
+ return '';
+ case NotificationPermissionType.ccDigital:
+ return 'digital_media_subtitle'.tr();
+ case NotificationPermissionType.ccSms:
+ return 'sms_subtitle'.tr();
+ case NotificationPermissionType.b2bDigital:
+ return '';
+ case NotificationPermissionType.companyEmail:
+ return '';
+ case NotificationPermissionType.companyNotifications:
+ return 'club_newsletter_subtitle'.tr();
+ case NotificationPermissionType.stayEmails:
+ return '';
+ case NotificationPermissionType.appNotifications:
+ return '';
+ case NotificationPermissionType.similarProducts:
+ return '';
+ }
+ }
}
@JsonSerializable()
class NotificationPermission {
final int id;
final String name;
- bool allowed;
+ final String? notificationPermissionDescription;
+ final bool allowed;
NotificationPermission({
required this.id,
required this.name,
+ this.notificationPermissionDescription,
required this.allowed,
});
@@ -83,14 +113,17 @@ class NotificationPermission {
Json toJson() => _$NotificationPermissionToJson(this);
- NotificationPermission copyWith({bool? allowed}) => NotificationPermission(
+ NotificationPermission copyWith(
+ {bool? allowed, String? notificationPermissionDescription}) =>
+ NotificationPermission(
id: id,
name: name,
+ notificationPermissionDescription: notificationPermissionDescription,
allowed: allowed ?? this.allowed,
);
@override
String toString() {
- return 'NotificationPermission{id: $id, name: $name, allowed: $allowed}';
+ return 'NotificationPermission{id: $id, name: $name, allowed: $allowed, notificationPermissionDescription: $notificationPermissionDescription}';
}
}
diff --git a/comwell_key_app/lib/notifications/notifications_page.dart b/comwell_key_app/lib/notifications/notifications_page.dart
index 1ea96c51..ed59edb1 100644
--- a/comwell_key_app/lib/notifications/notifications_page.dart
+++ b/comwell_key_app/lib/notifications/notifications_page.dart
@@ -6,6 +6,7 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:comwell_key_app/notifications/cubit/notifications_cubit.dart';
+import 'components/communications_list.dart';
class NotificationsPage extends StatefulWidget {
final Iterable<NotificationPermission> notificationPermissions;
@@ -19,209 +20,90 @@ class _NotificationsPageState extends State<NotificationsPage> {
@override
Widget build(BuildContext context) {
final cubit = context.read<NotificationsCubit>();
- return BlocBuilder<NotificationsCubit, NotificationsState>(
+ return Scaffold(
+ backgroundColor: Colors.white,
+ appBar: const ComwellAppBar(),
+ body: BlocBuilder<NotificationsCubit, NotificationsState>(
builder: (context, state) {
- if (state is NotificationsLoading) {
- return const Scaffold(
- backgroundColor: Colors.white,
- appBar: ComwellAppBar(),
- body: Center(child: CircularProgressIndicator()));
- } else if (state is NotificationsLoaded) {
- return _buildNotificationsPage(cubit);
- } else {
- return Scaffold(
- backgroundColor: Colors.white,
- appBar: const ComwellAppBar(),
- body: Center(child: Text("notifications_error".tr())));
- }
- });
+ if (state.isLoading) {
+ return const Center(child: CircularProgressIndicator());
+ } else if (state.error != null) {
+ return Center(
+ child: Text(
+ "notifications_error".tr(),
+ ),
+ );
+ } else {
+ return _buildNotificationsPage(cubit);
+ }
+ },
+ ),
+ );
}
Widget _buildNotificationsPage(NotificationsCubit cubit) {
- return Scaffold(
- backgroundColor: Colors.white,
- appBar: const ComwellAppBar(),
- body: SafeArea(
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- 'notifications_page_title'.tr(),
- style: const TextStyle(
- fontSize: 28,
- fontWeight: FontWeight.w600,
- ),
+ return SafeArea(
+ child: Padding(
+ padding: const EdgeInsets.all(16.0),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(
+ 'notifications_page_title'.tr(),
+ style: const TextStyle(
+ fontSize: 28,
+ fontWeight: FontWeight.w600,
),
- const SizedBox(height: 20),
- SwitchListTile(
- activeColor: sandColor,
- inactiveTrackColor: Colors.grey[200],
- inactiveThumbColor: Colors.white,
- trackOutlineColor: const WidgetStatePropertyAll(Colors.white),
- title: Text(
- 'subscribe_all'.tr(),
- style: const TextStyle(fontSize: 16),
- ),
- value: cubit.state.allNotifications.every((e) => e.allowed),
- onChanged: (bool value) {
- cubit.updateAllPermissionsUI(value);
+ ),
+ const SizedBox(height: 20),
+ SwitchListTile(
+ activeColor: sandColor,
+ inactiveTrackColor: Colors.grey[200],
+ inactiveThumbColor: Colors.white,
+ trackOutlineColor: const WidgetStatePropertyAll(Colors.white),
+ title: Text(
+ 'subscribe_all'.tr(),
+ style: const TextStyle(fontSize: 16),
+ ),
+ value: cubit.state.allNotifications.every((e) => e.allowed),
+ onChanged: (bool value) {
+ cubit.updateAllPermissionsUI(value);
+ },
+ contentPadding: EdgeInsets.zero,
+ ),
+ const Divider(),
+ Expanded(
+ child: CommunicationsList(
+ notificationPermissions: cubit.state.allNotifications,
+ valueChanged: (String name) {
+ cubit.onNotificationPermissionClicked(name);
},
- contentPadding: EdgeInsets.zero,
),
- const Divider(),
- Expanded(
- child: SingleChildScrollView(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- SwitchListTile(
- activeColor: sandColor,
- inactiveTrackColor: Colors.grey[200],
- inactiveThumbColor: Colors.white,
- trackOutlineColor:
- const WidgetStatePropertyAll(Colors.white),
- title: Text(
- 'club_newsletter'.tr(),
- style: const TextStyle(fontSize: 16),
- ),
- subtitle: Text(
- 'club_newsletter_subtitle'.tr(),
- style: const TextStyle(
- fontSize: 12,
- color: Colors.grey,
- fontWeight: FontWeight.w400),
- ),
- value: cubit.state.allNotifications
- .firstWhere((permission) =>
- permission.name ==
- NotificationPermissionType
- .b2bNewsletter.displayName)
- .allowed,
- onChanged: (bool value) {
- cubit.onNotificationPermissionClicked(
- NotificationPermissionType
- .b2bNewsletter.displayName);
- },
- contentPadding: EdgeInsets.zero,
- ),
- SwitchListTile(
- activeColor: sandColor,
- inactiveTrackColor: Colors.grey[200],
- inactiveThumbColor: Colors.white,
- trackOutlineColor:
- const WidgetStatePropertyAll(Colors.white),
- title: Text(
- 'digital_media'.tr(),
- style: const TextStyle(fontSize: 16),
- ),
- subtitle: Text(
- 'digital_media_subtitle'.tr(),
- style: const TextStyle(
- fontSize: 12,
- color: Colors.grey,
- fontWeight: FontWeight.w400),
- ),
- value: cubit.state.allNotifications
- .firstWhere((permission) =>
- permission.name ==
- NotificationPermissionType
- .ccDigital.displayName)
- .allowed,
- onChanged: (bool value) {
- cubit.onNotificationPermissionClicked(
- NotificationPermissionType.ccDigital.displayName);
- },
- contentPadding: EdgeInsets.zero,
- ),
- SwitchListTile(
- activeColor: sandColor,
- inactiveTrackColor: Colors.grey[200],
- inactiveThumbColor: Colors.white,
- trackOutlineColor:
- const WidgetStatePropertyAll(Colors.white),
- title: Text(
- 'sms'.tr(),
- style: const TextStyle(fontSize: 16),
- ),
- subtitle: Text(
- 'sms_subtitle'.tr(),
- style: const TextStyle(
- fontSize: 12,
- color: Colors.grey,
- fontWeight: FontWeight.w400),
- ),
- value: cubit.state.allNotifications
- .firstWhere((permission) =>
- permission.name ==
- NotificationPermissionType.ccSms.displayName)
- .allowed,
- onChanged: (bool value) {
- cubit.onNotificationPermissionClicked(
- NotificationPermissionType.ccSms.displayName);
- },
- contentPadding: EdgeInsets.zero,
- ),
- SwitchListTile(
- activeColor: sandColor,
- inactiveTrackColor: Colors.grey[200],
- inactiveThumbColor: Colors.white,
- trackOutlineColor:
- const WidgetStatePropertyAll(Colors.white),
- title: Text(
- 'company_deal'.tr(),
- style: const TextStyle(fontSize: 16),
- ),
- subtitle: Text(
- 'company_deal_subtitle'.tr(),
- style: const TextStyle(
- fontSize: 12,
- color: Colors.grey,
- fontWeight: FontWeight.w400),
- ),
- value: cubit.state.allNotifications
- .firstWhere((permission) =>
- permission.name ==
- NotificationPermissionType
- .companyNotifications.displayName)
- .allowed,
- onChanged: (bool value) {
- cubit.onNotificationPermissionClicked(
- NotificationPermissionType
- .companyNotifications.displayName);
- },
- contentPadding: EdgeInsets.zero,
- ),
- ],
+ ),
+ SizedBox(
+ width: double.infinity,
+ height: 50,
+ child: ElevatedButton(
+ onPressed: () {
+ cubit.updatePreferences(
+ cubit.state.allNotifications, cubit.user!.id);
+ },
+ style: ElevatedButton.styleFrom(
+ backgroundColor: sandColor,
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(8),
),
),
- ),
- SizedBox(
- width: double.infinity,
- height: 50,
- child: ElevatedButton(
- onPressed: () {
- cubit.updatePreferences(
- cubit.state.allNotifications, cubit.user!.id);
- },
- style: ElevatedButton.styleFrom(
- backgroundColor: sandColor,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(8),
- ),
- ),
- child: Text(
- 'save'.tr(),
- style: const TextStyle(
- color: Colors.white,
- fontSize: 16,
- ),
+ child: Text(
+ 'save'.tr(),
+ style: const TextStyle(
+ color: Colors.white,
+ fontSize: 16,
),
),
),
- ],
- ),
+ ),
+ ],
),
),
);
diff --git a/comwell_key_app/lib/notifications/notifications_repository.dart b/comwell_key_app/lib/notifications/notifications_repository.dart
index a5076f86..d3886840 100644
--- a/comwell_key_app/lib/notifications/notifications_repository.dart
+++ b/comwell_key_app/lib/notifications/notifications_repository.dart
@@ -11,9 +11,10 @@ class NotificationsRepository {
final db = locator<ComwellDatabase>();
final ProfileSettingsRepository profileSettingsRepository =
locator<ProfileSettingsRepository>();
- User? user;
+ late User user;
+ late Iterable<NotificationPermission> notificationPermissions;
- Future<User?> fetchUser() async {
+ Future<User> fetchUser() async {
user = await profileSettingsRepository.fetchProfileSettings();
return user;
}
@@ -25,20 +26,43 @@ class NotificationsRepository {
Future<Iterable<NotificationPermission>> fetchNotificationPermissions(
int guestId) async {
- final response = await api.getNotificationPermissions(guestId);
+ notificationPermissions =
+ await db.notificationPermissionDAO.getNotificationPermissions();
+
+ if (notificationPermissions.isEmpty) {
+ final response = await api.getNotificationPermissions(guestId);
- if (response.statusCode == 200) {
final data = response.data as Map<String, dynamic>;
- final permissions = (data['permissions'] as List<dynamic>)
+
+ notificationPermissions = (data['permissions'] as List<dynamic>)
.map((json) => NotificationPermission.fromJson(json as Json))
.toList();
+
+ notificationPermissions =
+ addDescriptionsToPermissions(notificationPermissions);
+
await db.notificationPermissionDAO
- .saveNotificationPermission(permissions);
+ .saveNotificationPermission(notificationPermissions);
+ return notificationPermissions;
+ } else {
+ notificationPermissions =
+ addDescriptionsToPermissions(notificationPermissions);
- await db.notificationPermissionDAO.getNotificationPermissions();
- return permissions;
+ return notificationPermissions;
}
- return [];
+ }
+
+ Iterable<NotificationPermission> addDescriptionsToPermissions(
+ Iterable<NotificationPermission> permissions) {
+ return permissions.map((permission) {
+ final enumValue = NotificationPermissionType.values.firstWhere(
+ (type) => type.notificationPermissionId == permission.id,
+ );
+ return permission.copyWith(
+ notificationPermissionDescription:
+ enumValue.notificationPermissionDescription,
+ );
+ }).toList();
}
Future<dynamic> updateNotificationPreferences(int guestId,
@@ -48,11 +72,4 @@ class NotificationsRepository {
return api.updateNotificationPreferences(guestId, notificationPermissions);
}
-
- Future<Iterable<NotificationPermission>>
- getNotificationPermissionsFromDb() async {
- final permissions =
- await db.notificationPermissionDAO.getNotificationPermissions();
- return permissions;
- }
}
diff --git a/comwell_key_app/lib/profile/profile_repository.dart b/comwell_key_app/lib/profile/profile_repository.dart
index e9e5074a..c71dfee4 100644
--- a/comwell_key_app/lib/profile/profile_repository.dart
+++ b/comwell_key_app/lib/profile/profile_repository.dart
@@ -1,7 +1,5 @@
import 'package:comwell_key_app/database/comwell_db.dart';
-import 'package:comwell_key_app/notifications/models/notification_permission.dart';
import 'package:comwell_key_app/services/api.dart';
-import 'package:comwell_key_app/utils/json.dart';
import 'package:comwell_key_app/utils/locator.dart';
import 'package:comwell_key_app/utils/secure_storage.dart';
import 'package:comwell_key_app/utils/seos_repository.dart';
@@ -13,22 +11,4 @@ class ProfileRepository {
SeosRepository().seosMobileKeysPlugin;
final Api api = Api();
final db = locator<ComwellDatabase>();
-
- Future<Iterable<NotificationPermission>> fetchNotificationPermissions(
- int guestId) async {
- final response = await api.getNotificationPermissions(guestId);
-
- if (response.statusCode == 200) {
- final data = response.data as Map<String, dynamic>;
- final permissions = (data['permissions'] as List<dynamic>)
- .map((json) => NotificationPermission.fromJson(json as Json))
- .toList();
- await db.notificationPermissionDAO
- .saveNotificationPermission(permissions);
-
- await db.notificationPermissionDAO.getNotificationPermissions();
- return permissions;
- }
- return [];
- }
}
diff --git a/comwell_key_app/lib/profile_settings/repostiory/profile_settings_repository.dart b/comwell_key_app/lib/profile_settings/repostiory/profile_settings_repository.dart
index 42fbcb75..975b9ff7 100644
--- a/comwell_key_app/lib/profile_settings/repostiory/profile_settings_repository.dart
+++ b/comwell_key_app/lib/profile_settings/repostiory/profile_settings_repository.dart
@@ -10,7 +10,7 @@ class ProfileSettingsRepository {
final Api api = Api();
final db = locator<ComwellDatabase>();
- Future<User?> fetchProfileSettings() async {
+ Future<User> fetchProfileSettings() async {
final response = await api.fetchProfileSettings();
final data = response.data as Map<String, dynamic>;
if (data != null) {
@@ -18,7 +18,7 @@ class ProfileSettingsRepository {
db.userDAO.saveUser(user);
return user;
}
- return null;
+ throw Exception('Failed to fetch profile settings');
}
Future<void> updateUser(User updatedUser) async {
diff --git a/comwell_key_app/lib/services/api.dart b/comwell_key_app/lib/services/api.dart
index 7b5e583e..afe60604 100644
--- a/comwell_key_app/lib/services/api.dart
+++ b/comwell_key_app/lib/services/api.dart
@@ -75,7 +75,8 @@ class Api {
}
Future<Response<dynamic>> fetchProfileSettings() async {
- return await dio.get('/Members/v1/GetGuestProfile');
+ final response = await dio.get('/Members/v1/GetGuestProfile');
+ return response;
}
Future<void> deleteProfile() {
@@ -89,7 +90,8 @@ class Api {
{"id": 0, "allowed": false},
]
};
- return await dio.put(
+
+ final response = await dio.put(
'/Members/v1/guests/$guestId/communication-preference',
data: body,
options: Options(
@@ -98,6 +100,8 @@ class Api {
},
),
);
+
+ return response;
}
// TODO: Remove guestId when backend is updated
diff --git a/comwell_key_app/test/profile_settings_test/profile_settings_cubit_test.dart b/comwell_key_app/test/profile_settings_test/profile_settings_cubit_test.dart
index e249e3f2..bc50e9d8 100644
--- a/comwell_key_app/test/profile_settings_test/profile_settings_cubit_test.dart
+++ b/comwell_key_app/test/profile_settings_test/profile_settings_cubit_test.dart
@@ -43,7 +43,7 @@ void main() {
zipCode: '12345',
country: 'USA',
),
- birthday: DateTime(1990, 1, 1),
+ birthDate: DateTime(1990, 1, 1),
countryCode: '',
);