public async Task SaveAsync(TTCheck check)
 {
     var checks = (await findAllAsync()).ToList();
     checks.Add(check);
     var jsonString = JsonConvert.SerializeObject(checks);
     var file = await localFile();
     await FileIO.WriteTextAsync(file, jsonString);
 }
        public async Task DeleteAsync(TTCheck check)
        {
            var checks = (await findAllAsync()).ToList();

            TTCheck orginalCheck = checks.Single(c => c.UserName == check.UserName && c.DateTime.Ticks == check.DateTime.Ticks);

            checks.Remove(orginalCheck);

            var jsonString = JsonConvert.SerializeObject(checks);
            var file = await localFile();
            await FileIO.WriteTextAsync(file, jsonString);
        }
        public async Task<DateTime?> CheckInOrOutAsync()
        {
            var client = new TTClient();
                        
#if DEBUG
            var checkinDatetime = await this.doCheckInOrOutMockAsync();
#else
            var checkinDatetime = await client.DoCheckInOrOutAsync(SecurityService.getCredential().Item1, SecurityService.getCredential().Item2);
#endif

            if (checkinDatetime.HasValue)
            {
                var repository = new TTRepository();
                var check = new TTCheck();
                check.UserName = SecurityService.getCredential().Item1;
                check.DateTime = checkinDatetime.Value;
                await Historic.Historic.AddAsync(check);
            }

            return checkinDatetime;
        }
        public static async Task AddAsync(TTCheck check)
        {
            TTRepository repository = new TTRepository();

            await repository.SaveAsync(check);

            Month month = historic.SingleOrDefault(
                m => m.Check.DateTime.Month == check.DateTime.Month && m.Check.DateTime.Year == check.DateTime.Year);

            if (month != null)
            {
                Day day = month.Days.SingleOrDefault(d => d.Check.DateTime.Day == check.DateTime.Day);

                if (day != null)
                {
                    Hour hour = day.Hours.FirstOrDefault(h => h.Check.DateTime.Ticks < check.DateTime.Ticks);

                    int i = day.Hours.Count;

                    if (hour != null)
                        i = day.Hours.IndexOf(hour);                                       

                    day.Hours.Insert(i, new Hour(day.Hours)
                    {
                        Check = check
                    });
                }
                else
                    month.Days.Insert(0, new Day
                    {
                        Hours = getHours(new TTCheck[] { check }),
                        Check = check
                    });

            }
            else
                historic.Insert(0, new Month
                {
                    Check = check,
                    Days = new ObservableCollection<Day>(getDays(new TTCheck[] { check }))
                });
        }