- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendar.cpp
More file actions
Latest commit
56 lines (46 loc) · 1.63 KB
/
Copy pathCalendar.cpp
File metadata and controls
56 lines (46 loc) · 1.63 KB
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
//
// Created by Ed on 01/01/2024.
//
#include"Calendar.h"
#include<sstream>
#include<nlohmann/json.hpp>
Calendar::Calendar() = default;
using days = std::chrono::duration<int64_t, std::ratio<86400>>;
voidCalendar::fetch(RestClient::Connection *connection) {
autoconst time = floor<days>(std::chrono::system_clock::now());
std::time_t today = std::chrono::system_clock::to_time_t(time);
std::string cal_start(30, '\0');
cal_start.resize(std::strftime(&cal_start[0], cal_start.size(), "%Y-%m-%dT00:00:00.000Z", std::localtime(&today)));
today += (86400 * 60);
std::string cal_end(30, '\0');
cal_end.resize(std::strftime(&cal_end[0], cal_end.size(), "%Y-%m-%dT00:00:00.000Z", std::localtime(&today)));
std::stringstream ss;
ss << "/api/calendars/calendar.shared_calendar?start=" << cal_start << "&end=" << cal_end;
RestClient::Response r = connection->get(ss.str());
if (r.code != 200) {
return;
}
nlohmann::json json_result = nlohmann::json::parse(r.body);
clear_children();
int count = 0;
std::vector<std::shared_ptr<CalendarEntry>> entries;
for (constauto& item : json_result) {
entries.push_back(std::make_shared<CalendarEntry>(item, time));
if (++count == 10) {
break;
}
}
int x_offset = 50;
int y_offset = 0;
constunsignedint split = (entries.size() + 1) / 2;
unsignedint i = 0;
for (constauto& entry : entries) {
add_child(entry, x_offset, y_offset);
y_offset += entry->h();
i += 1;
if (i == split) {
x_offset = 590;
y_offset = 0;
}
}
}