esubghz_chat_chat_input.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "../esubghz_chat_i.h"
  2. /* If no message was entred this simply emits a MsgEntered event to the scene
  3. * manager to switch to the text box. If a message was entered it is appended
  4. * to the name string. The result is encrypted, if encryption is enabled, and
  5. * then copied into the TX buffer. The contents of the TX buffer are then
  6. * transmitted. The sent message is appended to the text box and a MsgEntered
  7. * event is sent to the scene manager to switch to the text box view. */
  8. static bool chat_input_validator(const char *text, FuriString *error,
  9. void *context)
  10. {
  11. UNUSED(error);
  12. furi_assert(context);
  13. ESubGhzChatState* state = context;
  14. /* no message, just switch to the text box view */
  15. if (strlen(text) == 0) {
  16. view_dispatcher_send_custom_event(state->view_dispatcher,
  17. ESubGhzChatEvent_MsgEntered);
  18. return true;
  19. }
  20. /* concatenate the name prefix and the actual message */
  21. furi_string_set(state->msg_input, state->name_prefix);
  22. furi_string_cat_str(state->msg_input, ": ");
  23. furi_string_cat_str(state->msg_input, text);
  24. /* append the message to the chat box */
  25. furi_string_cat_printf(state->chat_box_store, "\n%s",
  26. furi_string_get_cstr(state->msg_input));
  27. /* encrypt and transmit message */
  28. tx_msg_input(state);
  29. /* clear message input buffer */
  30. furi_string_set_char(state->msg_input, 0, 0);
  31. /* switch to text box view */
  32. view_dispatcher_send_custom_event(state->view_dispatcher,
  33. ESubGhzChatEvent_MsgEntered);
  34. return true;
  35. }
  36. /* Prepares the message input scene. */
  37. void scene_on_enter_chat_input(void* context)
  38. {
  39. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  40. furi_assert(context);
  41. ESubGhzChatState* state = context;
  42. state->text_input_store[0] = 0;
  43. text_input_reset(state->text_input);
  44. /* use validator for scene change to get around minimum length
  45. * requirement */
  46. text_input_set_result_callback(
  47. state->text_input,
  48. NULL,
  49. NULL,
  50. state->text_input_store,
  51. sizeof(state->text_input_store),
  52. true);
  53. text_input_set_validator(
  54. state->text_input,
  55. chat_input_validator,
  56. state);
  57. text_input_set_header_text(
  58. state->text_input,
  59. "Message");
  60. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  61. }
  62. /* Handles scene manager events for the message input scene. */
  63. bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
  64. {
  65. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  66. furi_assert(context);
  67. ESubGhzChatState* state = context;
  68. bool consumed = false;
  69. switch(event.type) {
  70. case SceneManagerEventTypeCustom:
  71. switch(event.event) {
  72. /* switch to text box scene */
  73. case ESubGhzChatEvent_MsgEntered:
  74. scene_manager_next_scene(state->scene_manager,
  75. ESubGhzChatScene_ChatBox);
  76. consumed = true;
  77. break;
  78. }
  79. break;
  80. case SceneManagerEventTypeBack:
  81. /* stop the application if the user presses back here */
  82. view_dispatcher_stop(state->view_dispatcher);
  83. consumed = true;
  84. break;
  85. default:
  86. consumed = false;
  87. break;
  88. }
  89. return consumed;
  90. }
  91. /* Cleans up the password input scene. */
  92. void scene_on_exit_chat_input(void* context)
  93. {
  94. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  95. furi_assert(context);
  96. ESubGhzChatState* state = context;
  97. text_input_reset(state->text_input);
  98. }