| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 8 | ) |
| 9 | |
| 10 | // Day navigation (level-up spec §4.4): the week strip on a daily note's |
| 11 | // Links panel and the month calendar. Weeks start on the reader's |
| 12 | // locale's first day, and the names are its own (build note 85). A dot is a |
| 13 | // day the viewer wrote a note, read from their own partition only, so |
| 14 | // nobody else's writing ever shows as a dot. |
| 15 | |
| 16 | // CalendarDay is one day in the week strip or the month. |
| 17 | type CalendarDay struct { |
| 18 | Date, Letter, Number string |
| 19 | // Wrote: the viewer has a note for the day. Today and Current are |
| 20 | // today and the day on screen. Outside is a day of the month before |
| 21 | // or after. |
| 22 | Wrote, Today, Current, Outside bool |
| 23 | } |
| 24 | |
| 25 | // WeekView is the strip: seven days and the weeks either side. |
| 26 | type WeekView struct { |
| 27 | Days []CalendarDay |
| 28 | Prev, Next string |
| 29 | // Month names the month the day on screen is in ("November"). |
| 30 | Month string |
| 31 | } |
| 32 | |
| 33 | // MonthView is the calendar: six rows of seven days, and the months |
| 34 | // either side as ?month= values. |
| 35 | type MonthView struct { |
| 36 | Title string |
| 37 | // Heads are the columns, from the locale's first day. |
| 38 | Heads []locale.Head |
| 39 | Weeks [][]CalendarDay |
| 40 | Prev, Next string |
| 41 | Date, Today string |
| 42 | } |
| 43 | |
| 44 | // Week is the strip for the week holding date. |
| 45 | func (index *Index) Week(ctx context.Context, scope Scope, date, |
| 46 | today string) (WeekView, error) { |
| 47 | day, err := time.Parse(time.DateOnly, date) |
| 48 | if err != nil { |
| 49 | return WeekView{}, err |
| 50 | } |
| 51 | if err := index.Sync(ctx); err != nil { |
| 52 | return WeekView{}, err |
| 53 | } |
| 54 | index.mutex.RLock() |
| 55 | defer index.mutex.RUnlock() |
| 56 | view := index.in(scope) |
| 57 | first := scope.Locale.WeekStart(day) |
| 58 | week := WeekView{Prev: iso(day.AddDate(0, 0, -7)), |
| 59 | Next: iso(day.AddDate(0, 0, 7)), |
| 60 | Month: scope.Locale.Format("month", day)} |
| 61 | for offset := range 7 { |
| 62 | week.Days = append(week.Days, view.calendarDay( |
| 63 | first.AddDate(0, 0, offset), date, today, day.Month())) |
| 64 | } |
| 65 | return week, nil |
| 66 | } |
| 67 | |
| 68 | // Month is the calendar for month ("2006-01"), or for date's month when |
| 69 | // month is empty or not a month. |
| 70 | func (index *Index) Month(ctx context.Context, scope Scope, month, date, |
| 71 | today string) (MonthView, error) { |
| 72 | first, err := monthStart(month, date) |
| 73 | if err != nil { |
| 74 | return MonthView{}, err |
| 75 | } |
| 76 | if err := index.Sync(ctx); err != nil { |
| 77 | return MonthView{}, err |
| 78 | } |
| 79 | index.mutex.RLock() |
| 80 | defer index.mutex.RUnlock() |
| 81 | view := index.in(scope) |
| 82 | cal := MonthView{Title: scope.Locale.Format("monthYear", first), |
| 83 | Heads: scope.Locale.Heads(), Date: date, Today: today, |
| 84 | Prev: first.AddDate(0, -1, 0).Format("2006-01"), |
| 85 | Next: first.AddDate(0, 1, 0).Format("2006-01")} |
| 86 | start := scope.Locale.WeekStart(first) |
| 87 | for row := range 6 { |
| 88 | var week []CalendarDay |
| 89 | for column := range 7 { |
| 90 | week = append(week, view.calendarDay( |
| 91 | start.AddDate(0, 0, row*7+column), date, today, |
| 92 | first.Month())) |
| 93 | } |
| 94 | cal.Weeks = append(cal.Weeks, week) |
| 95 | } |
| 96 | return cal, nil |
| 97 | } |
| 98 | |
| 99 | func (view scoped) calendarDay(day time.Time, date, today string, |
| 100 | month time.Month) CalendarDay { |
| 101 | at := iso(day) |
| 102 | return CalendarDay{Date: at, |
| 103 | Letter: view.scope.Locale.Letter(day.Weekday()), |
| 104 | Number: day.Format("2"), Wrote: view.wroteOn(at), |
| 105 | Today: at == today, Current: at == date, |
| 106 | Outside: day.Month() != month} |
| 107 | } |
| 108 | |
| 109 | func monthStart(month, date string) (time.Time, error) { |
| 110 | if first, err := time.Parse("2006-01", month); err == nil { |
| 111 | return first, nil |
| 112 | } |
| 113 | day, err := time.Parse(time.DateOnly, date) |
| 114 | if err != nil { |
| 115 | return time.Time{}, err |
| 116 | } |
| 117 | return time.Date(day.Year(), day.Month(), 1, 0, 0, 0, 0, time.UTC), |
| 118 | nil |
| 119 | } |
| 120 | |