flip_social_draw.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. // Send the user to the feed
  218. if (!easy_flipper_set_loading(&app_instance->loading, FlipSocialViewLoading, flip_social_callback_to_submenu_logged_in, &app_instance->view_dispatcher))
  219. {
  220. FURI_LOG_E(TAG, "Failed to set loading screen");
  221. return; // already on the submenu
  222. }
  223. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoading);
  224. if (flip_social_get_feed()) // start the async request
  225. {
  226. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  227. fhttp.state = RECEIVING;
  228. }
  229. else
  230. {
  231. FURI_LOG_E(HTTP_TAG, "Failed to send request");
  232. fhttp.state = ISSUE;
  233. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  234. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoading);
  235. loading_free(app_instance->loading);
  236. return;
  237. }
  238. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  239. {
  240. // Wait for the request to be received
  241. furi_delay_ms(100);
  242. }
  243. furi_timer_stop(fhttp.get_timeout_timer);
  244. // load feed info
  245. flip_feed_info = flip_social_parse_json_feed();
  246. if (!flip_feed_info || flip_feed_info->count < 1)
  247. {
  248. FURI_LOG_E(TAG, "Failed to parse JSON feed");
  249. fhttp.state = ISSUE;
  250. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  251. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoading);
  252. loading_free(app_instance->loading);
  253. return;
  254. }
  255. // load the current feed post
  256. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  257. {
  258. FURI_LOG_E(TAG, "Failed to load feed post");
  259. fhttp.state = ISSUE;
  260. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  261. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoading);
  262. loading_free(app_instance->loading);
  263. return;
  264. }
  265. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInFeed);
  266. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoading);
  267. loading_free(app_instance->loading);
  268. }
  269. else if (action == ActionBack)
  270. {
  271. action = ActionNone;
  272. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  273. }
  274. else
  275. {
  276. action = ActionNone;
  277. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInCompose);
  278. }
  279. }
  280. }
  281. // function to draw the dialog canvas
  282. 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)
  283. {
  284. canvas_set_color(canvas, ColorBlack);
  285. canvas_set_font(canvas, FontPrimary);
  286. canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignCenter, user_username);
  287. canvas_set_font(canvas, FontSecondary);
  288. char flip_count_str[12];
  289. if (flip_count == 1)
  290. {
  291. snprintf(flip_count_str, sizeof(flip_count_str), "%d Flip", flip_count);
  292. canvas_draw_str_aligned(canvas, 106, 54, AlignLeft, AlignTop, flip_count_str);
  293. }
  294. else
  295. {
  296. snprintf(flip_count_str, sizeof(flip_count_str), "%d Flips", flip_count);
  297. if (flip_count < 10)
  298. {
  299. canvas_draw_str_aligned(canvas, 100, 54, AlignLeft, AlignTop, flip_count_str);
  300. }
  301. else if (flip_count < 100)
  302. {
  303. canvas_draw_str_aligned(canvas, 94, 54, AlignLeft, AlignTop, flip_count_str);
  304. }
  305. else
  306. {
  307. canvas_draw_str_aligned(canvas, 88, 54, AlignLeft, AlignTop, flip_count_str);
  308. }
  309. }
  310. draw_user_message(canvas, user_message, 0, 12);
  311. // combine and shift icons/labels around if not show_prev or show_next
  312. if (show_prev && show_next && !is_flipped)
  313. {
  314. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  315. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  316. canvas_draw_icon(canvas, 30, 54, &I_ButtonRight_4x7);
  317. canvas_draw_str_aligned(canvas, 36, 54, AlignLeft, AlignTop, "Next");
  318. canvas_draw_icon(canvas, 58, 54, &I_ButtonOK_7x7);
  319. canvas_draw_str_aligned(canvas, 67, 54, AlignLeft, AlignTop, "Flip");
  320. }
  321. else if (show_prev && !show_next && !is_flipped)
  322. {
  323. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  324. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  325. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  326. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "Flip");
  327. }
  328. else if (!show_prev && show_next && !is_flipped)
  329. {
  330. canvas_draw_icon(canvas, 0, 54, &I_ButtonRight_4x7);
  331. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Next");
  332. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  333. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "Flip");
  334. }
  335. else if (show_prev && show_next && is_flipped)
  336. {
  337. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  338. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  339. canvas_draw_icon(canvas, 28, 54, &I_ButtonRight_4x7);
  340. canvas_draw_str_aligned(canvas, 34, 54, AlignLeft, AlignTop, "Next");
  341. canvas_draw_icon(canvas, 54, 54, &I_ButtonOK_7x7);
  342. canvas_draw_str_aligned(canvas, 63, 54, AlignLeft, AlignTop, "UnFlip");
  343. }
  344. else if (show_prev && !show_next && is_flipped)
  345. {
  346. canvas_draw_icon(canvas, 0, 54, &I_ButtonLeft_4x7);
  347. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Prev");
  348. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  349. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "UnFlip");
  350. }
  351. else if (!show_prev && show_next && is_flipped)
  352. {
  353. canvas_draw_icon(canvas, 0, 54, &I_ButtonRight_4x7);
  354. canvas_draw_str_aligned(canvas, 6, 54, AlignLeft, AlignTop, "Next");
  355. canvas_draw_icon(canvas, 28, 54, &I_ButtonOK_7x7);
  356. canvas_draw_str_aligned(canvas, 37, 54, AlignLeft, AlignTop, "UnFlip");
  357. }
  358. else if (!show_prev && !show_next && is_flipped)
  359. {
  360. canvas_draw_icon(canvas, 0, 54, &I_ButtonOK_7x7);
  361. canvas_draw_str_aligned(canvas, 9, 54, AlignLeft, AlignTop, "UnFlip");
  362. }
  363. else
  364. {
  365. canvas_draw_icon(canvas, 0, 54, &I_ButtonOK_7x7);
  366. canvas_draw_str_aligned(canvas, 9, 54, AlignLeft, AlignTop, "Flip");
  367. }
  368. }
  369. // Callback function to handle the feed dialog
  370. void flip_social_callback_draw_feed(Canvas *canvas, void *model)
  371. {
  372. UNUSED(model);
  373. if (!canvas)
  374. {
  375. FURI_LOG_E(TAG, "Canvas is NULL");
  376. return;
  377. }
  378. if (!app_instance)
  379. {
  380. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  381. return;
  382. }
  383. if (!flip_social_dialog_shown)
  384. {
  385. flip_social_dialog_shown = true;
  386. app_instance->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  387. app_instance->input_event = furi_pubsub_subscribe(app_instance->input_event_queue, on_input, NULL);
  388. auth_headers_alloc();
  389. }
  390. // handle action
  391. switch (action)
  392. {
  393. case ActionNone:
  394. 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);
  395. break;
  396. case ActionNext:
  397. canvas_clear(canvas);
  398. if (flip_feed_info->index < flip_feed_info->count - 1)
  399. {
  400. flip_feed_info->index++;
  401. }
  402. // load the next feed item
  403. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  404. {
  405. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  406. fhttp.state = ISSUE;
  407. return;
  408. }
  409. 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);
  410. action = ActionNone;
  411. break;
  412. case ActionPrev:
  413. canvas_clear(canvas);
  414. if (flip_feed_info->index > 0)
  415. {
  416. flip_feed_info->index--;
  417. }
  418. // load the previous feed item
  419. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  420. {
  421. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  422. fhttp.state = ISSUE;
  423. return;
  424. }
  425. 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);
  426. action = ActionNone;
  427. break;
  428. case ActionFlip:
  429. canvas_clear(canvas);
  430. // Moved to above the is_flipped check
  431. if (!flip_feed_item->is_flipped)
  432. {
  433. // increase the flip count
  434. flip_feed_item->flips++;
  435. }
  436. else
  437. {
  438. // decrease the flip count
  439. flip_feed_item->flips--;
  440. }
  441. // change the flip status
  442. flip_feed_item->is_flipped = !flip_feed_item->is_flipped;
  443. // send post request to flip the message
  444. if (app_instance->login_username_logged_in == NULL)
  445. {
  446. FURI_LOG_E(TAG, "Username is NULL");
  447. return;
  448. }
  449. char payload[256];
  450. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%u\"}", app_instance->login_username_logged_in, flip_feed_item->id);
  451. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", auth_headers, payload);
  452. 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);
  453. action = ActionNone;
  454. break;
  455. case ActionBack:
  456. canvas_clear(canvas);
  457. flip_social_dialog_stop = true;
  458. flip_feed_info->index = 0;
  459. action = ActionNone;
  460. break;
  461. default:
  462. break;
  463. }
  464. if (flip_social_dialog_stop)
  465. {
  466. furi_pubsub_unsubscribe(app_instance->input_event_queue, app_instance->input_event);
  467. flip_social_dialog_shown = false;
  468. flip_social_dialog_stop = false;
  469. action = ActionNone;
  470. }
  471. }