From 2ec7fd139ea501719277c3ebedd39a2cfc9b5bc2 Mon Sep 17 00:00:00 2001 From: Sreedev Kodichath Date: Sat, 22 Aug 2026 08:35:57 +0200 Subject: [PATCH] [ADD] app: open the station and stand pages --- src/c/departures_window.c | 16 ++- src/c/labels.c | 3 +- src/c/labels.h | 3 +- src/c/main.c | 3 + src/c/model.c | 13 +++ src/c/model.h | 8 ++ src/c/station_window.c | 229 ++++++++++++++++++++++++++++++++++++++ src/c/station_window.h | 7 ++ tests/c/test_labels.c | 9 +- tests/c/test_model.c | 17 +++ 10 files changed, 300 insertions(+), 8 deletions(-) create mode 100644 src/c/station_window.c create mode 100644 src/c/station_window.h diff --git a/src/c/departures_window.c b/src/c/departures_window.c index 6858803..49d0e85 100644 --- a/src/c/departures_window.c +++ b/src/c/departures_window.c @@ -1,6 +1,7 @@ #include "departures_window.h" #include "line_window.h" #include "menu_window.h" +#include "station_window.h" #include "anim.h" #include "badge.h" #include "labels.h" @@ -174,10 +175,19 @@ static void select_click(MenuLayer *menu, MenuIndex *index, void *ctx) { DepartureRef *ref = &inst->refs[index->row]; Stop *stop = model_stop_at(ref->stop); Row *row = &stop->rows[ref->row]; - if (model_row_info(row)) { - return; + switch (model_row_select(row)) { + case ROW_SELECT_STATION: + station_window_push(ref->stop); + break; + case ROW_SELECT_STAND: + stand_window_push(ref->stop); + break; + case ROW_SELECT_LINE: + line_window_push_label(stop->id, row->line, row->destination); + break; + case ROW_SELECT_INERT: + break; } - line_window_push_label(stop->id, row->line, row->destination); } static void push_mode(DeparturesMode mode); diff --git a/src/c/labels.c b/src/c/labels.c index f26ee18..41b71fc 100644 --- a/src/c/labels.c +++ b/src/c/labels.c @@ -1,12 +1,13 @@ #include "labels.h" -static const char *s_texts[6][3] = { +static const char *s_texts[7][3] = { {"Loading...", "Chargement...", "Laden..."}, {"No GPS fix", "Pas de GPS", "Geen GPS"}, {"No network", "Pas de reseau", "Geen netwerk"}, {"Phone unreachable", "Telephone injoignable", "Telefoon onbereikbaar"}, {"Nothing right now", "Rien pour le moment", "Nu even niets"}, {"No data for this line", "Pas de donnees pour cette ligne", "Geen gegevens voor deze lijn"}, + {"Platform", "Voie", "Spoor"}, }; const char *label_text(Label label, uint8_t lang) { diff --git a/src/c/labels.h b/src/c/labels.h index d13d221..857fbb2 100644 --- a/src/c/labels.h +++ b/src/c/labels.h @@ -9,7 +9,8 @@ typedef enum { LABEL_NO_NETWORK, LABEL_NO_PHONE, LABEL_NOTHING, - LABEL_NO_LINE + LABEL_NO_LINE, + LABEL_PLATFORM } Label; const char *label_text(Label label, uint8_t lang); diff --git a/src/c/main.c b/src/c/main.c index 5998ac6..89e37b5 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -2,6 +2,7 @@ #include "departures_window.h" #include "line_window.h" #include "disruption_window.h" +#include "station_window.h" #include "model.h" #include "msg.h" #include "glance.h" @@ -9,6 +10,7 @@ static void on_update(void) { departures_window_refresh(); line_window_refresh(); + station_window_refresh(); } static void on_connection(bool connected) { @@ -28,6 +30,7 @@ static void on_tick(struct tm *tick_time, TimeUnits changed) { departures_window_refresh(); line_window_refresh(); disruption_window_refresh(); + station_window_refresh(); } static void on_wakeup(WakeupId id, int32_t cookie) { diff --git a/src/c/model.c b/src/c/model.c index 2c10d62..9b621e6 100644 --- a/src/c/model.c +++ b/src/c/model.c @@ -106,6 +106,19 @@ bool model_row_info(const Row *row) { return (row->flags & ROW_FLAG_END) && row->line[0] != '\0'; } +RowSelect model_row_select(const Row *row) { + if (row->flags & ROW_FLAG_END) { + return ROW_SELECT_INERT; + } + if (row->kind == 3) { + return ROW_SELECT_STATION; + } + if (row->kind == 4) { + return ROW_SELECT_STAND; + } + return row->line[0] ? ROW_SELECT_LINE : ROW_SELECT_INERT; +} + static uint32_t departure_key(const DepartureRef *ref) { const Row *row = &s_stops[ref->stop].rows[ref->row]; uint32_t key = row->min1 < 0 ? UINT16_MAX : (uint16_t)row->min1; diff --git a/src/c/model.h b/src/c/model.h index 917783a..b52bca1 100644 --- a/src/c/model.h +++ b/src/c/model.h @@ -78,6 +78,13 @@ typedef enum { DEPARTURES_FAVORITES } DeparturesMode; +typedef enum { + ROW_SELECT_INERT, + ROW_SELECT_LINE, + ROW_SELECT_STATION, + ROW_SELECT_STAND +} RowSelect; + typedef enum { STATE_LOADING, STATE_OK, @@ -107,6 +114,7 @@ uint8_t model_lang(void); void model_set_config(bool vibrate, uint8_t lang); bool model_row_visible(const Row *row); bool model_row_info(const Row *row); +RowSelect model_row_select(const Row *row); uint8_t model_departures(DeparturesMode mode, DepartureRef *out, uint8_t max); uint8_t model_distinct_stops(const DepartureRef *refs, uint8_t count); LineDetail *model_line_detail(void); diff --git a/src/c/station_window.c b/src/c/station_window.c new file mode 100644 index 0000000..dbe34f4 --- /dev/null +++ b/src/c/station_window.c @@ -0,0 +1,229 @@ +#include "station_window.h" +#include "badge.h" +#include "labels.h" +#include "model.h" + +#define HEADER_H 54 +#define HEADER_BADGE_W 48 +#define HEADER_BADGE_H 32 +#define ROW_H 42 +#define ROW_BADGE_W 36 +#define ROW_BADGE_H 24 +#define TEXT_X 46 +#define TIME_W 44 +#define STATION_BLUE GColorCobaltBlue + +static Window *s_window; +static Layer *s_header; +static MenuLayer *s_menu; +static uint8_t s_stop_index; + +static Window *s_stand_window; +static Layer *s_stand_layer; +static GBitmap *s_stand_icon; +static uint8_t s_stand_index; + +static uint16_t station_rows(void) { + Stop *stop = model_stop_at(s_stop_index); + uint8_t n = stop ? stop->row_count : 0; + return n > MAX_ROWS ? MAX_ROWS : n; +} + +static void header_update(Layer *layer, GContext *ctx) { + GRect bounds = layer_get_bounds(layer); + Stop *stop = model_stop_at(s_stop_index); + graphics_context_set_fill_color(ctx, STATION_BLUE); + graphics_fill_rect(ctx, bounds, 0, GCornerNone); + GRect badge = GRect(8, (HEADER_H - HEADER_BADGE_H) / 2, HEADER_BADGE_W, HEADER_BADGE_H); + draw_line_badge(ctx, badge, "S", STATION_BLUE, GColorWhite); + graphics_context_set_stroke_color(ctx, GColorWhite); + graphics_context_set_stroke_width(ctx, 2); + graphics_draw_round_rect(ctx, badge, 6); + graphics_context_set_text_color(ctx, GColorWhite); + graphics_draw_text(ctx, stop ? stop->name : "", fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD), + GRect(64, 12, bounds.size.w - 64 - 6, 28), + GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL); +} + +static uint16_t get_num_rows(MenuLayer *menu, uint16_t section, void *ctx) { + uint16_t n = station_rows(); + return n ? n : 1; +} + +static int16_t get_cell_height(MenuLayer *menu, MenuIndex *index, void *ctx) { + return ROW_H; +} + +static void draw_row(GContext *ctx, const Layer *cell, MenuIndex *index, void *data) { + GRect bounds = layer_get_bounds(cell); + Stop *stop = model_stop_at(s_stop_index); + if (!stop || index->row >= station_rows()) { + return; + } + Row *row = &stop->rows[index->row]; + if (row->line[0]) { + draw_line_badge(ctx, GRect(4, (ROW_H - ROW_BADGE_H) / 2, ROW_BADGE_W, ROW_BADGE_H), + row->line, row->color, row->text_color); + } + graphics_context_set_text_color(ctx, GColorWhite); + graphics_draw_text(ctx, row->destination, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD), + GRect(TEXT_X, 2, bounds.size.w - TEXT_X - TIME_W, 16), + GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL); + char buf[24]; + if (row->min1 >= 0) { + snprintf(buf, sizeof(buf), "%d'", row->min1); + graphics_draw_text(ctx, buf, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD), + GRect(bounds.size.w - TIME_W, 0, TIME_W - 4, 20), + GTextOverflowModeTrailingEllipsis, GTextAlignmentRight, NULL); + } + if (row->platform[0]) { + snprintf(buf, sizeof(buf), "%s %s", label_text(LABEL_PLATFORM, model_lang()), row->platform); + graphics_context_set_text_color(ctx, GColorLightGray); + graphics_draw_text(ctx, buf, fonts_get_system_font(FONT_KEY_GOTHIC_14), + GRect(TEXT_X, 20, bounds.size.w - TEXT_X - TIME_W, 16), + GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL); + } + if (row->delay_seconds > 0) { + snprintf(buf, sizeof(buf), "+%d'", (row->delay_seconds + 59) / 60); + graphics_context_set_text_color(ctx, GColorRed); + graphics_draw_text(ctx, buf, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD), + GRect(bounds.size.w - TIME_W, 20, TIME_W - 4, 16), + GTextOverflowModeTrailingEllipsis, GTextAlignmentRight, NULL); + } +} + +static void window_load(Window *window) { + window_set_background_color(window, GColorBlack); + Layer *root = window_get_root_layer(window); + GRect bounds = layer_get_bounds(root); + s_header = layer_create(GRect(0, 0, bounds.size.w, HEADER_H)); + layer_set_update_proc(s_header, header_update); + layer_add_child(root, s_header); + s_menu = menu_layer_create(GRect(0, HEADER_H, bounds.size.w, bounds.size.h - HEADER_H)); + menu_layer_set_callbacks(s_menu, NULL, (MenuLayerCallbacks){ + .get_num_rows = get_num_rows, + .get_cell_height = get_cell_height, + .draw_row = draw_row, + }); + menu_layer_set_normal_colors(s_menu, GColorBlack, GColorWhite); + menu_layer_set_highlight_colors(s_menu, GColorDarkGray, GColorWhite); + menu_layer_set_click_config_onto_window(s_menu, window); + layer_add_child(root, menu_layer_get_layer(s_menu)); +} + +static void window_unload(Window *window) { + menu_layer_destroy(s_menu); + s_menu = NULL; + layer_destroy(s_header); + s_header = NULL; + window_destroy(window); + s_window = NULL; +} + +void station_window_push(uint8_t stop_index) { + if (s_window) { + return; + } + s_stop_index = stop_index; + s_window = window_create(); + window_set_window_handlers(s_window, (WindowHandlers){ + .load = window_load, + .unload = window_unload, + }); + window_stack_push(s_window, true); +} + +static const char *stand_fallback(Stop *stop) { + for (uint8_t i = 0; i < stop->row_count && i < MAX_ROWS; i++) { + if (stop->rows[i].kind == 4) { + return stop->rows[i].destination; + } + } + return ""; +} + +static void stand_update(Layer *layer, GContext *ctx) { + GRect bounds = layer_get_bounds(layer); + Stop *stop = model_stop_at(s_stand_index); + if (!stop) { + return; + } + int16_t x = 8; + if (s_stand_icon) { + GRect ib = gbitmap_get_bounds(s_stand_icon); + graphics_context_set_compositing_mode(ctx, GCompOpSet); + graphics_draw_bitmap_in_rect(ctx, s_stand_icon, GRect(8, 14, ib.size.w, ib.size.h)); + x = 8 + ib.size.w + 6; + } + graphics_context_set_text_color(ctx, GColorWhite); + graphics_draw_text(ctx, stop->name, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD), + GRect(x, 4, bounds.size.w - x - 6, 48), + GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL); + if (stop->bikes_mechanical < 0 && stop->bikes_electrical < 0 && stop->docks < 0) { + graphics_draw_text(ctx, stand_fallback(stop), fonts_get_system_font(FONT_KEY_GOTHIC_18), + GRect(8, bounds.size.h / 2 - 20, bounds.size.w - 16, 60), + GTextOverflowModeWordWrap, GTextAlignmentCenter, NULL); + return; + } + int16_t counts[3] = { stop->bikes_mechanical, stop->bikes_electrical, stop->docks }; + const char *names[3] = { "mech", "elec", "free" }; + int16_t colw = bounds.size.w / 3; + int16_t top = bounds.size.h / 2 - 30; + char buf[8]; + for (int i = 0; i < 3; i++) { + if (counts[i] < 0) { + snprintf(buf, sizeof(buf), "-"); + } else { + snprintf(buf, sizeof(buf), "%d", counts[i]); + } + graphics_context_set_text_color(ctx, GColorWhite); + graphics_draw_text(ctx, buf, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD), + GRect(i * colw, top, colw, 32), + GTextOverflowModeTrailingEllipsis, GTextAlignmentCenter, NULL); + graphics_context_set_text_color(ctx, GColorLightGray); + graphics_draw_text(ctx, names[i], fonts_get_system_font(FONT_KEY_GOTHIC_14), + GRect(i * colw, top + 36, colw, 16), + GTextOverflowModeTrailingEllipsis, GTextAlignmentCenter, NULL); + } +} + +static void stand_load(Window *window) { + window_set_background_color(window, GColorBlack); + Layer *root = window_get_root_layer(window); + s_stand_icon = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ICON_BIKE); + s_stand_layer = layer_create(layer_get_bounds(root)); + layer_set_update_proc(s_stand_layer, stand_update); + layer_add_child(root, s_stand_layer); +} + +static void stand_unload(Window *window) { + layer_destroy(s_stand_layer); + s_stand_layer = NULL; + gbitmap_destroy(s_stand_icon); + s_stand_icon = NULL; + window_destroy(window); + s_stand_window = NULL; +} + +void stand_window_push(uint8_t stop_index) { + if (s_stand_window) { + return; + } + s_stand_index = stop_index; + s_stand_window = window_create(); + window_set_window_handlers(s_stand_window, (WindowHandlers){ + .load = stand_load, + .unload = stand_unload, + }); + window_stack_push(s_stand_window, true); +} + +void station_window_refresh(void) { + if (s_window) { + layer_mark_dirty(s_header); + menu_layer_reload_data(s_menu); + } + if (s_stand_window) { + layer_mark_dirty(s_stand_layer); + } +} diff --git a/src/c/station_window.h b/src/c/station_window.h new file mode 100644 index 0000000..1bc9cbb --- /dev/null +++ b/src/c/station_window.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +void station_window_push(uint8_t stop_index); +void stand_window_push(uint8_t stop_index); +void station_window_refresh(void); diff --git a/tests/c/test_labels.c b/tests/c/test_labels.c index 32a20b4..654c8cc 100644 --- a/tests/c/test_labels.c +++ b/tests/c/test_labels.c @@ -1,10 +1,10 @@ #include #include "labels.h" -_Static_assert(LABEL_NO_LINE == 5, "label table has 6 rows"); +_Static_assert(LABEL_PLATFORM == 6, "label table has 7 rows"); int main(void) { - for (uint8_t label = 0; label <= LABEL_NO_LINE; label++) { + for (uint8_t label = 0; label <= LABEL_PLATFORM; label++) { for (uint8_t lang = 0; lang < 3; lang++) { const char *text = label_text((Label)label, lang); assert(text); @@ -14,7 +14,7 @@ int main(void) { assert(label_text((Label)label, 255) == label_text((Label)label, 0)); } for (uint8_t lang = 0; lang < 3; lang++) { - for (uint8_t a = 0; a <= LABEL_NO_LINE; a++) { + for (uint8_t a = 0; a <= LABEL_PLATFORM; a++) { for (uint8_t b = 0; b < a; b++) { assert(strcmp(label_text((Label)a, lang), label_text((Label)b, lang)) != 0); } @@ -22,5 +22,8 @@ int main(void) { } assert(strcmp(label_text(LABEL_NOTHING, 0), "Nothing right now") == 0); assert(strcmp(label_text(LABEL_LOADING, 0), "Loading...") == 0); + assert(strcmp(label_text(LABEL_PLATFORM, 0), "Platform") == 0); + assert(strcmp(label_text(LABEL_PLATFORM, 1), "Voie") == 0); + assert(strcmp(label_text(LABEL_PLATFORM, 2), "Spoor") == 0); return 0; } diff --git a/tests/c/test_model.c b/tests/c/test_model.c index 19c5e5c..977ff00 100644 --- a/tests/c/test_model.c +++ b/tests/c/test_model.c @@ -156,6 +156,22 @@ static void test_row_info_predicate(void) { assert(!model_row_info(&row)); } +static void test_row_select_routing(void) { + Row row = { .line = "62", .kind = 0, .flags = 0 }; + assert(model_row_select(&row) == ROW_SELECT_LINE); + row.kind = 3; + assert(model_row_select(&row) == ROW_SELECT_STATION); + row.kind = 4; + assert(model_row_select(&row) == ROW_SELECT_STAND); + row.flags = ROW_FLAG_END; + assert(model_row_select(&row) == ROW_SELECT_INERT); + row.kind = 0; + assert(model_row_select(&row) == ROW_SELECT_INERT); + row.flags = 0; + row.line[0] = '\0'; + assert(model_row_select(&row) == ROW_SELECT_INERT); +} + static void test_departures_nearby_appends_info_rows(void) { model_begin(1); put_stop(0, "a", "A", false, 5); @@ -519,6 +535,7 @@ int main(void) { test_line_complete_needs_header(); test_row_visible_filter(); test_row_info_predicate(); + test_row_select_routing(); test_departures_sorts_interleaved(); test_departures_absent_last(); test_departures_tie_keeps_board_order(); -- knotmirror 0.0.1