Browse Source

Implementation of Bookings Page and its View Model

master
Kaustav Chaudhuri 1 year ago
parent
commit
54f6600e69
3 changed files with 343 additions and 0 deletions
  1. +162
    -0
      GMCabsDriverAssistantSolution/ViewModels/BookingsViewModel.cs
  2. +139
    -0
      GMCabsDriverAssistantSolution/Views/BookingsPage.xaml
  3. +42
    -0
      GMCabsDriverAssistantSolution/Views/BookingsPage.xaml.cs

+ 162
- 0
GMCabsDriverAssistantSolution/ViewModels/BookingsViewModel.cs View File

@ -0,0 +1,162 @@
using GMCabsDriverAssistant.Models;
using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;
using GMCabsDriverAssistantSolution.Models.Rydo;
using GMCabsDriverAssistantSolution.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace GMCabsDriverAssistantSolution.ViewModels
{
class BookingsViewModel : BaseViewModel
{
#region Fields
private string startSuburb;
private string endSuburb;
private float distance;
private string formattedDistance;
private bool isFutureBooking;
private BookingDto selectedBooking;
#endregion
#region Properties
public string StartSuburb
{
get => startSuburb;
set => SetProperty(ref startSuburb, value);
}
public string EndSuburb
{
get => endSuburb;
set => SetProperty(ref endSuburb, value);
}
public float Distance
{
get => distance;
set => SetProperty(ref distance, value);
}
public string FormattedDistance
{
get => (distance >= 1000) ? $"{distance / 1000:0.##}k" : $"{(int)distance}m";
set => SetProperty(ref formattedDistance, value);
}
public bool IsFutureBooking
{
get => isFutureBooking;
set => SetProperty(ref isFutureBooking, value);
}
public BookingDto SelectedBooking
{
get => selectedBooking;
set
{
SetProperty(ref selectedBooking, value);
OnBookingSelected(value);
}
}
public Command OnRefreshClicked { get; }
public ObservableCollection<BookingDto> Bookings { get; }
public Command<BookingDto> BookingTapped { get; }
public double CurrentLat { get; set; }
public double CurrentLng { get; set; }
#endregion
#region Constructor
public BookingsViewModel()
{
Title = "Available Bookings";
Bookings = new ObservableCollection<BookingDto>();
OnRefreshClicked = new Command(async () => { await GetBookings(); });
BookingTapped = new Command<BookingDto>(OnBookingSelected);
}
#endregion
#region Methods
public void OnAppearing()
{
//IsBusy = true;
SelectedBooking = null;
Task.Run(async () =>
{
await GetBookings();
});
}
private async Task GetBookings()
{
List<string> seenBookingList = new List<string>();
var seenBooking = Preferences.Get(SecureStorageData.UnSeenBooking, "");
if (!String.IsNullOrEmpty(seenBooking))
{
var arrList = seenBooking.Split(',');
if (arrList != null)
{
foreach (var item in arrList)
{
seenBookingList.Add(item);
}
}
}
string rydoAccessToken = Preferences.Get(EftposLoginResponse.RYDO_ACCESS_TOKEN, "");
Guid driverId = Guid.Parse(Preferences.Get(LoginResponseDto.USER_CODE, ""));
GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
Bookings.Clear();
var bookings = await gmCabsDriverService.GetBookings(rydoAccessToken, driverId, CurrentLat, CurrentLng);
if (bookings.Count > 0)
{
foreach (BookingDto booking in bookings)
{
if (seenBookingList.Count > 0 && seenBookingList.Contains(booking.BookingId.ToString()))
{
booking.IsSeenBooking = false;
}
else
{
booking.IsSeenBooking = true;
}
Bookings.Add(booking);
}
}
else
{
Preferences.Set(SecureStorageData.UnSeenBooking, "");
await Shell.Current.GoToAsync("..");
}
}
async void OnBookingSelected(BookingDto booking)
{
if (booking == null)
return;
// This will push the ItemDetailPage onto the navigation stack
var seenBooking = Preferences.Get(SecureStorageData.UnSeenBooking, "");
if (String.IsNullOrEmpty(seenBooking))
{
seenBooking = booking.BookingId.ToString();
}
else
{
seenBooking = seenBooking + "," + booking.BookingId.ToString();
}
Preferences.Set(SecureStorageData.UnSeenBooking, seenBooking);
// await Shell.Current.GoToAsync($"{nameof(BookingDetailsPage)}?{nameof(BookingDetailViewModel.BookingId)}={booking.BookingId:N}");
string bookingJson = JsonSerializer.Serialize(booking);
await Shell.Current.GoToAsync($"{nameof(BookingDetailsPage)}?{nameof(BookingDetailViewModel.BookingJson)}={bookingJson}");
}
#endregion
}
}

+ 139
- 0
GMCabsDriverAssistantSolution/Views/BookingsPage.xaml View File

