Exemplo n.º 1
0
        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
        }
Exemplo n.º 2
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;
        }