public async Task<bool> updateDB(string timerText, TimeSpan timerdata, string activityName) { bool res = await checkIfFileExists("activityDB"); RootObjectTrackAct rtrackact = new RootObjectTrackAct(); if (res) { string response = await readFile("activityDB"); rtrackact = TrackAct.trackactDataDeserializer(response); int activity_pos = -1; for (int i = 0; i < rtrackact.activity.Count; i++) { if (rtrackact.activity[i].name == activityName) { activity_pos = i; } } /// If the activity exists if (activity_pos != -1) { TimerData tdata = new TimerData(); tdata.position = rtrackact.activity[activity_pos].timer_data[rtrackact.activity[activity_pos].timer_data.Count - 1].position + 1; // The mumbo jumbo is to get the value of 'position' in the last element in the track_data list and adding 1 to it. if (tdata.position >= countLimit) { rtrackact.activity[activity_pos].timer_data.RemoveAt(0); } tdata.time_in_seconds = (long)timerdata.TotalSeconds; SortedSet<long> time_in_seconds = new SortedSet<long>(); for (int i = 0; i < rtrackact.activity[activity_pos].timer_data.Count; i++) { time_in_seconds.Add(rtrackact.activity[activity_pos].timer_data[i].time_in_seconds); } time_in_seconds.Add((long)timerdata.TotalSeconds); long mediansec = (time_in_seconds.ElementAtOrDefault(time_in_seconds.Count / 2)) ;//time_in_seconds[time_in_seconds.Count / 2]; rtrackact.activity[activity_pos].median = String.Format("{0:00}:{1:00}:{2:00}", mediansec / 3600, (mediansec / 60) % 60, mediansec % 60); int pos = (int)(0.9 * (time_in_seconds.Count - 1) + 1); // 0 1 3 4 5 8 long ninentypercentilesecond = (time_in_seconds.ElementAtOrDefault(pos)); rtrackact.activity[activity_pos].ninetypercentile = String.Format("{0:00}:{1:00}:{2:00}", ninentypercentilesecond / 3600, (ninentypercentilesecond / 60) % 60, ninentypercentilesecond % 60); long personal_best = (time_in_seconds.ElementAtOrDefault(time_in_seconds.Count - 1)); rtrackact.activity[activity_pos].personal_best = String.Format("{0:00}:{1:00}:{2:00}", (personal_best) / 3600, ((personal_best) / 60) % 60, (personal_best) % 60); rtrackact.activity[activity_pos].timer_data.Add(tdata); //findMedian(rtrackact.activity[activity_pos]); } /// If the activity does not exist else { ActivityData ractivitydata = new ActivityData(); ractivitydata.name = activityName; TimerData tdata = new TimerData(); tdata.position = 0; // Since this is a new activity, it won't have any data already associated with it. tdata.time_in_seconds = (long)timerdata.TotalSeconds; ractivitydata.timer_data.Add(tdata); ractivitydata.personal_best = String.Format("{0:00}:{1:00}:{2:00}", timerdata.Hours, (timerdata.Minutes), (long)timerdata.Seconds); ractivitydata.median = String.Format("{0:00}:{1:00}:{2:00}", timerdata.Hours, timerdata.Minutes, (long)timerdata.Seconds); ractivitydata.ninetypercentile = String.Format("{0:00}:{1:00}:{2:00}", timerdata.Hours, (timerdata.Minutes), (long)timerdata.Seconds); rtrackact.activity.Add(ractivitydata); } } else { ActivityData ractivitydata = new ActivityData(); ractivitydata.name = activityName; TimerData tdata = new TimerData(); tdata.position = 0; // Since this is a new activity, it won't have any data already associated with it. tdata.time_in_seconds = (long)timerdata.TotalSeconds; ractivitydata.timer_data = new List<TimerData>(); ractivitydata.timer_data.Add(tdata); rtrackact.activity = new List<ActivityData>(); ractivitydata.personal_best = String.Format("{0:00}:{1:00}:{2:00}", timerdata.Hours, (timerdata.Minutes), (long)timerdata.Seconds); ractivitydata.median = String.Format("{0:00}:{1:00}:{2:00}", timerdata.Hours, (timerdata.Minutes), (long)timerdata.Seconds); ractivitydata.ninetypercentile = String.Format("{0:00}:{1:00}:{2:00}", timerdata.Hours, (timerdata.Minutes), (long)timerdata.Seconds); rtrackact.activity.Add(ractivitydata); } try { await writeFile("activityDB", TrackAct.trackactSerializer(rtrackact)); } catch (Exception ex) { Debug.WriteLine(ex); } Debug.WriteLine("DB update finished at : " + DateTime.Now.Millisecond); return true; }
private void RefreshUI() { activity_d = new ObservableCollection<ActivityData>(); Debug.WriteLine("In Refresh UI"); personalBest.Visibility = Visibility.Visible; //string res = await library.readFile("activityDB"); //RootObjectTrackAct rtrackact = TrackAct.trackactDataDeserializer(res); if ((bool)ApplicationData.Current.LocalSettings.Values["FirstLaunch"] == true) { try { activityName.Text = (string)ApplicationData.Current.LocalSettings.Values["CurrentAct"]; ActivityData ractivitydata = new ActivityData(); ractivitydata.name = activityName.Text; ractivitydata.createdTime = DateTime.UtcNow; ractivitydata.totalTime = 0; TimerData tdata = new TimerData(); tdata.position = 0; // Since this is a new activity, it won't have any data already associated with it. tdata.time_in_seconds = (long)timerdata_TimeSpan.TotalSeconds; ractivitydata.totalTime = ractivitydata.totalTime + tdata.time_in_seconds; tdata.startTime = timerstartTime; tdata.endTime = timerendTime; ractivitydata.lastattempt = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); //lastTime.Text = ractivitydata.lastattempt; ractivitydata.timer_data = new List<TimerData>(); ractivitydata.timer_data.Add(tdata); ractivitydata.personal_best = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); ractivitydata.median = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); ractivitydata.ninetypercentile = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); rtrackact.activity = new List<ActivityData>(); rtrackact.activity.Add(ractivitydata); personalBest.Text = ractivitydata.personal_best; personalBestGrid.Opacity = 100; MedianTextBlock.Text = ractivitydata.median; NinetyPercentileTextBlock.Text = ractivitydata.ninetypercentile; StatisticsGrid.Opacity = 100; ApplicationData.Current.LocalSettings.Values["FirstLaunch"] = false; activity_d.Add(ractivitydata); } catch (Exception ex) { Debug.WriteLine("In Refresh UI FirstLaunch set to true : " + ex); } } else { int activity_pos = -1; for (int i = 0; i < rtrackact.activity.Count; i++) { if (rtrackact.activity[i].name == activityName.Text) { activity_pos = i; } } if (activity_pos == -1) { Debug.Write("In RefreshUI. Activity doesn't exist"); ActivityData ractivitydata = new ActivityData(); ractivitydata.name = activityName.Text; ractivitydata.createdTime = DateTime.UtcNow; ractivitydata.totalTime = 0; TimerData tdata = new TimerData(); //tdata.position = 0; // Since this is a new activity, it won't have any data already associated with it. tdata.time_in_seconds = (long)timerdata_TimeSpan.TotalSeconds; ractivitydata.lastattempt = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); ractivitydata.totalTime = ractivitydata.totalTime + tdata.time_in_seconds; //lastTime.Text = ractivitydata.lastattempt; tdata.startTime = timerstartTime; tdata.endTime = timerendTime; ractivitydata.timer_data = new List<TimerData>(); ractivitydata.timer_data.Add(tdata); ractivitydata.personal_best = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); ractivitydata.median = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); ractivitydata.ninetypercentile = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); rtrackact.activity.Add(ractivitydata); personalBest.Text = ractivitydata.personal_best; personalBestGrid.Opacity = 100; MedianTextBlock.Text = ractivitydata.median; NinetyPercentileTextBlock.Text = ractivitydata.ninetypercentile; StatisticsGrid.Opacity = 100; } else { TimerData tdata = new TimerData(); tdata.position = rtrackact.activity[activity_pos].timer_data[rtrackact.activity[activity_pos].timer_data.Count - 1].position + 1; // The mumbo jumbo is to get the value of 'position' in the last element in the track_data list and adding 1 to it. tdata.startTime = timerstartTime; tdata.endTime = timerendTime; if (tdata.position >= countLimit) { rtrackact.activity[activity_pos].timer_data.RemoveAt(0); } tdata.time_in_seconds = (long)timerdata_TimeSpan.TotalSeconds; SortedSet<long> time_in_seconds = new SortedSet<long>(); for (int i = 0; i < rtrackact.activity[activity_pos].timer_data.Count; i++) { time_in_seconds.Add(rtrackact.activity[activity_pos].timer_data[i].time_in_seconds); } time_in_seconds.Add((long)timerdata_TimeSpan.TotalSeconds); long mediansec = (time_in_seconds.ElementAtOrDefault(time_in_seconds.Count / 2));//time_in_seconds[time_in_seconds.Count / 2]; rtrackact.activity[activity_pos].totalTime = rtrackact.activity[activity_pos].totalTime + tdata.time_in_seconds; rtrackact.activity[activity_pos].median = String.Format("{0:00}:{1:00}:{2:00}", mediansec / 3600, (mediansec / 60) % 60, mediansec % 60); rtrackact.activity[activity_pos].lastattempt = String.Format("{0:00}:{1:00}:{2:00}", (long)timerdata_TimeSpan.TotalSeconds / 3600, ((long)timerdata_TimeSpan.TotalSeconds / 60) % 60, (long)timerdata_TimeSpan.TotalSeconds % 60); // lastTime.Text = rtrackact.activity[activity_pos].lastattempt; int pos = (int)(0.9 * (time_in_seconds.Count - 1) + 1); // 0 1 3 4 5 8 long ninentypercentilesecond = (time_in_seconds.ElementAtOrDefault(pos)); rtrackact.activity[activity_pos].ninetypercentile = String.Format("{0:00}:{1:00}:{2:00}", ninentypercentilesecond / 3600, (ninentypercentilesecond / 60) % 60, ninentypercentilesecond % 60); long personal_best = (time_in_seconds.ElementAtOrDefault(time_in_seconds.Count - 1)); rtrackact.activity[activity_pos].personal_best = String.Format("{0:00}:{1:00}:{2:00}", (personal_best) / 3600, ((personal_best) / 60) % 60, (personal_best) % 60); rtrackact.activity[activity_pos].timer_data.Add(tdata); personalBest.Text = rtrackact.activity[activity_pos].personal_best; personalBestGrid.Opacity = 100; MedianTextBlock.Text = rtrackact.activity[activity_pos].median; NinetyPercentileTextBlock.Text = rtrackact.activity[activity_pos].ninetypercentile; StatisticsGrid.Opacity = 100; } } }
private void updateDBFileExists(string timerText, TimeSpan timerdata, int activity_pos) { if(activity_pos != -1) { TimerData tdata = new TimerData(); //tdata.position = } }