flip_trader_callback.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #include <callback/flip_trader_callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) \
  6. furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  7. #define DEV_CRASH() furi_crash()
  8. #else
  9. #define FURI_LOG_DEV(tag, format, ...)
  10. #define DEV_CRASH()
  11. #endif
  12. // hold the price of the asset
  13. char asset_price[64];
  14. bool sent_get_request = false;
  15. bool get_request_success = false;
  16. bool request_processed = false;
  17. static void flip_trader_request_error_draw(Canvas* canvas) {
  18. if(canvas == NULL) {
  19. FURI_LOG_E(TAG, "flip_trader_request_error_draw - canvas is NULL");
  20. DEV_CRASH();
  21. return;
  22. }
  23. if(fhttp.last_response != NULL) {
  24. if(strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") !=
  25. NULL) {
  26. canvas_clear(canvas);
  27. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  28. canvas_draw_str(canvas, 0, 22, "Failed to reconnect.");
  29. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  30. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  31. } else if(strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL) {
  32. canvas_clear(canvas);
  33. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  34. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  35. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  36. } else if(strstr(fhttp.last_response, "[PONG]") != NULL) {
  37. canvas_clear(canvas);
  38. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  39. } else {
  40. canvas_clear(canvas);
  41. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  42. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  43. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  44. }
  45. } else {
  46. canvas_clear(canvas);
  47. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  48. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  49. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  50. }
  51. }
  52. static bool send_price_request(AssetLoaderModel* model) {
  53. UNUSED(model);
  54. if(fhttp.state == INACTIVE) {
  55. return false;
  56. }
  57. if(!sent_get_request && fhttp.state == IDLE) {
  58. sent_get_request = true;
  59. char url[128];
  60. char* headers = jsmn("Content-Type", "application/json");
  61. snprintf(
  62. fhttp.file_path,
  63. sizeof(fhttp.file_path),
  64. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_trader/price.txt");
  65. fhttp.save_received_data = true;
  66. snprintf(
  67. url,
  68. 128,
  69. "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=%s&apikey=2X90WLEFMP43OJKE",
  70. asset_names[asset_index]);
  71. get_request_success = flipper_http_get_request_with_headers(url, headers);
  72. free(headers);
  73. if(!get_request_success) {
  74. FURI_LOG_E(TAG, "Failed to send GET request");
  75. fhttp.state = ISSUE;
  76. return false;
  77. }
  78. fhttp.state = RECEIVING;
  79. }
  80. return true;
  81. }
  82. static char* process_asset_price(AssetLoaderModel* model) {
  83. UNUSED(model);
  84. if(!request_processed) {
  85. // load the received data from the saved file
  86. FuriString* price_data = flipper_http_load_from_file(fhttp.file_path);
  87. if(price_data == NULL) {
  88. FURI_LOG_E(TAG, "Failed to load received data from file.");
  89. fhttp.state = ISSUE;
  90. return NULL;
  91. }
  92. char* data_cstr = (char*)furi_string_get_cstr(price_data);
  93. if(data_cstr == NULL) {
  94. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  95. furi_string_free(price_data);
  96. fhttp.state = ISSUE;
  97. return NULL;
  98. }
  99. request_processed = true;
  100. char* global_quote = get_json_value("Global Quote", data_cstr, MAX_TOKENS);
  101. if(global_quote == NULL) {
  102. FURI_LOG_E(TAG, "Failed to get Global Quote");
  103. fhttp.state = ISSUE;
  104. furi_string_free(price_data);
  105. free(global_quote);
  106. free(data_cstr);
  107. return NULL;
  108. }
  109. char* price = get_json_value("05. price", global_quote, MAX_TOKENS);
  110. if(price == NULL) {
  111. FURI_LOG_E(TAG, "Failed to get price");
  112. fhttp.state = ISSUE;
  113. furi_string_free(price_data);
  114. free(global_quote);
  115. free(price);
  116. free(data_cstr);
  117. return NULL;
  118. }
  119. // store the price "Asset: $price"
  120. snprintf(asset_price, 64, "%s: $%s", asset_names[asset_index], price);
  121. fhttp.state = IDLE;
  122. furi_string_free(price_data);
  123. free(global_quote);
  124. free(price);
  125. free(data_cstr);
  126. }
  127. return asset_price;
  128. }
  129. static void flip_trader_asset_switch_to_view(FlipTraderApp* app) {
  130. flip_trader_generic_switch_to_view(
  131. app,
  132. "Fetching..",
  133. send_price_request,
  134. process_asset_price,
  135. 1,
  136. callback_to_assets_submenu,
  137. FlipTraderViewLoader);
  138. }
  139. void callback_submenu_choices(void* context, uint32_t index) {
  140. FlipTraderApp* app = (FlipTraderApp*)context;
  141. if(!app) {
  142. FURI_LOG_E(TAG, "FlipTraderApp is NULL");
  143. return;
  144. }
  145. switch(index) {
  146. // view the assets submenu
  147. case FlipTradeSubmenuIndexAssets:
  148. view_dispatcher_switch_to_view(app->view_dispatcher, FlipTraderViewAssetsSubmenu);
  149. break;
  150. // view the about screen
  151. case FlipTraderSubmenuIndexAbout:
  152. view_dispatcher_switch_to_view(app->view_dispatcher, FlipTraderViewAbout);
  153. break;
  154. // view the wifi settings screen
  155. case FlipTraderSubmenuIndexSettings:
  156. view_dispatcher_switch_to_view(app->view_dispatcher, FlipTraderViewWiFiSettings);
  157. break;
  158. default:
  159. // handle FlipTraderSubmenuIndexAssetStartIndex + index
  160. if(index >= FlipTraderSubmenuIndexAssetStartIndex) {
  161. asset_index = index - FlipTraderSubmenuIndexAssetStartIndex;
  162. flip_trader_asset_switch_to_view(app);
  163. } else {
  164. FURI_LOG_E(TAG, "Unknown submenu index");
  165. }
  166. break;
  167. }
  168. }
  169. void text_updated_ssid(void* context) {
  170. FlipTraderApp* app = (FlipTraderApp*)context;
  171. if(!app) {
  172. FURI_LOG_E(TAG, "FlipTraderApp is NULL");
  173. return;
  174. }
  175. // store the entered text
  176. strncpy(
  177. app->uart_text_input_buffer_ssid,
  178. app->uart_text_input_temp_buffer_ssid,
  179. app->uart_text_input_buffer_size_ssid);
  180. // Ensure null-termination
  181. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  182. // update the variable item text
  183. if(app->variable_item_ssid) {
  184. variable_item_set_current_value_text(
  185. app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  186. }
  187. // save settings
  188. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  189. // save wifi settings to devboard
  190. if(strlen(app->uart_text_input_buffer_ssid) > 0 &&
  191. strlen(app->uart_text_input_buffer_password) > 0) {
  192. if(!flipper_http_save_wifi(
  193. app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password)) {
  194. FURI_LOG_E(TAG, "Failed to save wifi settings");
  195. }
  196. }
  197. // switch to the settings view
  198. view_dispatcher_switch_to_view(app->view_dispatcher, FlipTraderViewWiFiSettings);
  199. }
  200. void text_updated_password(void* context) {
  201. FlipTraderApp* app = (FlipTraderApp*)context;
  202. if(!app) {
  203. FURI_LOG_E(TAG, "FlipTraderApp is NULL");
  204. return;
  205. }
  206. // store the entered text
  207. strncpy(
  208. app->uart_text_input_buffer_password,
  209. app->uart_text_input_temp_buffer_password,
  210. app->uart_text_input_buffer_size_password);
  211. // Ensure null-termination
  212. app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
  213. // update the variable item text
  214. if(app->variable_item_password) {
  215. variable_item_set_current_value_text(
  216. app->variable_item_password, app->uart_text_input_buffer_password);
  217. }
  218. // save settings
  219. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  220. // save wifi settings to devboard
  221. if(strlen(app->uart_text_input_buffer_ssid) > 0 &&
  222. strlen(app->uart_text_input_buffer_password) > 0) {
  223. if(!flipper_http_save_wifi(
  224. app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password)) {
  225. FURI_LOG_E(TAG, "Failed to save wifi settings");
  226. }
  227. }
  228. // switch to the settings view
  229. view_dispatcher_switch_to_view(app->view_dispatcher, FlipTraderViewWiFiSettings);
  230. }
  231. uint32_t callback_to_submenu(void* context) {
  232. if(!context) {
  233. FURI_LOG_E(TAG, "Context is NULL");
  234. return VIEW_NONE;
  235. }
  236. UNUSED(context);
  237. sent_get_request = false;
  238. get_request_success = false;
  239. request_processed = false;
  240. asset_index = 0;
  241. return FlipTraderViewMainSubmenu;
  242. }
  243. uint32_t callback_to_wifi_settings(void* context) {
  244. if(!context) {
  245. FURI_LOG_E(TAG, "Context is NULL");
  246. return VIEW_NONE;
  247. }
  248. UNUSED(context);
  249. return FlipTraderViewWiFiSettings;
  250. }
  251. uint32_t callback_to_assets_submenu(void* context) {
  252. if(!context) {
  253. FURI_LOG_E(TAG, "Context is NULL");
  254. return VIEW_NONE;
  255. }
  256. UNUSED(context);
  257. sent_get_request = false;
  258. get_request_success = false;
  259. request_processed = false;
  260. asset_index = 0;
  261. return FlipTraderViewAssetsSubmenu;
  262. }
  263. void settings_item_selected(void* context, uint32_t index) {
  264. FlipTraderApp* app = (FlipTraderApp*)context;
  265. if(!app) {
  266. FURI_LOG_E(TAG, "FlipTraderApp is NULL");
  267. return;
  268. }
  269. switch(index) {
  270. case 0: // Input SSID
  271. view_dispatcher_switch_to_view(app->view_dispatcher, FlipTraderViewTextInputSSID);
  272. break;
  273. case 1: // Input Password
  274. view_dispatcher_switch_to_view(app->view_dispatcher, FlipTraderViewTextInputPassword);
  275. break;
  276. default:
  277. FURI_LOG_E(TAG, "Unknown configuration item index");
  278. break;
  279. }
  280. }
  281. static void flip_trader_widget_set_text(char* message, Widget** widget) {
  282. if(widget == NULL) {
  283. FURI_LOG_E(TAG, "flip_trader_set_widget_text - widget is NULL");
  284. DEV_CRASH();
  285. return;
  286. }
  287. if(message == NULL) {
  288. FURI_LOG_E(TAG, "flip_trader_set_widget_text - message is NULL");
  289. DEV_CRASH();
  290. return;
  291. }
  292. widget_reset(*widget);
  293. uint32_t message_length = strlen(message); // Length of the message
  294. uint32_t i = 0; // Index tracker
  295. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  296. char* formatted_message; // Buffer to hold the final formatted message
  297. if(!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1)) {
  298. return;
  299. }
  300. while(i < message_length) {
  301. // TODO: Use canvas_glyph_width to calculate the maximum characters for the line
  302. uint32_t max_line_length = 29; // Maximum characters per line
  303. uint32_t remaining_length = message_length - i; // Remaining characters
  304. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length :
  305. max_line_length;
  306. // Temporary buffer to hold the current line
  307. char line[30];
  308. strncpy(line, message + i, line_length);
  309. line[line_length] = '\0';
  310. // Check if the line ends in the middle of a word and adjust accordingly
  311. if(line_length == 29 && message[i + line_length] != '\0' &&
  312. message[i + line_length] != ' ') {
  313. // Find the last space within the 30-character segment
  314. char* last_space = strrchr(line, ' ');
  315. if(last_space != NULL) {
  316. // Adjust the line length to avoid cutting the word
  317. line_length = last_space - line;
  318. line[line_length] = '\0'; // Null-terminate at the space
  319. }
  320. }
  321. // Manually copy the fixed line into the formatted_message buffer
  322. for(uint32_t j = 0; j < line_length; j++) {
  323. formatted_message[formatted_index++] = line[j];
  324. }
  325. // Add a newline character for line spacing
  326. formatted_message[formatted_index++] = '\n';
  327. // Move i forward to the start of the next word
  328. i += line_length;
  329. // Skip spaces at the beginning of the next line
  330. while(message[i] == ' ') {
  331. i++;
  332. }
  333. }
  334. // Add the formatted message to the widget
  335. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  336. }
  337. void flip_trader_loader_draw_callback(Canvas* canvas, void* model) {
  338. if(!canvas || !model) {
  339. FURI_LOG_E(TAG, "flip_trader_loader_draw_callback - canvas or model is NULL");
  340. return;
  341. }
  342. SerialState http_state = fhttp.state;
  343. AssetLoaderModel* asset_loader_model = (AssetLoaderModel*)model;
  344. AssetState asset_state = asset_loader_model->asset_state;
  345. char* title = asset_loader_model->title;
  346. canvas_set_font(canvas, FontSecondary);
  347. if(http_state == INACTIVE) {
  348. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  349. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  350. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  351. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  352. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  353. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  354. return;
  355. }
  356. if(asset_state == AssetStateError || asset_state == AssetStateParseError) {
  357. flip_trader_request_error_draw(canvas);
  358. return;
  359. }
  360. canvas_draw_str(canvas, 0, 7, title);
  361. canvas_draw_str(canvas, 0, 17, "Loading...");
  362. if(asset_state == AssetStateInitial) {
  363. return;
  364. }
  365. if(http_state == SENDING) {
  366. canvas_draw_str(canvas, 0, 27, "Sending...");
  367. return;
  368. }
  369. if(http_state == RECEIVING || asset_state == AssetStateRequested) {
  370. canvas_draw_str(canvas, 0, 27, "Receiving...");
  371. return;
  372. }
  373. if(http_state == IDLE && asset_state == AssetStateReceived) {
  374. canvas_draw_str(canvas, 0, 27, "Processing...");
  375. return;
  376. }
  377. if(http_state == IDLE && asset_state == AssetStateParsed) {
  378. canvas_draw_str(canvas, 0, 27, "Processed...");
  379. return;
  380. }
  381. }
  382. static void flip_trader_loader_process_callback(void* context) {
  383. if(context == NULL) {
  384. FURI_LOG_E(TAG, "flip_trader_loader_process_callback - context is NULL");
  385. DEV_CRASH();
  386. return;
  387. }
  388. FlipTraderApp* app = (FlipTraderApp*)context;
  389. View* view = app->view_loader;
  390. AssetState current_asset_state;
  391. with_view_model(
  392. view, AssetLoaderModel * model, { current_asset_state = model->asset_state; }, false);
  393. if(current_asset_state == AssetStateInitial) {
  394. with_view_model(
  395. view,
  396. AssetLoaderModel * model,
  397. {
  398. model->asset_state = AssetStateRequested;
  399. AssetLoaderFetch fetch = model->fetcher;
  400. if(fetch == NULL) {
  401. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  402. model->asset_state = AssetStateError;
  403. return;
  404. }
  405. // Clear any previous responses
  406. strncpy(fhttp.last_response, "", 1);
  407. bool request_status = fetch(model);
  408. if(!request_status) {
  409. model->asset_state = AssetStateError;
  410. }
  411. },
  412. true);
  413. } else if(current_asset_state == AssetStateRequested || current_asset_state == AssetStateError) {
  414. if(fhttp.state == IDLE && fhttp.last_response != NULL) {
  415. if(strstr(fhttp.last_response, "[PONG]") != NULL) {
  416. FURI_LOG_DEV(TAG, "PONG received.");
  417. } else if(strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0) {
  418. FURI_LOG_DEV(
  419. TAG,
  420. "SUCCESS received. %s",
  421. fhttp.last_response ? fhttp.last_response : "NULL");
  422. } else if(strncmp(fhttp.last_response, "[ERROR]", 9) == 0) {
  423. FURI_LOG_DEV(
  424. TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  425. } else if(strlen(fhttp.last_response) == 0) {
  426. // Still waiting on response
  427. } else {
  428. with_view_model(
  429. view,
  430. AssetLoaderModel * model,
  431. { model->asset_state = AssetStateReceived; },
  432. true);
  433. }
  434. } else if(fhttp.state == SENDING || fhttp.state == RECEIVING) {
  435. // continue waiting
  436. } else if(fhttp.state == INACTIVE) {
  437. // inactive. try again
  438. } else if(fhttp.state == ISSUE) {
  439. with_view_model(
  440. view, AssetLoaderModel * model, { model->asset_state = AssetStateError; }, true);
  441. } else {
  442. FURI_LOG_DEV(
  443. TAG,
  444. "Unexpected state: %d lastresp: %s",
  445. fhttp.state,
  446. fhttp.last_response ? fhttp.last_response : "NULL");
  447. DEV_CRASH();
  448. }
  449. } else if(current_asset_state == AssetStateReceived) {
  450. with_view_model(
  451. view,
  452. AssetLoaderModel * model,
  453. {
  454. char* asset_text;
  455. if(model->parser == NULL) {
  456. asset_text = NULL;
  457. FURI_LOG_DEV(TAG, "Parser is NULL");
  458. DEV_CRASH();
  459. } else {
  460. asset_text = model->parser(model);
  461. }
  462. FURI_LOG_DEV(
  463. TAG,
  464. "Parsed asset: %s\r\ntext: %s",
  465. fhttp.last_response ? fhttp.last_response : "NULL",
  466. asset_text ? asset_text : "NULL");
  467. model->asset_text = asset_text;
  468. if(asset_text == NULL) {
  469. model->asset_state = AssetStateParseError;
  470. } else {
  471. model->asset_state = AssetStateParsed;
  472. }
  473. },
  474. true);
  475. } else if(current_asset_state == AssetStateParsed) {
  476. with_view_model(
  477. view,
  478. AssetLoaderModel * model,
  479. {
  480. if(++model->request_index < model->request_count) {
  481. model->asset_state = AssetStateInitial;
  482. } else {
  483. flip_trader_widget_set_text(
  484. model->asset_text != NULL ? model->asset_text : "Empty result",
  485. &app_instance->widget_result);
  486. if(model->asset_text != NULL) {
  487. free(model->asset_text);
  488. model->asset_text = NULL;
  489. }
  490. view_set_previous_callback(
  491. widget_get_view(app_instance->widget_result), model->back_callback);
  492. view_dispatcher_switch_to_view(
  493. app_instance->view_dispatcher, FlipTraderViewWidgetResult);
  494. }
  495. },
  496. true);
  497. }
  498. }
  499. static void flip_trader_loader_timer_callback(void* context) {
  500. if(context == NULL) {
  501. FURI_LOG_E(TAG, "flip_trader_loader_timer_callback - context is NULL");
  502. DEV_CRASH();
  503. return;
  504. }
  505. FlipTraderApp* app = (FlipTraderApp*)context;
  506. view_dispatcher_send_custom_event(app->view_dispatcher, FlipTraderCustomEventProcess);
  507. }
  508. static void flip_trader_loader_on_enter(void* context) {
  509. if(context == NULL) {
  510. FURI_LOG_E(TAG, "flip_trader_loader_on_enter - context is NULL");
  511. DEV_CRASH();
  512. return;
  513. }
  514. FlipTraderApp* app = (FlipTraderApp*)context;
  515. View* view = app->view_loader;
  516. with_view_model(
  517. view,
  518. AssetLoaderModel * model,
  519. {
  520. view_set_previous_callback(view, model->back_callback);
  521. if(model->timer == NULL) {
  522. model->timer = furi_timer_alloc(
  523. flip_trader_loader_timer_callback, FuriTimerTypePeriodic, app);
  524. }
  525. furi_timer_start(model->timer, 250);
  526. },
  527. true);
  528. }
  529. static void flip_trader_loader_on_exit(void* context) {
  530. if(context == NULL) {
  531. FURI_LOG_E(TAG, "flip_trader_loader_on_exit - context is NULL");
  532. DEV_CRASH();
  533. return;
  534. }
  535. FlipTraderApp* app = (FlipTraderApp*)context;
  536. View* view = app->view_loader;
  537. with_view_model(
  538. view,
  539. AssetLoaderModel * model,
  540. {
  541. if(model->timer) {
  542. furi_timer_stop(model->timer);
  543. }
  544. },
  545. false);
  546. }
  547. void flip_trader_loader_init(View* view) {
  548. if(view == NULL) {
  549. FURI_LOG_E(TAG, "flip_trader_loader_init - view is NULL");
  550. DEV_CRASH();
  551. return;
  552. }
  553. view_allocate_model(view, ViewModelTypeLocking, sizeof(AssetLoaderModel));
  554. view_set_enter_callback(view, flip_trader_loader_on_enter);
  555. view_set_exit_callback(view, flip_trader_loader_on_exit);
  556. }
  557. void flip_trader_loader_free_model(View* view) {
  558. if(view == NULL) {
  559. FURI_LOG_E(TAG, "flip_trader_loader_free_model - view is NULL");
  560. DEV_CRASH();
  561. return;
  562. }
  563. with_view_model(
  564. view,
  565. AssetLoaderModel * model,
  566. {
  567. if(model->timer) {
  568. furi_timer_free(model->timer);
  569. model->timer = NULL;
  570. }
  571. if(model->parser_context) {
  572. free(model->parser_context);
  573. model->parser_context = NULL;
  574. }
  575. },
  576. false);
  577. }
  578. bool flip_trader_custom_event_callback(void* context, uint32_t index) {
  579. if(context == NULL) {
  580. FURI_LOG_E(TAG, "flip_trader_custom_event_callback - context is NULL");
  581. DEV_CRASH();
  582. return false;
  583. }
  584. switch(index) {
  585. case FlipTraderCustomEventProcess:
  586. flip_trader_loader_process_callback(context);
  587. return true;
  588. default:
  589. FURI_LOG_DEV(TAG, "flip_trader_custom_event_callback. Unknown index: %ld", index);
  590. return false;
  591. }
  592. }
  593. void flip_trader_generic_switch_to_view(
  594. FlipTraderApp* app,
  595. char* title,
  596. AssetLoaderFetch fetcher,
  597. AssetLoaderParser parser,
  598. size_t request_count,
  599. ViewNavigationCallback back,
  600. uint32_t view_id) {
  601. if(app == NULL) {
  602. FURI_LOG_E(TAG, "flip_trader_generic_switch_to_view - app is NULL");
  603. DEV_CRASH();
  604. return;
  605. }
  606. View* view = app->view_loader;
  607. if(view == NULL) {
  608. FURI_LOG_E(TAG, "flip_trader_generic_switch_to_view - view is NULL");
  609. DEV_CRASH();
  610. return;
  611. }
  612. with_view_model(
  613. view,
  614. AssetLoaderModel * model,
  615. {
  616. model->title = title;
  617. model->fetcher = fetcher;
  618. model->parser = parser;
  619. model->request_index = 0;
  620. model->request_count = request_count;
  621. model->back_callback = back;
  622. model->asset_state = AssetStateInitial;
  623. model->asset_text = NULL;
  624. },
  625. true);
  626. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  627. }