Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi selection #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Calendar.Plugin/Shared/Controls/MultiSelectionCalendar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Xamarin.Plugin.Calendar.Controls.SelectionEngines;

namespace Xamarin.Plugin.Calendar.Controls
{
public class MultiSelectionCalendar : Calendar
{
private readonly MultiSelectionEngine _multiSelectionEngine;

public MultiSelectionCalendar()
{
monthDaysView.CurrentSelectionEngine = _multiSelectionEngine = new MultiSelectionEngine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Xamarin.Plugin.Calendar.Controls.Interfaces;
using Xamarin.Plugin.Calendar.Models;

namespace Xamarin.Plugin.Calendar.Controls.SelectionEngines
{
public class MultiSelectionEngine : ISelectionEngine
{
private readonly HashSet<DateTime> _selectedDates;

public MultiSelectionEngine()
{
_selectedDates = new HashSet<DateTime>();
}

public string GetSelectedDateText(string selectedDateTextFormat, CultureInfo culture)
{
return _selectedDates
.Select(item => item.ToString(selectedDateTextFormat, culture))
.Aggregate((a, b) => $"{a}, {b}");
}

public bool TryGetSelectedEvents(EventCollection allEvents, out ICollection selectedEvents)
{
return allEvents.TryGetValues(_selectedDates, out selectedEvents);
}

public bool IsDateSelected(DateTime dateToCheck)
{
return _selectedDates.Contains(dateToCheck);
}

public List<DateTime> PerformDateSelection(DateTime dateToSelect)
{
if (_selectedDates.Contains(dateToSelect))
_selectedDates.Remove(dateToSelect);
else
_selectedDates.Add(dateToSelect);

return _selectedDates.ToList();
}

public void UpdateDateSelection(List<DateTime> datesToSelect)
{
datesToSelect.ForEach(date => _selectedDates.Add(date));
}
}
}