xaml:
<ContentPage.Resources> <ResourceDictionary> <Style x:Key="darkerButton" TargetType="Button"> <Setter Property="BackgroundColor" Value="#ddd"/> <Setter Property="TextColor" Value="Black" /> <Setter Property="BorderColor" Value="Blue"/> <Setter Property="FontSize" Value="Default" /> <Setter Property="CornerRadius" Value="10" /> </Style> </ResourceDictionary> </ContentPage.Resources> <ContentPage.Content> <StackLayout> <Frame Style="{StaticResource FrameStyle}"> <StackLayout > <Label x:Name="ApointingPersonName" Text="" Style="{StaticResource LableHeading}"/> <Label x:Name="ApointingPersonTag" Text="" Style="{StaticResource LableSubHeading}"/> </StackLayout> </Frame> <!--<StackLayout HeightRequest="90" BackgroundColor="#E8AD00">--> <Frame OutlineColor="#EFEFEF" HeightRequest="78" Margin="1" Padding="3"> <CollectionView ItemsSource="{Binding dayList}"> <CollectionView.ItemsLayout> <GridItemsLayout Orientation="Horizontal" HorizontalItemSpacing="5" VerticalItemSpacing="1" /> </CollectionView.ItemsLayout> <CollectionView.ItemTemplate> <DataTemplate x:DataType="models:DayList"> <Button x:Name="DayButton" Text="{Binding BindDayDate }" MinimumHeightRequest="25" Clicked="DayButton_Clicked" Style="{StaticResource darkerButton}"/> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> </Frame> <BoxView HeightRequest="1" BackgroundColor="MediumTurquoise"/> <Frame Style="{StaticResource FrameStyle}"> <StackLayout> <StackLayout> <Label x:Name="SelectedDayHead" Text="" Style="{StaticResource LableHeading}" HorizontalTextAlignment="Center" LineBreakMode="HeadTruncation"/> </StackLayout> <ContentView x:Name="SlotNOView" IsVisible="False" > <Label x:Name="SlotNOAvailable" Text="" FontSize="Medium" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Red"/> </ContentView> <RefreshView x:Name="SlotContentView" IsVisible="False"> <CollectionView x:Name="SlotListView"> <CollectionView.ItemsLayout> <GridItemsLayout Orientation="Vertical" Span="4" HorizontalItemSpacing="5" VerticalItemSpacing="5" SnapPointsType="Mandatory"/> </CollectionView.ItemsLayout> <CollectionView.ItemTemplate> <DataTemplate x:DataType="models:SlotListData"> <StackLayout> <Button Text="{Binding slot}" Clicked="SelectedSlot_Clicked"/> </StackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> </RefreshView> </StackLayout> </Frame> </StackLayout> </ContentPage.Content>
xaml.cs:
namespace EZQueue.Views.BookAppointment { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class TimeSlotList : ContentPage { VMTimeSlotList VMModel; public ServiceSlot serviceSlot { get; set; } public WorkTemplate workTemplate { get; set; } public ApointingPerson apointingPerson { get; set; } DayList dayList { get; set; } public TimeSlotList(ServiceSlot InputServiceSlot,ApointingPerson InputapointingPerson) { InitializeComponent(); apointingPerson = InputapointingPerson; ApointingPersonName.Text = apointingPerson.Name; ApointingPersonTag.Text = InputServiceSlot.TagList; serviceSlot = InputServiceSlot; VMModel = new VMTimeSlotList(serviceSlot); BindingContext = VMModel; GetTimeSlotList(); GetWorkTemplateList(); GetCancelServiceList(); } async void GetTimeSlotList() { await VMModel.RefreshListAsync(); } async void GetWorkTemplateList() { await VMModel.RefreshWorkTemplateAsync(); } async void GetCancelServiceList() { await VMModel.RefreshGetCancelServiceAsync(); } async void GetSlotListData() { await VMModel.RefreshGetSlotListDataAsync(); SlotListView.ItemsSource = VMModel.SlotListDataGroup; SlotContentView.IsVisible = true; } private void DayButton_Clicked(object sender, EventArgs e) { SlotContentView.IsVisible = false; SlotNOView.IsVisible = false; Button btn = (Button)sender; dayList=(DayList)btn.BindingContext; VMTimeSlotList.SelectedDate = DateTime.Parse(dayList.date); SelectedDayHead.Text = dayList.dayString + ", " + dayList.date; checkSoltAvailableORNot(); } public void checkSoltAvailableORNot() { switch (dayList.dayString) { case "Monday": if (VMModel.workTemplate.Monday == true) { GetSlotListData(); } else { slotNOAvailable(); } break; case "Tuesday": if (VMModel.workTemplate.Tuesday == true) { GetSlotListData(); } else { slotNOAvailable(); } break; case "Wednesday": if (VMModel.workTemplate.Wednesday == true) { GetSlotListData(); } else { slotNOAvailable(); } break; case "Thursday": if (VMModel.workTemplate.Thursday == true) { GetSlotListData(); } else { slotNOAvailable(); } break; case "Friday": if (VMModel.workTemplate.Friday == true) { GetSlotListData(); } else { slotNOAvailable(); } break; case "Saturday": if (VMModel.workTemplate.Saturday == true) { GetSlotListData(); } else { slotNOAvailable(); } break; case "Sunday": if (VMModel.workTemplate.Sunday == true) { GetSlotListData(); } else { slotNOAvailable(); } break; } } public void slotNOAvailable() { SlotNOAvailable.Text = "Slot NOT Available...!!"; SlotNOView.IsVisible = true; } private void SelectedSlot_Clicked(object sender, EventArgs e) { Button btn = (Button)sender; SlotListData SelectedTime = (SlotListData)btn.BindingContext; Navigation.PushAsync(new CustomerDetail(SelectedTime,VMModel.workTemplate,apointingPerson,dayList) { Title = "Patient Details" }); } } } vMmodel: namespace EZQueue.ViewModels { public class CancelStartEnd { public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } } public class VMTimeSlotList : INotifyPropertyChanged { public ObservableCollection<DayList> dayListAll { get; set; } = new ObservableCollection<DayList>(); public ObservableCollection<DayList> dayList { get; set; } = new ObservableCollection<DayList>(); public ObservableCollection<CancelService> cancelServiceAll { get; set; } = new ObservableCollection<CancelService>(); public ObservableCollection<CancelService> cancelService { get; set; } = new ObservableCollection<CancelService>(); public CancelStartEnd cancelStartEnd { get; set; } = new CancelStartEnd(); public WorkTemplate workTemplateAll { get; set; } = new WorkTemplate(); public WorkTemplate workTemplate { get; set; } = new WorkTemplate(); public List<SlotListData> SlotListDataGroupAll { get; set; } = new List<SlotListData>(); public List<SlotListData> SlotListDataGroup { get; set; } = new List<SlotListData>(); DateTime TodayDate = DateTime.Today.AddDays(0); public ObservableCollection<Appointment> appointment { get; set; } = new ObservableCollection<Appointment>(); public int serviceSlotID { get; set; } public static DateTime SelectedDate { get; set; } public List<string> BookedAppointmentTime { get; set; } = new List<string>(); public VMTimeSlotList(ServiceSlot InputServiceSlot) { serviceSlotID = InputServiceSlot.ServiceSlotID; } private void UpdatelistDayList() { dayList.Clear(); foreach (var item in dayListAll) { dayList.Add(item); } UpdateUserPromt(); } public async Task RefreshListAsync() { IsRefreshing = true; try { dayListAll = await WebManager.GetDayListAsync(TodayDate, serviceSlotID); UpdatelistDayList(); } catch { } IsRefreshing = false; } private void UpdateWorkTemplate() { workTemplate = workTemplateAll; UpdateUserPromt(); } private void UpdateSlotListData() { SlotListDataGroup.Clear(); foreach (var item in SlotListDataGroupAll) { SlotListDataGroup.Add(item); } UpdateUserPromt(); } public async Task RefreshWorkTemplateAsync() { IsRefreshing = true; try { workTemplateAll = await WebManager.GetWorkTemplateAsync(serviceSlotID); UpdateWorkTemplate(); } catch {} IsRefreshing = false; } public async Task RefreshGetSlotListDataAsync() { IsRefreshing = true; try { await getCancelServiceAsync(); await getappointmentAsync(); SlotListDataGroupAll.Clear(); DateTime StartTime = workTemplate.StartTime; DateTime EndTime = workTemplate.EndTime; TimeSpan span = EndTime.Subtract(StartTime); int TotalMinutes = (int)span.TotalMinutes; int TimeSpan = (int)workTemplate.SlotGranularity.TotalMinutes; int count = TotalMinutes / TimeSpan; DateTime dateTime = StartTime; DateTime t2 = Convert.ToDateTime((cancelStartEnd.StartTime.ToShortTimeString())); DateTime t3 = Convert.ToDateTime((cancelStartEnd.EndTime.ToShortTimeString())); for (int j = 0; j < count; j++) { DateTime t1 = Convert.ToDateTime((dateTime.ToShortTimeString())); int v1 = DateTime.Compare(t1, t2); int v2 = DateTime.Compare(t1, t3); /* if t1 is less than t2 then result is -1 * if t1 equals t2 then result is 0 * if t1 is greater than t2 then result 1 */ if (!(BookedAppointmentTime.Contains(dateTime.ToShortTimeString()))&&((v1>=0) & v2>=0 )||(v1<0 & v2<0)&&!(BookedAppointmentTime.Contains(dateTime.ToShortTimeString()))) { SlotListDataGroupAll.Add(new SlotListData() { slot = dateTime.ToShortTimeString() }); } dateTime = dateTime.AddMinutes(TimeSpan); } UpdateSlotListData(); } catch(Exception e) { } IsRefreshing = false; } public async Task RefreshGetCancelServiceAsync() { cancelServiceAll = await WebManager.GetCancelServiceAsync(workTemplate.ServiceSlotID); } private async Task getCancelServiceAsync() { foreach (var item in cancelServiceAll) { if(SelectedDate.ToShortDateString()==item.Date.ToShortDateString()) { cancelStartEnd=new CancelStartEnd() { StartTime = item.StartTime, EndTime = item.EndTime }; } } } private async Task getappointmentAsync() { string ServiceSlotIDSelectedDate = workTemplate.ServiceSlotID.ToString() + "," + SelectedDate.ToString(); appointment = await WebManager.GetAppointmentAsync(ServiceSlotIDSelectedDate); BookedAppointmentTime.Clear(); foreach (var item in appointment) { BookedAppointmentTime.Add(item.StartTime.ToShortTimeString()); } } public void UpdateUserPromt() { if (0 == SlotListDataGroupAll.Count) { if (false == NetworkInterface.GetIsNetworkAvailable()) { ShowMessage(EZQResource.SMInternateNotAvailable, true); } } } public void ShowMessage(string Message, bool bIsError = false) { UserPromt = Message; if (bIsError) { UserPromtColor = Color.Red; } else { UserPromtColor = Color.Black; } } public string m_UserPromt = ""; public string UserPromt { get { return m_UserPromt; } set { if (m_UserPromt != value) { m_UserPromt = value; NotifyPropertyChanged(); } } } public Color m_UserPromtColor; public Color UserPromtColor { get { return m_UserPromtColor; } set { if (m_UserPromtColor != value) { m_UserPromtColor = value; NotifyPropertyChanged(); } } } private bool m_isRefreshing = false; public bool IsRefreshing { get { return m_isRefreshing; } set { if (m_isRefreshing != value) { m_isRefreshing = value; NotifyPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; void NotifyPropertyChanged([CallerMemberName] string name = "") { if (name != "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } } } data ~~~~ DayList: public class DayList { public int serviceSlotID { get; set; } public string dayString { get; set; } public string date { get; set; } public string BindDayDate { get { return string.Format("{0} \n{1}", dayString,DateTime.Parse(date).ToShortDateString()); } } } classWorkTemplate:~~~~ public class WorkTemplate { public int WorkTemplateID { get; set; } public string Name { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public TimeSpan SlotGranularity { get; set; } public bool Monday { get; set; } public bool Tuesday { get; set; } public bool Wednesday { get; set; } public bool Thursday { get; set; } public bool Friday { get; set; } public bool Saturday { get; set; } public bool Sunday { get; set; } public int ServiceSlotID { get; set; } }
VideoLink:
https://youtu.be/kpKBlJ91wyo