Example #1
0
        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;
        }
Example #2
0
        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);
        }