Просмотр исходного кода

Add a message preview in the chat input

twisted_pear 2 лет назад
Родитель
Сommit
ba72937d7b
3 измененных файлов с 42 добавлено и 16 удалено
  1. 33 10
      esubghz_chat.c
  2. 6 0
      esubghz_chat_i.h
  3. 3 6
      scenes/esubghz_chat_chat_input.c

+ 33 - 10
esubghz_chat.c

@@ -23,6 +23,36 @@ static void have_read_cb(void* context)
 	state->last_time_rx_data = furi_get_tick();
 }
 
+/* Sets the header for the chat input field depending on whether or not a
+ * message preview exists. */
+void set_chat_input_header(ESubGhzChatState *state)
+{
+	if (strlen(state->msg_preview) == 0) {
+		text_input_set_header_text(state->text_input, "Message");
+	} else {
+		text_input_set_header_text(state->text_input,
+				state->msg_preview);
+	}
+}
+
+/* Appends the latest message to the chat box and prepares the message preview.
+ */
+void append_msg(ESubGhzChatState *state, const char *msg)
+{
+	/* append message to text box */
+	furi_string_cat_printf(state->chat_box_store, "\n%s", msg);
+
+	/* prepare message preview */
+	strncpy(state->msg_preview, msg, MSG_PREVIEW_SIZE);
+	state->msg_preview[MSG_PREVIEW_SIZE] = 0;
+	set_chat_input_header(state);
+
+	/* reset text box contents and focus */
+	text_box_set_text(state->chat_box,
+			furi_string_get_cstr(state->chat_box_store));
+	text_box_set_focus(state->chat_box, TextBoxFocusEnd);
+}
+
 /* Decrypts a message for post_rx(). */
 static bool post_rx_decrypt(ESubGhzChatState *state, size_t rx_size)
 {
@@ -39,8 +69,7 @@ static bool post_rx_decrypt(ESubGhzChatState *state, size_t rx_size)
 	return ret;
 }
 
-/* Post RX handler, decrypts received messages, displays them in the text box
- * and sends a notification. */
+/* Post RX handler, decrypts received messages and calls append_msg(). */
 static void post_rx(ESubGhzChatState *state, size_t rx_size)
 {
 	furi_assert(state);
@@ -68,17 +97,11 @@ static void post_rx(ESubGhzChatState *state, size_t rx_size)
 		}
 	}
 
-	/* append message to text box */
-	furi_string_cat_printf(state->chat_box_store, "\n%s",
-			state->rx_str_buffer);
+	/* append message to text box and prepare message preview */
+	append_msg(state, state->rx_str_buffer);
 
 	/* send notification (make the flipper vibrate) */
 	notification_message(state->notification, &sequence_single_vibro);
-
-	/* reset text box contents and focus */
-	text_box_set_text(state->chat_box,
-			furi_string_get_cstr(state->chat_box_store));
-	text_box_set_focus(state->chat_box, TextBoxFocusEnd);
 }
 
 /* Reads the message from msg_input, encrypts it if necessary and then

+ 6 - 0
esubghz_chat_i.h

@@ -30,6 +30,7 @@
 
 #define CHAT_BOX_STORE_SIZE 4096
 #define TEXT_INPUT_STORE_SIZE 256
+#define MSG_PREVIEW_SIZE 32
 
 #define KEY_HEX_STR_SIZE ((KEY_BITS / 8) * 3)
 
@@ -63,6 +64,9 @@ typedef struct {
 	FuriString *name_prefix;
 	FuriString *msg_input;
 
+	// message preview
+	char msg_preview[MSG_PREVIEW_SIZE + 1];
+
 	// encryption
 	bool encrypted;
 	ESubGhzChatCryptoCtx *crypto_ctx;
@@ -113,5 +117,7 @@ typedef enum {
 	ESubGhzChatView_NfcPopup,
 } ESubGhzChatView;
 
+void set_chat_input_header(ESubGhzChatState *state);
+void append_msg(ESubGhzChatState *state, const char *msg);
 void tx_msg_input(ESubGhzChatState *state);
 void enter_chat(ESubGhzChatState *state);

+ 3 - 6
scenes/esubghz_chat_chat_input.c

@@ -26,9 +26,8 @@ static bool chat_input_validator(const char *text, FuriString *error,
 	furi_string_cat_str(state->msg_input, ": ");
 	furi_string_cat_str(state->msg_input, text);
 
-	/* append the message to the chat box */
-	furi_string_cat_printf(state->chat_box_store, "\n%s",
-		furi_string_get_cstr(state->msg_input));
+	/* append the message to the chat box and prepare message preview */
+	append_msg(state, furi_string_get_cstr(state->msg_input));
 
 	/* encrypt and transmit message */
 	tx_msg_input(state);
@@ -66,9 +65,7 @@ void scene_on_enter_chat_input(void* context)
 			state->text_input,
 			chat_input_validator,
 			state);
-	text_input_set_header_text(
-			state->text_input,
-			"Message");
+	set_chat_input_header(state);
 
 	view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
 }