esubghz_chat_chat_input.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, void* context) {
  9. UNUSED(error);
  10. furi_assert(context);
  11. ESubGhzChatState* state = context;
  12. /* no message, just switch to the text box view */
  13. if(strlen(text) == 0) {
  14. view_dispatcher_send_custom_event(state->view_dispatcher, ESubGhzChatEvent_MsgEntered);
  15. return true;
  16. }
  17. /* concatenate the name prefix and the actual message */
  18. furi_string_set(state->msg_input, state->name_prefix);
  19. furi_string_cat_str(state->msg_input, ": ");
  20. furi_string_cat_str(state->msg_input, text);
  21. /* append the message to the chat box and prepare message preview */
  22. append_msg(state, furi_string_get_cstr(state->msg_input));
  23. /* encrypt and transmit message */
  24. tx_msg_input(state);
  25. /* clear message input buffer */
  26. furi_string_set_char(state->msg_input, 0, 0);
  27. /* switch to text box view */
  28. view_dispatcher_send_custom_event(state->view_dispatcher, ESubGhzChatEvent_MsgEntered);
  29. return true;
  30. }
  31. /* Prepares the message input scene. */
  32. void scene_on_enter_chat_input(void* context) {
  33. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  34. furi_assert(context);
  35. ESubGhzChatState* state = context;
  36. state->text_input_store[0] = 0;
  37. text_input_reset(state->text_input);
  38. /* use validator for scene change to get around minimum length
  39. * requirement */
  40. text_input_set_result_callback(
  41. state->text_input,
  42. NULL,
  43. NULL,
  44. state->text_input_store,
  45. sizeof(state->text_input_store),
  46. true);
  47. text_input_set_validator(state->text_input, chat_input_validator, state);
  48. set_chat_input_header(state);
  49. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  50. }
  51. /* Handles scene manager events for the message input scene. */
  52. bool scene_on_event_chat_input(void* context, SceneManagerEvent event) {
  53. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  54. furi_assert(context);
  55. ESubGhzChatState* state = context;
  56. bool consumed = false;
  57. switch(event.type) {
  58. case SceneManagerEventTypeCustom:
  59. switch(event.event) {
  60. /* switch to text box scene */
  61. case ESubGhzChatEvent_MsgEntered:
  62. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_ChatBox);
  63. consumed = true;
  64. break;
  65. }
  66. break;
  67. case SceneManagerEventTypeBack:
  68. /* stop the application if the user presses back here */
  69. view_dispatcher_stop(state->view_dispatcher);
  70. consumed = true;
  71. break;
  72. default:
  73. consumed = false;
  74. break;
  75. }
  76. return consumed;
  77. }
  78. /* Cleans up the password input scene. */
  79. void scene_on_exit_chat_input(void* context) {
  80. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  81. furi_assert(context);
  82. ESubGhzChatState* state = context;
  83. text_input_reset(state->text_input);
  84. }