Files
pyxis/lib/tdeck_ui/UI/LXMF/ConversationListScreen.h
T
Torlando 362db2c805 fix(ui): fail closed when the queue mutex is unavailable
Greptile follow-up on 67c1083: if xSemaphoreCreateMutex() failed,
QueueGuard silently locked nothing and the deferred queues still ran
unsynchronized. The constructor now logs the allocation failure and
every guarded site refuses mutation when _queue_mutex is null:
requesters stop enqueuing, flushers stop draining (nothing queued can
exist), and the one-shot preview commit is skipped. The screen keeps
rendering and chatting; only the deferred list mutations degrade.
2026-09-04 02:49:26 +00:00

322 lines
12 KiB
C++

// Copyright (c) 2024 microReticulum contributors
// SPDX-License-Identifier: MIT
#ifndef UI_LXMF_CONVERSATIONLISTSCREEN_H
#define UI_LXMF_CONVERSATIONLISTSCREEN_H
#ifdef ARDUINO
#include <Arduino.h>
#include <lvgl.h>
#include <vector>
#include <functional>
#include <string>
#include <utility>
#include <microReticulum/Bytes.h>
#include "LXMF/MessageStore.h"
#include <microReticulum/Interface.h>
// TinyGPS++ pulled in for TinyGPSCustom (the in-view-satellites parser
// hook; declared as a member below). The forward decl on TinyGPSPlus
// stays since the field is a pointer.
#include <TinyGPS++.h>
class TinyGPSPlus; // Forward declaration
namespace UI {
namespace LXMF {
/**
* Conversation List Screen
*
* Shows a scrollable list of all LXMF conversations with:
* - Peer name/hash (truncated)
* - Last message preview
* - Timestamp
* - Unread count indicator
* - Navigation buttons (New message, Settings)
*
* Layout:
* ┌─────────────────────────────────────┐
* │ LXMF Messages [New] [☰] │ 32px header
* ├─────────────────────────────────────┤
* │ ┌─ Alice (a1b2c3...) │
* │ │ Hey, how are you? │
* │ │ 2 hours ago [2] │ Unread count
* │ └─ │
* │ ┌─ Bob (d4e5f6...) │ 176px scrollable
* │ │ See you tomorrow! │
* │ │ Yesterday │
* │ └─ │
* ├─────────────────────────────────────┤
* │ [💬] [👤] [📡] [⚙️] │ 32px bottom nav
* └─────────────────────────────────────┘
*/
class ConversationListScreen {
public:
/**
* Conversation item data
*/
struct ConversationItem {
RNS::Bytes peer_hash;
String peer_name; // Or truncated hash if no name
String last_message; // Preview of last message
String timestamp_str; // Human-readable time
uint32_t timestamp; // Unix timestamp
uint16_t unread_count;
};
/**
* Callback types
*/
using ConversationSelectedCallback = std::function<void(const RNS::Bytes& peer_hash)>;
using ComposeCallback = std::function<void()>;
using SyncCallback = std::function<void()>;
using HomeCallback = std::function<void()>;
using PeersCallback = std::function<void()>;
/**
* Create conversation list screen
* @param parent Parent LVGL object (usually lv_scr_act())
*/
ConversationListScreen(lv_obj_t* parent = nullptr);
/**
* Destructor
*/
~ConversationListScreen();
/**
* Load conversations from message store
* @param store Message store to load from
*/
void load_conversations(::LXMF::MessageStore& store);
/**
* Refresh conversation list (reload from store)
*/
void refresh();
// Flush display-name write-throughs deferred by refresh(). MUST be called
// OUTSIDE the LVGL lock (UIManager::update() calls it before taking the
// lock) — set_display_name() hits microStore/LittleFS.
void flush_pending_name_writes();
/**
* Request that a conversation be marked read. Defers the store
* mutation (LittleFS index commit) out of the LVGL event callback;
* MUST be drained by flush_pending_mark_reads() OUTSIDE the LVGL
* lock (UIManager::update() calls it before taking the lock), the
* same pattern as the deferred display-name write-throughs.
*/
void request_mark_read(const RNS::Bytes& peer_hash);
/**
* Commit pending mark-read requests to the store. MUST be called
* OUTSIDE the LVGL lock.
*/
void flush_pending_mark_reads();
/**
* Remove the unread badge from a rendered conversation row.
*/
void clear_unread_badge(lv_obj_t* container);
/**
* Queue a corrupt/unreadable message for deletion. The actual
* store mutation (index commit + payload removal) is drained by
* flush_pending_drops() OUTSIDE the LVGL lock, like mark-read.
*/
void request_drop_message(const RNS::Bytes& message_hash);
/**
* Drop queued unreadable messages from the store. MUST be called
* OUTSIDE the LVGL lock.
*/
void flush_pending_drops();
/**
* Commit the persisted conversation index once when this boot has
* fallen back to message-file reads (the one-shot preview-cache
* warm-up: cold boot after a firmware/index upgrade, or a tail
* cleared by a delete). The in-memory repop alone is lost on
* reboot, so without this commit the first list refresh after
* EVERY boot re-reads every newest message file. MUST be called
* OUTSIDE the LVGL lock (UIManager::update() calls it after
* draining drops, before mark-read, so all three land in one
* sensible order — each save_index() rewrites the whole index).
*/
void flush_pending_index_commit();
/**
* Set callback for conversation selection
* @param callback Function to call when conversation is selected
*/
void set_conversation_selected_callback(ConversationSelectedCallback callback);
/**
* Set callback for compose (envelope icon in bottom nav)
* @param callback Function to call when compose is requested
*/
void set_compose_callback(ComposeCallback callback);
/**
* Set callback for sync button
* @param callback Function to call when sync button is pressed
*/
void set_sync_callback(SyncCallback callback);
/**
* Set callback for settings button
* @param callback Function to call when settings button is pressed
*/
void set_home_callback(HomeCallback callback);
void set_peers_callback(PeersCallback callback) { _peers_callback = std::move(callback); }
/**
* Show the screen
*/
void show();
/**
* Hide the screen
*/
void hide();
/**
* Get the root LVGL object
* @return Root object
*/
lv_obj_t* get_object();
/**
* Update status indicators (WiFi RSSI, LoRa RSSI, and battery)
* Call periodically from main loop
*/
void update_status();
/**
* Set LoRa interface for RSSI display
* @param iface LoRa interface implementation
*/
void set_lora_interface(RNS::Interface* iface) { _lora_interface = iface; }
/**
* Set BLE interface for connection count display
* @param iface BLE interface implementation
*/
void set_ble_interface(RNS::Interface* iface) { _ble_interface = iface; }
/**
* Set GPS for satellite count display
*
* Also binds a custom $GPGSV field-3 ("satellites in view") parser
* to the same TinyGPSPlus instance so the top bar can show
* "in-view but not yet locked" status (yellow "?N") before the
* fix has any locked satellites.
*
* @param gps TinyGPSPlus instance
*/
void set_gps(TinyGPSPlus* gps) {
_gps = gps;
if (gps) {
// GSV sentence: $GPGSV,<total_sentences>,<sentence_num>,
// <total_in_view>,<sat1_id>,...
// Field 3 is the count of satellites visible to the receiver.
_gps_in_view.begin(*gps, "GPGSV", 3);
}
}
private:
lv_obj_t* _screen;
lv_obj_t* _header;
lv_obj_t* _list;
lv_obj_t* _bottom_nav;
lv_obj_t* _btn_new;
lv_obj_t* _btn_home;
lv_obj_t* _btn_compose;
lv_obj_t* _btn_peers;
bool _visible = false;
lv_obj_t* _label_wifi;
lv_obj_t* _label_lora;
lv_obj_t* _label_gps;
lv_obj_t* _label_ble;
lv_obj_t* _battery_container;
lv_obj_t* _label_battery_icon;
lv_obj_t* _label_battery_pct;
RNS::Interface* _lora_interface;
RNS::Interface* _ble_interface;
TinyGPSPlus* _gps;
TinyGPSCustom _gps_in_view; // $GPGSV field 3 — satellites in view
::LXMF::MessageStore* _message_store;
std::vector<ConversationItem> _conversations;
std::vector<lv_obj_t*> _conversation_containers; // For focus group management
std::vector<lv_obj_t*> _badge_pool; // Unread badge per row (nullptr when none); index-aligned with _conversation_containers
std::vector<RNS::Bytes> _peer_hash_pool; // Object pool to avoid per-item allocations
RNS::Bytes _pending_delete_hash; // Hash of conversation pending deletion
bool _has_unresolved_names = false; // True if any conversation shows hash instead of name
// Display-name write-throughs deferred out of refresh() (which runs under
// the LVGL lock). set_display_name() hits microStore/LittleFS; doing it
// under the lock stalls the render task on a cold-boot announce burst.
// Drained by UIManager::update() before it takes the lock.
std::vector<std::pair<RNS::Bytes, std::string>> _pending_name_writes;
// Mark-read requests deferred the same way: the LVGL click handler
// only queues the hash; UIManager::update() commits it (LittleFS
// index write) before taking the lock.
std::vector<RNS::Bytes> _pending_mark_reads;
// Corrupt/unreadable messages queued for deletion (one per unreadable
// tail walk, bounded per refresh); UIManager::update() drops them
// before taking the lock so the index converges to the newest
// readable message.
std::vector<RNS::Bytes> _pending_drops;
// Set by refresh() when at least one conversation fell back to
// load_message_metadata() this boot (unpopulated preview cache:
// cold boot on a pre-c8d3156-generation index, or a tail cleared by
// delete). flush_pending_index_commit() persists the repop once so
// the next boot serves previews straight from the index.
bool _index_commit_pending = false;
// Guards _pending_name_writes / _pending_mark_reads / _pending_drops
// / _index_commit_pending: producers run under the LVGL lock (LVGL
// task: click handlers, refresh() during navigation) while the
// flush_*() consumers run on the main loop WITHOUT the LVGL lock
// (their store I/O must not run under it). Critical sections hold
// no LVGL lock, no store I/O, and never block — a brief vector
// swap/push — so portMAX_DELAY acquisition cannot deadlock or stall
// a task. NULL (allocation failure) means every guarded site fails
// closed: queue mutation is refused and the affected features
// degrade rather than run unsynchronized.
SemaphoreHandle_t _queue_mutex = nullptr;
ConversationSelectedCallback _conversation_selected_callback;
ComposeCallback _compose_callback;
SyncCallback _sync_callback;
HomeCallback _home_callback;
PeersCallback _peers_callback;
// UI construction
void create_header();
void create_list();
void create_bottom_nav();
void create_conversation_item(const ConversationItem& item);
// Event handlers
static void on_conversation_clicked(lv_event_t* event);
static void on_conversation_long_pressed(lv_event_t* event);
static void on_delete_confirmed(lv_event_t* event);
static void on_sync_clicked(lv_event_t* event);
static void on_bottom_nav_clicked(lv_event_t* event);
static void msgbox_close_cb(lv_event_t* event);
// Utility
static String format_timestamp(uint32_t timestamp);
static String truncate_hash(const RNS::Bytes& hash);
static String parse_display_name(const RNS::Bytes& app_data);
};
} // namespace LXMF
} // namespace UI
#endif // ARDUINO
#endif // UI_LXMF_CONVERSATIONLISTSCREEN_H