Files
tactility_apps/Apps/ImmichElias/main/Source/main.c
T
Hermes Reyna Bot 869c97d7b9 ImmichElias: dynamic display sizing for 3.5 inch (480x320)
- detect display via lv_display_get_horizontal/vertical_resolution
- img target = full width x (disp_h - overhead) for any screen
- fetch_png_from_proxy now takes req_w/h params (was hardcoded 320x240)
- proxy 8106 already handles w/h: tested 320x240 81KB, 480x320 141KB, 320x480 176KB, 800x480 297KB
- THUMB_BUF_SIZE 200KB->512KB for larger PNGs
- Works on yellow 320x240 and new 3.5 inch 480x320 both
2026-07-25 14:32:38 -04:00

602 lines
27 KiB
C

#include <tt_app.h>
#include <tt_lvgl.h>
#include <tt_lvgl_toolbar.h>
#include <tactility/lvgl_fonts.h>
#include <esp_log.h>
#include <esp_http_client.h>
#include <esp_heap_caps.h>
#include <cJSON.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
static const char* TAG = "ImmichElias";
#define IMMICH_BASE_URL "http://192.168.68.110:2283"
#define IMMICH_API_KEY "tactility-elias-2af3d9c44b72f987f41afc3438c3dd740862417797a3e5a6"
#define IMMICH_PERSON_ID "cbece6a9-720f-482a-a945-c9ba5ca41fd4"
#define PERSON_NAME "Elias"
#define PERSON_COUNT_STR "10739"
#define RESIZE_PROXY_URL "http://192.168.68.110:8106"
#define MAX_PHOTOS 50
#define MAX_IMAGE_SIZE (512 * 1024)
#define JSON_BUF_SIZE (60 * 1024)
#define THUMB_BUF_SIZE (512 * 1024)
#define PNG_PATH "/data/immich_cur.png"
#define PNG_PATH_FB1 "/tmp/immich_cur.png"
#define PNG_PATH_FB2 "/sdcard/immich_cur.png"
// Fallback defaults if display detection fails
#define FALLBACK_W 320
#define FALLBACK_H 240
typedef struct {
char id[64];
char thumbnail_url[256];
char fileCreatedAt[64];
char originalFileName[128];
char type[16];
int width;
int height;
} Photo;
static Photo photos[MAX_PHOTOS];
static int photo_count = 0;
static int current_index = 0;
typedef struct {
uint8_t* buf;
size_t cap;
size_t len;
} HttpBuf;
static esp_err_t http_evt_handler(esp_http_client_event_t* evt) {
HttpBuf* hb = (HttpBuf*)evt->user_data;
if (hb == NULL) return ESP_OK;
if (evt->event_id == HTTP_EVENT_ON_DATA && evt->data && evt->data_len > 0) {
size_t copy_len = (size_t)evt->data_len;
if (hb->len + copy_len > hb->cap) copy_len = hb->cap - hb->len;
if (copy_len > 0) { memcpy(hb->buf + hb->len, evt->data, copy_len); hb->len += copy_len; }
}
return ESP_OK;
}
static int http_get_with_auth(const char* url, HttpBuf* out) {
if (out) out->len = 0;
esp_http_client_config_t cfg = {
.url = url,
.event_handler = http_evt_handler,
.user_data = out,
.timeout_ms = 15000,
.buffer_size = 4096,
.buffer_size_tx = 1024,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (!client) return -1;
esp_http_client_set_method(client, HTTP_METHOD_GET);
esp_http_client_set_header(client, "x-api-key", IMMICH_API_KEY);
esp_err_t err = esp_http_client_perform(client);
int status = esp_http_client_get_status_code(client);
esp_http_client_cleanup(client);
return (err == ESP_OK) ? status : -1;
}
static int http_get_no_auth(const char* url, HttpBuf* out) {
if (out) out->len = 0;
esp_http_client_config_t cfg = {
.url = url,
.event_handler = http_evt_handler,
.user_data = out,
.timeout_ms = 20000,
.buffer_size = 4096,
.buffer_size_tx = 1024,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (!client) return -1;
esp_http_client_set_method(client, HTTP_METHOD_GET);
esp_err_t err = esp_http_client_perform(client);
int status = esp_http_client_get_status_code(client);
ESP_LOGI(TAG, "GET noauth %s status %d len %d", url, status, out ? (int)out->len : -1);
esp_http_client_cleanup(client);
return (err == ESP_OK) ? status : -1;
}
static int http_post_json(const char* url, const char* json_body, HttpBuf* out) {
if (out) out->len = 0;
esp_http_client_config_t cfg = {
.url = url,
.event_handler = http_evt_handler,
.user_data = out,
.timeout_ms = 15000,
.buffer_size = 4096,
.buffer_size_tx = 2048,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (!client) return -1;
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "x-api-key", IMMICH_API_KEY);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_post_field(client, json_body, strlen(json_body));
esp_err_t err = esp_http_client_perform(client);
int status = esp_http_client_get_status_code(client);
esp_http_client_cleanup(client);
return (err == ESP_OK) ? status : -1;
}
static int fetch_person_assets(void) {
char* json_buf = heap_caps_malloc(JSON_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!json_buf) { json_buf = malloc(JSON_BUF_SIZE); if (!json_buf) return -1; }
HttpBuf hb = { .buf = (uint8_t*)json_buf, .cap = JSON_BUF_SIZE - 1, .len = 0 };
char url[256];
snprintf(url, sizeof(url), "%s/api/search/metadata", IMMICH_BASE_URL);
char body[256];
snprintf(body, sizeof(body), "{\"personIds\": [\"%s\"], \"size\": %d}", IMMICH_PERSON_ID, MAX_PHOTOS);
int status = http_post_json(url, body, &hb);
if (status != 200) { free(json_buf); return -1; }
json_buf[hb.len] = '\0';
cJSON* root = cJSON_Parse(json_buf);
if (!root) { free(json_buf); return -1; }
cJSON* assets = cJSON_GetObjectItem(root, "assets");
cJSON* items = NULL;
if (assets) items = cJSON_GetObjectItem(assets, "items");
else { items = cJSON_GetObjectItem(root, "items"); if (!items && cJSON_IsArray(root)) items = root; }
if (!items || !cJSON_IsArray(items)) { cJSON_Delete(root); free(json_buf); return -1; }
int n = cJSON_GetArraySize(items);
photo_count = 0;
for (int i = 0; i < n && photo_count < MAX_PHOTOS; i++) {
cJSON* it = cJSON_GetArrayItem(items, i);
if (!it) continue;
cJSON* jid = cJSON_GetObjectItem(it, "id");
if (!jid || !cJSON_IsString(jid)) continue;
Photo* p = &photos[photo_count];
memset(p, 0, sizeof(Photo));
strncpy(p->id, jid->valuestring, sizeof(p->id)-1);
cJSON* jw = cJSON_GetObjectItem(it, "width"); if (jw && cJSON_IsNumber(jw)) p->width = jw->valueint;
cJSON* jh = cJSON_GetObjectItem(it, "height"); if (jh && cJSON_IsNumber(jh)) p->height = jh->valueint;
cJSON* jfc = cJSON_GetObjectItem(it, "fileCreatedAt"); if (jfc && cJSON_IsString(jfc)) strncpy(p->fileCreatedAt, jfc->valuestring, sizeof(p->fileCreatedAt)-1);
cJSON* jfn = cJSON_GetObjectItem(it, "originalFileName"); if (jfn && cJSON_IsString(jfn)) strncpy(p->originalFileName, jfn->valuestring, sizeof(p->originalFileName)-1);
cJSON* jt = cJSON_GetObjectItem(it, "type"); if (jt && cJSON_IsString(jt)) strncpy(p->type, jt->valuestring, sizeof(p->type)-1);
char tmp_id[64]; strncpy(tmp_id, p->id, sizeof(tmp_id)-1); tmp_id[sizeof(tmp_id)-1]='\0';
snprintf(p->thumbnail_url, sizeof(p->thumbnail_url), "%s/api/assets/%s/thumbnail?size=thumbnail", IMMICH_BASE_URL, tmp_id);
photo_count++;
}
cJSON_Delete(root);
free(json_buf);
current_index = 0;
return photo_count;
}
static const char* save_buffer_to_file(const uint8_t* buf, size_t len) {
const char* candidates[] = { PNG_PATH, PNG_PATH_FB1, PNG_PATH_FB2, "/data/immich.png", "/tmp/immich.png" };
for (int i=0;i<5;i++) {
const char* path = candidates[i];
FILE* f = fopen(path, "wb");
if (f) {
size_t written = fwrite(buf, 1, len, f);
fclose(f);
ESP_LOGI(TAG, "Saved %d bytes to %s written %d", (int)len, path, (int)written);
if (written == len) return path;
} else {
ESP_LOGW(TAG, "fopen fail %s", path);
}
}
return NULL;
}
static int download_thumbnail_to_buf(const char* assetId, uint8_t* buf, size_t cap, size_t* out_len) {
char url[320];
snprintf(url, sizeof(url), "%s/api/assets/%s/thumbnail?size=thumbnail", IMMICH_BASE_URL, assetId);
HttpBuf hb = { .buf = buf, .cap = cap, .len = 0 };
int status = http_get_with_auth(url, &hb);
if (status == 200) { if (out_len) *out_len = hb.len; return (int)hb.len; }
return -1;
}
static int fetch_png_from_proxy(const char* assetId, uint8_t* buf, size_t cap, size_t* out_len, int req_w, int req_h) {
if (req_w <= 0) req_w = FALLBACK_W;
if (req_h <= 0) req_h = FALLBACK_H;
// Clamp to sane limits for ESP32 memory
if (req_w > 800) req_w = 800;
if (req_h > 600) req_h = 600;
char url[384];
snprintf(url, sizeof(url), "%s/resize?assetId=%s&w=%d&h=%d&format=png", RESIZE_PROXY_URL, assetId, req_w, req_h);
ESP_LOGI(TAG, "Fetching proxy PNG %s (req %dx%d)", url, req_w, req_h);
HttpBuf hb = { .buf = buf, .cap = cap, .len = 0 };
int status = http_get_no_auth(url, &hb);
if (status == 200 && hb.len > 0) {
if (out_len) *out_len = hb.len;
ESP_LOGI(TAG, "Proxy PNG OK %d bytes for %dx%d", (int)hb.len, req_w, req_h);
return (int)hb.len;
}
ESP_LOGE(TAG, "Proxy PNG fail id %s %dx%d status %d", assetId, req_w, req_h, status);
return -1;
}
typedef struct {
AppHandle app;
bool visible;
lv_obj_t* lbl_title;
lv_obj_t* lbl_count;
lv_obj_t* lbl_info;
lv_obj_t* lbl_status;
lv_obj_t* lbl_index;
lv_obj_t* img;
lv_obj_t* btn_prev;
lv_obj_t* btn_next;
lv_obj_t* btn_reload;
int current_idx;
char status_text[256];
bool fetch_in_progress;
bool thumb_in_progress;
uint8_t* thumb_buf;
size_t thumb_len;
TaskHandle_t fetch_task;
TaskHandle_t thumb_task;
bool stop_requested;
char saved_img_path[256];
bool has_image;
// Dynamic display sizing for 3.5" etc.
int disp_w;
int disp_h;
int img_w;
int img_h;
} AppCtx;
static void start_fetch_task(AppCtx* ctx);
static void start_thumb_task(AppCtx* ctx);
static void ui_update_all(AppCtx* ctx);
static void thumb_task_fn(void* arg);
static void fetch_task_fn(void* arg);
static void detect_display_size(AppCtx* ctx, lv_obj_t* parent) {
if (!ctx) return;
int w = FALLBACK_W;
int h = FALLBACK_H;
lv_display_t* disp = NULL;
if (parent) disp = lv_obj_get_display(parent);
if (!disp) disp = lv_display_get_default();
if (disp) {
int dw = lv_display_get_horizontal_resolution(disp);
int dh = lv_display_get_vertical_resolution(disp);
if (dw > 0 && dh > 0) {
w = dw;
h = dh;
}
}
// Also try NULL arg per GameBoy pattern (returns default)
if (w == FALLBACK_W && h == FALLBACK_H) {
int dw2 = lv_display_get_horizontal_resolution(NULL);
int dh2 = lv_display_get_vertical_resolution(NULL);
if (dw2 > 0 && dh2 > 0) {
w = dw2;
h = dh2;
}
}
ctx->disp_w = w;
ctx->disp_h = h;
// Calculate image target: use full width, leave ~100-120px for toolbar/title/nav
// For landscape 3.5" 480x320, this gives 480x ~220-240; for portrait 320x480 => 320x ~360
// Better: 68% of height for image area, max filling
int overhead = 120;
if (h >= 480) overhead = 140;
else if (h <= 240) overhead = 80;
int target_h = h - overhead;
if (target_h < 120) target_h = h * 2 / 3;
int target_w = w;
// Keep some margin: if target_w > 800, clamp (proxy also clamps)
if (target_w > 800) target_w = 800;
if (target_h > 600) target_h = 600;
ctx->img_w = target_w;
ctx->img_h = target_h;
ESP_LOGI(TAG, "Display detected %dx%d -> img target %dx%d", w, h, target_w, target_h);
}
static void ui_update_all(AppCtx* ctx) {
if (!ctx || !ctx->visible) return;
if (!tt_lvgl_lock(pdMS_TO_TICKS(1000))) return;
if (!ctx->visible) { tt_lvgl_unlock(); return; }
if (ctx->lbl_count) {
if (photo_count > 0) lv_label_set_text_fmt(ctx->lbl_count, "%s: %d / %s - %dx%d PNG via 8106 (disp %dx%d)", PERSON_NAME, photo_count, PERSON_COUNT_STR, ctx->img_w, ctx->img_h, ctx->disp_w, ctx->disp_h);
else lv_label_set_text(ctx->lbl_count, ctx->status_text);
}
if (ctx->lbl_index) {
if (photo_count > 0) lv_label_set_text_fmt(ctx->lbl_index, "%d/%d", ctx->current_idx + 1, photo_count);
else lv_label_set_text(ctx->lbl_index, "0/0");
}
if (ctx->lbl_info) {
if (photo_count > 0 && ctx->current_idx < photo_count) {
Photo* p = &photos[ctx->current_idx];
lv_label_set_text_fmt(ctx->lbl_info, "ID: %.36s\nFile: %.32s\nDate: %.24s\n%dx%d %s", p->id, p->originalFileName, p->fileCreatedAt, p->width, p->height, p->type);
} else lv_label_set_text(ctx->lbl_info, ctx->status_text);
}
if (ctx->lbl_status) {
if (ctx->thumb_len > 0) {
Photo* p = &photos[ctx->current_idx];
if (ctx->has_image) lv_label_set_text_fmt(ctx->lbl_status, "PNG %d bytes OK %dx%d\n%s\nID %.8s..", (int)ctx->thumb_len, ctx->img_w, ctx->img_h, ctx->saved_img_path, p->id);
else lv_label_set_text_fmt(ctx->lbl_status, "Thumb %d bytes\nID %.8s..\nSaved %s", (int)ctx->thumb_len, p->id, ctx->saved_img_path);
} else lv_label_set_text(ctx->lbl_status, ctx->status_text);
}
if (ctx->img) {
if (ctx->has_image && ctx->saved_img_path[0] != '\0') {
char lvgl_path[320];
if (ctx->saved_img_path[0] == '/') snprintf(lvgl_path, sizeof(lvgl_path), "A:%s", ctx->saved_img_path);
else snprintf(lvgl_path, sizeof(lvgl_path), "%s", ctx->saved_img_path);
lv_image_set_src(ctx->img, lvgl_path);
lv_obj_clear_flag(ctx->img, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_size(ctx->img, ctx->img_w, ctx->img_h);
ESP_LOGI(TAG, "lv_img_set_src %s %dx%d", lvgl_path, ctx->img_w, ctx->img_h);
} else {
lv_obj_add_flag(ctx->img, LV_OBJ_FLAG_HIDDEN);
}
}
tt_lvgl_unlock();
}
static void thumb_task_fn(void* arg) {
AppCtx* ctx = (AppCtx*)arg;
if (!ctx->thumb_buf) {
ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!ctx->thumb_buf) ctx->thumb_buf = malloc(THUMB_BUF_SIZE);
}
if (!ctx->thumb_buf) {
snprintf(ctx->status_text, sizeof(ctx->status_text), "OOM thumb buf %d", THUMB_BUF_SIZE);
ui_update_all(ctx);
ctx->thumb_task = NULL; ctx->thumb_in_progress = false; vTaskDelete(NULL); return;
}
int idx = ctx->current_idx;
if (idx < 0 || idx >= photo_count) idx = 0;
Photo* p = &photos[idx];
ESP_LOGI(TAG, "Downloading PNG idx %d id %s %dx%d", idx, p->id, ctx->img_w, ctx->img_h);
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching PNG %d/%d %dx%d via 8106", idx+1, photo_count, ctx->img_w, ctx->img_h);
ui_update_all(ctx);
size_t out_len = 0;
int r = fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &out_len, ctx->img_w, ctx->img_h);
if (r > 0) {
ctx->thumb_len = out_len;
const char* saved = save_buffer_to_file(ctx->thumb_buf, out_len);
if (saved) {
strncpy(ctx->saved_img_path, saved, sizeof(ctx->saved_img_path)-1);
ctx->has_image = true;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes %dx%d saved %s", (int)out_len, ctx->img_w, ctx->img_h, saved);
} else if (ctx->app) {
char tmp_path[256]; size_t tmp_sz = sizeof(tmp_path);
tt_app_get_user_data_child_path(ctx->app, "immich_cur.png", tmp_path, &tmp_sz);
FILE* f = fopen(tmp_path, "wb");
if (f) {
fwrite(ctx->thumb_buf, 1, out_len, f); fclose(f);
strncpy(ctx->saved_img_path, tmp_path, sizeof(ctx->saved_img_path)-1);
ctx->has_image = true;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d %dx%d via user_data %s", (int)out_len, ctx->img_w, ctx->img_h, tmp_path);
} else {
ctx->has_image = false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes but save fail", (int)out_len);
}
} else {
ctx->has_image = false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes no handle", (int)out_len);
}
} else {
ctx->thumb_len = 0; ctx->has_image = false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Proxy PNG fail %d fallback thumb", r);
size_t ol2=0; int r2 = download_thumbnail_to_buf(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol2);
if (r2>0) { ctx->thumb_len = ol2; snprintf(ctx->status_text, sizeof(ctx->status_text), "Thumb %d fallback", (int)ol2); }
}
ui_update_all(ctx);
ctx->thumb_task = NULL; ctx->thumb_in_progress = false; vTaskDelete(NULL);
}
static void start_thumb_task(AppCtx* ctx) {
if (ctx->thumb_in_progress) return;
if (photo_count==0) return;
ctx->thumb_in_progress = true; ctx->thumb_len=0; ctx->has_image=false;
xTaskCreate(thumb_task_fn, "thumb_fetch", 8192, ctx, 5, &ctx->thumb_task);
}
static void fetch_task_fn(void* arg) {
AppCtx* ctx = (AppCtx*)arg;
ctx->fetch_in_progress = true;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching %s photos...", PERSON_NAME);
ui_update_all(ctx);
int n = fetch_person_assets();
if (n>0) {
current_index=0; ctx->current_idx=0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loaded %d photos, %dx%d PNG via 8106...", n, ctx->img_w, ctx->img_h);
ui_update_all(ctx);
if (!ctx->thumb_buf) {
ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!ctx->thumb_buf) ctx->thumb_buf = malloc(THUMB_BUF_SIZE);
}
if (ctx->thumb_buf) {
size_t ol=0;
Photo* p=&photos[0];
int r=fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol, ctx->img_w, ctx->img_h);
if (r>0) {
ctx->thumb_len=ol;
const char* saved=save_buffer_to_file(ctx->thumb_buf, ol);
if (saved) { strncpy(ctx->saved_img_path, saved, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d %dx%d %s", (int)ol, ctx->img_w, ctx->img_h, saved); }
else if (ctx->app) {
char tmp_path[256]; size_t tmp_sz=sizeof(tmp_path);
tt_app_get_user_data_child_path(ctx->app, "immich_cur.png", tmp_path, &tmp_sz);
FILE* f=fopen(tmp_path,"wb"); if(f){fwrite(ctx->thumb_buf,1,ol,f); fclose(f); strncpy(ctx->saved_img_path, tmp_path, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text,sizeof(ctx->status_text),"PNG %d %dx%d user_data %s",(int)ol, ctx->img_w, ctx->img_h, tmp_path);}
}
}
ui_update_all(ctx);
}
} else {
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetch failed %s", IMMICH_BASE_URL);
ui_update_all(ctx);
}
ctx->fetch_task=NULL; ctx->fetch_in_progress=false; vTaskDelete(NULL);
}
static void start_fetch_task(AppCtx* ctx) {
if (ctx->fetch_in_progress) return;
ctx->fetch_in_progress=true;
xTaskCreate(fetch_task_fn, "immich_fetch", 8192, ctx, 5, &ctx->fetch_task);
}
static void prev_event_cb(lv_event_t* e) {
AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e);
if (!ctx || photo_count==0) return;
if (ctx->thumb_in_progress || ctx->fetch_in_progress) return;
ctx->current_idx--; if (ctx->current_idx<0) ctx->current_idx=photo_count-1;
current_index=ctx->current_idx; ctx->thumb_len=0; ctx->has_image=false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d %dx%d 8106 PNG", ctx->current_idx+1, photo_count, ctx->img_w, ctx->img_h);
ui_update_all(ctx); start_thumb_task(ctx);
}
static void next_event_cb(lv_event_t* e) {
AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e);
if (!ctx || photo_count==0) return;
if (ctx->thumb_in_progress || ctx->fetch_in_progress) return;
ctx->current_idx++; if (ctx->current_idx>=photo_count) ctx->current_idx=0;
current_index=ctx->current_idx; ctx->thumb_len=0; ctx->has_image=false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d %dx%d 8106 PNG", ctx->current_idx+1, photo_count, ctx->img_w, ctx->img_h);
ui_update_all(ctx); start_thumb_task(ctx);
}
static void reload_event_cb(lv_event_t* e) {
AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e);
if (!ctx) return;
if (ctx->fetch_in_progress || ctx->thumb_in_progress) return;
photo_count=0; ctx->current_idx=0; current_index=0; ctx->thumb_len=0; ctx->has_image=false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Reloading %dx%d...", ctx->img_w, ctx->img_h);
ui_update_all(ctx); start_fetch_task(ctx);
}
static void* create_data(void) {
AppCtx* ctx = calloc(1, sizeof(AppCtx));
if (ctx) {
ctx->current_idx=0;
ctx->disp_w = FALLBACK_W;
ctx->disp_h = FALLBACK_H;
ctx->img_w = FALLBACK_W;
ctx->img_h = FALLBACK_H;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Init %s proxy %s", IMMICH_BASE_URL, RESIZE_PROXY_URL);
}
return ctx;
}
static void destroy_data(void* data) {
AppCtx* ctx = (AppCtx*)data; if (!ctx) return;
if (ctx->thumb_buf) heap_caps_free(ctx->thumb_buf);
free(ctx);
}
static void on_create(AppHandle app, void* data) { AppCtx* ctx=(AppCtx*)data; if(ctx) ctx->app=app; ESP_LOGI(TAG,"onCreate proxy=%s", RESIZE_PROXY_URL); }
static void on_destroy(AppHandle app, void* data) { (void)app;(void)data; }
static void on_show(AppHandle app, void* data, lv_obj_t* parent) {
AppCtx* ctx=(AppCtx*)data; if(!ctx) return;
ctx->visible=true; ctx->app=app;
detect_display_size(ctx, parent);
lv_obj_remove_flag(parent, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(parent, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_pad_all(parent, 4, LV_PART_MAIN);
lv_obj_set_style_pad_row(parent, 4, LV_PART_MAIN);
lv_obj_set_style_bg_color(parent, lv_color_hex(0x121212), LV_PART_MAIN);
lv_obj_set_style_bg_opa(parent, LV_OPA_COVER, LV_PART_MAIN);
lv_obj_t* toolbar=tt_lvgl_toolbar_create_for_app(parent, app);
lv_obj_t* title_cont=lv_obj_create(parent);
lv_obj_set_width(title_cont, lv_pct(100)); lv_obj_set_height(title_cont, LV_SIZE_CONTENT);
lv_obj_set_flex_flow(title_cont, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(title_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_bg_opa(title_cont, LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_set_style_border_width(title_cont, 0, LV_PART_MAIN);
lv_obj_set_style_pad_all(title_cont, 2, LV_PART_MAIN);
ctx->lbl_title=lv_label_create(title_cont);
lv_label_set_text_fmt(ctx->lbl_title, "%s - %s photos %dx%d 8106 PNG", PERSON_NAME, PERSON_COUNT_STR, ctx->img_w, ctx->img_h);
lv_obj_set_style_text_font(ctx->lbl_title, lvgl_get_text_font(FONT_SIZE_LARGE), LV_PART_MAIN);
lv_obj_set_style_text_color(ctx->lbl_title, lv_color_white(), LV_PART_MAIN);
lv_obj_set_width(ctx->lbl_title, lv_pct(100));
lv_obj_set_style_text_align(ctx->lbl_title, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
ctx->lbl_count=lv_label_create(title_cont);
lv_label_set_text(ctx->lbl_count, "Loading...");
lv_obj_set_style_text_font(ctx->lbl_count, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN);
lv_obj_set_style_text_color(ctx->lbl_count, lv_color_hex(0xAAAAAA), LV_PART_MAIN);
lv_obj_set_width(ctx->lbl_count, lv_pct(100));
lv_label_set_long_mode(ctx->lbl_count, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(ctx->lbl_count, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
lv_obj_t* main_cont=lv_obj_create(parent);
lv_obj_set_width(main_cont, lv_pct(100));
lv_obj_set_flex_grow(main_cont, 1);
lv_obj_set_flex_flow(main_cont, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(main_cont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_bg_color(main_cont, lv_color_hex(0x1E1E1E), LV_PART_MAIN);
lv_obj_set_style_bg_opa(main_cont, LV_OPA_COVER, LV_PART_MAIN);
lv_obj_set_style_radius(main_cont, 8, LV_PART_MAIN);
lv_obj_set_style_pad_all(main_cont, 6, LV_PART_MAIN);
ctx->img=lv_image_create(main_cont);
lv_obj_add_flag(ctx->img, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_size(ctx->img, ctx->img_w, ctx->img_h);
ctx->lbl_status=lv_label_create(main_cont);
lv_label_set_text(ctx->lbl_status, ctx->status_text);
lv_obj_set_width(ctx->lbl_status, lv_pct(100));
lv_label_set_long_mode(ctx->lbl_status, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_font(ctx->lbl_status, lvgl_get_text_font(FONT_SIZE_DEFAULT), LV_PART_MAIN);
lv_obj_set_style_text_align(ctx->lbl_status, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
ctx->lbl_info=lv_label_create(parent);
lv_obj_set_width(ctx->lbl_info, lv_pct(100));
lv_label_set_long_mode(ctx->lbl_info, LV_LABEL_LONG_WRAP);
lv_label_set_text(ctx->lbl_info, "loading...");
lv_obj_set_style_text_font(ctx->lbl_info, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN);
lv_obj_t* nav_row=lv_obj_create(parent);
lv_obj_set_width(nav_row, lv_pct(100)); lv_obj_set_height(nav_row, 48);
lv_obj_set_flex_flow(nav_row, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(nav_row, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_bg_opa(nav_row, LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_set_style_border_width(nav_row, 0, LV_PART_MAIN);
lv_obj_set_style_pad_all(nav_row, 2, LV_PART_MAIN);
ctx->btn_prev=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_prev, 60, 40);
lv_obj_t* lp=lv_label_create(ctx->btn_prev); lv_label_set_text(lp, "<"); lv_obj_center(lp);
lv_obj_add_event_cb(ctx->btn_prev, prev_event_cb, LV_EVENT_CLICKED, ctx);
ctx->lbl_index=lv_label_create(nav_row);
lv_label_set_text(ctx->lbl_index, "0/0");
lv_obj_set_style_text_font(ctx->lbl_index, lvgl_get_text_font(FONT_SIZE_DEFAULT), LV_PART_MAIN);
lv_obj_set_flex_grow(ctx->lbl_index, 1);
lv_obj_set_style_text_align(ctx->lbl_index, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
ctx->btn_next=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_next, 60, 40);
lv_obj_set_style_bg_color(ctx->btn_next, lv_color_hex(0x6200EE), LV_PART_MAIN);
lv_obj_t* ln=lv_label_create(ctx->btn_next); lv_label_set_text(ln, ">"); lv_obj_center(ln);
lv_obj_add_event_cb(ctx->btn_next, next_event_cb, LV_EVENT_CLICKED, ctx);
ctx->btn_reload=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_reload, 40, 40);
lv_obj_t* lr=lv_label_create(ctx->btn_reload); lv_label_set_text(lr, LV_SYMBOL_REFRESH); lv_obj_center(lr);
lv_obj_add_event_cb(ctx->btn_reload, reload_event_cb, LV_EVENT_CLICKED, ctx);
if (photo_count>0) { ctx->current_idx=current_index; if(ctx->current_idx>=photo_count) ctx->current_idx=0; snprintf(ctx->status_text,sizeof(ctx->status_text),"Cached %d photos %dx%d via 8106 disp %dx%d",photo_count, ctx->img_w, ctx->img_h, ctx->disp_w, ctx->disp_h); ui_update_all(ctx); start_thumb_task(ctx); }
else { snprintf(ctx->status_text,sizeof(ctx->status_text),"Fetching %s %dx%d via %s disp %dx%d",PERSON_NAME, ctx->img_w, ctx->img_h, RESIZE_PROXY_URL, ctx->disp_w, ctx->disp_h); ui_update_all(ctx); start_fetch_task(ctx); }
}
static void on_hide(AppHandle app, void* data) {
(void)app; AppCtx* ctx=(AppCtx*)data; if(!ctx) return;
ctx->visible=false;
int timeout=20; while((ctx->fetch_task||ctx->thumb_task) && timeout>0){ vTaskDelay(pdMS_TO_TICKS(50)); timeout--; }
}
int main(int argc, char* argv[]) {
(void)argc;(void)argv;
tt_app_register((AppRegistration){.createData=create_data,.destroyData=destroy_data,.onCreate=on_create,.onDestroy=on_destroy,.onShow=on_show,.onHide=on_hide});
return 0;
}