| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794 |
- #include <callback/flip_social_callback.h>
- // Below added by Derek Jamison
- // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
- #ifdef DEVELOPMENT
- #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
- #define DEV_CRASH() furi_crash()
- #else
- #define FURI_LOG_DEV(tag, format, ...)
- #define DEV_CRASH()
- #endif
- static void flip_social_request_error_draw(Canvas *canvas)
- {
- if (canvas == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_request_error_draw - canvas is NULL");
- DEV_CRASH();
- return;
- }
- if (fhttp.last_response != NULL)
- {
- if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- else if (strstr(fhttp.last_response, "[PONG]") != NULL)
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
- }
- else
- {
- canvas_clear(canvas);
- FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
- canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
- }
- }
- else
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- }
- static bool flip_social_login_fetch(DataLoaderModel *model)
- {
- UNUSED(model);
- if (!app_instance)
- {
- FURI_LOG_E(TAG, "app_instance is NULL");
- return false;
- }
- 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)
- {
- return false;
- }
- char buffer[256];
- snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
- auth_headers_alloc();
- return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", auth_headers, buffer);
- }
- static char *flip_social_login_parse(DataLoaderModel *model)
- {
- UNUSED(model);
- if (!app_instance)
- {
- FURI_LOG_E(TAG, "app_instance is NULL");
- return "Failed to login...";
- }
- if (!fhttp.last_response)
- {
- flipper_http_deinit();
- return "Failed to login...";
- }
- // read response
- if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
- {
- flipper_http_deinit();
- app_instance->is_logged_in = "true";
- // set the logged_in_username and change_password_logged_in
- if (app_instance->login_username_logged_out)
- {
- strcpy(app_instance->login_username_logged_in, app_instance->login_username_logged_out);
- }
- if (app_instance->login_password_logged_out)
- {
- strcpy(app_instance->change_password_logged_in, app_instance->login_password_logged_out);
- }
- 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->change_bio_logged_in, app_instance->is_logged_in);
- // send user to the logged in submenu
- view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
- return "Login successful!";
- }
- else if (strstr(fhttp.last_response, "User not found") != NULL)
- {
- flipper_http_deinit();
- return "Account not found...";
- }
- else
- {
- flipper_http_deinit();
- return "Failed to login...";
- }
- }
- static void flip_social_login_switch_to_view(FlipSocialApp *app)
- {
- flip_social_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 1, flip_social_callback_to_login_logged_out, FlipSocialViewLoader);
- }
- static bool flip_social_register_fetch(DataLoaderModel *model)
- {
- UNUSED(model);
- // check if the username and password are valid
- 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)
- {
- FURI_LOG_E(TAG, "Username or password is NULL");
- return false;
- }
- // check if both passwords match
- if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
- {
- FURI_LOG_E(TAG, "Passwords do not match");
- return false;
- }
- char buffer[128];
- snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
- return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", buffer);
- }
- static char *flip_social_register_parse(DataLoaderModel *model)
- {
- UNUSED(model);
- // read response
- if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
- {
- flipper_http_deinit();
- // set the login credentials
- if (app_instance->login_username_logged_out)
- {
- app_instance->login_username_logged_out = app_instance->register_username_logged_out;
- }
- if (app_instance->login_password_logged_out)
- {
- app_instance->login_password_logged_out = app_instance->register_password_logged_out;
- app_instance->change_password_logged_in = app_instance->register_password_logged_out;
- }
- if (app_instance->login_username_logged_in)
- {
- app_instance->login_username_logged_in = app_instance->register_username_logged_out;
- }
- app_instance->is_logged_in = "true";
- // update header credentials
- auth_headers_alloc();
- // save the credentials
- 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->change_bio_logged_in, app_instance->is_logged_in);
- // send user to the logged in submenu
- view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
- return "Registration successful!\nWelcome to FlipSocial!";
- }
- else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
- {
- flipper_http_deinit();
- return "Please enter your credentials.\nPress BACK to return.";
- }
- else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
- {
- flipper_http_deinit();
- return "Registration failed...\nUsername already exists.\nPress BACK to return.";
- }
- else
- {
- flipper_http_deinit();
- return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
- }
- }
- static void flip_social_register_switch_to_view(FlipSocialApp *app)
- {
- flip_social_generic_switch_to_view(app, "Registering...", flip_social_register_fetch, flip_social_register_parse, 1, flip_social_callback_to_register_logged_out, FlipSocialViewLoader);
- }
- /**
- * @brief Navigation callback to go back to the submenu Logged out.
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedOutSubmenu)
- */
- uint32_t flip_social_callback_to_submenu_logged_out(void *context)
- {
- UNUSED(context);
- return FlipSocialViewLoggedOutSubmenu;
- }
- /**
- * @brief Navigation callback to go back to the submenu Logged in.
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInSubmenu)
- */
- uint32_t flip_social_callback_to_submenu_logged_in(void *context)
- {
- UNUSED(context);
- free_about_widget(false);
- //
- flip_social_free_friends();
- flip_social_free_message_users();
- flip_feed_info_free();
- if (flip_feed_item)
- {
- free(flip_feed_item);
- flip_feed_item = NULL;
- }
- // free the about widget if it exists
- free_about_widget(true);
- flip_social_free_explore_dialog();
- flip_social_free_friends_dialog();
- flip_social_free_messages_dialog();
- flip_social_free_compose_dialog();
- return FlipSocialViewLoggedInSubmenu;
- }
- /**
- * @brief Navigation callback to bring the user back to the (Logged out) Login screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedOutLogin)
- */
- uint32_t flip_social_callback_to_login_logged_out(void *context)
- {
- UNUSED(context);
- flip_social_sent_login_request = false;
- flip_social_login_success = false;
- return FlipSocialViewVariableItemList;
- }
- /**
- * @brief Navigation callback to bring the user back to the (Logged out) Register screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedOutRegister)
- */
- uint32_t flip_social_callback_to_register_logged_out(void *context)
- {
- UNUSED(context);
- flip_social_sent_register_request = false;
- flip_social_register_success = false;
- return FlipSocialViewVariableItemList;
- }
- /**
- * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedOutWifiSettings)
- */
- uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
- {
- UNUSED(context);
- return FlipSocialViewVariableItemList;
- }
- /**
- * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInSettingsWifi)
- */
- uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
- {
- UNUSED(context);
- return FlipSocialViewVariableItemList;
- }
- /**
- * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInSettingsWifi)
- */
- uint32_t flip_social_callback_to_settings_logged_in(void *context)
- {
- UNUSED(context);
- return FlipSocialViewSubmenu;
- }
- /**
- * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInCompose)
- */
- uint32_t flip_social_callback_to_compose_logged_in(void *context)
- {
- UNUSED(context);
- return FlipSocialViewSubmenu;
- }
- /**
- * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInProfile)
- */
- uint32_t flip_social_callback_to_profile_logged_in(void *context)
- {
- UNUSED(context);
- return FlipSocialViewVariableItemList;
- }
- /**
- * @brief Navigation callback to bring the user back to the Explore submenu
- * @param context The context - unused
- * @return next view id (FlipSocialViewSubmenu)
- */
- uint32_t flip_social_callback_to_explore_logged_in(void *context)
- {
- UNUSED(context);
- flip_social_dialog_stop = false;
- flip_social_dialog_shown = false;
- if (flip_social_explore)
- {
- flip_social_explore->index = 0;
- }
- return FlipSocialViewSubmenu;
- }
- /**
- * @brief Navigation callback to bring the user back to the Friends submenu
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
- */
- uint32_t flip_social_callback_to_friends_logged_in(void *context)
- {
- UNUSED(context);
- flip_social_dialog_stop = false;
- flip_social_dialog_shown = false;
- flip_social_friends->index = 0;
- return FlipSocialViewSubmenu;
- }
- /**
- * @brief Navigation callback to bring the user back to the Messages submenu
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInMessagesSubmenu)
- */
- uint32_t flip_social_callback_to_messages_logged_in(void *context)
- {
- UNUSED(context);
- return FlipSocialViewSubmenu;
- }
- /**
- * @brief Navigation callback to bring the user back to the User Choices screen
- * @param context The context - unused
- * @return next view id (FlipSocialViewLoggedInMessagesUserChoices)
- */
- uint32_t flip_social_callback_to_messages_user_choices(void *context)
- {
- UNUSED(context);
- return FlipSocialViewSubmenu;
- }
- /**
- * @brief Navigation callback for exiting the application
- * @param context The context - unused
- * @return next view id (VIEW_NONE to exit the app)
- */
- uint32_t flip_social_callback_exit_app(void *context)
- {
- // Exit the application
- UNUSED(context);
- free_all(true, true);
- return VIEW_NONE;
- }
- static void explore_dialog_callback(DialogExResult result, void *context)
- {
- furi_assert(context);
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (result == DialogExResultLeft) // Remove
- {
- if (flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- // remove friend
- char remove_payload[128];
- snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
- auth_headers_alloc();
- flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- flip_social_free_explore_dialog();
- furi_delay_ms(1000);
- flipper_http_deinit();
- }
- }
- else if (result == DialogExResultRight)
- {
- if (flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- // add friend
- char add_payload[128];
- snprintf(add_payload, sizeof(add_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
- auth_headers_alloc();
- flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/add-friend/", auth_headers, add_payload);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- flip_social_free_explore_dialog();
- furi_delay_ms(1000);
- flipper_http_deinit();
- }
- }
- }
- static void friends_dialog_callback(DialogExResult result, void *context)
- {
- furi_assert(context);
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (result == DialogExResultLeft) // Remove
- {
- if (flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- // remove friend
- char remove_payload[128];
- snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_friends->usernames[flip_social_friends->index]);
- auth_headers_alloc();
- flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- flip_social_free_friends_dialog();
- furi_delay_ms(1000);
- flipper_http_deinit();
- }
- }
- }
- void messages_dialog_callback(DialogExResult result, void *context)
- {
- furi_assert(context);
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (result == DialogExResultLeft) // Previous message
- {
- if (flip_social_messages->index > 0)
- {
- flip_social_messages->index--;
- dialog_ex_reset(app->dialog_messages);
- dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
- dialog_ex_set_text(app->dialog_messages, updated_user_message(flip_social_messages->messages[flip_social_messages->index]), 0, 10, AlignLeft, AlignTop);
- if (flip_social_messages->index != 0)
- {
- dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
- }
- dialog_ex_set_right_button_text(app->dialog_messages, "Next");
- dialog_ex_set_center_button_text(app->dialog_messages, "Create");
- // switch view, free dialog, re-alloc dialog, switch back to dialog
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
- flip_social_free_messages_dialog();
- messages_dialog_alloc(false);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
- }
- }
- else if (result == DialogExResultRight) // Next message
- {
- if (flip_social_messages->index < flip_social_messages->count - 1)
- {
- flip_social_messages->index++;
- dialog_ex_reset(app->dialog_messages);
- dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
- dialog_ex_set_text(app->dialog_messages, updated_user_message(flip_social_messages->messages[flip_social_messages->index]), 0, 10, AlignLeft, AlignTop);
- dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
- if (flip_social_messages->index != flip_social_messages->count - 1)
- {
- dialog_ex_set_right_button_text(app->dialog_messages, "Next");
- }
- dialog_ex_set_center_button_text(app->dialog_messages, "Create");
- // switch view, free dialog, re-alloc dialog, switch back to dialog
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
- flip_social_free_messages_dialog();
- messages_dialog_alloc(false);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
- }
- }
- else if (result == DialogExResultCenter) // new message
- {
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedInMessagesNewMessageInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- }
- }
- static void compose_dialog_callback(DialogExResult result, void *context)
- {
- furi_assert(context);
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (result == DialogExResultLeft) // Delete
- {
- // Ensure index is within bounds
- if (app_instance->pre_saved_messages.index >= app_instance->pre_saved_messages.count)
- {
- FURI_LOG_E(TAG, "Invalid index for deletion: %zu", app_instance->pre_saved_messages.index);
- return;
- }
- // Shift messages to remove the selected message
- for (size_t i = app_instance->pre_saved_messages.index; i < app_instance->pre_saved_messages.count - 1; i++)
- {
- strncpy(app_instance->pre_saved_messages.messages[i],
- app_instance->pre_saved_messages.messages[i + 1],
- MAX_MESSAGE_LENGTH);
- }
- // Clear the last message after shifting
- memset(app_instance->pre_saved_messages.messages[app_instance->pre_saved_messages.count - 1], 0, MAX_MESSAGE_LENGTH);
- app_instance->pre_saved_messages.count--;
- // Reset and rebuild the submenu
- submenu_reset(app_instance->submenu);
- submenu_add_item(app_instance->submenu, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
- for (size_t i = 0; i < app_instance->pre_saved_messages.count; i++)
- {
- submenu_add_item(app_instance->submenu,
- app_instance->pre_saved_messages.messages[i],
- FlipSocialSubemnuComposeIndexStartIndex + i,
- flip_social_callback_submenu_choices,
- app);
- }
- // Save the updated playlist
- save_playlist(&app_instance->pre_saved_messages);
- // Switch back to the compose view
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- // Free the dialog resources
- flip_social_free_compose_dialog();
- }
- else if (result == DialogExResultRight) // Post
- {
- // post the message
- // send selected_message
- if (!flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
- return;
- }
- if (selected_message && app_instance->login_username_logged_in)
- {
- if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
- {
- FURI_LOG_E(TAG, "Message is too long");
- flipper_http_deinit();
- return;
- }
- // Send the selected_message
- char command[256];
- snprintf(command, sizeof(command), "{\"username\":\"%s\",\"content\":\"%s\"}",
- app_instance->login_username_logged_in, selected_message);
- if (!flipper_http_post_request_with_headers(
- "https://www.flipsocial.net/api/feed/post/",
- auth_headers,
- command))
- {
- FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
- flipper_http_deinit();
- return;
- }
- fhttp.state = RECEIVING;
- furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
- }
- else
- {
- FURI_LOG_E(TAG, "Message or username is NULL");
- flipper_http_deinit();
- return;
- }
- while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
- {
- furi_delay_ms(100);
- }
- if (flip_social_load_initial_feed(false, 1))
- {
- flip_social_free_compose_dialog();
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to load the initial feed");
- flipper_http_deinit();
- }
- }
- }
- void feed_dialog_callback(DialogExResult result, void *context)
- {
- furi_assert(context);
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (result == DialogExResultLeft) // Previous message
- {
- if (flip_feed_info->index > 0)
- {
- flip_feed_info->index--;
- }
- // switch view, free dialog, re-alloc dialog, switch back to dialog
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
- flip_social_free_feed_view();
- // load feed item
- if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
- {
- FURI_LOG_E(TAG, "Failed to load nexy feed post");
- fhttp.state = ISSUE;
- return;
- }
- if (feed_view_alloc())
- {
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to allocate feed dialog");
- fhttp.state = ISSUE;
- return;
- }
- }
- else if (result == DialogExResultRight) // Next message
- {
- if (flip_feed_info->index < flip_feed_info->count - 1)
- {
- flip_feed_info->index++;
- }
- // switch view, free dialog, re-alloc dialog, switch back to dialog
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
- flip_social_free_feed_view();
- // load feed item
- if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
- {
- FURI_LOG_E(TAG, "Failed to load nexy feed post");
- fhttp.state = ISSUE;
- return;
- }
- if (feed_view_alloc())
- {
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to allocate feed dialog");
- fhttp.state = ISSUE;
- return;
- }
- }
- else if (result == DialogExResultCenter) // Flip/Unflip
- {
- // Moved to above the is_flipped check
- if (!flip_feed_item->is_flipped)
- {
- // increase the flip count
- flip_feed_item->flips++;
- }
- else
- {
- // decrease the flip count
- flip_feed_item->flips--;
- }
- // change the flip status
- flip_feed_item->is_flipped = !flip_feed_item->is_flipped;
- // send post request to flip the message
- if (app_instance->login_username_logged_in == NULL)
- {
- FURI_LOG_E(TAG, "Username is NULL");
- return;
- }
- if (!flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
- return;
- }
- auth_headers_alloc();
- char payload[256];
- snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%u\"}", app_instance->login_username_logged_in, flip_feed_item->id);
- if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", auth_headers, payload))
- {
- // save feed item
- char new_save[256];
- snprintf(new_save, sizeof(new_save), "{\"id\":%u,\"username\":\"%s\",\"message\":\"%s\",\"flip_count\":%u,\"flipped\":%s}",
- flip_feed_item->id, flip_feed_item->username, flip_feed_item->message, flip_feed_item->flips, flip_feed_item->is_flipped ? "true" : "false");
- // if (!flip_social_save_post((char *)flip_feed_item->id, new_save))
- // {
- // FURI_LOG_E(TAG, "Failed to save the feed post");
- // fhttp.state = ISSUE;
- // return;
- // }
- }
- // switch view, free dialog, re-alloc dialog, switch back to dialog
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
- flip_social_free_feed_view();
- if (feed_view_alloc())
- {
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to allocate feed dialog");
- }
- furi_delay_ms(1000);
- flipper_http_deinit();
- }
- }
- static bool flip_social_get_user_info()
- {
- if (!flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
- fhttp.state = ISSUE;
- return false;
- }
- char url[256];
- snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/users/%s/extended/", flip_social_explore->usernames[flip_social_explore->index]);
- if (!flipper_http_get_request_with_headers(url, auth_headers))
- {
- FURI_LOG_E(TAG, "Failed to send HTTP request for user info");
- fhttp.state = ISSUE;
- return false;
- }
- fhttp.state = RECEIVING;
- return true;
- }
- static bool flip_social_parse_user_info()
- {
- if (fhttp.last_response == NULL)
- {
- FURI_LOG_E(TAG, "Response is NULL");
- return false;
- }
- if (!app_instance->explore_user_bio)
- {
- FURI_LOG_E(TAG, "App instance is NULL");
- return false;
- }
- char *bio = get_json_value("bio", fhttp.last_response);
- char *friends = get_json_value("friends", fhttp.last_response);
- bool parse_success = false;
- if (bio && friends)
- {
- if (strlen(bio) != 0)
- {
- snprintf(app_instance->explore_user_bio, MAX_MESSAGE_LENGTH, "%s (%s friends)", bio, friends);
- }
- else
- {
- snprintf(app_instance->explore_user_bio, MAX_MESSAGE_LENGTH, "%s friends", friends);
- }
- free(bio);
- free(friends);
- parse_success = true;
- }
- flipper_http_deinit();
- return parse_success;
- }
- /**
- * @brief Handle ALL submenu item selections.
- * @param context The context - FlipSocialApp object.
- * @param index The FlipSocialSubmenuIndex item that was clicked.
- * @return void
- */
- void flip_social_callback_submenu_choices(void *context, uint32_t index)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- switch (index)
- {
- case FlipSocialSubmenuLoggedOutIndexLogin:
- flip_social_sent_login_request = false;
- flip_social_login_success = false;
- free_all(true, true);
- if (!alloc_variable_item_list(FlipSocialViewLoggedOutLogin))
- {
- FURI_LOG_E(TAG, "Failed to allocate variable item list");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- break;
- case FlipSocialSubmenuLoggedOutIndexRegister:
- flip_social_sent_register_request = false;
- flip_social_register_success = false;
- free_all(true, true);
- if (!alloc_variable_item_list(FlipSocialViewLoggedOutRegister))
- {
- FURI_LOG_E(TAG, "Failed to allocate variable item list");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- break;
- case FlipSocialSubmenuLoggedOutIndexAbout:
- if (!about_widget_alloc(false))
- {
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
- break;
- case FlipSocialSubmenuLoggedOutIndexWifiSettings:
- free_all(false, false);
- if (!alloc_variable_item_list(FlipSocialViewLoggedOutWifiSettings))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- break;
- case FlipSocialSubmenuLoggedInIndexProfile:
- free_all(true, true);
- if (!alloc_variable_item_list(FlipSocialViewLoggedInProfile))
- {
- FURI_LOG_E(TAG, "Failed to allocate variable item list");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- break;
- case FlipSocialSubmenuLoggedInIndexMessages:
- free_all(true, true);
- if (!alloc_submenu(FlipSocialViewLoggedInMessagesSubmenu))
- {
- FURI_LOG_E(TAG, "Failed to allocate submenu");
- return;
- }
- flipper_http_loading_task(
- flip_social_get_message_users, // get the message users
- flip_social_parse_json_message_users, // parse the message users
- FlipSocialViewSubmenu, // switch to the messages submenu if successful
- FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
- &app->view_dispatcher); // view dispatcher
- break;
- case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
- // they need to search for the user to send a message
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedInMessageUsersInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case FlipSocialSubmenuLoggedInIndexFeed:
- free_all(true, true);
- if (!flip_social_load_initial_feed(true, 1))
- {
- FURI_LOG_E(TAG, "Failed to load the initial feed");
- return;
- }
- break;
- case FlipSocialSubmenuExploreIndex:
- free_all(true, true);
- if (!alloc_text_input(FlipSocialViewLoggedInExploreInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case FlipSocialSubmenuLoggedInIndexCompose:
- free_all(true, true);
- if (!alloc_submenu(FlipSocialViewLoggedInCompose))
- {
- FURI_LOG_E(TAG, "Failed to allocate submenu");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- break;
- case FlipSocialSubmenuLoggedInIndexSettings:
- free_all(true, true);
- if (!alloc_submenu(FlipSocialViewLoggedInSettings))
- {
- FURI_LOG_E(TAG, "Failed to allocate submenu");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- break;
- case FlipSocialSubmenuLoggedInIndexAbout:
- free_all(true, false);
- if (!about_widget_alloc(true))
- {
- FURI_LOG_E(TAG, "Failed to allocate about widget");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
- break;
- case FlipSocialSubmenuLoggedInIndexWifiSettings:
- free_all(true, false);
- if (!alloc_variable_item_list(FlipSocialViewLoggedInSettingsWifi))
- {
- FURI_LOG_E(TAG, "Failed to allocate variable item list");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- break;
- case FlipSocialSubmenuLoggedInIndexUserSettings:
- free_all(true, false);
- if (!alloc_variable_item_list(FlipSocialViewLoggedInSettingsUser))
- {
- FURI_LOG_E(TAG, "Failed to allocate variable item list");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- break;
- case FlipSocialSubmenuLoggedInSignOutButton:
- free_all(true, true);
- app->is_logged_in = "false";
- save_settings(app->wifi_ssid_logged_out, app->wifi_password_logged_out, app->login_username_logged_out, app->login_username_logged_in, app->login_password_logged_out, app->change_password_logged_in, app->change_bio_logged_in, app->is_logged_in);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
- break;
- case FlipSocialSubmenuComposeIndexAddPreSave:
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedInComposeAddPreSaveInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- default:
- // Handle the pre-saved message selection (has a max of 25 items)
- if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
- {
- app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
- snprintf(selected_message, MAX_MESSAGE_LENGTH, "%s", app->pre_saved_messages.messages[app->pre_saved_messages.index]);
- if (!selected_message)
- {
- FURI_LOG_E(TAG, "Selected message is NULL");
- return;
- }
- flip_social_free_compose_dialog();
- if (!app->dialog_compose)
- {
- if (!easy_flipper_set_dialog_ex(
- &app->dialog_compose,
- FlipSocialViewComposeDialog,
- "New Feed Post",
- 0,
- 0,
- updated_user_message(selected_message),
- 0,
- 10,
- "Delete",
- "Post",
- NULL,
- compose_dialog_callback,
- flip_social_callback_to_compose_logged_in,
- &app->view_dispatcher,
- app))
- {
- return;
- }
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewComposeDialog);
- }
- // Handle the explore selection
- else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
- {
- if (!flip_social_explore)
- {
- FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
- return;
- }
- flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
- // loading task to get the user info
- if (app->explore_user_bio)
- {
- free(app->explore_user_bio);
- app->explore_user_bio = NULL;
- }
- if (!easy_flipper_set_buffer(&app->explore_user_bio, MAX_MESSAGE_LENGTH))
- {
- return;
- }
- if (flipper_http_process_response_async(flip_social_get_user_info, flip_social_parse_user_info))
- {
- flip_social_free_explore_dialog();
- if (!app->dialog_explore)
- {
- if (!easy_flipper_set_dialog_ex(
- &app->dialog_explore,
- FlipSocialViewExploreDialog,
- flip_social_explore->usernames[flip_social_explore->index],
- 0,
- 0,
- updated_user_message(app->explore_user_bio),
- 0,
- 10,
- "Remove", // remove if user is a friend (future update)
- "Add", // add if user is not a friend (future update)
- NULL,
- explore_dialog_callback,
- flip_social_callback_to_explore_logged_in,
- &app->view_dispatcher,
- app))
- {
- return;
- }
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
- }
- else
- {
- flip_social_free_explore_dialog();
- if (!app->dialog_explore)
- {
- if (!easy_flipper_set_dialog_ex(
- &app->dialog_explore,
- FlipSocialViewExploreDialog,
- flip_social_explore->usernames[flip_social_explore->index],
- 0,
- 0,
- "",
- 0,
- 10,
- "Remove", // remove if user is a friend (future update)
- "Add", // add if user is not a friend (future update)
- NULL,
- explore_dialog_callback,
- flip_social_callback_to_explore_logged_in,
- &app->view_dispatcher,
- app))
- {
- return;
- }
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
- }
- }
- // handle the friends selection
- else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
- {
- if (!flip_social_friends)
- {
- FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
- return;
- }
- flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
- flip_social_free_friends_dialog();
- if (!app->dialog_friends)
- {
- if (!easy_flipper_set_dialog_ex(
- &app->dialog_friends,
- FlipSocialViewFriendsDialog,
- "Friend Options",
- 0,
- 0,
- flip_social_friends->usernames[flip_social_friends->index],
- 0,
- 10,
- "Remove",
- "",
- NULL,
- friends_dialog_callback,
- flip_social_callback_to_friends_logged_in,
- &app->view_dispatcher,
- app))
- {
- return;
- }
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFriendsDialog);
- }
- // handle the messages selection
- else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart && index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS)
- {
- if (!flip_social_message_users)
- {
- FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
- return;
- }
- flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
- flipper_http_loading_task(
- flip_social_get_messages_with_user, // get the messages with the selected user
- flip_social_parse_json_messages, // parse the messages
- FlipSocialViewMessagesDialog, // switch to the messages process if successful
- FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
- &app->view_dispatcher // view dispatcher
- );
- }
- // handle the messages user choices selection
- else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart && index < FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS)
- {
- if (!flip_social_explore)
- {
- FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
- return;
- }
- flip_social_explore->index = index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- } // switch to the text input view
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- }
- else
- {
- FURI_LOG_E(TAG, "Unknown submenu index");
- }
- break;
- }
- }
- /**
- * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered name
- strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
- // Store the entered name in the logged in name field
- strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
- strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
- // Update the name item text
- if (app->variable_item_logged_out_wifi_settings_ssid)
- {
- variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
- }
- // update the wifi settings
- if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
- {
- if (flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
- {
- FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- }
- furi_delay_ms(500);
- flipper_http_deinit();
- }
- }
- // Save the settings
- 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->change_bio_logged_in, app_instance->is_logged_in);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_out_wifi_settings_password_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered WiFi password
- strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
- // Store the entered WiFi password in the logged in password field
- strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
- strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
- // Update the password item text
- if (app->variable_item_logged_out_wifi_settings_password)
- {
- variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
- }
- // update the wifi settings
- if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
- {
- if (flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
- {
- FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- }
- furi_delay_ms(500);
- flipper_http_deinit();
- }
- }
- // Save the settings
- 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->change_bio_logged_in, app_instance->is_logged_in);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @param index The index of the selected item.
- * @return void
- */
- void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- switch (index)
- {
- case 0: // Input SSID
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedOutWifiSettingsSSIDInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 1: // Input Password
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedOutWifiSettingsPasswordInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- default:
- FURI_LOG_E(TAG, "Unknown configuration item index");
- break;
- }
- }
- /**
- * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_out_login_username_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered name
- strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
- // Store the entered name in the logged in username field
- strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
- strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
- // Update the name item text
- if (app->variable_item_logged_out_login_username)
- {
- variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
- }
- // Save the settings
- 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->change_bio_logged_in, app_instance->is_logged_in);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_out_login_password_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered password
- strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
- // Store the entered password in the change password field
- strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
- strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
- // Update the password item text
- if (app->variable_item_logged_out_login_password)
- {
- // dont show the password on the screen (version 0.2)
- // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
- }
- // Save the settings
- 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->change_bio_logged_in, app_instance->is_logged_in);
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Callback when the user selects a menu item in the login (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @param index The index of the selected item.
- * @return void
- */
- void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- switch (index)
- {
- case 0: // Input Username
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
- free_all(false, true);
- if (!alloc_text_input(FlipSocialViewLoggedOutLoginUsernameInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 1: // Input Password
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
- free_all(false, true);
- if (!alloc_text_input(FlipSocialViewLoggedOutLoginPasswordInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 2: // Login Button
- if (!flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
- return;
- }
- flip_social_login_switch_to_view(app);
- break;
- default:
- FURI_LOG_E(TAG, "Unknown configuration item index");
- break;
- }
- }
- /**
- * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_out_register_username_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered name
- strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
- // Update the name item text
- if (app->variable_item_logged_out_register_username)
- {
- variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_out_register_password_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered password
- strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
- // Update the password item text
- if (app->variable_item_logged_out_register_password)
- {
- variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_out_register_password_2_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered password
- strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
- // Update the password item text
- if (app->variable_item_logged_out_register_password_2)
- {
- variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Callback when the user selects a menu item in the register (logged out) screen.
- * @param context The context - FlipSocialApp object.
- * @param index The index of the selected item.
- * @return void
- */
- void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- switch (index)
- {
- case 0: // Input Username
- free_all(false, true);
- if (!alloc_text_input(FlipSocialViewLoggedOutRegisterUsernameInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 1: // Input Password
- free_all(false, true);
- if (!alloc_text_input(FlipSocialViewLoggedOutRegisterPasswordInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 2: // Input Password 2
- free_all(false, true);
- if (!alloc_text_input(FlipSocialViewLoggedOutRegisterPassword2Input))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 3: // Register button
- if (!flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
- return;
- }
- flip_social_register_switch_to_view(app);
- break;
- default:
- FURI_LOG_E(TAG, "Unknown configuration item index");
- break;
- }
- }
- /**
- * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered SSID
- strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
- // Store the entered SSID in the logged out SSID
- strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
- strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
- // Ensure null-termination
- app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
- // Update the name item text
- if (app->variable_item_logged_in_wifi_settings_ssid)
- {
- variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
- }
- // Save the settings
- 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->change_bio_logged_in, app_instance->is_logged_in);
- // update the wifi settings
- if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
- {
- if (flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
- {
- FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- }
- furi_delay_ms(500);
- flipper_http_deinit();
- }
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_in_wifi_settings_password_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered password
- strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
- // Store the entered password in the logged out password
- strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
- strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
- // Ensure null-termination
- app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
- // Update the password item text
- if (app->variable_item_logged_in_wifi_settings_password)
- {
- // dont show the password on the screen (version 0.2)
- // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
- }
- // Save the settings
- 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->change_bio_logged_in, app_instance->is_logged_in);
- // update the wifi settings
- if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
- {
- if (flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
- {
- FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- }
- furi_delay_ms(500);
- flipper_http_deinit();
- }
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
- }
- /**
- * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
- * @param context The context - FlipSocialApp object.
- * @param index The index of the selected item.
- * @return void
- */
- void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- switch (index)
- {
- case 0: // Input SSID
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
- free_all(false, false);
- if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsSSIDInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input for SSID");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 1: // Input Password
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
- free_all(false, false);
- if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsPasswordInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input for Password");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- default:
- FURI_LOG_E(TAG, "Unknown configuration item index");
- break;
- }
- }
- void flip_social_logged_in_user_settings_item_selected(void *context, uint32_t index)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Switch to the appropriate view
- switch (index)
- {
- case 0: // Feed Type
- break;
- }
- }
- /**
- * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_in_compose_pre_save_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Check if the message is empty or if adding the message would exceed MAX_PRE_SAVED_MESSAGES
- if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
- {
- FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- return;
- }
- // Copy the entered message into the next available slot
- strncpy(
- app->pre_saved_messages.messages[app->pre_saved_messages.count],
- app->compose_pre_save_logged_in_temp_buffer,
- MAX_MESSAGE_LENGTH - 1);
- // Ensure null-termination
- app->pre_saved_messages.messages[app->pre_saved_messages.count][MAX_MESSAGE_LENGTH - 1] = '\0';
- // Increment the count
- app->pre_saved_messages.count++;
- // Rebuild the submenu
- submenu_reset(app->submenu);
- submenu_add_item(
- app->submenu,
- "Add Pre-Save",
- FlipSocialSubmenuComposeIndexAddPreSave,
- flip_social_callback_submenu_choices,
- app);
- for (size_t i = 0; i < app->pre_saved_messages.count; i++)
- {
- submenu_add_item(
- app->submenu,
- app->pre_saved_messages.messages[i],
- FlipSocialSubemnuComposeIndexStartIndex + i,
- flip_social_callback_submenu_choices,
- app);
- }
- // Save the updated playlist
- save_playlist(&app->pre_saved_messages);
- // Switch back to the compose view
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- }
- /**
- * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_in_profile_change_password_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Correct type: old_pass should be a pointer to a string (char *)
- const char *old_password = app->login_password_logged_out;
- // Store the entered message
- strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
- // store the entered password in the logged out password
- strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
- strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
- // Ensure null-termination
- app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
- // Update the message item text
- if (app->variable_item_logged_in_profile_change_password)
- {
- // dont show the password on the screen (version 0.2)
- // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
- }
- // send post request to change password
- if (flipper_http_init(flipper_http_rx_callback, app))
- {
- auth_headers_alloc();
- char payload[256];
- snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
- if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
- {
- FURI_LOG_E(TAG, "Failed to send post request to change password");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- }
- flipper_http_deinit();
- }
- // Save the settings
- 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->change_bio_logged_in, app_instance->is_logged_in);
- // instead of going to a view, just show a success message
- easy_flipper_dialog("Success", "Password updated successfully\n\n\nPress BACK to return :D");
- }
- void flip_social_logged_in_profile_change_bio_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // Store the entered message
- strncpy(app->change_bio_logged_in, app->change_bio_logged_in_temp_buffer, app->change_bio_logged_in_temp_buffer_size);
- // Ensure null-termination
- app->change_bio_logged_in[app->change_bio_logged_in_temp_buffer_size - 1] = '\0';
- // Update the message item text
- if (app->variable_item_logged_in_profile_change_bio)
- {
- variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_bio, app->change_bio_logged_in);
- }
- // send post request to change bio
- if (flipper_http_init(flipper_http_rx_callback, app))
- {
- auth_headers_alloc();
- char payload[256];
- snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"bio\":\"%s\"}", app->login_username_logged_out, app->change_bio_logged_in);
- if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-bio/", auth_headers, payload))
- {
- FURI_LOG_E(TAG, "Failed to send post request to change bio");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- }
- furi_delay_ms(500);
- flipper_http_deinit();
- }
- // Save the settings
- save_settings(app->wifi_ssid_logged_out, app->wifi_password_logged_out, app->login_username_logged_out, app->login_username_logged_in, app->login_password_logged_out, app->change_password_logged_in, app->change_bio_logged_in, app->is_logged_in);
- // instead of going to a view, just show a success message
- easy_flipper_dialog("Success", "Bio updated successfully\n\n\nPress BACK to return :D");
- }
- /**
- * @brief Callback when a user selects a menu item in the profile (logged in) screen.
- * @param context The context - FlipSocialApp object.
- * @param index The index of the selected item.
- * @return void
- */
- void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- switch (index)
- {
- case 0: // Change Username
- // do nothing since username cannot be changed
- break;
- case 1: // Change Password
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedInChangePasswordInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input for change password");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 2: // Change Bio
- // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangeBioInput);
- free_text_input();
- if (!alloc_text_input(FlipSocialViewLoggedInChangeBioInput))
- {
- FURI_LOG_E(TAG, "Failed to allocate text input for change bio");
- return;
- }
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- break;
- case 3: // Friends
- free_all(false, true);
- if (!alloc_submenu(FlipSocialViewLoggedInFriendsSubmenu))
- {
- FURI_LOG_E(TAG, "Failed to allocate submenu for friends");
- return;
- }
- if (!flipper_http_init(flipper_http_rx_callback, app))
- {
- FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
- return;
- }
- flipper_http_loading_task(
- flip_social_get_friends,
- flip_social_parse_json_friends,
- FlipSocialViewSubmenu,
- FlipSocialViewVariableItemList,
- &app->view_dispatcher);
- break;
- default:
- FURI_LOG_E(TAG, "Unknown configuration item index");
- break;
- }
- }
- /**
- * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_in_messages_user_choice_message_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // check if the message is empty
- if (app->message_user_choice_logged_in_temp_buffer_size == 0)
- {
- FURI_LOG_E(TAG, "Message is empty");
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
- return;
- }
- // Store the entered message
- strncpy(app->message_user_choice_logged_in, app->message_user_choice_logged_in_temp_buffer, app->message_user_choice_logged_in_temp_buffer_size);
- // Ensure null-termination
- app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
- // send post request to send message
- if (!flipper_http_init(flipper_http_rx_callback, app))
- {
- FURI_LOG_E(TAG, "Failed to initialize HTTP");
- return;
- }
- auth_headers_alloc();
- char url[128];
- char payload[256];
- snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
- snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
- if (!flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
- {
- FURI_LOG_E(TAG, "Failed to send post request to send message");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- }
- furi_delay_ms(1000);
- flipper_http_deinit();
- // add user to the top of the list if not already there
- for (int i = 0; i < flip_social_message_users->count; i++)
- {
- if (strcmp(flip_social_message_users->usernames[i], flip_social_explore->usernames[flip_social_explore->index]) == 0)
- {
- // remove the user from the list
- for (int j = i; j < flip_social_message_users->count - 1; j++)
- {
- strncpy(flip_social_message_users->usernames[j], flip_social_message_users->usernames[j + 1], strlen(flip_social_message_users->usernames[j + 1]));
- }
- flip_social_message_users->count--;
- break;
- }
- }
- // add the user to the top of the list
- for (int i = flip_social_message_users->count; i > 0; i--)
- {
- strncpy(flip_social_message_users->usernames[i], flip_social_message_users->usernames[i - 1], strlen(flip_social_message_users->usernames[i - 1]));
- }
- strncpy(flip_social_message_users->usernames[0], flip_social_explore->usernames[flip_social_explore->index], strlen(flip_social_explore->usernames[flip_social_explore->index]));
- flip_social_message_users->count++;
- // redraw submenu
- flip_social_update_messages_submenu();
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- }
- /**
- * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
- * @param context The context - FlipSocialApp object.
- * @return void
- */
- void flip_social_logged_in_messages_new_message_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // check if the message is empty
- if (app->messages_new_message_logged_in_temp_buffer_size == 0)
- {
- FURI_LOG_E(TAG, "Message is empty");
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
- return;
- }
- // Store the entered message
- strncpy(app->messages_new_message_logged_in, app->messages_new_message_logged_in_temp_buffer, app->messages_new_message_logged_in_temp_buffer_size);
- // Ensure null-termination
- app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
- bool send_message_to_user()
- {
- // send post request to send message
- if (!flipper_http_init(flipper_http_rx_callback, app))
- {
- FURI_LOG_E(TAG, "Failed to initialize HTTP");
- return false;
- }
- auth_headers_alloc();
- char url[128];
- char payload[256];
- snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
- snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
- if (!flipper_http_post_request_with_headers(url, auth_headers, payload))
- {
- FURI_LOG_E(TAG, "Failed to send post request to send message");
- FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
- easy_flipper_dialog("Error", "Failed to send message\n\n\nPress BACK to return :D");
- flipper_http_deinit();
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
- return false;
- }
- fhttp.state = RECEIVING;
- return true;
- }
- bool parse_message_to_user()
- {
- while (fhttp.state != IDLE)
- {
- furi_delay_ms(10);
- }
- flipper_http_deinit();
- return true;
- }
- // well, we got a freeze here, so let's use the loading task to switch views and force refresh
- flipper_http_loading_task(
- send_message_to_user,
- parse_message_to_user,
- FlipSocialViewSubmenu,
- FlipSocialViewLoggedInMessagesNewMessageInput,
- &app->view_dispatcher);
- }
- void flip_social_logged_in_explore_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // check if the message is empty
- if (app->explore_logged_in_temp_buffer_size == 0)
- {
- FURI_LOG_E(TAG, "Message is empty");
- strncpy(app->explore_logged_in, "a", 2);
- }
- else
- {
- // Store the entered message
- strncpy(app->explore_logged_in, app->explore_logged_in_temp_buffer, app->explore_logged_in_temp_buffer_size);
- }
- // Ensure null-termination
- app->explore_logged_in[app->explore_logged_in_temp_buffer_size - 1] = '\0';
- free_submenu();
- if (!alloc_submenu(FlipSocialViewLoggedInExploreSubmenu))
- {
- FURI_LOG_E(TAG, "Failed to allocate submenu for explore");
- return;
- }
- flipper_http_loading_task(
- flip_social_get_explore, // get the explore users
- flip_social_parse_json_explore, // parse the explore users
- FlipSocialViewSubmenu, // switch to the explore submenu if successful
- FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
- &app->view_dispatcher); // view dispatcher
- }
- void flip_social_logged_in_message_users_updated(void *context)
- {
- FlipSocialApp *app = (FlipSocialApp *)context;
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipSocialApp is NULL");
- return;
- }
- // check if the message is empty
- if (!app->message_users_logged_in_temp_buffer || app->message_users_logged_in_temp_buffer_size == 0)
- {
- FURI_LOG_E(TAG, "Message is empty");
- strncpy(app->message_users_logged_in, "a", 2);
- }
- else
- {
- // Store the entered message
- strncpy(app->message_users_logged_in, app->message_users_logged_in_temp_buffer, app->message_users_logged_in_temp_buffer_size);
- }
- // Ensure null-termination
- app->message_users_logged_in[app->message_users_logged_in_temp_buffer_size - 1] = '\0';
- free_submenu();
- if (!alloc_submenu(FlipSocialViewLoggedInExploreSubmenu))
- {
- FURI_LOG_E(TAG, "Failed to allocate submenu for explore");
- return;
- }
- // get users
- flipper_http_loading_task(
- flip_social_get_explore_2, // get the explore users
- flip_social_parse_json_message_user_choices, // parse the explore users
- FlipSocialViewSubmenu, // switch to the explore submenu if successful
- FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
- &app->view_dispatcher); // view dispatcher
- }
- static void flip_social_widget_set_text(char *message, Widget **widget)
- {
- if (widget == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_set_widget_text - widget is NULL");
- DEV_CRASH();
- return;
- }
- if (message == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_set_widget_text - message is NULL");
- DEV_CRASH();
- return;
- }
- widget_reset(*widget);
- uint32_t message_length = strlen(message); // Length of the message
- uint32_t i = 0; // Index tracker
- uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
- char *formatted_message; // Buffer to hold the final formatted message
- // Allocate buffer with double the message length plus one for safety
- if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
- {
- return;
- }
- while (i < message_length)
- {
- uint32_t max_line_length = 31; // Maximum characters per line
- uint32_t remaining_length = message_length - i; // Remaining characters
- uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
- // Check for newline character within the current segment
- uint32_t newline_pos = i;
- bool found_newline = false;
- for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
- {
- if (message[newline_pos] == '\n')
- {
- found_newline = true;
- break;
- }
- }
- if (found_newline)
- {
- // If newline found, set line_length up to the newline
- line_length = newline_pos - i;
- }
- // Temporary buffer to hold the current line
- char line[32];
- strncpy(line, message + i, line_length);
- line[line_length] = '\0';
- // If newline was found, skip it for the next iteration
- if (found_newline)
- {
- i += line_length + 1; // +1 to skip the '\n' character
- }
- else
- {
- // Check if the line ends in the middle of a word and adjust accordingly
- if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
- {
- // Find the last space within the current line to avoid breaking a word
- char *last_space = strrchr(line, ' ');
- if (last_space != NULL)
- {
- // Adjust the line_length to avoid cutting the word
- line_length = last_space - line;
- line[line_length] = '\0'; // Null-terminate at the space
- }
- }
- // Move the index forward by the determined line_length
- i += line_length;
- // Skip any spaces at the beginning of the next line
- while (i < message_length && message[i] == ' ')
- {
- i++;
- }
- }
- // Manually copy the fixed line into the formatted_message buffer
- for (uint32_t j = 0; j < line_length; j++)
- {
- formatted_message[formatted_index++] = line[j];
- }
- // Add a newline character for line spacing
- formatted_message[formatted_index++] = '\n';
- }
- // Null-terminate the formatted_message
- formatted_message[formatted_index] = '\0';
- // Add the formatted message to the widget
- widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
- }
- void flip_social_loader_draw_callback(Canvas *canvas, void *model)
- {
- if (!canvas || !model)
- {
- FURI_LOG_E(TAG, "flip_social_loader_draw_callback - canvas or model is NULL");
- return;
- }
- SerialState http_state = fhttp.state;
- DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
- DataState data_state = data_loader_model->data_state;
- char *title = data_loader_model->title;
- canvas_set_font(canvas, FontSecondary);
- if (http_state == INACTIVE)
- {
- canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
- canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
- canvas_draw_str(canvas, 0, 32, "If your board is connected,");
- canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
- canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
- canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
- return;
- }
- if (data_state == DataStateError || data_state == DataStateParseError)
- {
- flip_social_request_error_draw(canvas);
- return;
- }
- canvas_draw_str(canvas, 0, 7, title);
- canvas_draw_str(canvas, 0, 17, "Loading...");
- if (data_state == DataStateInitial)
- {
- return;
- }
- if (http_state == SENDING)
- {
- canvas_draw_str(canvas, 0, 27, "Fetching...");
- return;
- }
- if (http_state == RECEIVING || data_state == DataStateRequested)
- {
- canvas_draw_str(canvas, 0, 27, "Receiving...");
- return;
- }
- if (http_state == IDLE && data_state == DataStateReceived)
- {
- canvas_draw_str(canvas, 0, 27, "Processing...");
- return;
- }
- if (http_state == IDLE && data_state == DataStateParsed)
- {
- canvas_draw_str(canvas, 0, 27, "Processed...");
- return;
- }
- }
- static void flip_social_loader_process_callback(void *context)
- {
- if (context == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_loader_process_callback - context is NULL");
- DEV_CRASH();
- return;
- }
- FlipSocialApp *app = (FlipSocialApp *)context;
- View *view = app->view_loader;
- DataState current_data_state;
- with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
- if (current_data_state == DataStateInitial)
- {
- with_view_model(
- view,
- DataLoaderModel * model,
- {
- model->data_state = DataStateRequested;
- DataLoaderFetch fetch = model->fetcher;
- if (fetch == NULL)
- {
- FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
- model->data_state = DataStateError;
- return;
- }
- // Clear any previous responses
- strncpy(fhttp.last_response, "", 1);
- bool request_status = fetch(model);
- if (!request_status)
- {
- model->data_state = DataStateError;
- }
- },
- true);
- }
- else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
- {
- if (fhttp.state == IDLE && fhttp.last_response != NULL)
- {
- if (strstr(fhttp.last_response, "[PONG]") != NULL)
- {
- FURI_LOG_DEV(TAG, "PONG received.");
- }
- else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
- {
- FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
- }
- else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
- {
- FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
- }
- else if (strlen(fhttp.last_response) == 0)
- {
- // Still waiting on response
- }
- else
- {
- with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
- }
- }
- else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
- {
- // continue waiting
- }
- else if (fhttp.state == INACTIVE)
- {
- // inactive. try again
- }
- else if (fhttp.state == ISSUE)
- {
- with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
- }
- else
- {
- FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
- DEV_CRASH();
- }
- }
- else if (current_data_state == DataStateReceived)
- {
- with_view_model(
- view,
- DataLoaderModel * model,
- {
- char *data_text;
- if (model->parser == NULL)
- {
- data_text = NULL;
- FURI_LOG_DEV(TAG, "Parser is NULL");
- DEV_CRASH();
- }
- else
- {
- data_text = model->parser(model);
- }
- FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
- model->data_text = data_text;
- if (data_text == NULL)
- {
- model->data_state = DataStateParseError;
- }
- else
- {
- model->data_state = DataStateParsed;
- }
- },
- true);
- }
- else if (current_data_state == DataStateParsed)
- {
- with_view_model(
- view,
- DataLoaderModel * model,
- {
- if (++model->request_index < model->request_count)
- {
- model->data_state = DataStateInitial;
- }
- else
- {
- flip_social_widget_set_text(model->data_text != NULL ? model->data_text : "", &app_instance->widget_result);
- if (model->data_text != NULL)
- {
- free(model->data_text);
- model->data_text = NULL;
- }
- view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
- view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewWidgetResult);
- }
- },
- true);
- }
- }
- static void flip_social_loader_timer_callback(void *context)
- {
- if (context == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_loader_timer_callback - context is NULL");
- DEV_CRASH();
- return;
- }
- FlipSocialApp *app = (FlipSocialApp *)context;
- view_dispatcher_send_custom_event(app->view_dispatcher, FlipSocialCustomEventProcess);
- }
- static void flip_social_loader_on_enter(void *context)
- {
- if (context == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_loader_on_enter - context is NULL");
- DEV_CRASH();
- return;
- }
- FlipSocialApp *app = (FlipSocialApp *)context;
- View *view = app->view_loader;
- with_view_model(
- view,
- DataLoaderModel * model,
- {
- view_set_previous_callback(view, model->back_callback);
- if (model->timer == NULL)
- {
- model->timer = furi_timer_alloc(flip_social_loader_timer_callback, FuriTimerTypePeriodic, app);
- }
- furi_timer_start(model->timer, 250);
- },
- true);
- }
- static void flip_social_loader_on_exit(void *context)
- {
- if (context == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_loader_on_exit - context is NULL");
- DEV_CRASH();
- return;
- }
- FlipSocialApp *app = (FlipSocialApp *)context;
- View *view = app->view_loader;
- with_view_model(
- view,
- DataLoaderModel * model,
- {
- if (model->timer)
- {
- furi_timer_stop(model->timer);
- }
- },
- false);
- }
- void flip_social_loader_init(View *view)
- {
- if (view == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_loader_init - view is NULL");
- DEV_CRASH();
- return;
- }
- view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
- view_set_enter_callback(view, flip_social_loader_on_enter);
- view_set_exit_callback(view, flip_social_loader_on_exit);
- }
- void flip_social_loader_free_model(View *view)
- {
- if (view == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_loader_free_model - view is NULL");
- DEV_CRASH();
- return;
- }
- with_view_model(
- view,
- DataLoaderModel * model,
- {
- if (model->timer)
- {
- furi_timer_free(model->timer);
- model->timer = NULL;
- }
- if (model->parser_context)
- {
- free(model->parser_context);
- model->parser_context = NULL;
- }
- },
- false);
- }
- bool flip_social_custom_event_callback(void *context, uint32_t index)
- {
- if (context == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_custom_event_callback - context is NULL");
- DEV_CRASH();
- return false;
- }
- switch (index)
- {
- case FlipSocialCustomEventProcess:
- flip_social_loader_process_callback(context);
- return true;
- default:
- FURI_LOG_DEV(TAG, "flip_social_custom_event_callback. Unknown index: %ld", index);
- return false;
- }
- }
- void flip_social_generic_switch_to_view(FlipSocialApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
- {
- if (app == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - app is NULL");
- DEV_CRASH();
- return;
- }
- View *view = app->view_loader;
- if (view == NULL)
- {
- FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - view is NULL");
- DEV_CRASH();
- return;
- }
- with_view_model(
- view,
- DataLoaderModel * model,
- {
- model->title = title;
- model->fetcher = fetcher;
- model->parser = parser;
- model->request_index = 0;
- model->request_count = request_count;
- model->back_callback = back;
- model->data_state = DataStateInitial;
- model->data_text = NULL;
- },
- true);
- view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
- }
- static bool flip_social_get_home_notification()
- {
- if (!app_instance)
- {
- FURI_LOG_E(TAG, "app_instance is NULL");
- return false;
- }
- if (!flipper_http_init(flipper_http_rx_callback, app_instance))
- {
- FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
- return false;
- }
- // Create the directory for saving settings
- char directory_path[256];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data");
- // Create the directory
- Storage *storage = furi_record_open(RECORD_STORAGE);
- storage_common_mkdir(storage, directory_path);
- furi_record_close(RECORD_STORAGE);
- auth_headers_alloc();
- snprintf(
- fhttp.file_path,
- sizeof(fhttp.file_path),
- STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data/notification.json");
- fhttp.save_received_data = true;
- return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/flip-social-notifications/", auth_headers);
- }
- static bool flip_social_parse_home_notification()
- {
- FuriString *notification = flipper_http_load_from_file(fhttp.file_path);
- if (notification == NULL)
- {
- FURI_LOG_E(TAG, "Failed to load notification from file");
- flipper_http_deinit();
- return false;
- }
- flipper_http_deinit();
- // Check if announcement and analytics key exists
- FuriString *announcement_json = get_json_value_furi("announcement", notification);
- FuriString *analytics_json = get_json_value_furi("analytics", notification);
- if (announcement_json == NULL || analytics_json == NULL)
- {
- FURI_LOG_E(TAG, "Failed to get announcement or analytics from notification");
- if (announcement_json)
- {
- furi_string_free(announcement_json);
- }
- if (analytics_json)
- {
- furi_string_free(analytics_json);
- }
- furi_string_free(notification);
- return false;
- }
- // Extract values from JSON
- FuriString *announcement_value = get_json_value_furi("content", announcement_json);
- FuriString *announcement_time = get_json_value_furi("date_created", announcement_json);
- FuriString *analytics_value = get_json_value_furi("count", analytics_json);
- FuriString *analytics_time = get_json_value_furi("time", analytics_json);
- if (!announcement_value || !announcement_time || !analytics_value || !analytics_time)
- {
- FURI_LOG_E(TAG, "Failed to get announcement or analytics value from notification");
- if (announcement_value)
- {
- furi_string_free(announcement_value);
- }
- if (announcement_time)
- {
- furi_string_free(announcement_time);
- }
- if (analytics_value)
- {
- furi_string_free(analytics_value);
- }
- if (analytics_time)
- {
- furi_string_free(analytics_time);
- }
- furi_string_free(announcement_json);
- furi_string_free(analytics_json);
- furi_string_free(notification);
- return false;
- }
- // Load previous announcement and analytics times
- char past_analytics_time[32] = {0};
- char past_announcement_time[32] = {0};
- bool analytics_time_loaded = load_char("analytics_time", past_analytics_time, sizeof(past_analytics_time));
- bool announcement_time_loaded = load_char("announcement_time", past_announcement_time, sizeof(past_announcement_time));
- bool new_announcement = false;
- bool new_analytics = false;
- // Check for new announcement
- if (!announcement_time_loaded || strcmp(furi_string_get_cstr(announcement_time), past_announcement_time) != 0)
- {
- new_announcement = true;
- }
- // Check for new analytics
- if (!analytics_time_loaded || strcmp(furi_string_get_cstr(analytics_time), past_analytics_time) != 0)
- {
- new_analytics = true;
- }
- // If no new announcement and no new analytics, exit early
- if (!new_announcement && !new_analytics)
- {
- FURI_LOG_D(TAG, "No new announcement or analytics");
- furi_string_free(announcement_value);
- furi_string_free(announcement_time);
- furi_string_free(analytics_value);
- furi_string_free(analytics_time);
- furi_string_free(announcement_json);
- furi_string_free(analytics_json);
- furi_string_free(notification);
- return true;
- }
- // Save the new announcement and analytics times if they are new
- if (new_announcement)
- {
- save_char("announcement_time", furi_string_get_cstr(announcement_time));
- }
- if (new_analytics)
- {
- save_char("analytics_time", furi_string_get_cstr(analytics_time));
- }
- // Prepare and show dialogs based on what is new
- if (new_announcement)
- {
- easy_flipper_dialog("Announcement", (char *)furi_string_get_cstr(announcement_value));
- }
- if (new_analytics)
- {
- char analytics_text[128] = {0};
- // Determine the new posts count
- if (atoi(furi_string_get_cstr(analytics_value)) > 0)
- {
- char past_analytics_value[32] = {0};
- int new_posts = 0;
- if (load_char("analytics_value", past_analytics_value, sizeof(past_analytics_value)))
- {
- int past_posts = atoi(past_analytics_value);
- int current_posts = atoi(furi_string_get_cstr(analytics_value));
- new_posts = current_posts - past_posts;
- snprintf(analytics_text, sizeof(analytics_text), "%d new posts", new_posts);
- }
- else
- {
- snprintf(analytics_text, sizeof(analytics_text), "%s feed posts", furi_string_get_cstr(analytics_value));
- }
- save_char("analytics_value", furi_string_get_cstr(analytics_value));
- }
- else
- {
- snprintf(analytics_text, sizeof(analytics_text), "%s feed posts", furi_string_get_cstr(analytics_value));
- }
- easy_flipper_dialog("Notifications", analytics_text);
- }
- // Free allocated resources
- furi_string_free(announcement_value);
- furi_string_free(announcement_time);
- furi_string_free(analytics_value);
- furi_string_free(analytics_time);
- furi_string_free(announcement_json);
- furi_string_free(analytics_json);
- furi_string_free(notification);
- return true;
- }
- // home notification
- bool flip_social_home_notification()
- {
- return flipper_http_process_response_async(flip_social_get_home_notification, flip_social_parse_home_notification);
- }
|