esubghz_chat_chat_input.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 and prepare message preview */
  25. append_msg(state, furi_string_get_cstr(state->msg_input));
  26. /* encrypt and transmit message */
  27. tx_msg_input(state);
  28. /* clear message input buffer */
  29. furi_string_set_char(state->msg_input, 0, 0);
  30. /* switch to text box view */
  31. view_dispatcher_send_custom_event(state->view_dispatcher,
  32. ESubGhzChatEvent_MsgEntered);
  33. return true;
  34. }
  35. /* Prepares the message input scene. */
  36. void scene_on_enter_chat_input(void* context)
  37. {
  38. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  39. furi_assert(context);
  40. ESubGhzChatState* state = context;
  41. state->text_input_store[0] = 0;
  42. text_input_reset(state->text_input);
  43. /* use validator for scene change to get around minimum length
  44. * requirement */
  45. text_input_set_result_callback(
  46. state->text_input,
  47. NULL,
  48. NULL,
  49. state->text_input_store,
  50. sizeof(state->text_input_store),
  51. true);
  52. text_input_set_validator(
  53. state->text_input,
  54. chat_input_validator,
  55. state);
  56. set_chat_input_header(state);
  57. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  58. }
  59. /* Handles scene manager events for the message input scene. */
  60. bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
  61. {
  62. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  63. furi_assert(context);
  64. ESubGhzChatState* state = context;
  65. bool consumed = false;
  66. switch(event.type) {
  67. case SceneManagerEventTypeCustom:
  68. switch(event.event) {
  69. /* switch to text box scene */
  70. case ESubGhzChatEvent_MsgEntered:
  71. scene_manager_next_scene(state->scene_manager,
  72. ESubGhzChatScene_ChatBox);
  73. consumed = true;
  74. break;
  75. }
  76. break;
  77. case SceneManagerEventTypeBack:
  78. /* stop the application if the user presses back here */
  79. view_dispatcher_stop(state->view_dispatcher);
  80. consumed = true;
  81. break;
  82. default:
  83. consumed = false;
  84. break;
  85. }
  86. return consumed;
  87. }
  88. /* Cleans up the password input scene. */
  89. void scene_on_exit_chat_input(void* context)
  90. {
  91. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  92. furi_assert(context);
  93. ESubGhzChatState* state = context;
  94. text_input_reset(state->text_input);
  95. }