1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295

ash / system / time / calendar_view_controller.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_SYSTEM_TIME_CALENDAR_VIEW_CONTROLLER_H_
#define ASH_SYSTEM_TIME_CALENDAR_VIEW_CONTROLLER_H_

#include <deque>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>

#include "ash/ash_export.h"
#include "ash/system/time/calendar_model.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "google_apis/calendar/calendar_api_response_types.h"
#include "google_apis/common/api_error_codes.h"
#include "ui/events/event.h"

namespace ash {

class CalendarDateCellView;

// Controller of the `CalendarView`.
class ASH_EXPORT CalendarViewController {
 public:
  CalendarViewController();
  CalendarViewController(const CalendarViewController& other) = delete;
  CalendarViewController& operator=(const CalendarViewController& other) =
      delete;
  virtual ~CalendarViewController();

  class Observer : public base::CheckedObserver {
   public:
    // Gets called when `currently_shown_date_ ` changes.
    virtual void OnMonthChanged() {}

    // Invoked when a date cell is clicked to open the event list.
    virtual void OpenEventList() {}

    // Invoked when the close button is clicked to close the event list.
    virtual void CloseEventList() {}

    // Invoked when the selected date is updated in the
    // `CalendarViewController`.
    virtual void OnSelectedDateUpdated() {}

    // Invoked when the calendar UI has completed rendering (including business
    // logic like scrolling to the current month).
    virtual void OnCalendarLoaded() {}
  };

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Updates the `currently_shown_date_`.
  void UpdateMonth(const base::Time current_month_first_date);

  // A callback passed into the`CalendarDateCellView`, which is called when the
  // cell is clicked to show the event list view.
  void ShowEventListView(CalendarDateCellView* selected_calendar_date_cell_view,
                         base::Time selected_date,
                         int row_index);

  // A callback passed into the`CalendarEventListView`, which is called when the
  // close button is clicked to close the event list view.
  void CloseEventListView();

  // Gets called when the `CalendarEventListView` is opened.
  void OnEventListOpened();

  // Gets called when the `CalendarEventListView` is closed.
  void OnEventListClosed();

  // Gets called when the `CalendarView` has completed loading its UI.
  void CalendarLoaded();

  // Records the event list item being pressed on and the type of event.
  // Captures whether it was from the `CalendarEventListView` or implicitly the
  // `CalendarUpNextView` (the only other place the `CalendarEventListItemView`
  // is used currently).
  void RecordEventListItemActivated(const ui::Event& event);

  // Records a metric for the "Join" meeting button being pressed.
  // Captures whether it was from the `CalendarEventListView` or implicitly the
  // `CalendarUpNextView` (the only other place the `CalendarEventListItemView`
  // is used currently).
  void RecordJoinMeetingButtonPressed(const ui::Event& event);

  // Called when a calendar event is about to launch. Used to record metrics.
  void OnCalendarEventWillLaunch();

  // Called when the CalendarDateCellView representing today gets a fetch.
  void OnTodaysEventFetchComplete();

  // Called when the on screen month has finished loading and has any events to
  // display to the user. Logs a metric once per CalendarView lifetime.
  void EventsDisplayedToUser();

  // If the selected date in the current month. This is used to inform the
  // `CalendarView` if the month should be updated when a date is selected.
  bool IsSelectedDateInCurrentMonth();

  // Gets the first day of the `currently_shown_date_`'s month, in UTC time.
  // Time difference is applied.
  base::Time GetOnScreenMonthFirstDayUTC();

  // Gets the first day of the nth-previous month based on the
  // `currently_shown_date_`'s month, in UTC time. Time difference is applied.
  base::Time GetPreviousMonthFirstDayUTC(unsigned int num_months);

  // Gets the first day of the nth-next month based on the
  // `currently_shown_date_`'s month, in UTC time. Time difference is applied.
  base::Time GetNextMonthFirstDayUTC(unsigned int num_months);

  // Gets the month name of the `currently_shown_date_`'s month.
  std::u16string GetOnScreenMonthName() const;

  // Gets the month name of the next `num_months` month based on the
  // `currently_shown_date_`'s month.
  std::u16string GetNextMonthName(int num_months = 1);

  // Gets the month name of the previous month based `currently_shown_date_`'s
  // month.
  std::u16string GetPreviousMonthName();

  // Getters of the today's row position, top and bottom.
  int GetTodayRowTopHeight() const;
  int GetTodayRowBottomHeight() const;

  // The calendar events of the selected date.
  SingleDayEventList SelectedDateEvents();

  // Selected date events split into two lists, one of multi day events only,
  // the other excluding multi day events i.e. same day events. These are
  // displayed to the user in two separate list views.
  std::tuple<SingleDayEventList, SingleDayEventList>
  SelectedDateEventsSplitByMultiDayAndSameDay();

  // Returns upcoming events for the "Up next" view.
  SingleDayEventList UpcomingEvents();

  // The calendar events number of the `date`.
  int GetEventNumber(base::Time date);

