flip_social_callback.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. // flip_social_callback.h
  2. #ifndef FLIP_SOCIAL_CALLBACK_H
  3. #define FLIP_SOCIAL_CALLBACK_H
  4. #include "flip_social_feed.h"
  5. bool flip_social_sent_login_request = false;
  6. bool flip_social_sent_register_request = false;
  7. bool flip_social_login_success = false;
  8. bool flip_social_register_success = false;
  9. bool flip_social_dialog_shown = false;
  10. bool flip_social_dialog_stop = false;
  11. uint32_t flip_social_pre_saved_message_clicked_index = 0;
  12. static void flip_social_logged_in_compose_pre_save_updated(void *context);
  13. static void flip_social_callback_submenu_choices(void *context, uint32_t index);
  14. /**
  15. * @brief Navigation callback to go back to the submenu Logged out.
  16. * @param context The context - unused
  17. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  18. */
  19. static uint32_t flip_social_callback_to_submenu_logged_out(void *context)
  20. {
  21. UNUSED(context);
  22. return FlipSocialViewLoggedOutSubmenu;
  23. }
  24. /**
  25. * @brief Navigation callback to go back to the submenu Logged in.
  26. * @param context The context - unused
  27. * @return next view id (FlipSocialViewLoggedInSubmenu)
  28. */
  29. static uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  30. {
  31. UNUSED(context);
  32. // flip_social_get_feed(); // start the feed request
  33. return FlipSocialViewLoggedInSubmenu;
  34. }
  35. /**
  36. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  37. * @param context The context - unused
  38. * @return next view id (FlipSocialViewLoggedOutLogin)
  39. */
  40. static uint32_t flip_social_callback_to_login_logged_out(void *context)
  41. {
  42. UNUSED(context);
  43. flip_social_sent_login_request = false;
  44. flip_social_login_success = false;
  45. return FlipSocialViewLoggedOutLogin;
  46. }
  47. /**
  48. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  49. * @param context The context - unused
  50. * @return next view id (FlipSocialViewLoggedOutRegister)
  51. */
  52. static uint32_t flip_social_callback_to_register_logged_out(void *context)
  53. {
  54. UNUSED(context);
  55. flip_social_sent_register_request = false;
  56. flip_social_register_success = false;
  57. return FlipSocialViewLoggedOutRegister;
  58. }
  59. /**
  60. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  61. * @param context The context - unused
  62. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  63. */
  64. static uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
  65. {
  66. UNUSED(context);
  67. return FlipSocialViewLoggedOutWifiSettings;
  68. }
  69. /**
  70. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  71. * @param context The context - unused
  72. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  73. */
  74. static uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  75. {
  76. UNUSED(context);
  77. return FlipSocialViewLoggedInSettingsWifi;
  78. }
  79. /**
  80. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  81. * @param context The context - unused
  82. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  83. */
  84. static uint32_t flip_social_callback_to_settings_logged_in(void *context)
  85. {
  86. UNUSED(context);
  87. return FlipSocialViewLoggedInSettings;
  88. }
  89. /**
  90. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  91. * @param context The context - unused
  92. * @return next view id (FlipSocialViewLoggedInCompose)
  93. */
  94. static uint32_t flip_social_callback_to_compose_logged_in(void *context)
  95. {
  96. UNUSED(context);
  97. return FlipSocialViewLoggedInCompose;
  98. }
  99. /**
  100. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  101. * @param context The context - unused
  102. * @return next view id (FlipSocialViewLoggedInProfile)
  103. */
  104. static uint32_t flip_social_callback_to_profile_logged_in(void *context)
  105. {
  106. UNUSED(context);
  107. return FlipSocialViewLoggedInProfile;
  108. }
  109. static void on_input(const void *event, void *ctx)
  110. {
  111. UNUSED(ctx);
  112. InputKey key = ((InputEvent *)event)->key;
  113. InputType type = ((InputEvent *)event)->type;
  114. if (type != InputTypeRelease)
  115. {
  116. return;
  117. }
  118. switch (key)
  119. {
  120. case InputKeyOk:
  121. action = ActionFlip;
  122. break;
  123. case InputKeyBack:
  124. action = ActionBack;
  125. break;
  126. case InputKeyRight:
  127. action = ActionNext;
  128. break;
  129. case InputKeyLeft:
  130. action = ActionPrev;
  131. break;
  132. case InputKeyUp:
  133. action = ActionPrev;
  134. break;
  135. case InputKeyDown:
  136. action = ActionNext;
  137. break;
  138. default:
  139. action = ActionNone;
  140. break;
  141. }
  142. }
  143. // Function to draw the message on the canvas with word wrapping
  144. void draw_user_message(Canvas *canvas, const char *user_message, int x)
  145. {
  146. if (user_message == NULL)
  147. {
  148. FURI_LOG_E(TAG, "User message is NULL.");
  149. return;
  150. }
  151. size_t msg_length = strlen(user_message);
  152. size_t start = 0;
  153. int line_num = 0;
  154. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  155. while (start < msg_length && line_num < MAX_FEED_ITEMS)
  156. {
  157. size_t remaining = msg_length - start;
  158. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  159. if (remaining > MAX_LINE_LENGTH)
  160. {
  161. // Find the last space within the first 'len' characters
  162. size_t last_space = len;
  163. while (last_space > 0 && user_message[start + last_space - 1] != ' ')
  164. {
  165. last_space--;
  166. }
  167. if (last_space > 0)
  168. {
  169. len = last_space; // Adjust len to the position of the last space
  170. }
  171. }
  172. // Copy the substring to 'line' and null-terminate it
  173. memcpy(line, user_message + start, len);
  174. line[len] = '\0'; // Ensure the string is null-terminated
  175. // Draw the string on the canvas
  176. // Adjust the y-coordinate based on the line number
  177. canvas_draw_str_aligned(canvas, 0, x + line_num * 10, AlignLeft, AlignTop, line);
  178. // Update the start position for the next line
  179. start += len;
  180. // Skip any spaces to avoid leading spaces on the next line
  181. while (start < msg_length && user_message[start] == ' ')
  182. {
  183. start++;
  184. }
  185. // Increment the line number
  186. line_num++;
  187. }
  188. }
  189. static void flip_social_callback_draw_compose(Canvas *canvas, void *model)
  190. {
  191. UNUSED(model);
  192. if (!canvas)
  193. {
  194. FURI_LOG_E(TAG, "Canvas is NULL");
  195. return;
  196. }
  197. if (!app_instance)
  198. {
  199. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  200. return;
  201. }
  202. char *message = app_instance->pre_saved_messages.messages[flip_social_pre_saved_message_clicked_index];
  203. if (!flip_social_dialog_shown)
  204. {
  205. flip_social_dialog_shown = true;
  206. app_instance->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  207. app_instance->input_event = furi_pubsub_subscribe(app_instance->input_event_queue, on_input, NULL);
  208. }
  209. draw_user_message(canvas, message, 2);
  210. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  211. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  212. canvas_draw_icon(canvas, 52, 53, &I_ButtonBACK_10x8);
  213. canvas_draw_str_aligned(canvas, 64, 54, AlignLeft, AlignTop, "Back");
  214. canvas_draw_icon(canvas, 100, 53, &I_ButtonRight_4x7);
  215. canvas_draw_str_aligned(canvas, 107, 54, AlignLeft, AlignTop, "Post");
  216. // handle action
  217. switch (action)
  218. {
  219. case ActionNone:
  220. break;
  221. case ActionBack:
  222. flip_social_dialog_stop = true;
  223. break;
  224. case ActionNext:
  225. // send message
  226. if (message && app_instance->login_username_logged_in)
  227. {
  228. // Send the message
  229. char command[128];
  230. snprintf(command, sizeof(command), "{\"username\":\"%s\",\"content\":\"%s\"}",
  231. app_instance->login_username_logged_in, message);
  232. bool success = flipper_http_post_request_with_headers(
  233. "https://www.flipsocial.net/api/feed/post/",
  234. "{\"Content-Type\":\"application/json\"}",
  235. command);
  236. if (!success)
  237. {
  238. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  239. furi_check(success); // Log the error with furi_check
  240. return; // Exit early to avoid further errors
  241. }
  242. fhttp.state = RECEIVING;
  243. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  244. }
  245. else
  246. {
  247. FURI_LOG_E(TAG, "Message or username is NULL");
  248. furi_check(false); // Log as an error and return
  249. return;
  250. }
  251. int i = 0;
  252. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  253. {
  254. // Wait for the feed to be received
  255. furi_delay_ms(100);
  256. char dots_str[64] = "Receiving";
  257. // Append dots to the string based on the value of i
  258. int dot_count = i % 4;
  259. int len = strlen(dots_str);
  260. snprintf(dots_str + len, sizeof(dots_str) - len, "%.*s", dot_count, "....");
  261. // Draw the resulting string on the canvas
  262. canvas_draw_str(canvas, 0, 30, dots_str);
  263. i++;
  264. }
  265. flip_social_dialog_stop = true;
  266. furi_timer_stop(fhttp.get_timeout_timer);
  267. break;
  268. case ActionPrev:
  269. // delete message
  270. // remove the message from app_instance->pre_saved_messages
  271. app_instance->pre_saved_messages.messages[flip_social_pre_saved_message_clicked_index] = NULL;
  272. for (uint32_t i = flip_social_pre_saved_message_clicked_index; i < app_instance->pre_saved_messages.count - 1; i++)
  273. {
  274. app_instance->pre_saved_messages.messages[i] = app_instance->pre_saved_messages.messages[i + 1];
  275. }
  276. app_instance->pre_saved_messages.count--;
  277. // add the item to the submenu
  278. submenu_reset(app_instance->submenu_compose);
  279. submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app_instance);
  280. for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  281. {
  282. submenu_add_item(app_instance->submenu_compose, app_instance->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  283. }
  284. // save playlist
  285. save_playlist(&app_instance->pre_saved_messages);
  286. flip_social_dialog_stop = true;
  287. break;
  288. default:
  289. action = ActionNone;
  290. break;
  291. }
  292. if (flip_social_dialog_stop)
  293. {
  294. furi_pubsub_unsubscribe(app_instance->input_event_queue, app_instance->input_event);
  295. flip_social_dialog_shown = false;
  296. flip_social_dialog_stop = false;
  297. if (action == ActionBack || action == ActionNext)
  298. {
  299. action = ActionNone;
  300. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  301. }
  302. else
  303. {
  304. action = ActionNone;
  305. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInCompose);
  306. }
  307. }
  308. }
  309. // function to draw the dialog canvas
  310. static void flip_social_canvas_draw_message(Canvas *canvas, char *user_username, char *user_message, bool is_flipped, bool show_prev, bool show_next)
  311. {
  312. canvas_set_color(canvas, ColorBlack);
  313. canvas_set_font(canvas, FontPrimary);
  314. canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignCenter, user_username);
  315. canvas_set_font(canvas, FontSecondary);
  316. draw_user_message(canvas, user_message, 12);
  317. canvas_set_font(canvas, FontSecondary);
  318. if (show_prev)
  319. {
  320. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  321. canvas_draw_str_aligned(canvas, 9, 54, AlignLeft, AlignTop, "Prev");
  322. }
  323. if (!is_flipped)
  324. {
  325. canvas_draw_icon(canvas, 52, 53, &I_ButtonOK_7x7);
  326. canvas_draw_str_aligned(canvas, 61, 54, AlignLeft, AlignTop, "Flip");
  327. }
  328. else
  329. {
  330. canvas_draw_icon(canvas, 47, 53, &I_ButtonOK_7x7);
  331. canvas_draw_str_aligned(canvas, 56, 54, AlignLeft, AlignTop, "UnFlip");
  332. }
  333. if (show_next)
  334. {
  335. canvas_draw_icon(canvas, 98, 53, &I_ButtonRight_4x7);
  336. canvas_draw_str_aligned(canvas, 107, 54, AlignLeft, AlignTop, "Next");
  337. }
  338. }
  339. // Callback function to handle the feed dialog
  340. static void flip_social_callback_draw_feed(Canvas *canvas, void *model)
  341. {
  342. UNUSED(model);
  343. if (!canvas)
  344. {
  345. FURI_LOG_E(TAG, "Canvas is NULL");
  346. return;
  347. }
  348. if (!app_instance)
  349. {
  350. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  351. return;
  352. }
  353. if (!flip_social_dialog_shown)
  354. {
  355. flip_social_dialog_shown = true;
  356. app_instance->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  357. app_instance->input_event = furi_pubsub_subscribe(app_instance->input_event_queue, on_input, NULL);
  358. }
  359. // handle action
  360. switch (action)
  361. {
  362. case ActionNone:
  363. flip_social_canvas_draw_message(canvas, flip_social_feed.usernames[flip_social_feed.index], flip_social_feed.messages[flip_social_feed.index], flip_social_feed.is_flipped[flip_social_feed.index], flip_social_feed.index > 0, flip_social_feed.index < flip_social_feed.count - 1);
  364. break;
  365. case ActionNext:
  366. canvas_clear(canvas);
  367. if (flip_social_feed.index < flip_social_feed.count - 1)
  368. {
  369. flip_social_feed.index++;
  370. }
  371. flip_social_canvas_draw_message(canvas, flip_social_feed.usernames[flip_social_feed.index], flip_social_feed.messages[flip_social_feed.index], flip_social_feed.is_flipped[flip_social_feed.index], flip_social_feed.index > 0, flip_social_feed.index < flip_social_feed.count - 1);
  372. action = ActionNone;
  373. break;
  374. case ActionPrev:
  375. canvas_clear(canvas);
  376. if (flip_social_feed.index > 0)
  377. {
  378. flip_social_feed.index--;
  379. }
  380. flip_social_canvas_draw_message(canvas, flip_social_feed.usernames[flip_social_feed.index], flip_social_feed.messages[flip_social_feed.index], flip_social_feed.is_flipped[flip_social_feed.index], flip_social_feed.index > 0, flip_social_feed.index < flip_social_feed.count - 1);
  381. action = ActionNone;
  382. break;
  383. case ActionFlip:
  384. canvas_clear(canvas);
  385. flip_social_feed.is_flipped[flip_social_feed.index] = !flip_social_feed.is_flipped[flip_social_feed.index];
  386. flip_social_canvas_draw_message(canvas, flip_social_feed.usernames[flip_social_feed.index], flip_social_feed.messages[flip_social_feed.index], flip_social_feed.is_flipped[flip_social_feed.index], flip_social_feed.index > 0, flip_social_feed.index < flip_social_feed.count - 1);
  387. action = ActionNone;
  388. // send post request to flip the message
  389. if (app_instance->login_username_logged_in == NULL)
  390. {
  391. FURI_LOG_E(TAG, "Username is NULL");
  392. return;
  393. }
  394. char payload[256];
  395. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%lu\"}", app_instance->login_username_logged_in, flip_social_feed.ids[flip_social_feed.index]);
  396. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", "{\"Content-Type\":\"application/json\"}", payload);
  397. break;
  398. case ActionBack:
  399. canvas_clear(canvas);
  400. flip_social_dialog_stop = true;
  401. flip_social_feed.index = 0;
  402. action = ActionNone;
  403. break;
  404. default:
  405. break;
  406. }
  407. if (flip_social_dialog_stop)
  408. {
  409. furi_pubsub_unsubscribe(app_instance->input_event_queue, app_instance->input_event);
  410. flip_social_dialog_shown = false;
  411. flip_social_dialog_stop = false;
  412. action = ActionNone;
  413. }
  414. }
  415. /**
  416. * @brief Navigation callback for asynchonously handling the login process.
  417. * @param canvas The canvas to draw on.
  418. * @param model The model - unused
  419. * @return void
  420. */
  421. static void flip_social_callback_draw_login(Canvas *canvas, void *model)
  422. {
  423. UNUSED(model);
  424. if (!canvas)
  425. {
  426. FURI_LOG_E(TAG, "Canvas is NULL");
  427. return;
  428. }
  429. canvas_set_font(canvas, FontSecondary);
  430. if (!flip_social_board_is_active(canvas))
  431. {
  432. return;
  433. }
  434. canvas_draw_str(canvas, 0, 7, "Logging in...");
  435. // Perform login request
  436. if (!flip_social_sent_login_request)
  437. {
  438. if (!app_instance->login_username_logged_out || !app_instance->login_password_logged_out || strlen(app_instance->login_username_logged_out) == 0 || strlen(app_instance->login_password_logged_out) == 0)
  439. {
  440. canvas_clear(canvas);
  441. canvas_draw_str(canvas, 0, 10, "Please enter your credentials.");
  442. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  443. return;
  444. }
  445. flip_social_sent_login_request = true;
  446. char buffer[256];
  447. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  448. flip_social_login_success = flipper_http_post_request_with_headers("https://www.flipsocial.net/api/login/", "{\"Content-Type\":\"application/json\"}", buffer);
  449. if (flip_social_login_success)
  450. {
  451. fhttp.state = RECEIVING;
  452. return;
  453. }
  454. else
  455. {
  456. fhttp.state = ISSUE;
  457. return;
  458. }
  459. }
  460. // handle response
  461. if (flip_social_sent_login_request && flip_social_login_success)
  462. {
  463. canvas_set_font(canvas, FontSecondary);
  464. canvas_draw_str(canvas, 0, 17, "Request Sent!");
  465. canvas_draw_str(canvas, 0, 32, "Awaiting reponse...");
  466. if (fhttp.state == IDLE && fhttp.received_data != NULL)
  467. {
  468. // read response
  469. if (strstr(fhttp.received_data, "[SUCCESS]") != NULL || strstr(fhttp.received_data, "User found") != NULL)
  470. {
  471. canvas_draw_str(canvas, 0, 42, "Login successful!");
  472. canvas_draw_str(canvas, 0, 62, "Welcome back!");
  473. app_instance->is_logged_in = "true";
  474. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  475. // send user to the logged in submenu
  476. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  477. }
  478. else if (strstr(fhttp.received_data, "User not found") != NULL)
  479. {
  480. canvas_clear(canvas);
  481. canvas_draw_str(canvas, 0, 10, "Account not found...");
  482. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  483. }
  484. else
  485. {
  486. flip_social_handle_error(canvas);
  487. }
  488. }
  489. else if ((fhttp.state == ISSUE || fhttp.state == INACTIVE) && fhttp.received_data != NULL)
  490. {
  491. flip_social_handle_error(canvas);
  492. }
  493. else if (fhttp.state == IDLE && fhttp.received_data == NULL)
  494. {
  495. flip_social_handle_error(canvas);
  496. }
  497. }
  498. else if (flip_social_sent_login_request && !flip_social_login_success)
  499. {
  500. canvas_clear(canvas);
  501. canvas_draw_str(canvas, 0, 10, "Failed sending request.");
  502. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  503. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  504. }
  505. }
  506. /**
  507. * @brief Navigation callback for asynchonously handling the register process.
  508. * @param canvas The canvas to draw on.
  509. * @param model The model - unused
  510. * @return void
  511. */
  512. static void flip_social_callback_draw_register(Canvas *canvas, void *model)
  513. {
  514. UNUSED(model);
  515. if (!canvas)
  516. {
  517. FURI_LOG_E(TAG, "Canvas is NULL");
  518. return;
  519. }
  520. canvas_set_font(canvas, FontSecondary);
  521. if (!flip_social_board_is_active(canvas))
  522. {
  523. return;
  524. }
  525. canvas_draw_str(canvas, 0, 7, "Registering...");
  526. // Perform login request
  527. if (!flip_social_sent_register_request)
  528. {
  529. // check if the username and password are valid
  530. if (!app_instance->register_username_logged_out || !app_instance->register_password_logged_out || strlen(app_instance->register_username_logged_out) == 0 || strlen(app_instance->register_password_logged_out) == 0)
  531. {
  532. canvas_clear(canvas);
  533. canvas_draw_str(canvas, 0, 10, "Please enter your credentials.");
  534. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  535. return;
  536. }
  537. // check if both passwords match
  538. if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
  539. {
  540. canvas_clear(canvas);
  541. canvas_draw_str(canvas, 0, 10, "Passwords do not match.");
  542. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  543. return;
  544. }
  545. char buffer[128];
  546. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
  547. flip_social_register_success = flipper_http_post_request_with_headers("https://www.flipsocial.net/api/register/", "{\"Content-Type\":\"application/json\"}", buffer);
  548. flip_social_sent_register_request = true;
  549. if (flip_social_register_success)
  550. {
  551. // Set the state to RECEIVING to ensure we continue to see the receiving message
  552. fhttp.state = RECEIVING;
  553. }
  554. else
  555. {
  556. fhttp.state = ISSUE;
  557. }
  558. }
  559. // handle response
  560. if (flip_social_sent_register_request && flip_social_register_success)
  561. {
  562. canvas_set_font(canvas, FontSecondary);
  563. canvas_draw_str(canvas, 0, 17, "Request Sent!");
  564. canvas_draw_str(canvas, 0, 32, "Awaiting reponse...");
  565. if (fhttp.state == IDLE)
  566. {
  567. // read response
  568. if (fhttp.received_data != NULL && (strstr(fhttp.received_data, "[SUCCESS]") != NULL || strstr(fhttp.received_data, "User created") != NULL))
  569. {
  570. canvas_draw_str(canvas, 0, 42, "Registeration successful!");
  571. canvas_draw_str(canvas, 0, 62, "Welcome to FlipSocial!");
  572. // set the login credentials
  573. if (app_instance->login_username_logged_out)
  574. {
  575. app_instance->login_username_logged_out = app_instance->register_username_logged_out;
  576. }
  577. if (app_instance->login_password_logged_out)
  578. {
  579. app_instance->login_password_logged_out = app_instance->register_password_logged_out;
  580. app_instance->change_password_logged_in = app_instance->register_password_logged_out;
  581. }
  582. if (app_instance->login_username_logged_in)
  583. {
  584. app_instance->login_username_logged_in = app_instance->register_username_logged_out;
  585. }
  586. app_instance->is_logged_in = "true";
  587. // save the credentials
  588. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  589. // send user to the logged in submenu
  590. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  591. }
  592. else if (strstr(fhttp.received_data, "Username or password not provided") != NULL)
  593. {
  594. canvas_clear(canvas);
  595. canvas_draw_str(canvas, 0, 10, "Please enter your credentials.");
  596. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  597. }
  598. else if (strstr(fhttp.received_data, "User already exists") != NULL || strstr(fhttp.received_data, "Multiple users found") != NULL)
  599. {
  600. canvas_draw_str(canvas, 0, 42, "Registration failed...");
  601. canvas_draw_str(canvas, 0, 52, "Username already exists.");
  602. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  603. }
  604. else
  605. {
  606. canvas_draw_str(canvas, 0, 42, "Registration failed...");
  607. canvas_draw_str(canvas, 0, 52, "Update your credentials.");
  608. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  609. }
  610. }
  611. else if (fhttp.state == ISSUE || fhttp.state == INACTIVE)
  612. {
  613. flip_social_handle_error(canvas);
  614. }
  615. }
  616. else if (flip_social_sent_register_request && !flip_social_register_success)
  617. {
  618. canvas_clear(canvas);
  619. canvas_draw_str(canvas, 0, 10, "Failed sending request.");
  620. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  621. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  622. }
  623. }
  624. /**
  625. * @brief Navigation callback for exiting the application
  626. * @param context The context - unused
  627. * @return next view id (VIEW_NONE to exit the app)
  628. */
  629. static uint32_t flip_social_callback_exit_app(void *context)
  630. {
  631. // Exit the application
  632. if (!context)
  633. {
  634. FURI_LOG_E(TAG, "Context is NULL");
  635. return VIEW_NONE;
  636. }
  637. UNUSED(context);
  638. return VIEW_NONE; // Return VIEW_NONE to exit the app
  639. }
  640. /**
  641. * @brief Handle ALL submenu item selections.
  642. * @param context The context - FlipSocialApp object.
  643. * @param index The FlipSocialSubmenuIndex item that was clicked.
  644. * @return void
  645. */
  646. static void flip_social_callback_submenu_choices(void *context, uint32_t index)
  647. {
  648. FlipSocialApp *app = (FlipSocialApp *)context;
  649. if (!app)
  650. {
  651. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  652. return;
  653. }
  654. switch (index)
  655. {
  656. case FlipSocialSubmenuLoggedOutIndexLogin:
  657. flip_social_sent_login_request = false;
  658. flip_social_login_success = false;
  659. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  660. break;
  661. case FlipSocialSubmenuLoggedOutIndexRegister:
  662. flip_social_sent_register_request = false;
  663. flip_social_register_success = false;
  664. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  665. break;
  666. case FlipSocialSubmenuLoggedOutIndexAbout:
  667. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  668. break;
  669. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  670. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  671. break;
  672. case FlipSocialSubmenuLoggedInIndexProfile:
  673. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  674. break;
  675. case FlipSocialSubmenuLoggedInIndexFeed:
  676. if (flip_social_get_feed()) // start the async feed request
  677. {
  678. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  679. }
  680. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  681. {
  682. // Wait for the feed to be received
  683. furi_delay_ms(100);
  684. }
  685. furi_timer_stop(fhttp.get_timeout_timer);
  686. if (!flip_social_parse_json_feed()) // parse the JSON before switching to the feed (synchonous)
  687. {
  688. FURI_LOG_E(TAG, "Failed to parse the JSON feed. Using the temporary feed.");
  689. }
  690. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  691. break;
  692. case FlipSocialSubmenuLoggedInIndexCompose:
  693. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  694. break;
  695. case FlipSocialSubmenuLoggedInIndexSettings:
  696. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  697. break;
  698. case FlipSocialSubmenuLoggedInSignOutButton:
  699. app->is_logged_in = "false";
  700. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  701. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  702. break;
  703. case FlipSocialSubmenuComposeIndexAddPreSave:
  704. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  705. break;
  706. default:
  707. // handle FlipSocialSubemnuComposeIndexStartIndex + i
  708. // set temp variable to hold the index
  709. flip_social_pre_saved_message_clicked_index = index - FlipSocialSubemnuComposeIndexStartIndex;
  710. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProcessCompose);
  711. break;
  712. }
  713. }
  714. /**
  715. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  716. * @param context The context - FlipSocialApp object.
  717. * @return void
  718. */
  719. static void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  720. {
  721. FlipSocialApp *app = (FlipSocialApp *)context;
  722. if (!app)
  723. {
  724. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  725. return;
  726. }
  727. // Store the entered name
  728. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  729. // Store the entered name in the logged in name field
  730. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  731. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  732. // Ensure null-termination
  733. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  734. // Update the name item text
  735. if (app->variable_item_logged_out_wifi_settings_ssid)
  736. {
  737. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  738. }
  739. // update the wifi settings
  740. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  741. {
  742. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  743. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  744. }
  745. // Save the settings
  746. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  747. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  748. }
  749. /**
  750. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  751. * @param context The context - FlipSocialApp object.
  752. * @return void
  753. */
  754. static void flip_social_logged_out_wifi_settings_password_updated(void *context)
  755. {
  756. FlipSocialApp *app = (FlipSocialApp *)context;
  757. if (!app)
  758. {
  759. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  760. return;
  761. }
  762. // Store the entered WiFi password
  763. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  764. // Store the entered WiFi password in the logged in password field
  765. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  766. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  767. // Ensure null-termination
  768. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  769. // Update the password item text
  770. if (app->variable_item_logged_out_wifi_settings_password)
  771. {
  772. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  773. }
  774. // update the wifi settings
  775. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  776. {
  777. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  778. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  779. }
  780. // Save the settings
  781. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  782. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  783. }
  784. /**
  785. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  786. * @param context The context - FlipSocialApp object.
  787. * @param index The index of the selected item.
  788. * @return void
  789. */
  790. static void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  791. {
  792. FlipSocialApp *app = (FlipSocialApp *)context;
  793. if (!app)
  794. {
  795. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  796. return;
  797. }
  798. switch (index)
  799. {
  800. case 0: // Input SSID
  801. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  802. break;
  803. case 1: // Input Password
  804. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  805. break;
  806. default:
  807. FURI_LOG_E(TAG, "Unknown configuration item index");
  808. break;
  809. }
  810. }
  811. /**
  812. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  813. * @param context The context - FlipSocialApp object.
  814. * @return void
  815. */
  816. static void flip_social_logged_out_login_username_updated(void *context)
  817. {
  818. FlipSocialApp *app = (FlipSocialApp *)context;
  819. if (!app)
  820. {
  821. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  822. return;
  823. }
  824. // Store the entered name
  825. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  826. // Store the entered name in the logged in username field
  827. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  828. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  829. // Ensure null-termination
  830. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  831. // Update the name item text
  832. if (app->variable_item_logged_out_login_username)
  833. {
  834. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  835. }
  836. // Save the settings
  837. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  838. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  839. }
  840. /**
  841. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  842. * @param context The context - FlipSocialApp object.
  843. * @return void
  844. */
  845. static void flip_social_logged_out_login_password_updated(void *context)
  846. {
  847. FlipSocialApp *app = (FlipSocialApp *)context;
  848. if (!app)
  849. {
  850. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  851. return;
  852. }
  853. // Store the entered password
  854. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  855. // Store the entered password in the change password field
  856. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  857. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  858. // Ensure null-termination
  859. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  860. // Update the password item text
  861. if (app->variable_item_logged_out_login_password)
  862. {
  863. // dont show the password on the screen (version 0.2)
  864. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  865. }
  866. // Save the settings
  867. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  868. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  869. }
  870. /**
  871. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  872. * @param context The context - FlipSocialApp object.
  873. * @param index The index of the selected item.
  874. * @return void
  875. */
  876. static void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  877. {
  878. FlipSocialApp *app = (FlipSocialApp *)context;
  879. if (!app)
  880. {
  881. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  882. return;
  883. }
  884. switch (index)
  885. {
  886. case 0: // Input Username
  887. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  888. break;
  889. case 1: // Input Password
  890. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  891. break;
  892. case 2: // Login Button
  893. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessLogin);
  894. break;
  895. default:
  896. FURI_LOG_E(TAG, "Unknown configuration item index");
  897. break;
  898. }
  899. }
  900. /**
  901. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  902. * @param context The context - FlipSocialApp object.
  903. * @return void
  904. */
  905. static void flip_social_logged_out_register_username_updated(void *context)
  906. {
  907. FlipSocialApp *app = (FlipSocialApp *)context;
  908. if (!app)
  909. {
  910. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  911. return;
  912. }
  913. // Store the entered name
  914. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  915. // Ensure null-termination
  916. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  917. // Update the name item text
  918. if (app->variable_item_logged_out_register_username)
  919. {
  920. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  921. }
  922. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  923. }
  924. /**
  925. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  926. * @param context The context - FlipSocialApp object.
  927. * @return void
  928. */
  929. static void flip_social_logged_out_register_password_updated(void *context)
  930. {
  931. FlipSocialApp *app = (FlipSocialApp *)context;
  932. if (!app)
  933. {
  934. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  935. return;
  936. }
  937. // Store the entered password
  938. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  939. // Ensure null-termination
  940. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  941. // Update the password item text
  942. if (app->variable_item_logged_out_register_password)
  943. {
  944. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  945. }
  946. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  947. }
  948. /**
  949. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  950. * @param context The context - FlipSocialApp object.
  951. * @return void
  952. */
  953. static void flip_social_logged_out_register_password_2_updated(void *context)
  954. {
  955. FlipSocialApp *app = (FlipSocialApp *)context;
  956. if (!app)
  957. {
  958. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  959. return;
  960. }
  961. // Store the entered password
  962. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  963. // Ensure null-termination
  964. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  965. // Update the password item text
  966. if (app->variable_item_logged_out_register_password_2)
  967. {
  968. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  969. }
  970. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  971. }
  972. /**
  973. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  974. * @param context The context - FlipSocialApp object.
  975. * @param index The index of the selected item.
  976. * @return void
  977. */
  978. static void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  979. {
  980. FlipSocialApp *app = (FlipSocialApp *)context;
  981. if (!app)
  982. {
  983. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  984. return;
  985. }
  986. switch (index)
  987. {
  988. case 0: // Input Username
  989. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  990. break;
  991. case 1: // Input Password
  992. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  993. break;
  994. case 2: // Input Password 2
  995. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  996. break;
  997. case 3: // Register button
  998. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessRegister);
  999. break;
  1000. default:
  1001. FURI_LOG_E(TAG, "Unknown configuration item index");
  1002. break;
  1003. }
  1004. }
  1005. /**
  1006. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  1007. * @param context The context - FlipSocialApp object.
  1008. * @return void
  1009. */
  1010. static void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  1011. {
  1012. FlipSocialApp *app = (FlipSocialApp *)context;
  1013. if (!app)
  1014. {
  1015. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1016. return;
  1017. }
  1018. // Store the entered SSID
  1019. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  1020. // Store the entered SSID in the logged out SSID
  1021. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1022. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1023. // Ensure null-termination
  1024. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  1025. // Update the name item text
  1026. if (app->variable_item_logged_in_wifi_settings_ssid)
  1027. {
  1028. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  1029. }
  1030. // Save the settings
  1031. save_settings(app_instance->wifi_ssid_logged_in, app_instance->wifi_password_logged_in, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  1032. // update the wifi settings
  1033. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1034. {
  1035. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1036. {
  1037. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1038. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1039. }
  1040. }
  1041. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1042. }
  1043. /**
  1044. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  1045. * @param context The context - FlipSocialApp object.
  1046. * @return void
  1047. */
  1048. static void flip_social_logged_in_wifi_settings_password_updated(void *context)
  1049. {
  1050. FlipSocialApp *app = (FlipSocialApp *)context;
  1051. if (!app)
  1052. {
  1053. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1054. return;
  1055. }
  1056. // Store the entered password
  1057. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  1058. // Store the entered password in the logged out password
  1059. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1060. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1061. // Ensure null-termination
  1062. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  1063. // Update the password item text
  1064. if (app->variable_item_logged_in_wifi_settings_password)
  1065. {
  1066. // dont show the password on the screen (version 0.2)
  1067. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  1068. }
  1069. // Save the settings
  1070. save_settings(app_instance->wifi_ssid_logged_in, app_instance->wifi_password_logged_in, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  1071. // update the wifi settings
  1072. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1073. {
  1074. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1075. {
  1076. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1077. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1078. }
  1079. }
  1080. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1081. }
  1082. /**
  1083. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  1084. * @param context The context - FlipSocialApp object.
  1085. * @param index The index of the selected item.
  1086. * @return void
  1087. */
  1088. static void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  1089. {
  1090. FlipSocialApp *app = (FlipSocialApp *)context;
  1091. if (!app)
  1092. {
  1093. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1094. return;
  1095. }
  1096. switch (index)
  1097. {
  1098. case 0: // Input SSID
  1099. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  1100. break;
  1101. case 1: // Input Password
  1102. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  1103. break;
  1104. default:
  1105. FURI_LOG_E(TAG, "Unknown configuration item index");
  1106. break;
  1107. }
  1108. }
  1109. /**
  1110. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  1111. * @param context The context - FlipSocialApp object.
  1112. * @return void
  1113. */
  1114. static void flip_social_logged_in_compose_pre_save_updated(void *context)
  1115. {
  1116. FlipSocialApp *app = (FlipSocialApp *)context;
  1117. if (!app)
  1118. {
  1119. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1120. return;
  1121. }
  1122. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  1123. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  1124. {
  1125. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  1126. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1127. return;
  1128. }
  1129. // Store the entered message
  1130. strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
  1131. // Ensure null-termination
  1132. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  1133. // add the item to the submenu
  1134. submenu_reset(app->submenu_compose);
  1135. // loop through the items and add them to the submenu
  1136. app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
  1137. app->pre_saved_messages.count++;
  1138. submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  1139. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  1140. {
  1141. submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  1142. }
  1143. // save playlist
  1144. save_playlist(&app->pre_saved_messages);
  1145. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1146. }
  1147. /**
  1148. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1149. * @param context The context - FlipSocialApp object.
  1150. * @return void
  1151. */
  1152. static void flip_social_logged_in_profile_change_password_updated(void *context)
  1153. {
  1154. FlipSocialApp *app = (FlipSocialApp *)context;
  1155. if (!app)
  1156. {
  1157. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1158. return;
  1159. }
  1160. // Correct type: old_pass should be a pointer to a string (char *)
  1161. const char *old_password = app->login_password_logged_out;
  1162. // Store the entered message
  1163. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1164. // store the entered password in the logged out password
  1165. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1166. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1167. // Ensure null-termination
  1168. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1169. // Update the message item text
  1170. if (app->variable_item_logged_in_profile_change_password)
  1171. {
  1172. // dont show the password on the screen (version 0.2)
  1173. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1174. }
  1175. // send post request to change password
  1176. char payload[256];
  1177. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1178. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/change-password/", "Content-Type: application/json", payload))
  1179. {
  1180. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1181. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1182. return;
  1183. }
  1184. // Save the settings
  1185. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  1186. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  1187. }
  1188. /**
  1189. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1190. * @param context The context - FlipSocialApp object.
  1191. * @param index The index of the selected item.
  1192. * @return void
  1193. */
  1194. static void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1195. {
  1196. FlipSocialApp *app = (FlipSocialApp *)context;
  1197. if (!app)
  1198. {
  1199. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1200. return;
  1201. }
  1202. switch (index)
  1203. {
  1204. case 0: // Change Username
  1205. // do nothing since username cannot be changed
  1206. break;
  1207. case 1: // Change Password
  1208. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1209. break;
  1210. default:
  1211. FURI_LOG_E(TAG, "Unknown configuration item index");
  1212. break;
  1213. }
  1214. }
  1215. /**
  1216. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  1217. * @param context The context - FlipSocialApp object.
  1218. * @param index The index of the selected item.
  1219. * @return void
  1220. */
  1221. static void flip_social_text_input_logged_in_settings_item_selected(void *context, uint32_t index)
  1222. {
  1223. FlipSocialApp *app = (FlipSocialApp *)context;
  1224. if (!app)
  1225. {
  1226. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1227. return;
  1228. }
  1229. switch (index)
  1230. {
  1231. case 0: // About
  1232. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  1233. break;
  1234. case 1: // Wifi
  1235. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1236. break;
  1237. default:
  1238. break;
  1239. }
  1240. }
  1241. #endif // FLIP_SOCIAL_CALLBACK_H