flip_social_draw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include "flip_social_draw.h"
  2. Action action = ActionNone;
  3. bool flip_social_board_is_active(Canvas *canvas)
  4. {
  5. if (fhttp.state == INACTIVE)
  6. {
  7. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  8. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  9. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  10. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  11. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  12. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  13. return false;
  14. }
  15. return true;
  16. }
  17. void on_input(const void *event, void *ctx)
  18. {
  19. UNUSED(ctx);
  20. InputKey key = ((InputEvent *)event)->key;
  21. InputType type = ((InputEvent *)event)->type;
  22. if (type != InputTypeRelease)
  23. {
  24. return;
  25. }
  26. switch (key)
  27. {
  28. case InputKeyOk:
  29. action = ActionFlip;
  30. break;
  31. case InputKeyBack:
  32. action = ActionBack;
  33. break;
  34. case InputKeyRight:
  35. action = ActionNext;
  36. break;
  37. case InputKeyLeft:
  38. action = ActionPrev;
  39. break;
  40. case InputKeyUp:
  41. action = ActionPrev;
  42. break;
  43. case InputKeyDown:
  44. action = ActionNext;
  45. break;
  46. default:
  47. action = ActionNone;
  48. break;
  49. }
  50. }
  51. // Function to draw the message on the canvas with word wrapping
  52. void draw_user_message(Canvas *canvas, const char *user_message, int x, int y)
  53. {
  54. if (user_message == NULL)
  55. {
  56. FURI_LOG_E(TAG, "User message is NULL.");
  57. return;
  58. }
  59. size_t msg_length = strlen(user_message);
  60. size_t start = 0;
  61. int line_num = 0;
  62. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  63. while (start < msg_length && line_num < 4)
  64. {
  65. size_t remaining = msg_length - start;
  66. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  67. if (remaining > MAX_LINE_LENGTH)
  68. {
  69. // Find the last space within the first 'len' characters
  70. size_t last_space = len;
  71. while (last_space > 0 && user_message[start + last_space - 1] != ' ')
  72. {
  73. last_space--;
  74. }
  75. if (last_space > 0)
  76. {
  77. len = last_space; // Adjust len to the position of the last space
  78. }
  79. }
  80. // Copy the substring to 'line' and null-terminate it
  81. memcpy(line, user_message + start, len);
  82. line[len] = '\0'; // Ensure the string is null-terminated
  83. // Draw the string on the canvas
  84. // Adjust the y-coordinate based on the line number
  85. canvas_draw_str_aligned(canvas, x, y + line_num * 10, AlignLeft, AlignTop, line);
  86. // Update the start position for the next line
  87. start += len;
  88. // Skip any spaces to avoid leading spaces on the next line
  89. while (start < msg_length && user_message[start] == ' ')
  90. {
  91. start++;
  92. }
  93. // Increment the line number
  94. line_num++;
  95. }
  96. }
  97. void flip_social_callback_draw_compose(Canvas *canvas, void *model)
  98. {
  99. UNUSED(model);
  100. if (!canvas)
  101. {
  102. FURI_LOG_E(TAG, "Canvas is NULL");
  103. return;
  104. }
  105. if (!app_instance)
  106. {
  107. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  108. return;
  109. }
  110. if (!selected_message)
  111. {
  112. FURI_LOG_E(TAG, "Selected message is NULL");
  113. return;
  114. }
  115. if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
  116. {
  117. FURI_LOG_E(TAG, "Message is too long");
  118. return;
  119. }
  120. if (!flip_social_dialog_shown)
  121. {
  122. flip_social_dialog_shown = true;
  123. app_instance->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  124. app_instance->input_event = furi_pubsub_subscribe(app_instance->input_event_queue, on_input, NULL);
  125. auth_headers_alloc();
  126. }
  127. draw_user_message(canvas, selected_message, 0, 2);
  128. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  129. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  130. canvas_draw_icon(canvas, 52, 53, &I_ButtonBACK_10x8);
  131. canvas_draw_str_aligned(canvas, 64, 54, AlignLeft, AlignTop, "Back");
  132. canvas_draw_icon(canvas, 100, 53, &I_ButtonRight_4x7);
  133. canvas_draw_str_aligned(canvas, 107, 54, AlignLeft, AlignTop, "Post");
  134. // handle action
  135. switch (action)
  136. {
  137. case ActionNone:
  138. break;
  139. case ActionBack:
  140. flip_social_dialog_stop = true;
  141. break;
  142. case ActionNext:
  143. // send selected_message
  144. if (selected_message && app_instance->login_username_logged_in)
  145. {
  146. if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
  147. {
  148. FURI_LOG_E(TAG, "Message is too long");
  149. return;
  150. }
  151. // Send the selected_message
  152. char command[256];
  153. snprintf(command, sizeof(command), "{\"username\":\"%s\",\"content\":\"%s\"}",
  154. app_instance->login_username_logged_in, selected_message);
  155. if (!flipper_http_post_request_with_headers(
  156. "https://www.flipsocial.net/api/feed/post/",
  157. auth_headers,
  158. command))
  159. {
  160. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  161. fhttp.state = ISSUE;
  162. return;
  163. }
  164. fhttp.state = RECEIVING;
  165. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  166. }
  167. else
  168. {
  169. FURI_LOG_E(TAG, "Message or username is NULL");
  170. return;
  171. }
  172. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  173. {
  174. // Wait for the feed to be received
  175. furi_delay_ms(100);
  176. // Draw the resulting string on the canvas
  177. canvas_draw_str(canvas, 0, 30, "Receiving..");
  178. }
  179. flip_social_dialog_stop = true;
  180. furi_timer_stop(fhttp.get_timeout_timer);
  181. break;
  182. case ActionPrev:
  183. // delete message
  184. app_instance->pre_saved_messages.messages[app_instance->pre_saved_messages.index] = NULL;
  185. for (uint32_t i = app_instance->pre_saved_messages.index; i < app_instance->pre_saved_messages.count - 1; i++)
  186. {
  187. app_instance->pre_saved_messages.messages[i] = app_instance->pre_saved_messages.messages[i + 1];
  188. }
  189. app_instance->pre_saved_messages.count--;
  190. // add the item to the submenu
  191. submenu_reset(app_instance->submenu_compose);
  192. submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app_instance);
  193. for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  194. {
  195. submenu_add_item(app_instance->submenu_compose, app_instance->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  196. }
  197. // save playlist
  198. save_playlist(&app_instance->pre_saved_messages);
  199. flip_social_dialog_stop = true;
  200. break;
  201. default:
  202. action = ActionNone;
  203. break;
  204. }
  205. if (flip_social_dialog_stop)
  206. {
  207. furi_pubsub_unsubscribe(app_instance->input_event_queue, app_instance->input_event);
  208. flip_social_dialog_shown = false;
  209. flip_social_dialog_stop = false;
  210. if (action == ActionNext)
  211. {
  212. canvas_clear(canvas);
  213. canvas_draw_str(canvas, 0, 10, "Sent successfully!");
  214. canvas_draw_str(canvas, 0, 50, "Loading feed :D");
  215. canvas_draw_str(canvas, 0, 60, "Please wait...");
  216. action = ActionNone;
  217. if (!flip_social_load_initial_feed())
  218. {
  219. FURI_LOG_E(TAG, "Failed to load initial feed");
  220. return;
  221. }
  222. }
  223. else if (action == ActionBack)
  224. {
  225. action = ActionNone;
  226. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  227. }
  228. else
  229. {
  230. action = ActionNone;
  231. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInCompose);
  232. }
  233. }
  234. }
  235. // function to draw the dialog canvas
  236. void flip_social_canvas_draw_message(Canvas *canvas, char *user_username, char *user_message, bool is_flipped, bool show_prev, bool show_next, int flip_count)
  237. {
  238. canvas_set_color(canvas, ColorBlack);
  239. canvas_set_font(canvas, FontPrimary);
  240. canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignCenter, user_username);
  241. canvas_set_font(canvas, FontSecondary);
  242. char flip_count_str[12];
  243. if (flip_count == 1)
  244. {
  245. snprintf(flip_count_str, sizeof(flip_count_str), "%d Flip", flip_count);
  246. canvas_draw_str_aligned(canvas, 106, 54, AlignLeft, AlignTop, flip_count_str);
  247. }
  248. else
  249. {
  250. snprintf(flip_count_str, sizeof(flip_count_str), "%d Flips", flip_count);
  251. if (flip_count < 10)
  252. {
  253. canvas_draw_str_aligned(canvas, 100, 54, AlignLeft, AlignTop, flip_count_str);
  254. }
  255. else if (flip_count < 100)
  256. {
  257. canvas_draw_str_aligned(canvas, 94, 54, AlignLeft, AlignTop, flip_count_str);
  258. }
  259. else
  260. {
  261. canvas_draw_str_aligned(canvas, 88, 54, AlignLeft, AlignTop, flip_count_str);
  262. }
  263. }
  264. draw_user_message(canvas, user_message, 0, 12);
  265. // combine and shift icons/labels around if not show_prev or show_next
  266. if (show_prev && show_next && !is_flipped)
  267. {
  268. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  269. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  270. canvas_draw_icon(canvas, 30, 54, &I_ButtonRight_4x7);
  271. canvas_draw_str_aligned(canvas, 36, 54, AlignLeft, AlignTop, "Next");
  272. canvas_draw_icon(canvas, 58, 54, &I_ButtonOK_7x7);
  273. canvas_draw_str_aligned(canvas, 67, 54, AlignLeft, AlignTop, "Flip");
  274. }
  275. else if (show_prev && !show_next && !is_flipped)
  276. {
  277. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  278. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  279. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  280. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "Flip");
  281. }
  282. else if (!show_prev && show_next && !is_flipped)
  283. {
  284. canvas_draw_icon(canvas, 0, 54, &I_ButtonRight_4x7);
  285. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Next");
  286. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  287. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "Flip");
  288. }
  289. else if (show_prev && show_next && is_flipped)
  290. {
  291. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  292. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  293. canvas_draw_icon(canvas, 28, 54, &I_ButtonRight_4x7);
  294. canvas_draw_str_aligned(canvas, 34, 54, AlignLeft, AlignTop, "Next");
  295. canvas_draw_icon(canvas, 54, 54, &I_ButtonOK_7x7);
  296. canvas_draw_str_aligned(canvas, 63, 54, AlignLeft, AlignTop, "UnFlip");
  297. }
  298. else if (show_prev && !show_next && is_flipped)
  299. {
  300. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  301. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  302. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  303. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "UnFlip");
  304. }
  305. else if (!show_prev && show_next && is_flipped)
  306. {
  307. canvas_draw_icon(canvas, 0, 54, &I_ButtonRight_4x7);
  308. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Next");
  309. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  310. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "UnFlip");
  311. }
  312. else if (!show_prev && !show_next && is_flipped)
  313. {
  314. canvas_draw_icon(canvas, 0, 54, &I_ButtonOK_7x7);
  315. canvas_draw_str_aligned(canvas, 9, 54, AlignLeft, AlignTop, "UnFlip");
  316. }
  317. else
  318. {
  319. canvas_draw_icon(canvas, 0, 54, &I_ButtonOK_7x7);
  320. canvas_draw_str_aligned(canvas, 9, 54, AlignLeft, AlignTop, "Flip");
  321. }
  322. }
  323. // Callback function to handle the feed dialog
  324. void flip_social_callback_draw_feed(Canvas *canvas, void *model)
  325. {
  326. UNUSED(model);
  327. if (!canvas)
  328. {
  329. FURI_LOG_E(TAG, "Canvas is NULL");
  330. return;
  331. }
  332. if (!app_instance)
  333. {
  334. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  335. return;
  336. }
  337. if (!flip_social_dialog_shown)
  338. {
  339. flip_social_dialog_shown = true;
  340. app_instance->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  341. app_instance->input_event = furi_pubsub_subscribe(app_instance->input_event_queue, on_input, NULL);
  342. auth_headers_alloc();
  343. }
  344. // handle action
  345. switch (action)
  346. {
  347. case ActionNone:
  348. flip_social_canvas_draw_message(canvas, flip_feed_item->username, flip_feed_item->message, flip_feed_item->is_flipped, flip_feed_info->index > 0, flip_feed_info->index < flip_feed_info->count - 1, flip_feed_item->flips);
  349. break;
  350. case ActionNext:
  351. canvas_clear(canvas);
  352. if (flip_feed_info->index < flip_feed_info->count - 1)
  353. {
  354. flip_feed_info->index++;
  355. }
  356. // load the next feed item
  357. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  358. {
  359. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  360. fhttp.state = ISSUE;
  361. return;
  362. }
  363. flip_social_canvas_draw_message(canvas, flip_feed_item->username, flip_feed_item->message, flip_feed_item->is_flipped, flip_feed_info->index > 0, flip_feed_info->index < flip_feed_info->count - 1, flip_feed_item->flips);
  364. action = ActionNone;
  365. break;
  366. case ActionPrev:
  367. canvas_clear(canvas);
  368. if (flip_feed_info->index > 0)
  369. {
  370. flip_feed_info->index--;
  371. }
  372. // load the previous feed item
  373. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  374. {
  375. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  376. fhttp.state = ISSUE;
  377. return;
  378. }
  379. flip_social_canvas_draw_message(canvas, flip_feed_item->username, flip_feed_item->message, flip_feed_item->is_flipped, flip_feed_info->index > 0, flip_feed_info->index < flip_feed_info->count - 1, flip_feed_item->flips);
  380. action = ActionNone;
  381. break;
  382. case ActionFlip:
  383. canvas_clear(canvas);
  384. // Moved to above the is_flipped check
  385. if (!flip_feed_item->is_flipped)
  386. {
  387. // increase the flip count
  388. flip_feed_item->flips++;
  389. }
  390. else
  391. {
  392. // decrease the flip count
  393. flip_feed_item->flips--;
  394. }
  395. // change the flip status
  396. flip_feed_item->is_flipped = !flip_feed_item->is_flipped;
  397. // send post request to flip the message
  398. if (app_instance->login_username_logged_in == NULL)
  399. {
  400. FURI_LOG_E(TAG, "Username is NULL");
  401. return;
  402. }
  403. char payload[256];
  404. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%u\"}", app_instance->login_username_logged_in, flip_feed_item->id);
  405. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", auth_headers, payload);
  406. flip_social_canvas_draw_message(canvas, flip_feed_item->username, flip_feed_item->message, flip_feed_item->is_flipped, flip_feed_info->index > 0, flip_feed_info->index < flip_feed_info->count - 1, flip_feed_item->flips);
  407. action = ActionNone;
  408. break;
  409. case ActionBack:
  410. canvas_clear(canvas);
  411. flip_social_dialog_stop = true;
  412. flip_feed_info->index = 0;
  413. action = ActionNone;
  414. break;
  415. default:
  416. break;
  417. }
  418. if (flip_social_dialog_stop)
  419. {
  420. furi_pubsub_unsubscribe(app_instance->input_event_queue, app_instance->input_event);
  421. flip_social_dialog_shown = false;
  422. flip_social_dialog_stop = false;
  423. action = ActionNone;
  424. }
  425. }