Exemple #1
0
        public static AppState Reduce(AppState previousState, object action)
        {
            switch (action)
            {
            case SelectHabitAction selectHabitAction:
            {
                if (previousState.SelectedHabit != null)
                {
                    var habitId = previousState.SelectedHabit.Id;

                    var previousSelectedHabit = previousState.Habits.Find(habit => habit.Id == habitId);

                    if (previousSelectedHabit != null)
                    {
                        previousSelectedHabit.Selected = false;
                    }
                }

                previousState.SelectedHabit          = selectHabitAction.Habit;
                previousState.SelectedHabit.Selected = true;

                previousState.Tasks.Clear();

                previousState.Tasks.AddRange(FormatTasks(previousState.SelectedHabit));


                return(previousState);
            }

            case UpdateHabitAction updateHabitAction:
                updateHabitAction.Habit.Title = updateHabitAction.NewTitle;
                return(previousState);

            case DeleteHabitAction deleteHabitAction:
                previousState.Habits.Remove(deleteHabitAction.Habit);
                return(previousState);

            case AddHabitAction addHabitAction:
                previousState.Habits.Add(addHabitAction.Habit);

                if (previousState.Habits.Count == 1)
                {
                    Reduce(previousState, new SelectHabitAction(addHabitAction.Habit));
                }

                return(previousState);

            case ChangeTaskStatusAction changeTaskStatusAction:

                var status = changeTaskStatusAction.TaskData.Status;
                var task   = changeTaskStatusAction.TaskData;

                if (status == TaskStatus.Completed)
                {
                    task.Status = TaskStatus.Failed;
                }
                else if (status == TaskStatus.Failed)
                {
                    task.Status = TaskStatus.Skipped;
                }
                else if (status == TaskStatus.Skipped || status == TaskStatus.UnSetted)
                {
                    task.Status = TaskStatus.Completed;
                }

                var selectedHabit = previousState.SelectedHabit;

                var searchedTask = selectedHabit.Tasks.Find(searchTask => searchTask.Id == task.Id);

                if (searchedTask != null)
                {
                    searchedTask.Status = task.Status;
                }

                searchedTask = previousState.Tasks.Find(searchTask => searchTask.Id == task.Id);

                if (searchedTask != null)
                {
                    searchedTask.Status = task.Status;
                }

                var searchedHabit = previousState.Habits.Find(searchHabit => searchHabit.Id == selectedHabit.Id);

                if (searchedHabit != null)
                {
                    searchedTask = searchedHabit.Tasks.Find(searchTask => searchTask.Id == task.Id);

                    if (searchedTask != null)
                    {
                        searchedTask.Status = task.Status;
                    }
                }

                return(previousState);
            }

            return(previousState);
        }