Skip to content

Commit

Permalink
Merge pull request #75 from builttoroam/feature/67-replace-int-conver…
Browse files Browse the repository at this point in the history
…ters

#67 Use int.TryParse instead of Convert.ToInt32
  • Loading branch information
ScottBTR authored Feb 6, 2020
2 parents ca626cd + 9bba3a0 commit 73a21d1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Samples/Samples/View/CalendarPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public CalendarPage()
protected override void OnAppearing()
{
base.OnAppearing();

var viewModel = BindingContext as CalendarViewModel;

viewModel?.RefreshCalendars();
}

Expand Down
5 changes: 3 additions & 2 deletions Samples/Samples/ViewModel/CalendarEventAddViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public bool IsChecked
set
{
isChecked = value;

OnPropertyChanged();
}
}
Expand All @@ -59,8 +60,8 @@ static TimeSpan RoundToNearestMinutes(TimeSpan input, int minutes)
new DayOfTheWeekSwitch() { Day = DayOfTheWeek.Wednesday, IsChecked = true },
new DayOfTheWeekSwitch() { Day = DayOfTheWeek.Thursday, IsChecked = true },
new DayOfTheWeekSwitch() { Day = DayOfTheWeek.Friday, IsChecked = true },
new DayOfTheWeekSwitch() { Day = DayOfTheWeek.Saturday, IsChecked = false },
new DayOfTheWeekSwitch() { Day = DayOfTheWeek.Sunday, IsChecked = false }
new DayOfTheWeekSwitch() { Day = DayOfTheWeek.Saturday },
new DayOfTheWeekSwitch() { Day = DayOfTheWeek.Sunday }
};

public CalendarEventAddViewModel(string calendarId, string calendarName, CalendarEvent existingEvent = null)
Expand Down
105 changes: 60 additions & 45 deletions Xamarin.Essentials/Calendars/Calendars.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public static partial class Calendars
const string weeklyFrequency = "WEEKLY";
const string monthlyFrequency = "MONTHLY";
const string yearlyFrequency = "YEARLY";
const string byFrequencySearch = "FREQ=";
const string byDaySearch = "BYDAY=";
const string byMonthDaySearch = "BYMONTHDAY=";
const string byMonthSearch = "BYMONTH=";
const string bySetPosSearch = "BYSETPOS=";
const string byIntervalSearch = "INTERVAL=";
const string byCountSearch = "COUNT=";
const string byUntilSearch = "UNTIL=";

