2022-04-25 22:15:15 +02:00
using System ;
using System.Threading.Tasks ;
2022-08-30 15:42:25 +02:00
using Serilog ;
using TINK.Model.Bikes.BikeInfoNS.CopriLock ;
2022-04-25 22:15:15 +02:00
using TINK.Model.Connector ;
2022-08-30 15:42:25 +02:00
using TINK.Model.Device ;
2022-04-25 22:15:15 +02:00
using TINK.Model.State ;
using TINK.Model.User ;
2022-08-30 15:42:25 +02:00
using TINK.MultilingualResources ;
using TINK.Repository.Exception ;
using TINK.View ;
2022-04-25 22:15:15 +02:00
namespace TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler
{
using IRequestHandler = BluetoothLock . IRequestHandler ;
public class DisposableClosed : Base , IRequestHandler
{
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...).</param>
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
public DisposableClosed (
IBikeInfoMutable selectedBike ,
Func < bool > isConnectedDelegate ,
Func < bool , IConnector > connectorFactory ,
Func < IPollingUpdateTaskManager > viewUpdateManager ,
ISmartDevice smartDevice ,
IViewService viewService ,
IBikesViewModel bikesViewModel ,
IUser activeUser ) : base (
selectedBike ,
AppResources . ActionOpenAndBook , // Button text: "Schloss öffnen & Rad mieten"
2022-08-30 15:42:25 +02:00
true , // Show copri button to enable booking and opening
2022-04-25 22:15:15 +02:00
isConnectedDelegate ,
connectorFactory ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser )
{
2022-08-30 15:42:25 +02:00
LockitButtonText = AppResources . ActionRequest ; // Copri text: "Rad reservieren"
IsLockitButtonVisible = true ;
2022-04-25 22:15:15 +02:00
}
/// <summary>Reserve bike and connect to lock.</summary>
2022-08-30 15:42:25 +02:00
public async Task < IRequestHandler > HandleRequestOption1 ( ) = > await BookAndRelease ( ) ;
2022-04-25 22:15:15 +02:00
2022-08-30 15:42:25 +02:00
public async Task < IRequestHandler > HandleRequestOption2 ( ) = > await Reserve ( ) ;
2022-04-25 22:15:15 +02:00
2022-08-30 15:42:25 +02:00
/// <summary>Book bike and open lock.</summary>
public async Task < IRequestHandler > BookAndRelease ( )
2022-04-25 22:15:15 +02:00
{
BikesViewModel . IsIdle = false ;
// Ask whether to really book bike?
var alertResult = await ViewService . DisplayAlert (
string . Empty ,
string . Format ( AppResources . QuestionOpenLockAndBookBike , SelectedBike . GetFullDisplayName ( ) ) ,
AppResources . MessageAnswerYes ,
AppResources . MessageAnswerNo ) ;
if ( alertResult = = false )
{
// User aborted booking process
2022-08-30 15:42:25 +02:00
Log . ForContext < DisposableClosed > ( ) . Information ( "User selected bike {bike} in order to book and release bike from station but action was cancelled." , SelectedBike ) ;
2022-04-25 22:15:15 +02:00
BikesViewModel . IsIdle = true ;
2022-08-30 15:42:25 +02:00
return this ;
2022-04-25 22:15:15 +02:00
}
2022-08-30 15:42:25 +02:00
// Book and release bike from station.
Log . ForContext < DisposableClosed > ( ) . Information ( "User selected bike {bike} in order to book and release bike from station." , SelectedBike ) ;
// Stop polling before returning bike.
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
await ViewUpdateManager ( ) . StopUpdatePeridically ( ) ;
2022-04-25 22:15:15 +02:00
// Book bike prior to opening lock.
BikesViewModel . ActionText = AppResources . ActivityTextRentingBike ;
IsConnected = IsConnectedDelegate ( ) ;
try
{
2022-08-30 15:42:25 +02:00
await ConnectorFactory ( IsConnected ) . Command . BookAndOpenAync ( SelectedBike ) ;
2022-04-25 22:15:15 +02:00
}
2022-08-30 15:42:25 +02:00
catch ( Exception exception )
2022-04-25 22:15:15 +02:00
{
BikesViewModel . ActionText = string . Empty ;
2022-08-30 15:42:25 +02:00
if ( exception is WebConnectFailureException )
2022-04-25 22:15:15 +02:00
{
// Copri server is not reachable.
2022-08-30 15:42:25 +02:00
Log . ForContext < DisposableClosed > ( ) . Information ( "User selected bike {id} but booking failed (Copri server not reachable)." , SelectedBike . Id ) ;
2022-04-25 22:15:15 +02:00
await ViewService . DisplayAdvancedAlert (
AppResources . MessageRentingBikeErrorConnectionTitle ,
WebConnectFailureException . GetHintToPossibleExceptionsReasons ,
2022-08-30 15:42:25 +02:00
exception . Message ,
2022-04-25 22:15:15 +02:00
AppResources . MessageAnswerOk ) ;
}
else
{
2022-08-30 15:42:25 +02:00
Log . ForContext < DisposableClosed > ( ) . Error ( "User selected bike {id} but booking failed. {@exception}" , SelectedBike . Id , exception ) ;
2022-04-25 22:15:15 +02:00
2022-08-30 15:42:25 +02:00
await ViewService . DisplayAlert (
2022-04-25 22:15:15 +02:00
AppResources . MessageRentingBikeErrorGeneralTitle ,
2022-08-30 15:42:25 +02:00
exception . Message ,
2022-04-25 22:15:15 +02:00
AppResources . MessageAnswerOk ) ;
}
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ; // Restart polling again.
2022-08-30 15:42:25 +02:00
BikesViewModel . ActionText = string . Empty ;
2022-04-25 22:15:15 +02:00
BikesViewModel . IsIdle = true ; // Unlock GUI
2022-08-30 15:42:25 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2022-04-25 22:15:15 +02:00
}
2022-08-30 15:42:25 +02:00
Log . ForContext < DisposableClosed > ( ) . Information ( "User booked and released bike {bike} successfully." , SelectedBike ) ;
2022-04-25 22:15:15 +02:00
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ; // Restart polling again.
BikesViewModel . ActionText = string . Empty ;
BikesViewModel . IsIdle = true ; // Unlock GUI
2022-08-30 15:42:25 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2022-04-25 22:15:15 +02:00
}
2022-08-30 15:42:25 +02:00
/// <summary>Reserve bike.</summary>
public async Task < IRequestHandler > Reserve ( )
2022-04-25 22:15:15 +02:00
{
2022-08-30 15:42:25 +02:00
BikesViewModel . IsIdle = false ;
// Ask whether to really reserve bike?
var alertResult = await ViewService . DisplayAlert (
string . Empty ,
string . Format ( AppResources . QuestionReserveBike , SelectedBike . GetFullDisplayName ( ) , StateRequestedInfo . MaximumReserveTime . Minutes ) ,
AppResources . MessageAnswerYes ,
AppResources . MessageAnswerNo ) ;
if ( alertResult = = false )
{
// User aborted booking process
Log . ForContext < DisposableClosed > ( ) . Information ( "User selected availalbe bike {bike} in order to reserve but action was canceled." , SelectedBike ) ;
BikesViewModel . IsIdle = true ;
return this ;
}
Log . ForContext < DisposableClosed > ( ) . Information ( "Request to reserve for bike {bike} detected." , SelectedBike ) ;
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
// Stop polling before requesting bike.
await ViewUpdateManager ( ) . StopUpdatePeridically ( ) ;
BikesViewModel . ActionText = AppResources . ActivityTextReservingBike ;
IsConnected = IsConnectedDelegate ( ) ;
try
{
await ConnectorFactory ( IsConnected ) . Command . DoReserve ( SelectedBike ) ;
}
catch ( Exception exception )
{
BikesViewModel . ActionText = string . Empty ;
if ( exception is BookingDeclinedException )
{
// Too many bikes booked.
Log . ForContext < DisposableClosed > ( ) . Information ( "Request declined because maximum count of bikes {l_oException.MaxBikesCount} already requested/ booked." , ( exception as BookingDeclinedException ) . MaxBikesCount ) ;
await ViewService . DisplayAlert (
AppResources . MessageTitleHint ,
string . Format ( AppResources . MessageReservationBikeErrorTooManyReservationsRentals , SelectedBike . GetFullDisplayName ( ) , ( exception as BookingDeclinedException ) . MaxBikesCount ) ,
AppResources . MessageAnswerOk ) ;
}
else if ( exception is WebConnectFailureException )
{
// Copri server is not reachable.
Log . ForContext < DisposableClosed > ( ) . Information ( "User selected availalbe bike {bike} but reserving failed (Copri server not reachable)." , SelectedBike ) ;
await ViewService . DisplayAdvancedAlert (
AppResources . MessageReservingBikeErrorConnectionTitle ,
WebConnectFailureException . GetHintToPossibleExceptionsReasons ,
exception . Message ,
AppResources . MessageAnswerOk ) ;
}
else
{
Log . ForContext < DisposableClosed > ( ) . Error ( "User selected availalbe bike {bike} but reserving failed. {@exception}" , SelectedBike , exception ) ;
await ViewService . DisplayAlert (
AppResources . MessageReservingBikeErrorGeneralTitle ,
exception . Message ,
AppResources . MessageAnswerOk ) ;
}
// Restart polling again.
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ;
BikesViewModel . ActionText = string . Empty ;
BikesViewModel . IsIdle = true ;
return this ;
}
Log . ForContext < DisposableClosed > ( ) . Information ( "User reserved bike {bike} successfully." , SelectedBike ) ;
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ; // Restart polling again.
BikesViewModel . ActionText = string . Empty ;
BikesViewModel . IsIdle = true ; // Unlock GUI
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2022-04-25 22:15:15 +02:00
}
}
}