2022-10-26 20:53:18 +02:00
using System ;
2021-05-13 20:03:07 +02:00
using System.Threading.Tasks ;
2022-08-30 15:42:25 +02:00
using Serilog ;
2021-05-13 20:03:07 +02:00
using TINK.Model.Connector ;
2022-08-30 15:42:25 +02:00
using TINK.Model.Device ;
2021-05-13 20:03:07 +02:00
using TINK.Model.State ;
using TINK.Model.User ;
using TINK.MultilingualResources ;
2022-08-30 15:42:25 +02:00
using TINK.Repository.Exception ;
2022-10-26 20:53:18 +02:00
using TINK.Services.CopriApi.Exception ;
2021-05-13 20:03:07 +02:00
using TINK.View ;
2022-08-30 15:42:25 +02:00
using BikeInfoMutable = TINK . Model . Bikes . BikeInfoNS . BC . BikeInfoMutable ;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.Bikes.Bike.BC.RequestHandler
{
2022-09-06 16:08:19 +02:00
/// <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 class Disposable : Base < BikeInfoMutable > , IRequestHandler
{
public Disposable (
BikeInfoMutable selectedBike ,
Func < bool > isConnectedDelegate ,
Func < bool , IConnector > connectorFactory ,
Func < IPollingUpdateTaskManager > viewUpdateManager ,
ISmartDevice smartDevice ,
IViewService viewService ,
IBikesViewModel bikesViewModel ,
IUser activeUser ) : base ( selectedBike , selectedBike . State . Value . GetActionText ( ) , true , isConnectedDelegate , connectorFactory , viewUpdateManager , smartDevice , viewService , bikesViewModel , activeUser )
{
}
/// <summary> Request bike. </summary>
public async Task < IRequestHandler > HandleRequest ( )
{
// Lock list to avoid multiple taps while copri action is pending.
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
BikesViewModel . IsIdle = false ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
var l_oResult = await ViewService . DisplayAlert (
string . Empty ,
2023-06-06 12:00:24 +02:00
string . Format (
AppResources . QuestionReserveBike ,
SelectedBike . GetFullDisplayName ( ) ,
SelectedBike . TariffDescription ? . MaxReservationTimeSpan . TotalMinutes ? ? 0 ) ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerYes ,
AppResources . MessageAnswerNo ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
if ( l_oResult = = false )
{
// User aborted booking process
2023-04-19 12:14:14 +02:00
Log . ForContext < Disposable > ( ) . Information ( "User selected centered bike {l_oId} in order to reserve but action was canceled." , SelectedBike . Id ) ;
2022-09-06 16:08:19 +02:00
BikesViewModel . IsIdle = true ;
return this ;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
// Stop polling before requesting bike.
2023-08-31 12:20:06 +02:00
await ViewUpdateManager ( ) . StopAsync ( ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
IsConnected = IsConnectedDelegate ( ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
try
{
await ConnectorFactory ( IsConnected ) . Command . DoReserve ( SelectedBike ) ;
}
2022-10-26 20:53:18 +02:00
catch ( Exception exception )
2022-09-06 16:08:19 +02:00
{
2022-10-26 20:53:18 +02:00
if ( exception is BookingDeclinedException )
2022-09-06 16:08:19 +02:00
{
// Too many bikes booked.
2022-10-26 20:53:18 +02:00
Log . ForContext < Disposable > ( ) . Information ( "Request declined because maximum count of bikes {l_oException.MaxBikesCount} already requested/ booked." , ( exception as BookingDeclinedException ) . MaxBikesCount ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = string . Empty ;
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . MessageHintTitle ,
string . Format ( AppResources . ErrorReservingBikeTooManyReservationsRentals , SelectedBike . Id , ( exception as BookingDeclinedException ) . MaxBikesCount ) ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
}
2022-10-26 20:53:18 +02:00
else if ( exception is WebConnectFailureException
| | exception is RequestNotCachableException )
2022-09-06 16:08:19 +02:00
{
// Copri server is not reachable.
2023-04-19 12:14:14 +02:00
Log . ForContext < Disposable > ( ) . Information ( "User selected centered bike {l_oId} but reserving failed (Copri server not reachable)." , SelectedBike . Id ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = string . Empty ;
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorNoConnectionTitle ,
AppResources . ErrorNoWeb ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
}
else
{
2023-04-19 12:14:14 +02:00
Log . ForContext < Disposable > ( ) . Error ( "User selected centered bike {l_oId} but reserving failed. {@l_oException}" , SelectedBike . Id , exception ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = string . Empty ;
2023-08-31 12:20:06 +02:00
await ViewService . DisplayAlert ( AppResources . ErrorReservingBikeTitle , exception . Message , AppResources . MessageAnswerOk ) ;
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = string . Empty ; // Todo: Remove this statement because in catch block ActionText is already set to empty above.
BikesViewModel . IsIdle = true ;
return this ;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
finally
{
// Restart polling again.
2023-08-31 12:20:06 +02:00
await ViewUpdateManager ( ) . StartAsync ( ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
// Update status text and unlock list of bikes because no more action is pending.
BikesViewModel . ActionText = string . Empty ; // Todo: Move this statement in front of finally block because in catch block ActionText is already set to empty.
BikesViewModel . IsIdle = true ;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
Log . ForContext < Disposable > ( ) . Information ( "User reserved bike {l_oId} successfully." , SelectedBike . Id ) ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
}
}
2021-05-13 20:03:07 +02:00
}