static async Task<IEnumerable<Calendar>> PlatformGetCalendarsAsync()
{
Expand Down Expand Up @@ -369,7 +377,7 @@ static async Task<string> PlatformCreateCalendarEvent(CalendarEvent newEvent)
var eventValues = SetupContentValues(newEvent);

var resultUri = Platform.AppContext.ApplicationContext.ContentResolver.Insert(eventUri, eventValues);
if (int.TryParse(resultUri.LastPathSegment, out result))
if (int.TryParse(resultUri?.LastPathSegment, out result))
{
return result.ToString();
}
Expand All @@ -393,10 +401,12 @@ static async Task<bool> PlatformUpdateCalendarEvent(CalendarEvent eventToUpdate)
{
await DeleteCalendarEventById(thisEvent.Id, thisEvent.CalendarId);
var resultUri = Platform.AppContext.ApplicationContext.ContentResolver.Insert(eventUri, eventValues);
if (int.TryParse(resultUri.LastPathSegment, out var result))
if (int.TryParse(resultUri?.LastPathSegment, out var result))
{
return true;
}

return false;
}
else if (Platform.AppContext.ApplicationContext.ContentResolver.Update(eventUri, eventValues, $"{CalendarContract.Attendees.InterfaceConsts.Id}={eventToUpdate.Id}", null) > 0)
{
Expand Down Expand Up @@ -445,7 +455,7 @@ static async Task<bool> PlatformDeleteCalendarEventInstanceByDate(string eventId
eventValues.Put(CalendarContract.Events.InterfaceConsts.Status, (int)EventsStatus.Canceled);

var resultUri = Platform.AppContext.ApplicationContext.ContentResolver.Insert(eventUri, eventValues);
if (int.TryParse(resultUri.LastPathSegment, out var result))
if (int.TryParse(resultUri?.LastPathSegment, out var result))
{
return result > 0;
}
Expand Down Expand Up @@ -494,9 +504,13 @@ static async Task<bool> PlatformAddAttendeeToEvent(CalendarEventAttendee newAtte
attendeeValues.Put(CalendarContract.Attendees.InterfaceConsts.AttendeeType, (int)newAttendee.Type);

var resultUri = Platform.AppContext.ApplicationContext.ContentResolver.Insert(attendeeUri, attendeeValues);
var result = Convert.ToInt32(resultUri.LastPathSegment);

return result > 0;
if (int.TryParse(resultUri?.LastPathSegment, out var result))
{
return result > 0;
}

return false;
}

static async Task<bool> PlatformRemoveAttendeeFromEvent(CalendarEventAttendee newAttendee, string eventId)
Expand All @@ -523,9 +537,9 @@ static async Task<bool> PlatformRemoveAttendeeFromEvent(CalendarEventAttendee ne
static RecurrenceRule GetRecurranceRuleForEvent(string rule)
{
var recurranceRule = new RecurrenceRule();
if (rule.Contains("FREQ="))
if (rule.Contains(byFrequencySearch, StringComparison.Ordinal))
{
var ruleFrequency = rule.Substring(rule.IndexOf("FREQ=", StringComparison.Ordinal) + 5);
var ruleFrequency = rule.Substring(rule.IndexOf(byFrequencySearch, StringComparison.Ordinal) + byFrequencySearch.Length);
ruleFrequency = ruleFrequency.Contains(";") ? ruleFrequency.Substring(0, ruleFrequency.IndexOf(";")) : ruleFrequency;
switch (ruleFrequency)
{
Expand All @@ -544,9 +558,9 @@ static RecurrenceRule GetRecurranceRuleForEvent(string rule)
}
}

if (rule.Contains("INTERVAL="))
if (rule.Contains(byIntervalSearch, StringComparison.Ordinal))
{
var ruleInterval = rule.Substring(rule.IndexOf("INTERVAL=", StringComparison.Ordinal) + 9);
var ruleInterval = rule.Substring(rule.IndexOf(byIntervalSearch, StringComparison.Ordinal) + byIntervalSearch.Length);
ruleInterval = ruleInterval.Contains(";") ? ruleInterval.Substring(0, ruleInterval.IndexOf(";", StringComparison.Ordinal)) : ruleInterval;
recurranceRule.Interval = uint.Parse(ruleInterval);
}
Expand All @@ -555,34 +569,33 @@ static RecurrenceRule GetRecurranceRuleForEvent(string rule)
recurranceRule.Interval = 1;
}

if (rule.Contains("COUNT="))
if (rule.Contains(byCountSearch, StringComparison.Ordinal))
{
var ruleOccurences = rule.Substring(rule.IndexOf("COUNT=", StringComparison.Ordinal) + 6);
var ruleOccurences = rule.Substring(rule.IndexOf(byCountSearch, StringComparison.Ordinal) + byCountSearch.Length);
ruleOccurences = ruleOccurences.Contains(";") ? ruleOccurences.Substring(0, ruleOccurences.IndexOf(";", StringComparison.Ordinal)) : ruleOccurences;
recurranceRule.TotalOccurrences = uint.Parse(ruleOccurences);
}

if (rule.Contains("UNTIL="))
if (rule.Contains(byUntilSearch, StringComparison.Ordinal))
{
var ruleEndDate = rule.Substring(rule.IndexOf("UNTIL=", StringComparison.Ordinal) + 6);
var ruleEndDate = rule.Substring(rule.IndexOf(byUntilSearch, StringComparison.Ordinal) + byUntilSearch.Length);
ruleEndDate = ruleEndDate.Contains(";") ? ruleEndDate.Substring(0, ruleEndDate.IndexOf(";", StringComparison.Ordinal)) : ruleEndDate;
recurranceRule.EndDate = DateTimeOffset.ParseExact(ruleEndDate.Replace("T", string.Empty).Replace("Z", string.Empty), "yyyyMMddHHmmss", null);
}

if (rule.Contains("BYDAY="))
if (rule.Contains(byDaySearch, StringComparison.Ordinal))
{
var ruleOccurenceDays = rule.Substring(rule.IndexOf("BYDAY=", StringComparison.Ordinal) + 6);
var ruleOccurenceDays = rule.Substring(rule.IndexOf(byDaySearch, StringComparison.Ordinal) + byDaySearch.Length);
ruleOccurenceDays = ruleOccurenceDays.Contains(";") ? ruleOccurenceDays.Substring(0, ruleOccurenceDays.IndexOf(";", StringComparison.Ordinal)) : ruleOccurenceDays;
recurranceRule.DaysOfTheWeek = new List<DayOfTheWeek>();
foreach (var ruleOccurenceDay in ruleOccurenceDays.Split(','))
{
var day = ruleOccurenceDay;
if (ruleOccurenceDay.Any(char.IsDigit))
var day = d;
var regex = new Regex(@"[-]?\d+");
var iterationOffset = regex.Match(d);
if (iterationOffset.Success)
{
var regex = new Regex(@"[-]?\d+");
var iterationOffset = regex.Match(ruleOccurenceDay);

day = ruleOccurenceDay.Substring(iterationOffset.Index + iterationOffset.Length);
day = d.Substring(iterationOffset.Index + iterationOffset.Length);

if (recurranceRule.Frequency == RecurrenceFrequency.Monthly)
{
Expand All @@ -592,7 +605,8 @@ static RecurrenceRule GetRecurranceRuleForEvent(string rule)
{
recurranceRule.Frequency = RecurrenceFrequency.YearlyOnDay;
}
recurranceRule.WeekOfMonth = (IterationOffset)Convert.ToInt32(iterationOffset.Value);
int.TryParse(iterationOffset.Value.Split(',').FirstOrDefault(), out var result);
recurranceRule.WeekOfMonth = (IterationOffset)result;
}
switch (day)
{
Expand Down Expand Up @@ -621,26 +635,27 @@ static RecurrenceRule GetRecurranceRuleForEvent(string rule)
}
}

if (rule.Contains("BYMONTHDAY="))
if (rule.Contains(byMonthDaySearch, StringComparison.Ordinal))
{
var ruleOccurenceMonthDays = rule.Substring(rule.IndexOf("BYMONTHDAY=", StringComparison.Ordinal) + 11);
var ruleOccurenceMonthDays = rule?.Substring(rule.IndexOf(byMonthDaySearch, StringComparison.Ordinal) + byMonthDaySearch.Length);
ruleOccurenceMonthDays = ruleOccurenceMonthDays.Contains(";") ? ruleOccurenceMonthDays.Substring(0, ruleOccurenceMonthDays.IndexOf(";", StringComparison.Ordinal)) : ruleOccurenceMonthDays;
recurranceRule.DayOfTheMonth = (uint)Math.Abs(Convert.ToInt32(ruleOccurenceMonthDays.Split(',').FirstOrDefault()));
uint.TryParse(ruleOccurenceMonthDays.Split(',').FirstOrDefault(), out var result);
recurranceRule.DayOfTheMonth = result;
}

if (rule.Contains("BYMONTH="))
if (rule.Contains(byMonthSearch, StringComparison.Ordinal))
{
var ruleOccurenceMonths = rule.Substring(rule.IndexOf("BYMONTH=", StringComparison.Ordinal) + 8);
var ruleOccurenceMonths = rule.Substring(rule.IndexOf(byMonthSearch, StringComparison.Ordinal) + byMonthSearch.Length);
ruleOccurenceMonths = ruleOccurenceMonths.Contains(";") ? ruleOccurenceMonths.Substring(0, ruleOccurenceMonths.IndexOf(";", StringComparison.Ordinal)) : ruleOccurenceMonths;
recurranceRule.MonthOfTheYear = (MonthOfYear)Convert.ToUInt32(ruleOccurenceMonths.Split(',').FirstOrDefault());
}

if (rule.Contains("BYSETPOS="))
if (rule.Contains(bySetPosSearch, StringComparison.Ordinal))
{
var ruleDayIterationOffset = rule.Substring(rule.IndexOf("BYSETPOS=", StringComparison.Ordinal) + 9);
var ruleDayIterationOffset = rule.Substring(rule.IndexOf(bySetPosSearch, StringComparison.Ordinal) + bySetPosSearch.Length);
ruleDayIterationOffset = ruleDayIterationOffset.Contains(";") ? ruleDayIterationOffset.Substring(0, ruleDayIterationOffset.IndexOf(";", StringComparison.Ordinal)) : ruleDayIterationOffset;
recurranceRule.WeekOfMonth = (IterationOffset)Convert.ToInt32(ruleDayIterationOffset.Split(',').FirstOrDefault());

Enum.TryParse<IterationOffset>(ruleDayIterationOffset.Split(',').FirstOrDefault(), out var result);
recurranceRule.WeekOfMonth = result;
if (recurranceRule.Frequency == RecurrenceFrequency.Monthly)
{
recurranceRule.Frequency = RecurrenceFrequency.MonthlyOnDay;
Expand All @@ -663,45 +678,45 @@ static string ConvertRule(this RecurrenceRule recurrenceRule)
case RecurrenceFrequency.Weekly:
if (recurrenceRule.DaysOfTheWeek != null && recurrenceRule.DaysOfTheWeek.Count > 0)
{
eventRecurrence += $"FREQ={weeklyFrequency};";
eventRecurrence += $"BYDAY={recurrenceRule.DaysOfTheWeek.ToDayString()};";
eventRecurrence += $"{byFrequencySearch}{weeklyFrequency};";
eventRecurrence += $"{byDaySearch}{recurrenceRule.DaysOfTheWeek.ToDayString()};";
}
else
{
eventRecurrence += $"FREQ={dailyFrequency};";
eventRecurrence += $"{byFrequencySearch}{dailyFrequency};";
}
eventRecurrence += $"INTERVAL={recurrenceRule.Interval};";
eventRecurrence += $"{byIntervalSearch}{recurrenceRule.Interval};";
break;
case RecurrenceFrequency.Monthly:
eventRecurrence += $"FREQ={monthlyFrequency};";
eventRecurrence += $"{byFrequencySearch}{monthlyFrequency};";
if (recurrenceRule.DaysOfTheWeek != null && recurrenceRule.DaysOfTheWeek.Count > 0)
{
eventRecurrence += $"BYDAY={recurrenceRule.WeekOfMonth}{recurrenceRule.DaysOfTheWeek.ToDayString()};";
eventRecurrence += $"{byDaySearch}{recurrenceRule.WeekOfMonth}{recurrenceRule.DaysOfTheWeek.ToDayString()};";
}
else if (recurrenceRule.DayOfTheMonth != 0)
{
eventRecurrence += $"BYMONTHDAY={recurrenceRule.DayOfTheMonth};";
eventRecurrence += $"{byMonthDaySearch}{recurrenceRule.DayOfTheMonth};";
}
else
{
eventRecurrence += $"INTERVAL={recurrenceRule.Interval};";
eventRecurrence += $"{byIntervalSearch}{recurrenceRule.Interval};";
}
break;
case RecurrenceFrequency.Yearly:
eventRecurrence += $"FREQ={yearlyFrequency};";
eventRecurrence += $"{byFrequencySearch}{yearlyFrequency};";
if (recurrenceRule.DaysOfTheWeek != null && recurrenceRule.DaysOfTheWeek.Count > 0)
{
eventRecurrence += $"BYMONTH={(int)recurrenceRule.MonthOfTheYear};";
eventRecurrence += $"BYDAY={recurrenceRule.WeekOfMonth}{recurrenceRule.DaysOfTheWeek.ToDayString()};";
eventRecurrence += $"{byMonthSearch}{(int)recurrenceRule.MonthOfTheYear};";
eventRecurrence += $"{byDaySearch}{recurrenceRule.WeekOfMonth}{recurrenceRule.DaysOfTheWeek.ToDayString()};";
}
else if (recurrenceRule.DayOfTheMonth != 0)
{
eventRecurrence += $"BYMONTH={(int)recurrenceRule.MonthOfTheYear};";
eventRecurrence += $"BYMONTHDAY={recurrenceRule.DayOfTheMonth};";
eventRecurrence += $"{byMonthSearch}{(int)recurrenceRule.MonthOfTheYear};";
eventRecurrence += $"{byMonthDaySearch}{recurrenceRule.DayOfTheMonth};";
}
else
{
eventRecurrence += $"INTERVAL={recurrenceRule.Interval};";
eventRecurrence += $"{byIntervalSearch}{recurrenceRule.Interval};";
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions Xamarin.Essentials/Calendars/Calendars.ios.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using EventKit;
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Essentials/Types/Calendar.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public enum IterationOffset
First = 1,
Second = 2,
Third = 3,
Fourth = 4,
Fourth = 4
}

#else
Expand Down

0 comments on commit 73a21d1

Please sign in to comment.