Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Apps/Calendar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ Read-only CalDAV calendar viewer for Tactility.

- Syncs events from a CalDAV calendar collection using an authenticated `REPORT` request
- Downloads calendar events and caches them locally for subsequent app opens
- Loads large calendars while they download; events that don't repeat and ended over a year ago are left out, and the number of events is limited by the memory of the device
- Shows next 30 days from cached event times and recurring events
- Lets you configure a calendar collection URL, username and password
- Stores account settings using Tactility preferences
- Provides a navigable month page, a full-screen agenda for the selected day, and a separate agenda page for the next 30 days
- Lists the next 30 days of events, grouped by day, from today or from any selected date
- Small screens show the list, with a week strip on touch devices (swipe to change week) and a full-screen month view
- Larger screens show the month next to the list

## Setup

Expand Down
853 changes: 393 additions & 460 deletions Apps/Calendar/main/Source/CalendarApp.cpp

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions Apps/Calendar/main/Source/CalendarApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <app/instance.h>

#include <AppLocalisation.h>
#include "CalendarDate.h"
#include "CalendarSync.h"
#include "MonthGrid.h"
#include <lvgl.h>
#include <lvgl_window_manager/window_manager.h>
#include <tactility/concurrent/task_event_group.h>
Expand Down Expand Up @@ -44,20 +46,27 @@ struct CalendarContext {
bool statusShouldExpire = false;

bool settingsVisible = false;
bool dayAgendaVisible = false;
bool splitLayout = false;
// Ignored by the split layout, which shows both.
CalendarPage page = CalendarPage::Agenda;
int selectedYear = 0;
int selectedMonth = 0;
int selectedDay = 0;
// While true, the list runs from now and the selection is today, also after midnight.
// After picking a day it is false, and selectedDate is used.
bool followToday = true;
CalendarDate selectedDate {};
CalendarDate shownToday {};
int displayYear = 0;
int displayMonth = 0;
lv_obj_t* toolbar = nullptr;
lv_obj_t* todayButton = nullptr;
lv_obj_t* previousButton = nullptr;
lv_obj_t* nextButton = nullptr;
lv_obj_t* viewButton = nullptr;
lv_obj_t* contentPanel = nullptr;
lv_obj_t* agendaPanel = nullptr;
lv_obj_t* monthPanel = nullptr;
lv_obj_t* monthTitle = nullptr;
lv_obj_t* monthGrid = nullptr;
lv_obj_t* selectedDayPanel = nullptr;
lv_obj_t* selectedDayTitle = nullptr;
lv_obj_t* selectedDayEvents = nullptr;
lv_obj_t* weekStrip = nullptr;
MonthGrid monthGrid {};
lv_point_t stripPressPoint {};
CalendarDate stripPressDate {};
lv_obj_t* settingsPanel = nullptr;
lv_obj_t* statusLabel = nullptr;
lv_timer_t* statusTimer = nullptr;
Expand Down
35 changes: 35 additions & 0 deletions Apps/Calendar/main/Source/CalendarData.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ struct CalendarEvent {

bool calendarFormatDateKey(char* output, std::size_t size, int year, int month, int day);
bool calendarEventOverlapsDay(const CalendarEvent& event, const char* date);
// Parses a CalDAV multistatus reply while it arrives. Only the entry that is being received is
// buffered, so the size of the reply doesn't matter: the number of events does.
class CalendarResponseParser {
public:
enum class Error { None, Invalid, EntryTooLarge, OutOfMemory };
// Returns false when there is no memory to grow the event list to this many bytes.
using MemoryCheck = bool (*)(std::size_t bytes);

// keepFrom ("YYYY-MM-DD") drops events that don't repeat and ended before that day.
explicit CalendarResponseParser(std::vector<CalendarEvent>& events, const char* keepFrom = nullptr,
MemoryCheck hasMemory = nullptr);
// Returns false after an error: later data is ignored.
bool feed(const char* data, std::size_t length);
// Returns whether the reply was complete and all of it was loaded.
bool finish();
Error error() const { return failure; }

private:
enum class State { BeforeRoot, InRoot, Done };

void process();
bool append(const std::vector<CalendarEvent>& parsed);

std::vector<CalendarEvent>& events;
char keepFrom[11] {};
MemoryCheck hasMemory;
std::string buffer;
std::string rootName;
std::size_t searchFrom = 0;
std::size_t loaded = 0;
std::size_t collections = 0;
State state = State::BeforeRoot;
Error failure = Error::None;
};

bool calendarParseResponse(const std::string& xml, std::vector<CalendarEvent>& events);
bool calendarExpandEventsForMonth(const std::vector<CalendarEvent>& events,
int year, int month,
Expand Down
98 changes: 98 additions & 0 deletions Apps/Calendar/main/Source/CalendarDate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "CalendarDate.h"

#include "CalendarData.h"

#include <AppLocalisation.h>

#include <algorithm>
#include <ctime>

namespace {

constexpr const char* WEEKDAYS[] = {
tr_noop("Mon"), tr_noop("Tue"), tr_noop("Wed"), tr_noop("Thu"),
tr_noop("Fri"), tr_noop("Sat"), tr_noop("Sun")
};

constexpr const char* MONTHS[] = {
tr_noop("Jan"), tr_noop("Feb"), tr_noop("Mar"), tr_noop("Apr"), tr_noop("May"), tr_noop("Jun"),
tr_noop("Jul"), tr_noop("Aug"), tr_noop("Sep"), tr_noop("Oct"), tr_noop("Nov"), tr_noop("Dec")
};

// Noon avoids day changes caused by daylight saving time.
tm toTm(int year, int month, int day) {
tm value {};
value.tm_year = year - 1900;
value.tm_mon = month - 1;
value.tm_mday = day;
value.tm_hour = 12;
value.tm_isdst = -1;
mktime(&value);
return value;
}

CalendarDate fromTm(const tm& value) {
return {value.tm_year + 1900, value.tm_mon + 1, value.tm_mday};
}

// Translations choose the order, so the placeholders are named instead of printf arguments.
void replace(std::string& text, const char* placeholder, const std::string& value) {
const size_t position = text.find(placeholder);
if (position != std::string::npos) {
text.replace(position, std::char_traits<char>::length(placeholder), value);
}
}

} // namespace

CalendarDate calendarToday() {
const time_t now = time(nullptr);
tm local {};
localtime_r(&now, &local);
return fromTm(local);
}

CalendarDate calendarAddDays(const CalendarDate& date, int days) {
return fromTm(toTm(date.year, date.month, date.day + days));
}

CalendarDate calendarAddMonths(const CalendarDate& date, int months) {
const CalendarDate first = fromTm(toTm(date.year, date.month + months, 1));
return {first.year, first.month, std::min(date.day, calendarDaysInMonth(first.year, first.month))};
}

int calendarWeekday(const CalendarDate& date) {
return (toTm(date.year, date.month, date.day).tm_wday + 6) % 7;
}

int calendarDaysInMonth(int year, int month) {
return toTm(year, month + 1, 0).tm_mday;
}

void calendarDateKey(const CalendarDate& date, char (&key)[11]) {
if (!calendarFormatDateKey(key, sizeof(key), date.year, date.month, date.day)) key[0] = '\0';
}

const char* calendarWeekdayName(int weekday) {
return tr(WEEKDAYS[weekday]);
}

std::string calendarDayLabel(const CalendarDate& date, const CalendarDate& today) {
std::string text = tr("{weekday} {day} {month}");
replace(text, "{weekday}", calendarWeekdayName(calendarWeekday(date)));
replace(text, "{day}", std::to_string(date.day));
replace(text, "{month}", tr(MONTHS[date.month - 1]));
const char* prefix = date == today ? tr("Today, {date}")
: date == calendarAddDays(today, 1) ? tr("Tomorrow, {date}") : nullptr;
if (prefix == nullptr) return text;
std::string prefixed = prefix;
replace(prefixed, "{date}", text);
return prefixed;
}

std::string calendarMonthLabel(int year, int month) {
std::string text = tr("{month} {year}");
replace(text, "{month}", tr(MONTHS[month - 1]));
replace(text, "{year}", std::to_string(year));
return text;
}
31 changes: 31 additions & 0 deletions Apps/Calendar/main/Source/CalendarDate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <cstddef>
#include <string>

struct CalendarDate {
int year = 0;
int month = 0;
int day = 0;

bool operator==(const CalendarDate& other) const {
return year == other.year && month == other.month && day == other.day;
}
bool operator!=(const CalendarDate& other) const { return !(*this == other); }
};

CalendarDate calendarToday();
CalendarDate calendarAddDays(const CalendarDate& date, int days);
// Keeps the day of the month where the target month has it.
CalendarDate calendarAddMonths(const CalendarDate& date, int months);
// 0 is Monday.
int calendarWeekday(const CalendarDate& date);
int calendarDaysInMonth(int year, int month);
// "YYYY-MM-DD"
void calendarDateKey(const CalendarDate& date, char (&key)[11]);

const char* calendarWeekdayName(int weekday);
// "Mon 30 Nov", prefixed with "Today" or "Tomorrow" where that applies.
std::string calendarDayLabel(const CalendarDate& date, const CalendarDate& today);
// "Nov 2026"
std::string calendarMonthLabel(int year, int month);
Loading
Loading