public static void SaveTimeRecorded(TimeRecorderInfo info) { // Save the registry PlayerPrefs.SetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, JsonUtility.ToJson(info)); PlayerPrefs.Save(); if (TimeRecorderWindow.Instance) { TimeRecorderWindow.Instance.RepaintWindow(); } }
private static TimeRecorderInfo InitializeTimeRecorder() { Debug.Log("Inicializando time recorder"); DateTime dateTime = DateTime.Now; timeRecorderInfo = new TimeRecorderInfo() { years = new List <YearInfo>(), totalRecordedTime = 0 }; // Setup current year var year = new YearInfo() { year = dateTime.Year, months = new List <MonthInfo>() }; // Setup current month var month = new MonthInfo() { month = dateTime.Month, dates = new List <DateInfo>() }; // Setup current day var day = new DateInfo() { date = dateTime.Day, timeInSeconds = 0, }; // Setup time recorder object month.dates.Add(day); year.months.Add(month); timeRecorderInfo.years.Add(year); // Save the registry string timeRecorderJson = JsonUtility.ToJson(timeRecorderInfo); PlayerPrefs.SetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, timeRecorderJson); PlayerPrefs.Save(); Debug.Log("Time recorder initialized"); return(timeRecorderInfo); }
private void DrawWindow() { VisualElement root = rootVisualElement; info = TimeRecorder.LoadTimeRecorderInfoFromRegistry(); var timeRecorderTemplate = Resources.Load <VisualTreeAsset>(TimeRecorderExtras.CALENDAR_TEMPLATE_PATH); var timeRecorderTemplateStyle = Resources.Load <StyleSheet>(TimeRecorderExtras.CALENDAR_TEMPLATE_STYLE_PATH); root.styleSheets.Add(timeRecorderTemplateStyle); var dayElementTemplateStyle = Resources.Load <StyleSheet>(TimeRecorderExtras.DAY_CONTAINER_TEMPLATE_STYLE_PATH); root.styleSheets.Add(dayElementTemplateStyle); // Add tree to root element timeRecorderTemplate.CloneTree(root); // Update date label var dateLabel = root.Q <Label>(CalendarContainerTemplateNames.LABEL_DATE); dateLabel.text = $"{selectedDate.Day}-{selectedDate.Month}-{selectedDate.Year}"; // Fix buttons action var prevMonthBtn = root.Q <Button>(CalendarContainerTemplateNames.BTN_PREV_MONTH); var nextMonthBtn = root.Q <Button>(CalendarContainerTemplateNames.BTN_NEXT_MONTH); var timeRecorderPauseStateBtn = root.Q <Button>(CalendarContainerTemplateNames.TIME_RECORDER_STATE_BTN); prevMonthBtn.clicked += () => ChangeMonthOffset(-1); nextMonthBtn.clicked += () => ChangeMonthOffset(1); timeRecorderPauseStateBtn.clicked += ChangeTimeRecorderPauseState; timeRecorderPauseStateBtn.text = TimeRecorderExtras.GetPauseButtonLabelForState(TimeRecorder.isPaused).ToUpperInvariant(); // Set total label dev time var totalDevLabel = root.Q <Label>(CalendarContainerTemplateNames.LABEL_TOTAL_DEV_TIME); totalDevLabel.text = GetLabel(info?.totalRecordedTime ?? 0); // Generate days var daysContainers = new VisualElement[7]; // Get reference to all containers before generate elements for (int i = 0; i < 7; i++) { var dayElement = root.Q <VisualElement>("day-" + i); daysContainers[i] = dayElement; } int daysGenerated = 0; #region Step 1: Fill previous month var firstDay = new DateTime(selectedDate.Year, selectedDate.Month, 1); if (firstDay.DayOfWeek != DayOfWeek.Monday) { // Get previous month var previousMonthDate = selectedDate.AddMonths(-1); previousMonthDate = new DateTime(previousMonthDate.Year, previousMonthDate.Month, 1); int end = DateTime.DaysInMonth(previousMonthDate.Year, previousMonthDate.Month); int reduceDays = firstDay.DayOfWeek == DayOfWeek.Sunday ? 6 : ((int)firstDay.DayOfWeek - 1) % 7; int start = end - reduceDays; // Generate Elements FillDateToDate(daysContainers, previousMonthDate, start, end, ref daysGenerated, true); } #endregion #region Step 2: Fill selected month int daysInSelectedMonth = DateTime.DaysInMonth(selectedDate.Year, selectedDate.Month); FillDateToDate(daysContainers, selectedDate, 0, daysInSelectedMonth, ref daysGenerated); #endregion #region Step 3: Fill remaining month days var selectedMonthFinalDate = new DateTime(selectedDate.Year, selectedDate.Month, daysInSelectedMonth); if (selectedMonthFinalDate.DayOfWeek != DayOfWeek.Sunday) { // In this calendar the final day is "Sunday" // so if selected month ends in "Sunday" there is no need to fill remaining days var nextMonth = selectedDate.AddMonths(1); int remainingDays = 7 - ((int)selectedMonthFinalDate.DayOfWeek); FillDateToDate(daysContainers, nextMonth, 0, remainingDays, ref daysGenerated, true); } #endregion }
private void OnPressEditBtn(VisualElement dayElement) { if (info == null) { // Can be null the first time info = new TimeRecorderInfo(); } var editContainer = dayElement.Q <VisualElement>(DayContainerTemplateNames.EDIT_DAY_CONTAINER); var editBtn = dayElement.Q <Button>(DayContainerTemplateNames.EDIT_BTN); var labelHours = dayElement.Q <Label>(DayContainerTemplateNames.LABEL_HOURS); var textField = dayElement.Q <TextField>(DayContainerTemplateNames.INPUT_EDIT_MINUTES); var date = TimeRecorderExtras.GetDateByDayNameFormat(dayElement.name); var dateTime = new DateTime(date.year, date.month, date.day); var dateTimeInfo = info.VerifyByDatetime(dateTime); // User is editing this day? if (currentDayEditing == dayElement) // If true this btn is working as save // ◘◘◘◘◘ On press Save ◘◘◘◘◘ { var totalDevLabel = rootVisualElement.Q <Label>(CalendarContainerTemplateNames.LABEL_TOTAL_DEV_TIME); editBtn.text = TimeRecorderExtras.EDIT; currentDayEditing = null; editContainer.AddToClassList("no-display-element"); labelHours.RemoveFromClassList("no-display-element"); ClampDayMinutesValue(textField.value, textField.value, out var time); // ⚠⚠⚠⚠⚠ Transform input minutes to seconds ⚠⚠⚠⚠⚠ time = time * SECONDS_PER_MINUTE; info.totalRecordedTime = info.totalRecordedTime - dateTimeInfo.dayInfo.timeInSeconds + time; // Update total time dateTimeInfo.dayInfo.timeInSeconds = time; // Update day time labelHours.text = GetLabel(time); totalDevLabel.text = GetLabel(info.totalRecordedTime); TimeRecorder.SaveTimeRecorded(info); TimeRecorder.ReCalculateNextSave(); Debug.Log($"<color=green>Time of the day {dayElement.name} updated</color>"); return; } if (currentDayEditing != null) { // Hide previous editing button currentDayEditing.Q <Label>(DayContainerTemplateNames.LABEL_HOURS).RemoveFromClassList("no-display-element"); ChangeDayEditState(currentDayEditing, false); } // Set the info of selected date textField.value = (dateTimeInfo.dayInfo.timeInSeconds / SECONDS_PER_MINUTE).ToString(); editContainer.RemoveFromClassList("no-display-element"); labelHours.AddToClassList("no-display-element"); currentDayEditing = dayElement; editBtn.text = TimeRecorderExtras.SAVE; }
private static bool SaveTimeRecorded(bool countdownCompleted) { string timeRecorderJson = PlayerPrefs.GetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, ""); // parse stored json data try{ // Is a new time recorder? if (string.IsNullOrEmpty(timeRecorderJson)) { timeRecorderInfo = InitializeTimeRecorder(); } else { timeRecorderInfo = JsonUtility.FromJson <TimeRecorderInfo>(timeRecorderJson); } } catch (Exception e) { Debug.LogError("Any error ocurred trying to parse TimeRecorder JSON, a json file backup will be generated & data will be refreshed: " + e); PlayerPrefs.DeleteKey(TimeRecorderExtras.TIME_RECORDER_REGISTRY); PlayerPrefs.Save(); // Save local backgup string fileName = string.Format(TimeRecorderExtras.CORRUPTED_JSON_BACKUP, DateTime.Now.Ticks); string path = Path.Combine(Application.dataPath, fileName); Task saveBackup = WriteTextAsync(path, timeRecorderJson); // Save Data into the next iteration return(false); } #region Validate timeRecorderInfo object var currentTime = DateTime.Now; var dateTimeInfo = timeRecorderInfo.VerifyByDatetime(currentTime); #endregion int secondsToAdd = saveOnMinutes * 60; if (countdownCompleted) { // Add time reference for current date dateTimeInfo.dayInfo.timeInSeconds += secondsToAdd; } else { if (EditorApplication.timeSinceStartup < saveOnMinutes * 60) { // Editor recently oppened so i should save EditorApplication.timeSinceStartup secondsToAdd = (int)EditorApplication.timeSinceStartup; } else { // Save time elapsed from the last save time DateTime lastSave = nextSaveTime.AddMinutes(-saveOnMinutes); var diferencia = DateTime.Now - lastSave; secondsToAdd = diferencia.Seconds; } Debug.Log("No se completó pero aún así guardo"); } timeRecorderInfo.totalRecordedTime += secondsToAdd; // Save the registry SaveTimeRecorded(timeRecorderInfo); Debug.Log("Your develop time has been tracked"); return(true); }
private static bool SaveTimeRecorded(bool countdownCompleted) { string timeRecorderJson = PlayerPrefs.GetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, ""); // parse stored json data try{ // Is a new time recorder? if (string.IsNullOrEmpty(timeRecorderJson)) { timeRecorderInfo = InitializeTimeRecorder(); } else { timeRecorderInfo = JsonUtility.FromJson <TimeRecorderInfo>(timeRecorderJson); } } catch (Exception e) { Debug.LogError("Any error ocurred trying to parse TimeRecorder JSON, a json file backup will be generated & data will be refreshed: " + e); PlayerPrefs.DeleteKey(TimeRecorderExtras.TIME_RECORDER_REGISTRY); PlayerPrefs.Save(); // Save local backgup string fileName = string.Format(TimeRecorderExtras.CORRUPTED_JSON_BACKUP, DateTime.Now.Ticks); string path = Path.Combine(Application.dataPath, fileName); Task saveBackup = WriteTextAsync(path, timeRecorderJson); // Save Data into the next iteration return(false); } #region Validate timeRecorderInfo object var currentTime = DateTime.Now; // Save time if (timeRecorderInfo.years == null) { timeRecorderInfo.years = new List <YearInfo>(); } var yearInfoIdx = timeRecorderInfo.years.FindIndex(y => y.year == currentTime.Year); var yearInfo = new YearInfo(); if (yearInfoIdx == -1) { // Create & setup year yearInfo.year = currentTime.Year; yearInfo.months = new List <MonthInfo>(); timeRecorderInfo.years.Add(yearInfo); } else { // Find year yearInfo = timeRecorderInfo.years[yearInfoIdx]; } // Initialize year months if (yearInfo.months == null) { yearInfo.months = new List <MonthInfo>(); } var monthInfoIdx = yearInfo.months.FindIndex(m => m.month == currentTime.Month); var monthInfo = new MonthInfo(); if (monthInfoIdx == -1) { // Create & setup month monthInfo.month = currentTime.Month; monthInfo.dates = new List <DateInfo>(); yearInfo.months.Add(monthInfo); } else { // Find moth monthInfo = yearInfo.months[monthInfoIdx]; } // Initialize month dates if (monthInfo.dates == null) { monthInfo.dates = new List <DateInfo>(); } var dateInfoIdx = monthInfo.dates.FindIndex(m => m.date == currentTime.Day); var dateInfo = new DateInfo(); if (dateInfoIdx == -1) { // Create & setup day dateInfo.date = currentTime.Day; dateInfo.timeInSeconds = 0; dateInfoIdx = monthInfo.dates.Count; monthInfo.dates.Add(dateInfo); } else { // Find day dateInfo = monthInfo.dates[dateInfoIdx]; } #endregion int secondsToAdd = saveOnMinutes * 60; if (countdownCompleted) { // Add time reference for current date dateInfo.timeInSeconds += secondsToAdd; monthInfo.dates[dateInfoIdx] = dateInfo; } else { if (EditorApplication.timeSinceStartup < saveOnMinutes * 60) { // Editor recently oppened so i should save EditorApplication.timeSinceStartup secondsToAdd = (int)EditorApplication.timeSinceStartup; } else { // Save time elapsed from the last save time DateTime lastSave = nextSaveTime.AddMinutes(-saveOnMinutes); var diferencia = DateTime.Now - lastSave; secondsToAdd = diferencia.Seconds; } Debug.Log("No se completó pero aún así guardo"); } timeRecorderInfo.totalRecordedTime += secondsToAdd; // Save the registry timeRecorderJson = JsonUtility.ToJson(timeRecorderInfo); PlayerPrefs.SetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, timeRecorderJson); PlayerPrefs.Save(); if (TimeRecorderWindow.Instance) { TimeRecorderWindow.Instance.RepaintWindow(); } Debug.Log("Your develop time has been tracked"); return(true); }