  // Getters and setters: the row index when the event list view is showing,
  // today's row number, today's row height and expanded area height.
  int GetExpandedRowIndex() const;

  // Get the current date, which can be today or the first day of the current
  // month if current month is not today's month.
  base::Time currently_shown_date() { return currently_shown_date_; }

  // The currently selected date to show the event list.
  std::optional<base::Time> selected_date() { return selected_date_; }

  // The midnight of the currently selected date adjusted to the local timezone.
  base::Time selected_date_midnight() { return selected_date_midnight_; }

  // The midnight of the selected date in UTC time.
  base::Time selected_date_midnight_utc() {
    return selected_date_midnight_utc_;
  }

  // The row index of the currently selected date. This is used for auto
  // scrolling to this row when the event list is expanded.
  int selected_date_row_index() { return selected_date_row_index_; }

  void set_expanded_row_index(int row_index) {
    expanded_row_index_ = row_index;
  }

  int today_row() const { return today_row_; }
  void set_today_row(int row) { today_row_ = row; }

  int row_height() const { return row_height_; }
  void set_row_height(int height) { row_height_ = height; }

  CalendarDateCellView* selected_date_cell_view() {
    return selected_date_cell_view_;
  }
  void set_selected_date_cell_view(
      CalendarDateCellView* todays_date_cell_view) {
    selected_date_cell_view_ = todays_date_cell_view;
  }

  CalendarDateCellView* todays_date_cell_view() {
    return todays_date_cell_view_;
  }
  void set_todays_date_cell_view(CalendarDateCellView* todays_date_cell_view) {
    todays_date_cell_view_ = todays_date_cell_view;
  }

  bool is_date_cell_clickable() const { return is_date_cell_clickable_; }
  void set_is_date_cell_clickable(bool is_clickable) {
    is_date_cell_clickable_ = is_clickable;
  }

  bool is_event_list_showing() const { return is_event_list_showing_; }

  // Returns whether the events for `start_of_month` have been successfully
  // fetched. The `FetchingStatus` should be either `kSuccess` or `kRefetching`.
  bool IsSuccessfullyFetched(base::Time start_of_month);

 private:
  // For unit tests.
  friend class CalendarMonthViewTest;
  friend class CalendarViewAnimationTest;
  friend class CalendarViewEventListViewFetchTest;
  friend class CalendarViewEventListViewTest;
  friend class CalendarViewTest;
  friend class CalendarViewEventListItemViewTest;

  // Adds the time difference and returns the adjusted time.
  base::Time ApplyTimeDifference(base::Time date);

  // The currently shown date, which can be today or the first day of the
  // current month if current month is not today's month.
  base::Time currently_shown_date_;

  // The time the CalendarViewController was created, which coincides with the
  // time the view was created.
  base::TimeTicks calendar_open_time_;

  // The time the user spends in a month before navigating to another one.
  base::TimeTicks month_dwell_time_;

  // The today's date cell row number (which is index +1) in its
  // `CalendarMonthView`.
  int today_row_ = 0;

  // Each row's height. Every row should have the same height, so this height is
  // only updated once with today's row.
  int row_height_ = 0;

  // If the event list is expanded.
  bool is_event_list_showing_ = false;

  // Whether the user journey time has been recorded. It is recorded when an
  // event is launched, or when this (which is owned by the view) is destroyed.
  bool user_journey_time_recorded_ = false;

  // Whether the metric for recording time to show the user the first fetch of
  // events has been recorded.
  bool todays_date_cell_fetch_recorded_ = false;

  // Record if any events are displayed (via the dots in the current month) on
  // screen to the user.
  bool events_shown_to_user_recorded_ = false;

  // Whether date cells are clickable. When the event list animation is running,
  // date cells should become unclickable until the animation completes.
  bool is_date_cell_clickable_ = true;

  // The currently selected date.
  std::optional<base::Time> selected_date_;

  // The currently selected CalendarDateCellView
  raw_ptr<CalendarDateCellView> selected_date_cell_view_ = nullptr;

  // The CalendarDateCellView which represents today.
  raw_ptr<CalendarDateCellView> todays_date_cell_view_ = nullptr;

  // The midnight of the currently selected date adjusted to the local timezone.
  base::Time selected_date_midnight_;

  // The midnight of the selected date in UTC time.
  base::Time selected_date_midnight_utc_;

  // The row index of the currently selected date.
  int selected_date_row_index_ = 0;

  // The current row index when the event list view is shown.
  int expanded_row_index_ = 0;

  // Maximum distance, in months, from the on-screen month first displayed in
  // the calendar when it was opened. This is logged as a metric when the
  // calendar is closed.
  size_t max_distance_browsed_ = 0;

  // The first date shown, used to record max distance browsed metrics.
  const base::Time first_shown_date_;

  base::ObserverList<Observer> observers_;

  base::WeakPtrFactory<CalendarViewController> weak_factory_{this};
};

}  // namespace ash

#endif  // ASH_SYSTEM_TIME_CALENDAR_VIEW_CONTROLLER_H_