flip_social_callback.h 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. // flip_social_callback.h
  2. #ifndef FLIP_SOCIAL_CALLBACK_H
  3. #define FLIP_SOCIAL_CALLBACK_H
  4. static FlipSocialApp *app_instance = NULL;
  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. // include strndup
  15. char *strndup(const char *s, size_t n)
  16. {
  17. char *result;
  18. size_t len = strlen(s);
  19. if (n < len)
  20. len = n;
  21. result = (char *)malloc(len + 1);
  22. if (!result)
  23. return NULL;
  24. result[len] = '\0';
  25. return (char *)memcpy(result, s, len);
  26. }
  27. typedef struct
  28. {
  29. char *usernames[128];
  30. char *messages[128];
  31. bool is_flipped[128];
  32. uint32_t ids[128];
  33. size_t count;
  34. size_t index;
  35. } FlipSocialFeed;
  36. #define MAX_FEED_ITEMS 128
  37. #define MAX_LINE_LENGTH 30
  38. // temporary FlipSocialFeed object
  39. static FlipSocialFeed flip_social_feed = {
  40. .usernames = {"JBlanked", "FlipperKing", "FlipperQueen"},
  41. .messages = {"Welcome. This is a temp message. Either the feed didn't load or there was a server error.", "I am the Chosen Flipper.", "No one can flip like me."},
  42. .is_flipped = {false, false, true},
  43. .ids = {0, 1, 2},
  44. .count = 3,
  45. .index = 0};
  46. bool flip_social_get_feed()
  47. {
  48. // Get the feed from the server
  49. if (app_instance->login_username_logged_out == NULL)
  50. {
  51. FURI_LOG_E(TAG, "Username is NULL");
  52. return false;
  53. }
  54. char command[256];
  55. snprintf(command, 128, "https://www.flipsocial.net/api/feed/20/%s/", app_instance->login_username_logged_out);
  56. bool success = flipper_http_get_request_with_headers(command, "{\"Content-Type\":\"application/json\"}");
  57. if (!success)
  58. {
  59. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  60. return false;
  61. }
  62. fhttp.state = RECEIVING;
  63. return true;
  64. }
  65. #define MAX_TOKENS 128 // Adjust based on expected JSON size
  66. // Helper function to compare JSON keys
  67. int jsoneq(const char *json, jsmntok_t *tok, const char *s)
  68. {
  69. if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&
  70. strncmp(json + tok->start, s, tok->end - tok->start) == 0)
  71. {
  72. return 0;
  73. }
  74. return -1;
  75. }
  76. bool flip_social_parse_json_feed()
  77. {
  78. // Parse the JSON feed
  79. if (fhttp.received_data != NULL)
  80. {
  81. jsmn_parser parser;
  82. jsmn_init(&parser);
  83. // Allocate tokens array on the heap
  84. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * MAX_TOKENS);
  85. if (tokens == NULL)
  86. {
  87. FURI_LOG_E(TAG, "Failed to allocate memory for JSON tokens.");
  88. return false;
  89. }
  90. int ret = jsmn_parse(&parser, fhttp.received_data, strlen(fhttp.received_data), tokens, MAX_TOKENS);
  91. if (ret < 0)
  92. {
  93. // Handle parsing errors
  94. FURI_LOG_E(TAG, "Failed to parse JSON: %d", ret);
  95. free(tokens);
  96. return false;
  97. }
  98. // Ensure that the root element is an object
  99. if (ret < 1 || tokens[0].type != JSMN_OBJECT)
  100. {
  101. FURI_LOG_E(TAG, "Root element is not an object.");
  102. free(tokens);
  103. return false;
  104. }
  105. // Initialize feed count
  106. flip_social_feed.count = 0;
  107. // Loop over all keys in the root object
  108. int i = 0;
  109. for (i = 1; i < ret; i++)
  110. {
  111. if (jsoneq(fhttp.received_data, &tokens[i], "feed") == 0)
  112. {
  113. // Found "feed" key
  114. jsmntok_t *feed_array = &tokens[i + 1];
  115. if (feed_array->type != JSMN_ARRAY)
  116. {
  117. FURI_LOG_E(TAG, "'feed' is not an array.");
  118. break;
  119. }
  120. int j, k;
  121. int feed_index = 0;
  122. // Iterate over the feed array
  123. for (j = 0; j < feed_array->size; j++)
  124. {
  125. int idx = i + 2; // Position of the first feed item
  126. for (k = 0; k < j; k++)
  127. {
  128. // Skip tokens of previous feed items
  129. idx += tokens[idx].size * 2 + 1;
  130. }
  131. if (idx >= ret)
  132. {
  133. FURI_LOG_E(TAG, "Index out of bounds while accessing feed items.");
  134. break;
  135. }
  136. jsmntok_t *item = &tokens[idx];
  137. if (item->type != JSMN_OBJECT)
  138. {
  139. FURI_LOG_E(TAG, "Feed item is not an object.");
  140. continue;
  141. }
  142. // Variables to hold item data
  143. char *username = NULL;
  144. char *message = NULL;
  145. int flipped = 0;
  146. int id = 0;
  147. // Iterate over keys in the feed item
  148. int l;
  149. int item_size = item->size;
  150. int item_idx = idx + 1; // Position of the first key in the item
  151. for (l = 0; l < item_size; l++)
  152. {
  153. if (item_idx + 1 >= ret)
  154. {
  155. FURI_LOG_E(TAG, "Index out of bounds while accessing item properties.");
  156. break;
  157. }
  158. jsmntok_t *key = &tokens[item_idx];
  159. jsmntok_t *val = &tokens[item_idx + 1];
  160. if (jsoneq(fhttp.received_data, key, "username") == 0)
  161. {
  162. username = strndup(fhttp.received_data + val->start, val->end - val->start);
  163. }
  164. else if (jsoneq(fhttp.received_data, key, "message") == 0)
  165. {
  166. message = strndup(fhttp.received_data + val->start, val->end - val->start);
  167. }
  168. else if (jsoneq(fhttp.received_data, key, "flipped") == 0)
  169. {
  170. if (val->type == JSMN_PRIMITIVE)
  171. {
  172. if (strncmp(fhttp.received_data + val->start, "true", val->end - val->start) == 0)
  173. flipped = 1;
  174. else
  175. flipped = 0;
  176. }
  177. }
  178. else if (jsoneq(fhttp.received_data, key, "id") == 0)
  179. {
  180. if (val->type == JSMN_PRIMITIVE)
  181. {
  182. char id_str[16] = {0};
  183. uint32_t id_len = val->end - val->start;
  184. if (id_len >= sizeof(id_str))
  185. id_len = sizeof(id_str) - 1;
  186. strncpy(id_str, fhttp.received_data + val->start, id_len);
  187. id = atoi(id_str);
  188. }
  189. }
  190. item_idx += 2; // Move to the next key-value pair
  191. }
  192. // Store the data in flip_social_feed
  193. if (username && message && feed_index < MAX_FEED_ITEMS)
  194. {
  195. flip_social_feed.usernames[feed_index] = username;
  196. flip_social_feed.messages[feed_index] = message;
  197. flip_social_feed.is_flipped[feed_index] = flipped;
  198. flip_social_feed.ids[feed_index] = id;
  199. feed_index++;
  200. flip_social_feed.count = feed_index;
  201. }
  202. else
  203. {
  204. // Free allocated memory if not stored
  205. if (username)
  206. free(username);
  207. if (message)
  208. free(message);
  209. }
  210. }
  211. break; // Feed processed
  212. }
  213. }
  214. free(tokens); // Free the allocated tokens array
  215. }
  216. else
  217. {
  218. FURI_LOG_E(TAG, "No data received.");
  219. return false;
  220. }
  221. return true;
  222. }
  223. static void on_input(const void *event, void *ctx)
  224. {
  225. UNUSED(ctx);
  226. InputKey key = ((InputEvent *)event)->key;
  227. InputType type = ((InputEvent *)event)->type;
  228. if (type != InputTypeRelease)
  229. {
  230. return;
  231. }
  232. switch (key)
  233. {
  234. case InputKeyOk:
  235. action = ActionFlip;
  236. break;
  237. case InputKeyBack:
  238. action = ActionBack;
  239. break;
  240. case InputKeyRight:
  241. action = ActionNext;
  242. break;
  243. case InputKeyLeft:
  244. action = ActionPrev;
  245. break;
  246. case InputKeyUp:
  247. action = ActionPrev;
  248. break;
  249. case InputKeyDown:
  250. action = ActionNext;
  251. break;
  252. default:
  253. action = ActionNone;
  254. break;
  255. }
  256. }
  257. // Function to draw the message on the canvas with word wrapping
  258. void draw_user_message(Canvas *canvas, const char *user_message, int x)
  259. {
  260. if (user_message == NULL)
  261. {
  262. FURI_LOG_E(TAG, "User message is NULL.");
  263. return;
  264. }
  265. size_t msg_length = strlen(user_message);
  266. size_t start = 0;
  267. int line_num = 0;
  268. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  269. while (start < msg_length && line_num < MAX_FEED_ITEMS)
  270. {
  271. size_t remaining = msg_length - start;
  272. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  273. if (remaining > MAX_LINE_LENGTH)
  274. {
  275. // Find the last space within the first 'len' characters
  276. size_t last_space = len;
  277. while (last_space > 0 && user_message[start + last_space - 1] != ' ')
  278. {
  279. last_space--;
  280. }
  281. if (last_space > 0)
  282. {
  283. len = last_space; // Adjust len to the position of the last space
  284. }
  285. // If no space is found, len remains MAX_LINE_LENGTH to force split
  286. }
  287. // Copy the substring to 'line' and null-terminate it
  288. memcpy(line, user_message + start, len);
  289. line[len] = '\0'; // Ensure the string is null-terminated
  290. // Debug Logging: Print the current line being drawn
  291. FURI_LOG_D(TAG, "Drawing line %d: \"%s\"", line_num + 1, line);
  292. // Draw the string on the canvas
  293. // Adjust the y-coordinate based on the line number
  294. canvas_draw_str_aligned(canvas, 0, x + line_num * 10, AlignLeft, AlignTop, line);
  295. // Update the start position for the next line
  296. start += len;
  297. // Skip any spaces to avoid leading spaces on the next line
  298. while (start < msg_length && user_message[start] == ' ')
  299. {
  300. start++;
  301. }
  302. // Increment the line number
  303. line_num++;
  304. }
  305. // Handle any remaining text that wasn't processed due to exceeding MAX_FEED_ITEMS
  306. if (start < msg_length)
  307. {
  308. FURI_LOG_E(TAG, "Message exceeds maximum number of lines (%d).", MAX_FEED_ITEMS);
  309. }
  310. }
  311. static void flip_social_callback_draw_compose(Canvas *canvas, void *model)
  312. {
  313. UNUSED(model);
  314. if (!canvas)
  315. {
  316. FURI_LOG_E(TAG, "Canvas is NULL");
  317. return;
  318. }
  319. if (!app_instance)
  320. {
  321. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  322. return;
  323. }
  324. char *message = app_instance->pre_saved_messages.messages[flip_social_pre_saved_message_clicked_index];
  325. if (!flip_social_dialog_shown)
  326. {
  327. flip_social_dialog_shown = true;
  328. app_instance->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  329. app_instance->input_event = furi_pubsub_subscribe(app_instance->input_event_queue, on_input, NULL);
  330. }
  331. draw_user_message(canvas, message, 2);
  332. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  333. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  334. canvas_draw_icon(canvas, 52, 53, &I_ButtonBACK_10x8);
  335. canvas_draw_str_aligned(canvas, 64, 54, AlignLeft, AlignTop, "Back");
  336. canvas_draw_icon(canvas, 100, 53, &I_ButtonRight_4x7);
  337. canvas_draw_str_aligned(canvas, 107, 54, AlignLeft, AlignTop, "Post");
  338. // handle action
  339. switch (action)
  340. {
  341. case ActionNone:
  342. break;
  343. case ActionBack:
  344. flip_social_dialog_stop = true;
  345. break;
  346. case ActionNext:
  347. // send message
  348. if (message && app_instance->login_username_logged_in)
  349. {
  350. // Send the message
  351. char command[256];
  352. snprintf(command, sizeof(command), "{\"username\":\"%s\",\"content\":\"%s\"}",
  353. app_instance->login_username_logged_in, message);
  354. bool success = flipper_http_post_request_with_headers(
  355. "https://www.flipsocial.net/api/feed/post/",
  356. "{\"Content-Type\":\"application/json\"}",
  357. command);
  358. if (!success)
  359. {
  360. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  361. furi_check(success); // Log the error with furi_check
  362. return; // Exit early to avoid further errors
  363. }
  364. fhttp.state = RECEIVING;
  365. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  366. }
  367. else
  368. {
  369. FURI_LOG_E(TAG, "Message or username is NULL");
  370. furi_check(false); // Log as an error and return
  371. return;
  372. }
  373. int i = 0;
  374. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  375. {
  376. // Wait for the feed to be received
  377. furi_delay_ms(100);
  378. char dots_str[256] = "Receiving";
  379. // Append dots to the string based on the value of i
  380. int dot_count = i % 4;
  381. int len = strlen(dots_str);
  382. snprintf(dots_str + len, sizeof(dots_str) - len, "%.*s", dot_count, "....");
  383. // Draw the resulting string on the canvas
  384. canvas_draw_str(canvas, 0, 30, dots_str);
  385. i++;
  386. }
  387. flip_social_dialog_stop = true;
  388. furi_timer_stop(fhttp.get_timeout_timer);
  389. break;
  390. case ActionPrev:
  391. // delete message
  392. // remove the message from app_instance->pre_saved_messages
  393. app_instance->pre_saved_messages.messages[flip_social_pre_saved_message_clicked_index] = NULL;
  394. for (uint32_t i = flip_social_pre_saved_message_clicked_index; i < app_instance->pre_saved_messages.count - 1; i++)
  395. {
  396. app_instance->pre_saved_messages.messages[i] = app_instance->pre_saved_messages.messages[i + 1];
  397. }
  398. app_instance->pre_saved_messages.count--;
  399. // add the item to the submenu
  400. submenu_reset(app_instance->submenu_compose);
  401. submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app_instance);
  402. for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  403. {
  404. submenu_add_item(app_instance->submenu_compose, app_instance->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  405. }
  406. // save playlist
  407. save_playlist(&app_instance->pre_saved_messages);
  408. flip_social_dialog_stop = true;
  409. break;
  410. default:
  411. action = ActionNone;
  412. break;
  413. }
  414. if (flip_social_dialog_stop)
  415. {
  416. furi_pubsub_unsubscribe(app_instance->input_event_queue, app_instance->input_event);
  417. flip_social_dialog_shown = false;
  418. flip_social_dialog_stop = false;
  419. if (action == ActionBack || action == ActionNext)
  420. {
  421. action = ActionNone;
  422. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  423. }
  424. else
  425. {
  426. action = ActionNone;
  427. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInCompose);
  428. }
  429. }
  430. }
  431. // function to draw the dialog canvas
  432. static void flip_social_canvas_draw_message(Canvas *canvas, char *user_username, char *user_message, bool is_flipped, bool show_prev, bool show_next)
  433. {
  434. canvas_set_color(canvas, ColorBlack);
  435. canvas_set_font(canvas, FontPrimary);
  436. canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignCenter, user_username);
  437. canvas_set_font(canvas, FontSecondary);
  438. draw_user_message(canvas, user_message, 12);
  439. canvas_set_font(canvas, FontSecondary);
  440. if (show_prev)
  441. {
  442. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  443. canvas_draw_str_aligned(canvas, 9, 54, AlignLeft, AlignTop, "Prev");
  444. }
  445. if (!is_flipped)
  446. {
  447. canvas_draw_icon(canvas, 52, 53, &I_ButtonOK_7x7);
  448. canvas_draw_str_aligned(canvas, 61, 54, AlignLeft, AlignTop, "Flip");
  449. }
  450. else
  451. {
  452. canvas_draw_icon(canvas, 47, 53, &I_ButtonOK_7x7);
  453. canvas_draw_str_aligned(canvas, 56, 54, AlignLeft, AlignTop, "UnFlip");
  454. }
  455. if (show_next)
  456. {
  457. canvas_draw_icon(canvas, 98, 53, &I_ButtonRight_4x7);
  458. canvas_draw_str_aligned(canvas, 107, 54, AlignLeft, AlignTop, "Next");
  459. }
  460. }
  461. // Callback function to handle the feed dialog
  462. static void flip_social_callback_draw_feed(Canvas *canvas, void *model)
  463. {
  464. UNUSED(model);
  465. if (!canvas)
  466. {
  467. FURI_LOG_E(TAG, "Canvas is NULL");
  468. return;
  469. }
  470. if (!app_instance)
  471. {
  472. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  473. return;
  474. }
  475. if (!flip_social_dialog_shown)
  476. {
  477. flip_social_dialog_shown = true;
  478. app_instance->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  479. app_instance->input_event = furi_pubsub_subscribe(app_instance->input_event_queue, on_input, NULL);
  480. }
  481. // handle action
  482. switch (action)
  483. {
  484. case ActionNone:
  485. 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);
  486. break;
  487. case ActionNext:
  488. canvas_clear(canvas);
  489. if (flip_social_feed.index < flip_social_feed.count - 1)
  490. {
  491. flip_social_feed.index++;
  492. }
  493. 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);
  494. action = ActionNone;
  495. break;
  496. case ActionPrev:
  497. canvas_clear(canvas);
  498. if (flip_social_feed.index > 0)
  499. {
  500. flip_social_feed.index--;
  501. }
  502. 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);
  503. action = ActionNone;
  504. break;
  505. case ActionFlip:
  506. canvas_clear(canvas);
  507. flip_social_feed.is_flipped[flip_social_feed.index] = !flip_social_feed.is_flipped[flip_social_feed.index];
  508. 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);
  509. action = ActionNone;
  510. // send post request to flip the message
  511. if (app_instance->login_username_logged_in == NULL)
  512. {
  513. FURI_LOG_E(TAG, "Username is NULL");
  514. return;
  515. }
  516. char payload[256];
  517. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%lu\"}", app_instance->login_username_logged_in, flip_social_feed.ids[flip_social_feed.index]);
  518. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", "{\"Content-Type\":\"application/json\"}", payload);
  519. break;
  520. case ActionBack:
  521. canvas_clear(canvas);
  522. flip_social_dialog_stop = true;
  523. flip_social_feed.index = 0;
  524. action = ActionNone;
  525. break;
  526. default:
  527. break;
  528. }
  529. if (flip_social_dialog_stop)
  530. {
  531. furi_pubsub_unsubscribe(app_instance->input_event_queue, app_instance->input_event);
  532. flip_social_dialog_shown = false;
  533. flip_social_dialog_stop = false;
  534. action = ActionNone;
  535. }
  536. }
  537. /**
  538. * @brief Navigation callback for asynchonously handling the login process.
  539. * @param canvas The canvas to draw on.
  540. * @param model The model - unused
  541. * @return void
  542. */
  543. static void flip_social_callback_draw_login(Canvas *canvas, void *model)
  544. {
  545. UNUSED(model);
  546. if (!canvas)
  547. {
  548. FURI_LOG_E(TAG, "Canvas is NULL");
  549. return;
  550. }
  551. canvas_set_font(canvas, FontSecondary);
  552. if (fhttp.state == INACTIVE)
  553. {
  554. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  555. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  556. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  557. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  558. canvas_draw_str(canvas, 0, 52, "your Dev Board with the");
  559. canvas_draw_str(canvas, 0, 62, "FlipperHTTP firmware.");
  560. return;
  561. }
  562. canvas_draw_str(canvas, 0, 7, "Logging in...");
  563. // Perform login request
  564. if (!flip_social_sent_login_request)
  565. {
  566. 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)
  567. {
  568. canvas_clear(canvas);
  569. canvas_draw_str(canvas, 0, 10, "Please enter your credentials.");
  570. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  571. return;
  572. }
  573. flip_social_sent_login_request = true;
  574. char buffer[256];
  575. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  576. flip_social_login_success = flipper_http_post_request_with_headers("https://www.flipsocial.net/api/login/", "{\"Content-Type\":\"application/json\"}", buffer);
  577. fhttp.state = RECEIVING;
  578. }
  579. // handle response
  580. if (flip_social_sent_login_request && flip_social_login_success)
  581. {
  582. canvas_set_font(canvas, FontSecondary);
  583. canvas_draw_str(canvas, 0, 17, "Request Sent!");
  584. canvas_draw_str(canvas, 0, 32, "Awaiting reponse...");
  585. if (fhttp.state == IDLE && fhttp.received_data != NULL)
  586. {
  587. // read response
  588. if (strstr(fhttp.received_data, "[SUCCESS]") != NULL || strstr(fhttp.received_data, "User found") != NULL)
  589. {
  590. canvas_draw_str(canvas, 0, 42, "Login successful!");
  591. canvas_draw_str(canvas, 0, 62, "Welcome back!");
  592. app_instance->is_logged_in = "true";
  593. 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);
  594. // send user to the logged in submenu
  595. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  596. }
  597. else if (strstr(fhttp.received_data, "User not found") != NULL)
  598. {
  599. canvas_clear(canvas);
  600. canvas_draw_str(canvas, 0, 10, "Account not found...");
  601. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  602. }
  603. else if (strstr(fhttp.received_data, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  604. {
  605. canvas_clear(canvas);
  606. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  607. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  608. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  609. }
  610. else if (strstr(fhttp.received_data, "[ERROR] Failed to connect to Wifi.") != NULL)
  611. {
  612. canvas_clear(canvas);
  613. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  614. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  615. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  616. }
  617. else
  618. {
  619. canvas_draw_str(canvas, 0, 42, "Login failed...");
  620. canvas_draw_str(canvas, 0, 52, "Update your credentials.");
  621. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  622. }
  623. }
  624. else if ((fhttp.state == ISSUE || fhttp.state == INACTIVE) && fhttp.received_data != NULL)
  625. {
  626. if (strstr(fhttp.received_data, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  627. {
  628. canvas_clear(canvas);
  629. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  630. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  631. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  632. }
  633. else if (strstr(fhttp.received_data, "[ERROR] Failed to connect to Wifi.") != NULL)
  634. {
  635. canvas_clear(canvas);
  636. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  637. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  638. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  639. }
  640. else
  641. {
  642. FURI_LOG_E(TAG, "Received an error: %s", fhttp.received_data);
  643. canvas_draw_str(canvas, 0, 42, "Login failed...");
  644. canvas_draw_str(canvas, 0, 52, "Update your credentials.");
  645. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  646. }
  647. }
  648. }
  649. else if (flip_social_sent_login_request && !flip_social_login_success)
  650. {
  651. canvas_clear(canvas);
  652. canvas_draw_str(canvas, 0, 10, "Failed sending request.");
  653. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  654. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  655. }
  656. }
  657. /**
  658. * @brief Navigation callback for asynchonously handling the register process.
  659. * @param canvas The canvas to draw on.
  660. * @param model The model - unused
  661. * @return void
  662. */
  663. static void flip_social_callback_draw_register(Canvas *canvas, void *model)
  664. {
  665. UNUSED(model);
  666. if (!canvas)
  667. {
  668. FURI_LOG_E(TAG, "Canvas is NULL");
  669. return;
  670. }
  671. canvas_set_font(canvas, FontSecondary);
  672. if (fhttp.state == INACTIVE)
  673. {
  674. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  675. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  676. canvas_draw_str(canvas, 0, 32, "If you board is connected,");
  677. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  678. canvas_draw_str(canvas, 0, 52, "your Dev Board with the");
  679. canvas_draw_str(canvas, 0, 62, "FlipperHTTP firmware.");
  680. return;
  681. }
  682. canvas_draw_str(canvas, 0, 7, "Registering...");
  683. // Perform login request
  684. if (!flip_social_sent_register_request)
  685. {
  686. // check if the username and password are valid
  687. 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)
  688. {
  689. canvas_clear(canvas);
  690. canvas_draw_str(canvas, 0, 10, "Please enter your credentials.");
  691. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  692. return;
  693. }
  694. // check if both passwords match
  695. if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
  696. {
  697. canvas_clear(canvas);
  698. canvas_draw_str(canvas, 0, 10, "Passwords do not match.");
  699. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  700. return;
  701. }
  702. char buffer[256];
  703. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
  704. flip_social_register_success = flipper_http_post_request_with_headers("https://www.flipsocial.net/api/register/", "{\"Content-Type\":\"application/json\"}", buffer);
  705. flip_social_sent_register_request = true;
  706. // Set the state to RECEIVING to ensure we continue to see the receiving message
  707. fhttp.state = RECEIVING;
  708. }
  709. // handle response
  710. if (flip_social_sent_register_request && flip_social_register_success)
  711. {
  712. canvas_set_font(canvas, FontSecondary);
  713. canvas_draw_str(canvas, 0, 17, "Request Sent!");
  714. canvas_draw_str(canvas, 0, 32, "Awaiting reponse...");
  715. if (fhttp.state == IDLE)
  716. {
  717. // read response
  718. if (fhttp.received_data != NULL && (strstr(fhttp.received_data, "[SUCCESS]") != NULL || strstr(fhttp.received_data, "User created") != NULL))
  719. {
  720. canvas_draw_str(canvas, 0, 42, "Registeration successful!");
  721. canvas_draw_str(canvas, 0, 62, "Welcome to FlipSocial!");
  722. // set the login credentials
  723. if (app_instance->login_username_logged_out)
  724. {
  725. app_instance->login_username_logged_out = app_instance->register_username_logged_out;
  726. }
  727. if (app_instance->login_password_logged_out)
  728. {
  729. app_instance->login_password_logged_out = app_instance->register_password_logged_out;
  730. app_instance->change_password_logged_in = app_instance->register_password_logged_out;
  731. }
  732. app_instance->is_logged_in = "true";
  733. // save the credentials
  734. 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);
  735. // send user to the logged in submenu
  736. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  737. }
  738. else if (strstr(fhttp.received_data, "Username or password not provided") != NULL)
  739. {
  740. canvas_clear(canvas);
  741. canvas_draw_str(canvas, 0, 10, "Please enter your credentials.");
  742. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  743. }
  744. else if (strstr(fhttp.received_data, "User already exists") != NULL || strstr(fhttp.received_data, "Multiple users found") != NULL)
  745. {
  746. canvas_draw_str(canvas, 0, 42, "Registration failed...");
  747. canvas_draw_str(canvas, 0, 52, "Username already exists.");
  748. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  749. }
  750. else
  751. {
  752. canvas_draw_str(canvas, 0, 42, "Registration failed...");
  753. canvas_draw_str(canvas, 0, 52, "Update your credentials.");
  754. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  755. }
  756. }
  757. else if (fhttp.state == ISSUE || fhttp.state == INACTIVE)
  758. {
  759. if (strstr(fhttp.received_data, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  760. {
  761. canvas_clear(canvas);
  762. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  763. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  764. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  765. }
  766. else if (strstr(fhttp.received_data, "[ERROR] Failed to connect to Wifi.") != NULL)
  767. {
  768. canvas_clear(canvas);
  769. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  770. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  771. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  772. }
  773. else
  774. {
  775. canvas_draw_str(canvas, 0, 42, "Login failed...");
  776. canvas_draw_str(canvas, 0, 52, "Update your credentials.");
  777. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  778. }
  779. }
  780. }
  781. else if (flip_social_sent_register_request && !flip_social_register_success)
  782. {
  783. canvas_clear(canvas);
  784. canvas_draw_str(canvas, 0, 10, "Failed sending request.");
  785. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  786. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  787. }
  788. }
  789. /**
  790. * @brief Navigation callback for exiting the application
  791. * @param context The context - unused
  792. * @return next view id (VIEW_NONE to exit the app)
  793. */
  794. static uint32_t flip_social_callback_exit_app(void *context)
  795. {
  796. // Exit the application
  797. if (!context)
  798. {
  799. FURI_LOG_E(TAG, "Context is NULL");
  800. return VIEW_NONE;
  801. }
  802. UNUSED(context);
  803. return VIEW_NONE; // Return VIEW_NONE to exit the app
  804. }
  805. /**
  806. * @brief Handle ALL submenu item selections.
  807. * @param context The context - FlipSocialApp object.
  808. * @param index The FlipSocialSubmenuIndex item that was clicked.
  809. * @return void
  810. */
  811. static void flip_social_callback_submenu_choices(void *context, uint32_t index)
  812. {
  813. FlipSocialApp *app = (FlipSocialApp *)context;
  814. if (!app)
  815. {
  816. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  817. return;
  818. }
  819. switch (index)
  820. {
  821. case FlipSocialSubmenuLoggedOutIndexLogin:
  822. flip_social_sent_login_request = false;
  823. flip_social_login_success = false;
  824. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  825. break;
  826. case FlipSocialSubmenuLoggedOutIndexRegister:
  827. flip_social_sent_register_request = false;
  828. flip_social_register_success = false;
  829. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  830. break;
  831. case FlipSocialSubmenuLoggedOutIndexAbout:
  832. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  833. break;
  834. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  835. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  836. break;
  837. case FlipSocialSubmenuLoggedInIndexProfile:
  838. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  839. break;
  840. case FlipSocialSubmenuLoggedInIndexFeed:
  841. if (flip_social_get_feed()) // start the async feed request
  842. {
  843. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  844. }
  845. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  846. {
  847. // Wait for the feed to be received
  848. furi_delay_ms(100);
  849. }
  850. furi_timer_stop(fhttp.get_timeout_timer);
  851. if (!flip_social_parse_json_feed()) // parse the JSON before switching to the feed (synchonous)
  852. {
  853. FURI_LOG_E(TAG, "Failed to parse the JSON feed. Using the temporary feed.");
  854. }
  855. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  856. break;
  857. case FlipSocialSubmenuLoggedInIndexCompose:
  858. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  859. break;
  860. case FlipSocialSubmenuLoggedInIndexSettings:
  861. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  862. break;
  863. case FlipSocialSubmenuLoggedInSignOutButton:
  864. app->is_logged_in = "false";
  865. 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);
  866. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  867. break;
  868. case FlipSocialSubmenuComposeIndexAddPreSave:
  869. uart_text_input_set_header_text(app->text_input_logged_in_compose_pre_save_input, "Enter your message:");
  870. uart_text_input_set_result_callback(app->text_input_logged_in_compose_pre_save_input, flip_social_logged_in_compose_pre_save_updated, app, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size, false);
  871. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  872. break;
  873. default:
  874. // handle FlipSocialSubemnuComposeIndexStartIndex + i
  875. // set temp variable to hold the index
  876. flip_social_pre_saved_message_clicked_index = index - FlipSocialSubemnuComposeIndexStartIndex;
  877. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProcessCompose);
  878. break;
  879. }
  880. }
  881. /**
  882. * @brief Navigation callback to go back to the submenu Logged out.
  883. * @param context The context - unused
  884. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  885. */
  886. static uint32_t flip_social_callback_to_submenu_logged_out(void *context)
  887. {
  888. UNUSED(context);
  889. return FlipSocialViewLoggedOutSubmenu;
  890. }
  891. /**
  892. * @brief Navigation callback to go back to the submenu Logged in.
  893. * @param context The context - unused
  894. * @return next view id (FlipSocialViewLoggedInSubmenu)
  895. */
  896. static uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  897. {
  898. UNUSED(context);
  899. // flip_social_get_feed(); // start the feed request
  900. return FlipSocialViewLoggedInSubmenu;
  901. }
  902. /**
  903. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  904. * @param context The context - unused
  905. * @return next view id (FlipSocialViewLoggedOutLogin)
  906. */
  907. static uint32_t flip_social_callback_to_login_logged_out(void *context)
  908. {
  909. UNUSED(context);
  910. flip_social_sent_login_request = false;
  911. flip_social_login_success = false;
  912. return FlipSocialViewLoggedOutLogin;
  913. }
  914. /**
  915. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  916. * @param context The context - unused
  917. * @return next view id (FlipSocialViewLoggedOutRegister)
  918. */
  919. static uint32_t flip_social_callback_to_register_logged_out(void *context)
  920. {
  921. UNUSED(context);
  922. flip_social_sent_register_request = false;
  923. flip_social_register_success = false;
  924. return FlipSocialViewLoggedOutRegister;
  925. }
  926. /**
  927. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  928. * @param context The context - unused
  929. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  930. */
  931. static uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
  932. {
  933. UNUSED(context);
  934. return FlipSocialViewLoggedOutWifiSettings;
  935. }
  936. /**
  937. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  938. * @param context The context - unused
  939. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  940. */
  941. static uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  942. {
  943. UNUSED(context);
  944. return FlipSocialViewLoggedInSettingsWifi;
  945. }
  946. /**
  947. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  948. * @param context The context - unused
  949. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  950. */
  951. static uint32_t flip_social_callback_to_settings_logged_in(void *context)
  952. {
  953. UNUSED(context);
  954. return FlipSocialViewLoggedInSettings;
  955. }
  956. /**
  957. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  958. * @param context The context - unused
  959. * @return next view id (FlipSocialViewLoggedInCompose)
  960. */
  961. static uint32_t flip_social_callback_to_compose_logged_in(void *context)
  962. {
  963. UNUSED(context);
  964. return FlipSocialViewLoggedInCompose;
  965. }
  966. /**
  967. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  968. * @param context The context - unused
  969. * @return next view id (FlipSocialViewLoggedInProfile)
  970. */
  971. static uint32_t flip_social_callback_to_profile_logged_in(void *context)
  972. {
  973. UNUSED(context);
  974. return FlipSocialViewLoggedInProfile;
  975. }
  976. /**
  977. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  978. * @param context The context - FlipSocialApp object.
  979. * @return void
  980. */
  981. static void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  982. {
  983. FlipSocialApp *app = (FlipSocialApp *)context;
  984. if (!app)
  985. {
  986. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  987. return;
  988. }
  989. // Store the entered name
  990. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  991. // Ensure null-termination
  992. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  993. // Update the name item text
  994. if (app->variable_item_logged_out_wifi_settings_ssid)
  995. {
  996. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  997. }
  998. // update the wifi settings
  999. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1000. {
  1001. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1002. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1003. }
  1004. // Save the settings
  1005. 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);
  1006. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  1007. }
  1008. /**
  1009. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  1010. * @param context The context - FlipSocialApp object.
  1011. * @return void
  1012. */
  1013. static void flip_social_logged_out_wifi_settings_password_updated(void *context)
  1014. {
  1015. FlipSocialApp *app = (FlipSocialApp *)context;
  1016. if (!app)
  1017. {
  1018. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1019. return;
  1020. }
  1021. // Store the entered password
  1022. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1023. // Ensure null-termination
  1024. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  1025. // Update the password item text
  1026. if (app->variable_item_logged_out_wifi_settings_password)
  1027. {
  1028. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  1029. }
  1030. // update the wifi settings
  1031. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1032. {
  1033. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1034. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1035. }
  1036. // Save the settings
  1037. 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);
  1038. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  1039. }
  1040. /**
  1041. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  1042. * @param context The context - FlipSocialApp object.
  1043. * @param index The index of the selected item.
  1044. * @return void
  1045. */
  1046. static void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  1047. {
  1048. FlipSocialApp *app = (FlipSocialApp *)context;
  1049. if (!app)
  1050. {
  1051. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1052. return;
  1053. }
  1054. switch (index)
  1055. {
  1056. case 0: // Input SSID
  1057. // Initialize temp_buffer with the current name
  1058. if (app->wifi_ssid_logged_out && strlen(app->wifi_ssid_logged_out) > 0)
  1059. {
  1060. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer_size - 1);
  1061. }
  1062. else
  1063. {
  1064. strncpy(app->wifi_ssid_logged_out_temp_buffer, "", app->wifi_ssid_logged_out_temp_buffer_size - 1);
  1065. }
  1066. uart_text_input_set_header_text(app->text_input_logged_out_wifi_settings_ssid, "Enter SSID");
  1067. uart_text_input_set_result_callback(app->text_input_logged_out_wifi_settings_ssid, flip_social_logged_out_wifi_settings_ssid_updated, app, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size, false);
  1068. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  1069. break;
  1070. case 1: // Input Password
  1071. // Initialize temp_buffer with the current password
  1072. if (app->wifi_password_logged_out && strlen(app->wifi_password_logged_out) > 0)
  1073. {
  1074. strncpy(app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer_size - 1);
  1075. }
  1076. else
  1077. {
  1078. strncpy(app->wifi_password_logged_out_temp_buffer, "", app->wifi_password_logged_out_temp_buffer_size - 1);
  1079. }
  1080. uart_text_input_set_header_text(app->text_input_logged_out_wifi_settings_password, "Enter Password");
  1081. uart_text_input_set_result_callback(app->text_input_logged_out_wifi_settings_password, flip_social_logged_out_wifi_settings_password_updated, app, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size, false);
  1082. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  1083. break;
  1084. default:
  1085. FURI_LOG_E(TAG, "Unknown configuration item index");
  1086. break;
  1087. }
  1088. }
  1089. /**
  1090. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  1091. * @param context The context - FlipSocialApp object.
  1092. * @return void
  1093. */
  1094. static void flip_social_logged_out_login_username_updated(void *context)
  1095. {
  1096. FlipSocialApp *app = (FlipSocialApp *)context;
  1097. if (!app)
  1098. {
  1099. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1100. return;
  1101. }
  1102. // Store the entered name
  1103. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1104. // Ensure null-termination
  1105. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  1106. // Update the name item text
  1107. if (app->variable_item_logged_out_login_username)
  1108. {
  1109. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  1110. }
  1111. // Save the settings
  1112. 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);
  1113. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  1114. }
  1115. /**
  1116. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  1117. * @param context The context - FlipSocialApp object.
  1118. * @return void
  1119. */
  1120. static void flip_social_logged_out_login_password_updated(void *context)
  1121. {
  1122. FlipSocialApp *app = (FlipSocialApp *)context;
  1123. if (!app)
  1124. {
  1125. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1126. return;
  1127. }
  1128. // Store the entered password
  1129. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1130. // Ensure null-termination
  1131. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  1132. // Update the password item text
  1133. if (app->variable_item_logged_out_login_password)
  1134. {
  1135. variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  1136. }
  1137. // Save the settings
  1138. 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);
  1139. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  1140. }
  1141. /**
  1142. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  1143. * @param context The context - FlipSocialApp object.
  1144. * @param index The index of the selected item.
  1145. * @return void
  1146. */
  1147. static void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  1148. {
  1149. FlipSocialApp *app = (FlipSocialApp *)context;
  1150. if (!app)
  1151. {
  1152. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1153. return;
  1154. }
  1155. switch (index)
  1156. {
  1157. case 0: // Input Username
  1158. // Initialize temp_buffer with the current name
  1159. if (app->login_username_logged_out && strlen(app->login_username_logged_out) > 0)
  1160. {
  1161. strncpy(app->login_username_logged_out_temp_buffer, app->login_username_logged_out, app->login_username_logged_out_temp_buffer_size - 1);
  1162. }
  1163. else
  1164. {
  1165. strncpy(app->login_username_logged_out_temp_buffer, "", app->login_username_logged_out_temp_buffer_size - 1);
  1166. }
  1167. uart_text_input_set_header_text(app->text_input_logged_out_login_username, "Enter Username");
  1168. uart_text_input_set_result_callback(app->text_input_logged_out_login_username, flip_social_logged_out_login_username_updated, app, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size, false);
  1169. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  1170. break;
  1171. case 1: // Input Password
  1172. // Initialize temp_buffer with the current password
  1173. if (app->login_password_logged_out && strlen(app->login_password_logged_out) > 0)
  1174. {
  1175. strncpy(app->login_password_logged_out_temp_buffer, app->login_password_logged_out, app->login_password_logged_out_temp_buffer_size - 1);
  1176. }
  1177. else
  1178. {
  1179. strncpy(app->login_password_logged_out_temp_buffer, "", app->login_password_logged_out_temp_buffer_size - 1);
  1180. }
  1181. uart_text_input_set_header_text(app->text_input_logged_out_login_password, "Enter Password");
  1182. uart_text_input_set_result_callback(app->text_input_logged_out_login_password, flip_social_logged_out_login_password_updated, app, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size, false);
  1183. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  1184. break;
  1185. case 2: // Login Button
  1186. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessLogin);
  1187. break;
  1188. default:
  1189. FURI_LOG_E(TAG, "Unknown configuration item index");
  1190. break;
  1191. }
  1192. }
  1193. /**
  1194. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  1195. * @param context The context - FlipSocialApp object.
  1196. * @return void
  1197. */
  1198. static void flip_social_logged_out_register_username_updated(void *context)
  1199. {
  1200. FlipSocialApp *app = (FlipSocialApp *)context;
  1201. if (!app)
  1202. {
  1203. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1204. return;
  1205. }
  1206. // Store the entered name
  1207. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  1208. // Ensure null-termination
  1209. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  1210. // Update the name item text
  1211. if (app->variable_item_logged_out_register_username)
  1212. {
  1213. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  1214. }
  1215. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1216. }
  1217. /**
  1218. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  1219. * @param context The context - FlipSocialApp object.
  1220. * @return void
  1221. */
  1222. static void flip_social_logged_out_register_password_updated(void *context)
  1223. {
  1224. FlipSocialApp *app = (FlipSocialApp *)context;
  1225. if (!app)
  1226. {
  1227. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1228. return;
  1229. }
  1230. // Store the entered password
  1231. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  1232. // Ensure null-termination
  1233. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  1234. // Update the password item text
  1235. if (app->variable_item_logged_out_register_password)
  1236. {
  1237. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  1238. }
  1239. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1240. }
  1241. /**
  1242. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  1243. * @param context The context - FlipSocialApp object.
  1244. * @return void
  1245. */
  1246. static void flip_social_logged_out_register_password_2_updated(void *context)
  1247. {
  1248. FlipSocialApp *app = (FlipSocialApp *)context;
  1249. if (!app)
  1250. {
  1251. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1252. return;
  1253. }
  1254. // Store the entered password
  1255. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  1256. // Ensure null-termination
  1257. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  1258. // Update the password item text
  1259. if (app->variable_item_logged_out_register_password_2)
  1260. {
  1261. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  1262. }
  1263. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1264. }
  1265. /**
  1266. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  1267. * @param context The context - FlipSocialApp object.
  1268. * @param index The index of the selected item.
  1269. * @return void
  1270. */
  1271. static void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  1272. {
  1273. FlipSocialApp *app = (FlipSocialApp *)context;
  1274. if (!app)
  1275. {
  1276. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1277. return;
  1278. }
  1279. switch (index)
  1280. {
  1281. case 0: // Input Username
  1282. // Initialize temp_buffer with the current name
  1283. if (app->register_username_logged_out && strlen(app->register_username_logged_out) > 0)
  1284. {
  1285. strncpy(app->register_username_logged_out_temp_buffer, app->register_username_logged_out, app->register_username_logged_out_temp_buffer_size - 1);
  1286. }
  1287. else
  1288. {
  1289. strncpy(app->register_username_logged_out_temp_buffer, "", app->register_username_logged_out_temp_buffer_size - 1);
  1290. }
  1291. uart_text_input_set_header_text(app->text_input_logged_out_register_username, "Enter Username");
  1292. uart_text_input_set_result_callback(app->text_input_logged_out_register_username, flip_social_logged_out_register_username_updated, app, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size, false);
  1293. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  1294. break;
  1295. case 1: // Input Password
  1296. // Initialize temp_buffer with the current password
  1297. if (app->register_password_logged_out && strlen(app->register_password_logged_out) > 0)
  1298. {
  1299. strncpy(app->register_password_logged_out_temp_buffer, app->register_password_logged_out, app->register_password_logged_out_temp_buffer_size - 1);
  1300. }
  1301. else
  1302. {
  1303. strncpy(app->register_password_logged_out_temp_buffer, "", app->register_password_logged_out_temp_buffer_size - 1);
  1304. }
  1305. uart_text_input_set_header_text(app->text_input_logged_out_register_password, "Enter Password");
  1306. uart_text_input_set_result_callback(app->text_input_logged_out_register_password, flip_social_logged_out_register_password_updated, app, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size, false);
  1307. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  1308. break;
  1309. case 2: // Input Password 2
  1310. // Initialize temp_buffer with the current password
  1311. if (app->register_password_2_logged_out && strlen(app->register_password_2_logged_out) > 0)
  1312. {
  1313. strncpy(app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer_size - 1);
  1314. }
  1315. else
  1316. {
  1317. strncpy(app->register_password_2_logged_out_temp_buffer, "", app->register_password_2_logged_out_temp_buffer_size - 1);
  1318. }
  1319. uart_text_input_set_header_text(app->text_input_logged_out_register_password_2, "Enter Password Again");
  1320. uart_text_input_set_result_callback(app->text_input_logged_out_register_password_2, flip_social_logged_out_register_password_2_updated, app, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size, false);
  1321. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  1322. break;
  1323. case 3: // Register button
  1324. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessRegister);
  1325. break;
  1326. default:
  1327. FURI_LOG_E(TAG, "Unknown configuration item index");
  1328. break;
  1329. }
  1330. }
  1331. /**
  1332. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  1333. * @param context The context - FlipSocialApp object.
  1334. * @return void
  1335. */
  1336. static void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  1337. {
  1338. FlipSocialApp *app = (FlipSocialApp *)context;
  1339. if (!app)
  1340. {
  1341. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1342. return;
  1343. }
  1344. // Store the entered name
  1345. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  1346. // Ensure null-termination
  1347. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  1348. // Update the name item text
  1349. if (app->variable_item_logged_in_wifi_settings_ssid)
  1350. {
  1351. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  1352. }
  1353. // update the wifi settings
  1354. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1355. {
  1356. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1357. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1358. }
  1359. // Save the settings
  1360. 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);
  1361. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1362. }
  1363. /**
  1364. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  1365. * @param context The context - FlipSocialApp object.
  1366. * @return void
  1367. */
  1368. static void flip_social_logged_in_wifi_settings_password_updated(void *context)
  1369. {
  1370. FlipSocialApp *app = (FlipSocialApp *)context;
  1371. if (!app)
  1372. {
  1373. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1374. return;
  1375. }
  1376. // Store the entered password
  1377. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  1378. // Ensure null-termination
  1379. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  1380. // Update the password item text
  1381. if (app->variable_item_logged_in_wifi_settings_password)
  1382. {
  1383. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  1384. }
  1385. // update the wifi settings
  1386. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1387. {
  1388. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1389. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1390. }
  1391. // Save the settings
  1392. 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);
  1393. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1394. }
  1395. /**
  1396. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  1397. * @param context The context - FlipSocialApp object.
  1398. * @param index The index of the selected item.
  1399. * @return void
  1400. */
  1401. static void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  1402. {
  1403. FlipSocialApp *app = (FlipSocialApp *)context;
  1404. if (!app)
  1405. {
  1406. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1407. return;
  1408. }
  1409. switch (index)
  1410. {
  1411. case 0: // Input SSID
  1412. // Initialize temp_buffer with the current name
  1413. if (app->wifi_ssid_logged_in && strlen(app->wifi_ssid_logged_in) > 0)
  1414. {
  1415. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size - 1);
  1416. }
  1417. else
  1418. {
  1419. strncpy(app->wifi_ssid_logged_in_temp_buffer, "", app->wifi_ssid_logged_in_temp_buffer_size - 1);
  1420. }
  1421. uart_text_input_set_header_text(app->text_input_logged_in_wifi_settings_ssid, "Enter SSID");
  1422. uart_text_input_set_result_callback(app->text_input_logged_in_wifi_settings_ssid, flip_social_logged_in_wifi_settings_ssid_updated, app, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size, false);
  1423. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  1424. break;
  1425. case 1: // Input Password
  1426. // Initialize temp_buffer with the current password
  1427. if (app->wifi_password_logged_in && strlen(app->wifi_password_logged_in) > 0)
  1428. {
  1429. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size - 1);
  1430. }
  1431. else
  1432. {
  1433. strncpy(app->wifi_password_logged_in_temp_buffer, "", app->wifi_password_logged_in_temp_buffer_size - 1);
  1434. }
  1435. uart_text_input_set_header_text(app->text_input_logged_in_wifi_settings_password, "Enter Password");
  1436. uart_text_input_set_result_callback(app->text_input_logged_in_wifi_settings_password, flip_social_logged_in_wifi_settings_password_updated, app, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size, false);
  1437. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  1438. break;
  1439. default:
  1440. FURI_LOG_E(TAG, "Unknown configuration item index");
  1441. break;
  1442. }
  1443. }
  1444. /**
  1445. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  1446. * @param context The context - FlipSocialApp object.
  1447. * @return void
  1448. */
  1449. static void flip_social_logged_in_compose_pre_save_updated(void *context)
  1450. {
  1451. FlipSocialApp *app = (FlipSocialApp *)context;
  1452. if (!app)
  1453. {
  1454. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1455. return;
  1456. }
  1457. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  1458. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  1459. {
  1460. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  1461. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1462. return;
  1463. }
  1464. // Store the entered message
  1465. strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
  1466. // Ensure null-termination
  1467. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  1468. // add the item to the submenu
  1469. submenu_reset(app->submenu_compose);
  1470. // loop through the items and add them to the submenu
  1471. app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
  1472. app->pre_saved_messages.count++;
  1473. submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  1474. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  1475. {
  1476. submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  1477. }
  1478. // save playlist
  1479. save_playlist(&app->pre_saved_messages);
  1480. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1481. }
  1482. /**
  1483. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1484. * @param context The context - FlipSocialApp object.
  1485. * @return void
  1486. */
  1487. static void flip_social_logged_in_profile_change_password_updated(void *context)
  1488. {
  1489. FlipSocialApp *app = (FlipSocialApp *)context;
  1490. if (!app)
  1491. {
  1492. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1493. return;
  1494. }
  1495. // Correct type: old_pass should be a pointer to a string (char *)
  1496. const char *old_password = app->login_password_logged_out;
  1497. // Store the entered message
  1498. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1499. // store the entered password in the logged out password
  1500. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1501. // Ensure null-termination
  1502. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1503. // Update the message item text
  1504. if (app->variable_item_logged_in_profile_change_password)
  1505. {
  1506. variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1507. }
  1508. // send post request to change password
  1509. char payload[256];
  1510. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1511. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/change-password/", "Content-Type: application/json", payload))
  1512. {
  1513. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1514. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1515. return;
  1516. }
  1517. // Save the settings
  1518. 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);
  1519. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  1520. }
  1521. /**
  1522. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1523. * @param context The context - FlipSocialApp object.
  1524. * @param index The index of the selected item.
  1525. * @return void
  1526. */
  1527. static void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1528. {
  1529. FlipSocialApp *app = (FlipSocialApp *)context;
  1530. if (!app)
  1531. {
  1532. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1533. return;
  1534. }
  1535. switch (index)
  1536. {
  1537. case 0: // Change Username
  1538. // do nothing since username cannot be changed
  1539. break;
  1540. case 1: // Change Password
  1541. // Initialize temp_buffer with the current password
  1542. if (app->change_password_logged_in && strlen(app->change_password_logged_in) > 0)
  1543. {
  1544. strncpy(app->change_password_logged_in_temp_buffer, app->change_password_logged_in, app->change_password_logged_in_temp_buffer_size - 1);
  1545. }
  1546. else
  1547. {
  1548. strncpy(app->change_password_logged_in_temp_buffer, "", app->change_password_logged_in_temp_buffer_size - 1);
  1549. }
  1550. uart_text_input_set_header_text(app->text_input_logged_in_change_password, "Enter New Password");
  1551. uart_text_input_set_result_callback(app->text_input_logged_in_change_password, flip_social_logged_in_profile_change_password_updated, app, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size, false);
  1552. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1553. break;
  1554. default:
  1555. FURI_LOG_E(TAG, "Unknown configuration item index");
  1556. break;
  1557. }
  1558. }
  1559. /**
  1560. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  1561. * @param context The context - FlipSocialApp object.
  1562. * @param index The index of the selected item.
  1563. * @return void
  1564. */
  1565. static void flip_social_text_input_logged_in_settings_item_selected(void *context, uint32_t index)
  1566. {
  1567. FlipSocialApp *app = (FlipSocialApp *)context;
  1568. if (!app)
  1569. {
  1570. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1571. return;
  1572. }
  1573. switch (index)
  1574. {
  1575. case 0: // About
  1576. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  1577. break;
  1578. case 1: // Wifi
  1579. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1580. break;
  1581. default:
  1582. break;
  1583. }
  1584. }
  1585. #endif // FLIP_SOCIAL_CALLBACK_H