6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit c030d49e
Changed files
.../authentication/authentication_repository.dart | 5 ++-- .../authentication/bloc/authentication_bloc.dart | 19 ++++++-------- .../lib/booking_details/booking_details_page.dart | 20 ++------------- .../lib/check_in/bloc/check_in_cubit.dart | 10 ++------ .../lib/check_in/check_in_repository.dart | 22 ++++++++++++++-- comwell_key_app/lib/routing/app_router.dart | 2 +- comwell_key_app/lib/services/api.dart | 11 ++++---- comwell_key_app/lib/utils/seos_repository.dart | 30 +++++----------------- 8 files changed, 48 insertions(+), 71 deletions(-)
Diff
diff --git a/comwell_key_app/lib/authentication/authentication_repository.dart b/comwell_key_app/lib/authentication/authentication_repository.dart
index ad2c179e..ecdaa477 100644
--- a/comwell_key_app/lib/authentication/authentication_repository.dart
+++ b/comwell_key_app/lib/authentication/authentication_repository.dart
@@ -6,12 +6,13 @@ import 'package:comwell_key_app/utils/secure_storage.dart';
import 'package:comwell_key_app/common/const.dart' as constants;
import 'package:flutter/foundation.dart';
+import '../utils/locator.dart';
import '../utils/seos_repository.dart';
class AuthenticationRepository {
late SecureStorage secureStorage;
final _controller = StreamController<AuthenticationStatus>.broadcast();
- final seos = SeosRepository();
+ final seos = locator<SeosRepository>();
final api = Api();
AuthenticationRepository() {
@@ -31,7 +32,7 @@ class AuthenticationRepository {
await seos.startMobilePlugin();
}
} catch (e, st) {
- if(kDebugMode) print("e=$e, $st");
+ if (kDebugMode) print("e=$e, $st");
}
}
diff --git a/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart b/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart
index d3764ced..129777ec 100644
--- a/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart
+++ b/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart
@@ -5,28 +5,27 @@ import 'package:equatable/equatable.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
part 'authentication_event.dart';
+
part 'authentication_state.dart';
class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
+ final AuthenticationRepository authenticationRepository;
AuthenticationBloc({
- required AuthenticationRepository authenticationRepository,
- }) : _authenticationRepository = authenticationRepository,
- super(const AuthenticationState.unknown()) {
+ required this.authenticationRepository,
+ }) : super(const AuthenticationState.unknown()) {
on<AuthenticationSubscriptionRequested>(_onSubscriptionRequested);
on<AuthenticationLogoutPressed>(_onLogoutPressed);
on<AuthenticationLoginPressed>(_onLoginPressed);
on<AuthenticationRequestFailed>(_loginErrorFromAzureB2C);
}
- final AuthenticationRepository _authenticationRepository;
-
void _onLogoutPressed(
AuthenticationLogoutPressed event,
Emitter<AuthenticationState> emit,
) {
- _authenticationRepository.logOut();
+ authenticationRepository.logOut();
}
Future<void> _onSubscriptionRequested(
@@ -34,7 +33,7 @@ class AuthenticationBloc
Emitter<AuthenticationState> emit,
) {
return emit.onEach(
- _authenticationRepository.status.asBroadcastStream(),
+ authenticationRepository.status.asBroadcastStream(),
onData: (status) async {
switch (status) {
case AuthenticationStatus.unauthenticated:
@@ -44,8 +43,6 @@ class AuthenticationBloc
);
return emit(const AuthenticationState.unauthenticated());
case AuthenticationStatus.authenticated:
-
-
await FirebaseAnalytics.instance.logLogin();
await FirebaseAnalytics.instance.setUserProperty(
name: 'login_status',
@@ -70,7 +67,7 @@ class AuthenticationBloc
) {
try {
// Call the login method from the authenticationRepository
- _authenticationRepository.logIn();
+ authenticationRepository.logIn();
emit(const AuthenticationState.authenticated());
} catch (e) {
emit(const AuthenticationState.unauthenticated());
@@ -82,6 +79,6 @@ class AuthenticationBloc
Emitter<AuthenticationState> emit,
) {
emit(const AuthenticationState.unauthenticated());
- _authenticationRepository.logOut();
+ authenticationRepository.logOut();
}
}
diff --git a/comwell_key_app/lib/booking_details/booking_details_page.dart b/comwell_key_app/lib/booking_details/booking_details_page.dart
index cbb46823..93d7a259 100644
--- a/comwell_key_app/lib/booking_details/booking_details_page.dart
+++ b/comwell_key_app/lib/booking_details/booking_details_page.dart
@@ -14,30 +14,14 @@ import 'package:slider_button/slider_button.dart';
import 'bloc/booking_details_bloc.dart';
-class BookingDetailsPage extends StatefulWidget {
+class BookingDetailsPage extends StatelessWidget {
const BookingDetailsPage({super.key});
- @override
- State<StatefulWidget> createState() => _BookingDetailsPage();
-}
-
-class _BookingDetailsPage extends State<BookingDetailsPage> {
- @override
- void initState() {
- super.initState();
- }
-
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return BlocConsumer<BookingDetailsBloc, BookingDetailsState>(
- listener: (context, state) {
- if (state.status == BookingDetailsStatus.setupError) {
- ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
- content: Text('Failed to start Mobile Keys Plugin')));
- //context.read<BookingDetailsBloc>().add(SetupEndpoint());
- }
- },
+ listener: (context, state) {},
builder: (context, state) {
if (state.status == BookingDetailsStatus.initial) {
context.read<BookingDetailsBloc>().add(InitialEvent());
diff --git a/comwell_key_app/lib/check_in/bloc/check_in_cubit.dart b/comwell_key_app/lib/check_in/bloc/check_in_cubit.dart
index 4cb8d7f5..54663aa2 100644
--- a/comwell_key_app/lib/check_in/bloc/check_in_cubit.dart
+++ b/comwell_key_app/lib/check_in/bloc/check_in_cubit.dart
@@ -2,15 +2,10 @@ import 'package:bloc/bloc.dart';
import 'package:comwell_key_app/check_in/bloc/check_in_state.dart';
import 'package:comwell_key_app/check_in/check_in_repository.dart';
import 'package:comwell_key_app/overview/models/booking.dart';
-import 'package:comwell_key_app/utils/seos_repository.dart';
import 'package:flutter/foundation.dart';
-import 'package:seos_mobile_keys_plugin/app_usage_api.dart';
-
-import '../../utils/locator.dart';
class CheckInCubit extends Cubit<CheckInState> {
final _checkInRepository = CheckInRepository();
- final _seosRepository = locator<SeosRepository>();
late final Booking booking;
@@ -22,10 +17,9 @@ class CheckInCubit extends Cubit<CheckInState> {
try {
await Future<void>.delayed(const Duration(milliseconds: 500));
emit(state.checkInStatusLoading());
- await _seosRepository.startMobilePlugin();
+ await _checkInRepository.checkIfSetup();
emit(state.checkInStatusRoomFound(roomNumber: booking.roomNumber));
- final keys = await _seosRepository.refreshKeys();
- print("qqq $keys");
+ await _checkInRepository.getKeys(booking.id);
emit(state.checkInStatusYourDigitalCard(roomNumber: booking.roomNumber));
} catch (err, st) {
if (kDebugMode) print("qqq err=$err, $st");
diff --git a/comwell_key_app/lib/check_in/check_in_repository.dart b/comwell_key_app/lib/check_in/check_in_repository.dart
index 72c17676..bb070490 100644
--- a/comwell_key_app/lib/check_in/check_in_repository.dart
+++ b/comwell_key_app/lib/check_in/check_in_repository.dart
@@ -7,21 +7,39 @@ 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 '../utils/seos_repository.dart';
+
class CheckInRepository {
final api = Api();
final db = locator.get<ComwellDatabase>();
+ final seosRepository = locator<SeosRepository>();
Future<void> checkIn(String confirmationId) async {
await api.checkIn(confirmationId);
}
- Future<Booking> getBooking(String bookingId) async {
- //return mockBooking;
+ Future<Booking> getBookingDetails(String bookingId) async {
final entity = await db.bookingsDao.getBookingDetails(bookingId);
final Json json = jsonDecode(entity.json) as Json;
return Booking.fromJson(json);
}
+ Future<void> checkIfSetup() async {
+ final isSetup = await seosRepository.isEndpointSetup();
+ if (!isSetup) await seosRepository.startMobilePlugin();
+ }
+
+ Future<void> getKeys(String bookingId) async {
+ final keys = await seosRepository.refreshKeys();
+ if (keys.isEmpty) {
+ await seosRepository.provisionKey(bookingId: bookingId);
+ final keys = await seosRepository.refreshKeys();
+ if (keys.isEmpty) {
+ throw Exception("Could not provision key for this booking");
+ }
+ }
+ }
+
static final mockBooking = Booking(
id: "helloworld",
userId: "123",
diff --git a/comwell_key_app/lib/routing/app_router.dart b/comwell_key_app/lib/routing/app_router.dart
index dd83e491..606388ec 100644
--- a/comwell_key_app/lib/routing/app_router.dart
+++ b/comwell_key_app/lib/routing/app_router.dart
@@ -51,7 +51,7 @@ GoRouter goRouter(AuthenticationBloc authBloc) {
authBloc.state == const AuthenticationState.authenticated();
final isUnAuthenticated =
authBloc.state == const AuthenticationState.unauthenticated();
- final AuthenticationRepository authRepo = AuthenticationRepository();
+ final authRepo = authBloc.authenticationRepository;
if (authBloc.state == const AuthenticationState.unknown()) {
bool doesTokenExist = await authRepo.doesTokenExist();
diff --git a/comwell_key_app/lib/services/api.dart b/comwell_key_app/lib/services/api.dart
index 9b1b83cd..0403c5eb 100644
--- a/comwell_key_app/lib/services/api.dart
+++ b/comwell_key_app/lib/services/api.dart
@@ -38,14 +38,13 @@ class Api {
}
Future<String> createEndpointRegistration() async {
- final response =
- await dio.post<String>('/keys/v1/CreateEndpointRegistration');
- final json = jsonDecode(response.data!) as Map<String, String>;
- return json["invitationCode"] as String;
+ final response = await dio.post<String>('/keys/v1/CreateEndpointRegistration');
+ final json = jsonDecode(response.data!) as Map<String, dynamic>;
+ return json["InvitationCode"]! as String;
}
- Future<Response<dynamic>> provisionKey(String bookingId) async {
- return dio.post('/keys/v1/ProvisionKey', data: {'bookingId': bookingId});
+ Future<void> provisionKey(String bookingId) async {
+ await dio.post<void>('/keys/v1/ProvisionKey', data: {'bookingId': bookingId});
}
Future<dynamic> fetchProfileSettings() async {
diff --git a/comwell_key_app/lib/utils/seos_repository.dart b/comwell_key_app/lib/utils/seos_repository.dart
index 255a7aa4..cca6842a 100644
--- a/comwell_key_app/lib/utils/seos_repository.dart
+++ b/comwell_key_app/lib/utils/seos_repository.dart
@@ -11,8 +11,10 @@ class SeosRepository {
final secureStorage = SecureStorage();
final api = Api();
final seosMobileKeysPlugin = locator<SeosMobileKeysPlugin>();
+ bool _pluginStarted = false;
Future<void> startMobilePlugin() async {
+ if(_pluginStarted) return;
final mobileKeysOptions = {
"MobileKeysOptionApplicationId":
dotenv.env['MOBILEKEYSOPTIONAPPLICATIONID'],
@@ -25,12 +27,12 @@ class SeosRepository {
try {
await seosMobileKeysPlugin.startUp(mobileKeysOptions);
+ _pluginStarted = true;
final isEndpointSetup = await seosMobileKeysPlugin.isEndpointSetup();
if (isEndpointSetup) {
await seosMobileKeysPlugin.updateEndpoint();
} else {
final invitationCode = await api.createEndpointRegistration();
- print("qqq invCode=$invitationCode");
await seosMobileKeysPlugin.setupEndpoint(invitationCode);
await seosMobileKeysPlugin.updateEndpoint();
}
@@ -42,6 +44,7 @@ class SeosRepository {
}
Future<bool> isEndpointSetup({bool firstLaunch = true}) async {
+ if(!_pluginStarted) return false;
try {
return seosMobileKeysPlugin.isEndpointSetup();
} catch (e) {
@@ -49,10 +52,7 @@ class SeosRepository {
}
}
- Future<void> provisionKey({
- required String bookingId,
- bool forceProvision = false,
- }) async {
+ Future<void> provisionKey({required String bookingId}) async {
try {
final hasKey = await secureStorage.read(constants.hasKey);
if (hasKey != null) {
@@ -61,33 +61,17 @@ class SeosRepository {
return;
}
}
-
await api.provisionKey(bookingId);
secureStorage.write(constants.hasKey, DateTime.now().toString());
- await updateEndpoint();
- } catch (e) {
- throw Exception('Failed to provision a key - ${e.toString()}');
- }
- }
-
- Future<bool> updateEndpoint() async {
- try {
await seosMobileKeysPlugin.updateEndpoint();
- return true;
} catch (e) {
- throw Exception('Failed to update endpoint - ${e.toString()}');
+ throw Exception('Failed to provision a key - ${e.toString()}');
}
}
- Future<void> setupEndpoint() async {
- final invitationCode = await api.createEndpointRegistration();
- await seosMobileKeysPlugin.setupEndpoint(invitationCode);
- }
-
Future<List<MobileKeysKey>> refreshKeys() async {
try {
- final List<MobileKeysKey> listOfKeys =
- await seosMobileKeysPlugin.listMobileKeys();
+ final List<MobileKeysKey> listOfKeys = await seosMobileKeysPlugin.listMobileKeys();
await secureStorage.write(constants.hasKey, DateTime.now().toString());
return listOfKeys;
} catch (e) {