esubghz_chat_chat_input.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 void chat_input_cb(void *context)
  9. {
  10. furi_assert(context);
  11. ESubGhzChatState* state = context;
  12. /* no message, just switch to the text box view */
  13. #ifdef FW_ORIGIN_Official
  14. if (strcmp(state->text_input_store, " ") == 0) {
  15. #else /* FW_ORIGIN_Official */
  16. if (strlen(state->text_input_store) == 0) {
  17. #endif /* FW_ORIGIN_Official */
  18. scene_manager_handle_custom_event(state->scene_manager,
  19. ESubGhzChatEvent_MsgEntered);
  20. return;
  21. }
  22. /* concatenate the name prefix and the actual message */
  23. furi_string_set(state->msg_input, state->name_prefix);
  24. furi_string_cat_str(state->msg_input, ": ");
  25. furi_string_cat_str(state->msg_input, state->text_input_store);
  26. /* append the message to the chat box */
  27. furi_string_cat_printf(state->chat_box_store, "\n%s",
  28. furi_string_get_cstr(state->msg_input));
  29. /* encrypt and transmit message */
  30. tx_msg_input(state);
  31. /* clear message input buffer */
  32. furi_string_set_char(state->msg_input, 0, 0);
  33. /* switch to text box view */
  34. scene_manager_handle_custom_event(state->scene_manager,
  35. ESubGhzChatEvent_MsgEntered);
  36. }
  37. /* Prepares the message input scene. */
  38. void scene_on_enter_chat_input(void* context)
  39. {
  40. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  41. furi_assert(context);
  42. ESubGhzChatState* state = context;
  43. state->text_input_store[0] = 0;
  44. text_input_reset(state->text_input);
  45. text_input_set_result_callback(
  46. state->text_input,
  47. chat_input_cb,
  48. state,
  49. state->text_input_store,
  50. sizeof(state->text_input_store),
  51. true);
  52. text_input_set_validator(
  53. state->text_input,
  54. NULL,
  55. NULL);
  56. text_input_set_header_text(
  57. state->text_input,
  58. #ifdef FW_ORIGIN_Official
  59. "Message (space for none)");
  60. #else /* FW_ORIGIN_Official */
  61. "Message");
  62. text_input_set_minimum_length(state->text_input, 0);
  63. #endif /* FW_ORIGIN_Official */
  64. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  65. }
  66. /* Handles scene manager events for the message input scene. */
  67. bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
  68. {
  69. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  70. furi_assert(context);
  71. ESubGhzChatState* state = context;
  72. bool consumed = false;
  73. switch(event.type) {
  74. case SceneManagerEventTypeCustom:
  75. switch(event.event) {
  76. /* switch to text box scene */
  77. case ESubGhzChatEvent_MsgEntered:
  78. scene_manager_next_scene(state->scene_manager,
  79. ESubGhzChatScene_ChatBox);
  80. consumed = true;
  81. break;
  82. }
  83. break;
  84. case SceneManagerEventTypeBack:
  85. /* stop the application if the user presses back here */
  86. view_dispatcher_stop(state->view_dispatcher);
  87. consumed = true;
  88. break;
  89. default:
  90. consumed = false;
  91. break;
  92. }
  93. return consumed;
  94. }
  95. /* Cleans up the password input scene. */
  96. void scene_on_exit_chat_input(void* context)
  97. {
  98. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  99. furi_assert(context);
  100. ESubGhzChatState* state = context;
  101. text_input_reset(state->text_input);
  102. }