Compare commits

...

2 Commits

Author SHA1 Message Date
24aa979a69 add week selection
Some checks are pending
/ sync-to-origin (push) Waiting to run
2025-03-31 18:20:58 +02:00
64b4797908 make timetable request specific date 2025-03-31 11:27:48 +02:00
4 changed files with 78 additions and 12 deletions

View File

@ -32,11 +32,11 @@ void win_show(WINDOW *win, wchar_t *label, int label_color, int width,
int height, json marks_json, int SubjectIndex); int height, json marks_json, int SubjectIndex);
void marks_page() { void marks_page() {
// DONT FORGET TO UNCOMMENT json resp_from_api;
json resp_from_api = bakaapi::get_data_from_endpoint("api/3/marks"); {
// std::ifstream f("test-data/marks3.json"); std::string endpoint = "api/3/marks";
// json resp_from_api = json::parse(f); resp_from_api = bakaapi::get_data_from_endpoint(endpoint);
}
WINDOW **my_wins; WINDOW **my_wins;
size_t size_my_wins = resp_from_api["Subjects"].size(); size_t size_my_wins = resp_from_api["Subjects"].size();
my_wins = new (std::nothrow) WINDOW *[size_my_wins]; my_wins = new (std::nothrow) WINDOW *[size_my_wins];

View File

@ -162,11 +162,14 @@ void is_access_token_empty() {
} }
// supports all endpoints that only require access_token // supports all endpoints that only require access_token
json get_data_from_endpoint(std::string endpoint) { json get_data_from_endpoint(std::string &endpoint, std::string additional_data) {
is_access_token_empty(); is_access_token_empty();
access_token_refreshed: access_token_refreshed:
std::string req_data = std::string req_data =
std::format("Authorization=Bearer&access_token={}", access_token); std::format("Authorization=Bearer&access_token={}", access_token);
if(!additional_data.empty()) {
req_data.append(std::format("&{}", additional_data));
}
auto [response, http_code] = send_curl_request(endpoint, GET, req_data); auto [response, http_code] = send_curl_request(endpoint, GET, req_data);

View File

@ -11,6 +11,6 @@ extern CURL *curl;
namespace bakaapi { namespace bakaapi {
void login(std::string username, std::string password); void login(std::string username, std::string password);
void refresh_access_token(); void refresh_access_token();
json get_data_from_endpoint(std::string endpoint); json get_data_from_endpoint(std::string &endpoint, std::string data = "");
} // namespace bakaapi } // namespace bakaapi
#endif #endif

View File

@ -4,17 +4,24 @@
#include "helper_funcs.h" #include "helper_funcs.h"
#include "net.h" #include "net.h"
#include "types.h" #include "types.h"
#include <bits/chrono.h>
#include <cstdint> #include <cstdint>
#include <cstdio>
#include <ctime>
#include <curses.h> #include <curses.h>
#include <cwchar> #include <cwchar>
#include <iomanip>
#include <iostream> #include <iostream>
#include <ncurses.h> #include <ncurses.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <panel.h> #include <panel.h>
#include <sstream>
#include <string> #include <string>
#include <sys/types.h> #include <sys/types.h>
#include <vector> #include <vector>
using nlohmann::json; using nlohmann::json;
#define BOTTOM_PADDING 5 #define BOTTOM_PADDING 5
@ -80,11 +87,20 @@ json *find_atom_by_indexes(json &resp_from_api, uint8_t day_index,
} }
void timetable_page() { void timetable_page() {
// DONT FORGET TO UNCOMMENT auto dateSelected = std::chrono::system_clock::now();
reload_for_new_week:
std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected);
std::tm local_time = *std::localtime(&date_time_t);
std::stringstream date_stringstream;
date_stringstream << std::put_time(&local_time, "%Y-%m-%d");
std::string date_string = "date=" + date_stringstream.str();
std::string endpoint = "api/3/timetable/actual";
json resp_from_api = json resp_from_api =
bakaapi::get_data_from_endpoint("api/3/timetable/actual"); bakaapi::get_data_from_endpoint(endpoint, date_string);
/*std::ifstream f("test-data/timetable.json");
json resp_from_api = json::parse(f);*/
// this may be unnecessary but i dont have enaugh data to test it // this may be unnecessary but i dont have enaugh data to test it
// it sorts the hours by start time // it sorts the hours by start time
@ -248,7 +264,46 @@ void timetable_page() {
} }
attron(COLOR_PAIR(COLOR_BLUE)); attron(COLOR_PAIR(COLOR_BLUE));
mvprintw(LINES - 2, 0, mvprintw(LINES - 2, 0,
"Arrows/hjkl to select | ENTER to show info | F1 to exit"); "Arrows/hjkl to select | ENTER to show info | p/n to select weeks |F1 to exit");
{
std::tm end_week = local_time;
std::tm start_week = local_time;
// Convert tm_wday (0-6) to API day format (1-7)
int current_wday = (local_time.tm_wday == 0) ? 7 : local_time.tm_wday;
// Get days of week from API (1-7 format where Monday is 1)
uint8_t start_day = resp_from_api["Days"][0]["DayOfWeek"].get<uint8_t>();
uint8_t end_day = resp_from_api["Days"][resp_from_api["Days"].size() - 1]["DayOfWeek"].get<uint8_t>();
// Calculate days back to start day (handles week wraparound)
int days_back = (current_wday >= start_day)
? (current_wday - start_day)
: (current_wday + 7 - start_day);
// Calculate days forward to end day (handles week wraparound)
int days_forward = (current_wday <= end_day)
? (end_day - current_wday)
: (end_day + 7 - current_wday);
// Adjust dates
start_week.tm_mday -= days_back;
end_week.tm_mday += days_forward;
// Normalize the dates
std::mktime(&start_week);
std::mktime(&end_week);
// Format the dates as strings
std::stringstream start_week_strstream, end_week_strstream;
start_week_strstream << std::put_time(&start_week, "%d.%m.%Y");
end_week_strstream << std::put_time(&end_week, "%d.%m.%Y");
// kern. developer approved ↓↓
mvprintw(LINES - 2, COLS - (start_week_strstream.str().length() + 3 + end_week_strstream.str().length()),
"%s", (start_week_strstream.str() + " - " + end_week_strstream.str()).c_str());
}
attroff(COLOR_PAIR(COLOR_BLUE)); attroff(COLOR_PAIR(COLOR_BLUE));
update_panels(); update_panels();
@ -304,6 +359,14 @@ void timetable_page() {
case 'l': case 'l':
selected_cell.x++; selected_cell.x++;
break; break;
case 'p':
dateSelected = dateSelected - std::chrono::days(7);
goto reload_for_new_week;
break;
case 'n':
dateSelected = dateSelected + std::chrono::days(7);
goto reload_for_new_week;
break;
case 10: // ENTER case 10: // ENTER
json *atom = find_atom_by_indexes(resp_from_api, selected_cell.y, json *atom = find_atom_by_indexes(resp_from_api, selected_cell.y,
selected_cell.x, HourIdLookupTable); selected_cell.x, HourIdLookupTable);