sharee.bike-App/TINKLib/ViewModel/Bikes/Bike/CopriLock/RequestHandler/FeedbackPending.cs

125 lines
4.6 KiB
C#
Raw Normal View History

2023-04-19 12:14:14 +02:00
using System;
2022-08-30 15:42:25 +02:00
using System.Threading.Tasks;
using Serilog;
using TINK.Model.Bikes.BikeInfoNS.CopriLock;
using TINK.Model.Connector;
using TINK.Model.Device;
using TINK.Model.User;
using TINK.MultilingualResources;
using TINK.Repository.Exception;
using TINK.View;
namespace TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler
{
2022-09-06 16:08:19 +02:00
using IRequestHandler = BluetoothLock.IRequestHandler;
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
public class FeedbackPending : 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 FeedbackPending(
IBikeInfoMutable selectedBike,
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
Func<IPollingUpdateTaskManager> viewUpdateManager,
ISmartDevice smartDevice,
IViewService viewService,
IBikesViewModel bikesViewModel,
IUser activeUser) : base(
selectedBike,
nameof(FeedbackPending),
false, // No other action but give feedback allowed.
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser)
{
LockitButtonText = AppResources.ActionGiveFeedback;
IsLockitButtonVisible = true; // Show button to enable opening lock in case user took a pause and does not want to return the bike.
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Return bike. </summary>
public async Task<IRequestHandler> HandleRequestOption1() => await UnsupportedRequest();
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Open bike and update COPRI lock state. </summary>
public async Task<IRequestHandler> HandleRequestOption2() => await GiveFeedback();
2022-08-30 15:42:25 +02:00
2023-04-19 12:14:14 +02:00
/// <summary> Request is not supported, button should be disabled. </summary>
2022-09-06 16:08:19 +02:00
/// <returns></returns>
public async Task<IRequestHandler> UnsupportedRequest()
{
Log.ForContext<BookedClosed>().Error("Click of unsupported button click detected.");
return await Task.FromResult<IRequestHandler>(this);
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Open bike and update COPRI lock state. </summary>
public async Task<IRequestHandler> GiveFeedback()
{
BikesViewModel.IsIdle = false;
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
await ViewUpdateManager().StopUpdatePeridically();
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
// Do get Feedback
var feedback = await ViewService.DisplayUserFeedbackPopup(SelectedBike.Drive?.Battery, SelectedBike?.BookingFinishedModel?.Co2Saving);
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextSubmittingFeedback;
IsConnected = IsConnectedDelegate();
try
{
await ConnectorFactory(IsConnected).Command.DoSubmitFeedback(
new UserFeedbackDto
{
BikeId = SelectedBike.Id,
CurrentChargeBars = feedback.CurrentChargeBars,
IsBikeBroken = feedback.IsBikeBroken,
Message = feedback.Message
},
SelectedBike?.OperatorUri);
}
catch (Exception exception)
{
BikesViewModel.ActionText = string.Empty;
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
if (exception is ResponseException copriException)
{
// Copri server is not reachable.
Log.ForContext<FeedbackPending>().Information("Submitting feedback for bike {bike} failed. COPRI returned an error.", SelectedBike);
}
else
{
Log.ForContext<FeedbackPending>().Error("Submitting feedback for bike {bike} failed. {@Exception}", SelectedBike.Id, exception);
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
await ViewService.DisplayAlert(
AppResources.ErrorReturnSubmitFeedbackTitle,
AppResources.ErrorReturnSubmitFeedbackMessage,
AppResources.MessageAnswerOk);
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
await ViewUpdateManager().StartUpdateAyncPeridically();
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true;
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
// Feedback was given successfully.
// Set state from FeedbackPending to Available.
SelectedBike.State.Load(Model.State.InUseStateEnum.Disposable);
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
if (SelectedBike?.BookingFinishedModel?.MiniSurvey?.Questions?.Count > 0)
{
2023-04-19 12:14:14 +02:00
// No need to restart polling again because different page is shown.
2022-09-06 16:08:19 +02:00
await ViewService.PushModalAsync(ViewTypes.MiniSurvey);
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
await ViewUpdateManager().StartUpdateAyncPeridically();
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true;
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
}
}
2023-04-19 12:14:14 +02:00
}