@ -0,0 +1,139 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GMCabsDriverAssistantSolution.Views.BookingsPage"
xmlns:cmp="clr-namespace:Microsoft.Maui.Controls.Compatibility;assembly=Microsoft.Maui.Controls"
xmlns:vm="clr-namespace:GMCabsDriverAssistantSolution.ViewModels" x:DataType="vm:BookingsViewModel"
xmlns:models="clr-namespace:GMCabsDriverAssistant.Models"
Title="{Binding Title}">
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Accent">#96d1ff</Color>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout Orientation="Vertical"
BackgroundColor="#DCDCDC"
x:DataType="vm:BookingsViewModel">
<Frame Margin="10,40,10,10"
CornerRadius="10"
BackgroundColor="#E8E8E8">
<CollectionView ItemsSource="{Binding Bookings}">
<CollectionView.ItemsLayout>
<LinearItemsLayout
Orientation="Vertical"
ItemSpacing="10"
/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<cmp:RelativeLayout>
<Frame x:DataType="models:BookingDto"
CornerRadius="10"
BackgroundColor="{StaticResource Primary}"
Margin="0, 30, 0, 0"
Padding="8">
<StackLayout Orientation="Vertical" Margin="0,10,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Text="{Binding StartSuburb}"
TextColor="White"
FontFamily="Bold"
HorizontalOptions="StartAndExpand"
FontSize="17"/>
<Label
Grid.Row="0"
Grid.Column="1"
VerticalTextAlignment="Center"
HorizontalOptions="EndAndExpand"
FontSize="17"
TextColor="White">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding FutureBooking}" Value="True">
<Setter Property="Text" Value="{Binding FormattedPickUpTimeDateOnly}"/>
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding FutureBooking}" Value="False">
<Setter Property="Text" Value="{Binding FormattedDistance}" />
</DataTrigger>
</Label.Triggers>
</Label>
<Label Grid.Row="1"
Grid.Column="0"
Text="{Binding EndSuburb}"
TextColor="White"
FontFamily="Bold"
HorizontalOptions="StartAndExpand"
FontSize="17"/>
<Label
Grid.Row="1"
Grid.Column="1"
VerticalTextAlignment="Center"
HorizontalOptions="EndAndExpand"
FontSize="17"
TextColor="White">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding FutureBooking}" Value="True">
<Setter Property="Text" Value="{Binding FormattedPickUpTimeTimeOnly}" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding FutureBooking}" Value="False">
<Setter Property="Text" Value="ASAP" />
</DataTrigger>
</Label.Triggers>
</Label>
</Grid>
</StackLayout>
<Frame.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsRequired="1"
Command="{Binding Source={RelativeSource AncestorType={x:Type vm:BookingsViewModel}}, Path=BookingTapped}"
CommandParameter="{Binding .}">
</TapGestureRecognizer>
<!--<TapGestureRecognizer Tapped="OnBookingViewClicked"/>-->
</Frame.GestureRecognizers>
</Frame>
<Frame x:DataType="models:BookingDto"
IsVisible="{Binding IsSeenBooking}"
CornerRadius="10"
BackgroundColor="#B40431"
Padding="8,4"
Margin="9 ,16"
cmp:RelativeLayout.XConstraint=
"{cmp:ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=1,
Constant=-75
}">
<Label Grid.Row="0"
Grid.Column="0"
Text="NEW"
TextColor="White"
FontFamily="Bold"
HorizontalOptions="StartAndExpand"
FontSize="16"/>
</Frame>
</cmp:RelativeLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Frame>
<Button VerticalOptions="EndAndExpand"
Margin="40,0,40,15"
Text="REFRESH"
FontSize="20"
Visual="Default"
TextColor="White"
Command="{Binding OnRefreshClicked}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

+ 42
- 0
GMCabsDriverAssistantSolution/Views/BookingsPage.xaml.cs View File

@ -0,0 +1,42 @@
using GMCabsDriverAssistantSolution.ViewModels;
namespace GMCabsDriverAssistantSolution.Views;
public partial class BookingsPage : ContentPage
{
#region Fields
private readonly BookingsViewModel _viewModel;
#endregion
#region Properties
#endregion
#region Constructor
public BookingsPage(double? lat, double? lng)
{
InitializeComponent();
BindingContext = _viewModel = new BookingsViewModel();
if (lat == null)
{
var currentLocation = Geolocation.GetLastKnownLocationAsync();
lat = currentLocation.Result.Latitude;
lng = currentLocation.Result.Longitude;
}
_viewModel.CurrentLat = lat.Value;
_viewModel.CurrentLng = lng.Value;
}
#endregion
#region Methods
protected override void OnAppearing()
{
base.OnAppearing();
_viewModel.OnAppearing();
}
private async void OnBookingViewClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new BookingDetailsPage(), true);
}
#endregion
}

Loading…
Cancel
Save