flip_weather_callback.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. #include "callback/flip_weather_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. bool weather_request_success = false;
  13. bool sent_weather_request = false;
  14. bool got_weather_data = false;
  15. static void flip_weather_request_error_draw(Canvas* canvas) {
  16. if(canvas == NULL) {
  17. FURI_LOG_E(TAG, "flip_weather_request_error_draw - canvas is NULL");
  18. DEV_CRASH();
  19. return;
  20. }
  21. if(fhttp.last_response != NULL) {
  22. if(strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") !=
  23. NULL) {
  24. canvas_clear(canvas);
  25. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  26. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  27. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  28. } else if(strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL) {
  29. canvas_clear(canvas);
  30. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  31. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  32. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  33. } else if(
  34. strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") !=
  35. NULL) {
  36. canvas_clear(canvas);
  37. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  38. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  39. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  40. } else if(strstr(fhttp.last_response, "[PONG]") != NULL) {
  41. canvas_clear(canvas);
  42. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  43. } else {
  44. canvas_clear(canvas);
  45. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  46. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  47. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  48. }
  49. } else {
  50. canvas_clear(canvas);
  51. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  52. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  53. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  54. }
  55. }
  56. static void flip_weather_gps_switch_to_view(FlipWeatherApp* app) {
  57. flip_weather_generic_switch_to_view(
  58. app,
  59. "Fetching GPS data..",
  60. send_geo_location_request,
  61. process_geo_location,
  62. 1,
  63. callback_to_submenu,
  64. FlipWeatherViewLoader);
  65. }
  66. static void flip_weather_weather_switch_to_view(FlipWeatherApp* app) {
  67. flip_weather_generic_switch_to_view(
  68. app,
  69. "Fetching Weather data..",
  70. send_geo_weather_request,
  71. process_weather,
  72. 1,
  73. callback_to_submenu,
  74. FlipWeatherViewLoader);
  75. }
  76. void callback_submenu_choices(void* context, uint32_t index) {
  77. FlipWeatherApp* app = (FlipWeatherApp*)context;
  78. if(!app) {
  79. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  80. return;
  81. }
  82. switch(index) {
  83. case FlipWeatherSubmenuIndexWeather:
  84. flipper_http_loading_task(
  85. send_geo_location_request,
  86. process_geo_location_2,
  87. FlipWeatherViewSubmenu,
  88. FlipWeatherViewSubmenu,
  89. &app->view_dispatcher);
  90. flip_weather_weather_switch_to_view(app);
  91. break;
  92. case FlipWeatherSubmenuIndexGPS:
  93. flip_weather_gps_switch_to_view(app);
  94. break;
  95. case FlipWeatherSubmenuIndexAbout:
  96. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewAbout);
  97. break;
  98. case FlipWeatherSubmenuIndexSettings:
  99. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. void text_updated_ssid(void* context) {
  106. FlipWeatherApp* app = (FlipWeatherApp*)context;
  107. if(!app) {
  108. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  109. return;
  110. }
  111. // store the entered text
  112. strncpy(
  113. app->uart_text_input_buffer_ssid,
  114. app->uart_text_input_temp_buffer_ssid,
  115. app->uart_text_input_buffer_size_ssid);
  116. // Ensure null-termination
  117. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  118. // update the variable item text
  119. if(app->variable_item_ssid) {
  120. variable_item_set_current_value_text(
  121. app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  122. }
  123. // save settings
  124. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  125. // save wifi settings to devboard
  126. if(strlen(app->uart_text_input_buffer_ssid) > 0 &&
  127. strlen(app->uart_text_input_buffer_password) > 0) {
  128. if(!flipper_http_save_wifi(
  129. app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password)) {
  130. FURI_LOG_E(TAG, "Failed to save wifi settings");
  131. }
  132. }
  133. // switch to the settings view
  134. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  135. }
  136. void text_updated_password(void* context) {
  137. FlipWeatherApp* app = (FlipWeatherApp*)context;
  138. if(!app) {
  139. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  140. return;
  141. }
  142. // store the entered text
  143. strncpy(
  144. app->uart_text_input_buffer_password,
  145. app->uart_text_input_temp_buffer_password,
  146. app->uart_text_input_buffer_size_password);
  147. // Ensure null-termination
  148. app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
  149. // update the variable item text
  150. if(app->variable_item_password) {
  151. variable_item_set_current_value_text(
  152. app->variable_item_password, app->uart_text_input_buffer_password);
  153. }
  154. // save settings
  155. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  156. // save wifi settings to devboard
  157. if(strlen(app->uart_text_input_buffer_ssid) > 0 &&
  158. strlen(app->uart_text_input_buffer_password) > 0) {
  159. if(!flipper_http_save_wifi(
  160. app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password)) {
  161. FURI_LOG_E(TAG, "Failed to save wifi settings");
  162. }
  163. }
  164. // switch to the settings view
  165. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  166. }
  167. uint32_t callback_to_submenu(void* context) {
  168. if(!context) {
  169. FURI_LOG_E(TAG, "Context is NULL");
  170. return VIEW_NONE;
  171. }
  172. UNUSED(context);
  173. sent_get_request = false;
  174. get_request_success = false;
  175. got_ip_address = false;
  176. got_weather_data = false;
  177. geo_information_processed = false;
  178. weather_information_processed = false;
  179. sent_weather_request = false;
  180. weather_request_success = false;
  181. if(weather_data != NULL) {
  182. free(weather_data);
  183. weather_data = NULL;
  184. }
  185. if(total_data != NULL) {
  186. free(total_data);
  187. total_data = NULL;
  188. }
  189. return FlipWeatherViewSubmenu;
  190. }
  191. void settings_item_selected(void* context, uint32_t index) {
  192. FlipWeatherApp* app = (FlipWeatherApp*)context;
  193. if(!app) {
  194. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  195. return;
  196. }
  197. switch(index) {
  198. case 0: // Input SSID
  199. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewTextInputSSID);
  200. break;
  201. case 1: // Input Password
  202. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewTextInputPassword);
  203. break;
  204. default:
  205. FURI_LOG_E(TAG, "Unknown configuration item index");
  206. break;
  207. }
  208. }
  209. /**
  210. * @brief Navigation callback for exiting the application
  211. * @param context The context - unused
  212. * @return next view id (VIEW_NONE to exit the app)
  213. */
  214. uint32_t callback_exit_app(void* context) {
  215. // Exit the application
  216. if(!context) {
  217. FURI_LOG_E(TAG, "Context is NULL");
  218. return VIEW_NONE;
  219. }
  220. UNUSED(context);
  221. return VIEW_NONE; // Return VIEW_NONE to exit the app
  222. }
  223. uint32_t callback_to_wifi_settings(void* context) {
  224. if(!context) {
  225. FURI_LOG_E(TAG, "Context is NULL");
  226. return VIEW_NONE;
  227. }
  228. UNUSED(context);
  229. return FlipWeatherViewSettings;
  230. }
  231. static void flip_weather_widget_set_text(char* message, Widget** widget) {
  232. if(widget == NULL) {
  233. FURI_LOG_E(TAG, "flip_weather_set_widget_text - widget is NULL");
  234. DEV_CRASH();
  235. return;
  236. }
  237. if(message == NULL) {
  238. FURI_LOG_E(TAG, "flip_weather_set_widget_text - message is NULL");
  239. DEV_CRASH();
  240. return;
  241. }
  242. widget_reset(*widget);
  243. uint32_t message_length = strlen(message); // Length of the message
  244. uint32_t i = 0; // Index tracker
  245. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  246. char* formatted_message; // Buffer to hold the final formatted message
  247. // Allocate buffer with double the message length plus one for safety
  248. if(!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1)) {
  249. return;
  250. }
  251. while(i < message_length) {
  252. uint32_t max_line_length = 31; // Maximum characters per line
  253. uint32_t remaining_length = message_length - i; // Remaining characters
  254. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length :
  255. max_line_length;
  256. // Check for newline character within the current segment
  257. uint32_t newline_pos = i;
  258. bool found_newline = false;
  259. for(; newline_pos < i + line_length && newline_pos < message_length; newline_pos++) {
  260. if(message[newline_pos] == '\n') {
  261. found_newline = true;
  262. break;
  263. }
  264. }
  265. if(found_newline) {
  266. // If newline found, set line_length up to the newline
  267. line_length = newline_pos - i;
  268. }
  269. // Temporary buffer to hold the current line
  270. char line[32];
  271. strncpy(line, message + i, line_length);
  272. line[line_length] = '\0';
  273. // If newline was found, skip it for the next iteration
  274. if(found_newline) {
  275. i += line_length + 1; // +1 to skip the '\n' character
  276. } else {
  277. // Check if the line ends in the middle of a word and adjust accordingly
  278. if(line_length == max_line_length && message[i + line_length] != '\0' &&
  279. message[i + line_length] != ' ') {
  280. // Find the last space within the current line to avoid breaking a word
  281. char* last_space = strrchr(line, ' ');
  282. if(last_space != NULL) {
  283. // Adjust the line_length to avoid cutting the word
  284. line_length = last_space - line;
  285. line[line_length] = '\0'; // Null-terminate at the space
  286. }
  287. }
  288. // Move the index forward by the determined line_length
  289. i += line_length;
  290. // Skip any spaces at the beginning of the next line
  291. while(i < message_length && message[i] == ' ') {
  292. i++;
  293. }
  294. }
  295. // Manually copy the fixed line into the formatted_message buffer
  296. for(uint32_t j = 0; j < line_length; j++) {
  297. formatted_message[formatted_index++] = line[j];
  298. }
  299. // Add a newline character for line spacing
  300. formatted_message[formatted_index++] = '\n';
  301. }
  302. // Null-terminate the formatted_message
  303. formatted_message[formatted_index] = '\0';
  304. // Add the formatted message to the widget
  305. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  306. }
  307. void flip_weather_loader_draw_callback(Canvas* canvas, void* model) {
  308. if(!canvas || !model) {
  309. FURI_LOG_E(TAG, "flip_weather_loader_draw_callback - canvas or model is NULL");
  310. return;
  311. }
  312. SerialState http_state = fhttp.state;
  313. DataLoaderModel* data_loader_model = (DataLoaderModel*)model;
  314. DataState data_state = data_loader_model->data_state;
  315. char* title = data_loader_model->title;
  316. canvas_set_font(canvas, FontSecondary);
  317. if(http_state == INACTIVE) {
  318. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  319. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  320. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  321. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  322. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  323. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  324. return;
  325. }
  326. if(data_state == DataStateError || data_state == DataStateParseError) {
  327. flip_weather_request_error_draw(canvas);
  328. return;
  329. }
  330. canvas_draw_str(canvas, 0, 7, title);
  331. canvas_draw_str(canvas, 0, 17, "Loading...");
  332. if(data_state == DataStateInitial) {
  333. return;
  334. }
  335. if(http_state == SENDING) {
  336. canvas_draw_str(canvas, 0, 27, "Sending...");
  337. return;
  338. }
  339. if(http_state == RECEIVING || data_state == DataStateRequested) {
  340. canvas_draw_str(canvas, 0, 27, "Receiving...");
  341. return;
  342. }
  343. if(http_state == IDLE && data_state == DataStateReceived) {
  344. canvas_draw_str(canvas, 0, 27, "Processing...");
  345. return;
  346. }
  347. if(http_state == IDLE && data_state == DataStateParsed) {
  348. canvas_draw_str(canvas, 0, 27, "Processed...");
  349. return;
  350. }
  351. }
  352. static void flip_weather_loader_process_callback(void* context) {
  353. if(context == NULL) {
  354. FURI_LOG_E(TAG, "flip_weather_loader_process_callback - context is NULL");
  355. DEV_CRASH();
  356. return;
  357. }
  358. FlipWeatherApp* app = (FlipWeatherApp*)context;
  359. View* view = app->view_loader;
  360. DataState current_data_state;
  361. with_view_model(
  362. view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  363. if(current_data_state == DataStateInitial) {
  364. with_view_model(
  365. view,
  366. DataLoaderModel * model,
  367. {
  368. model->data_state = DataStateRequested;
  369. DataLoaderFetch fetch = model->fetcher;
  370. if(fetch == NULL) {
  371. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  372. model->data_state = DataStateError;
  373. return;
  374. }
  375. // Clear any previous responses
  376. strncpy(fhttp.last_response, "", 1);
  377. bool request_status = fetch(model);
  378. if(!request_status) {
  379. model->data_state = DataStateError;
  380. }
  381. },
  382. true);
  383. } else if(current_data_state == DataStateRequested || current_data_state == DataStateError) {
  384. if(fhttp.state == IDLE && fhttp.last_response != NULL) {
  385. if(strstr(fhttp.last_response, "[PONG]") != NULL) {
  386. FURI_LOG_DEV(TAG, "PONG received.");
  387. } else if(strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0) {
  388. FURI_LOG_DEV(
  389. TAG,
  390. "SUCCESS received. %s",
  391. fhttp.last_response ? fhttp.last_response : "NULL");
  392. } else if(strncmp(fhttp.last_response, "[ERROR]", 9) == 0) {
  393. FURI_LOG_DEV(
  394. TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  395. } else if(strlen(fhttp.last_response) == 0) {
  396. // Still waiting on response
  397. } else {
  398. with_view_model(
  399. view,
  400. DataLoaderModel * model,
  401. { model->data_state = DataStateReceived; },
  402. true);
  403. }
  404. } else if(fhttp.state == SENDING || fhttp.state == RECEIVING) {
  405. // continue waiting
  406. } else if(fhttp.state == INACTIVE) {
  407. // inactive. try again
  408. } else if(fhttp.state == ISSUE) {
  409. with_view_model(
  410. view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  411. } else {
  412. FURI_LOG_DEV(
  413. TAG,
  414. "Unexpected state: %d lastresp: %s",
  415. fhttp.state,
  416. fhttp.last_response ? fhttp.last_response : "NULL");
  417. DEV_CRASH();
  418. }
  419. } else if(current_data_state == DataStateReceived) {
  420. with_view_model(
  421. view,
  422. DataLoaderModel * model,
  423. {
  424. char* data_text;
  425. if(model->parser == NULL) {
  426. data_text = NULL;
  427. FURI_LOG_DEV(TAG, "Parser is NULL");
  428. DEV_CRASH();
  429. } else {
  430. data_text = model->parser(model);
  431. }
  432. FURI_LOG_DEV(
  433. TAG,
  434. "Parsed data: %s\r\ntext: %s",
  435. fhttp.last_response ? fhttp.last_response : "NULL",
  436. data_text ? data_text : "NULL");
  437. model->data_text = data_text;
  438. if(data_text == NULL) {
  439. model->data_state = DataStateParseError;
  440. } else {
  441. model->data_state = DataStateParsed;
  442. }
  443. },
  444. true);
  445. } else if(current_data_state == DataStateParsed) {
  446. with_view_model(
  447. view,
  448. DataLoaderModel * model,
  449. {
  450. if(++model->request_index < model->request_count) {
  451. model->data_state = DataStateInitial;
  452. } else {
  453. flip_weather_widget_set_text(
  454. model->data_text != NULL ? model->data_text : "Empty result",
  455. &app_instance->widget_result);
  456. if(model->data_text != NULL) {
  457. free(model->data_text);
  458. model->data_text = NULL;
  459. }
  460. view_set_previous_callback(
  461. widget_get_view(app_instance->widget_result), model->back_callback);
  462. view_dispatcher_switch_to_view(
  463. app_instance->view_dispatcher, FlipWeatherViewWidgetResult);
  464. }
  465. },
  466. true);
  467. }
  468. }
  469. static void flip_weather_loader_timer_callback(void* context) {
  470. if(context == NULL) {
  471. FURI_LOG_E(TAG, "flip_weather_loader_timer_callback - context is NULL");
  472. DEV_CRASH();
  473. return;
  474. }
  475. FlipWeatherApp* app = (FlipWeatherApp*)context;
  476. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWeatherCustomEventProcess);
  477. }
  478. static void flip_weather_loader_on_enter(void* context) {
  479. if(context == NULL) {
  480. FURI_LOG_E(TAG, "flip_weather_loader_on_enter - context is NULL");
  481. DEV_CRASH();
  482. return;
  483. }
  484. FlipWeatherApp* app = (FlipWeatherApp*)context;
  485. View* view = app->view_loader;
  486. with_view_model(
  487. view,
  488. DataLoaderModel * model,
  489. {
  490. view_set_previous_callback(view, model->back_callback);
  491. if(model->timer == NULL) {
  492. model->timer = furi_timer_alloc(
  493. flip_weather_loader_timer_callback, FuriTimerTypePeriodic, app);
  494. }
  495. furi_timer_start(model->timer, 250);
  496. },
  497. true);
  498. }
  499. static void flip_weather_loader_on_exit(void* context) {
  500. if(context == NULL) {
  501. FURI_LOG_E(TAG, "flip_weather_loader_on_exit - context is NULL");
  502. DEV_CRASH();
  503. return;
  504. }
  505. FlipWeatherApp* app = (FlipWeatherApp*)context;
  506. View* view = app->view_loader;
  507. with_view_model(
  508. view,
  509. DataLoaderModel * model,
  510. {
  511. if(model->timer) {
  512. furi_timer_stop(model->timer);
  513. }
  514. },
  515. false);
  516. }
  517. void flip_weather_loader_init(View* view) {
  518. if(view == NULL) {
  519. FURI_LOG_E(TAG, "flip_weather_loader_init - view is NULL");
  520. DEV_CRASH();
  521. return;
  522. }
  523. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  524. view_set_enter_callback(view, flip_weather_loader_on_enter);
  525. view_set_exit_callback(view, flip_weather_loader_on_exit);
  526. }
  527. void flip_weather_loader_free_model(View* view) {
  528. if(view == NULL) {
  529. FURI_LOG_E(TAG, "flip_weather_loader_free_model - view is NULL");
  530. DEV_CRASH();
  531. return;
  532. }
  533. with_view_model(
  534. view,
  535. DataLoaderModel * model,
  536. {
  537. if(model->timer) {
  538. furi_timer_free(model->timer);
  539. model->timer = NULL;
  540. }
  541. if(model->parser_context) {
  542. free(model->parser_context);
  543. model->parser_context = NULL;
  544. }
  545. },
  546. false);
  547. }
  548. bool flip_weather_custom_event_callback(void* context, uint32_t index) {
  549. if(context == NULL) {
  550. FURI_LOG_E(TAG, "flip_weather_custom_event_callback - context is NULL");
  551. DEV_CRASH();
  552. return false;
  553. }
  554. switch(index) {
  555. case FlipWeatherCustomEventProcess:
  556. flip_weather_loader_process_callback(context);
  557. return true;
  558. default:
  559. FURI_LOG_DEV(TAG, "flip_weather_custom_event_callback. Unknown index: %ld", index);
  560. return false;
  561. }
  562. }
  563. void flip_weather_generic_switch_to_view(
  564. FlipWeatherApp* app,
  565. char* title,
  566. DataLoaderFetch fetcher,
  567. DataLoaderParser parser,
  568. size_t request_count,
  569. ViewNavigationCallback back,
  570. uint32_t view_id) {
  571. if(app == NULL) {
  572. FURI_LOG_E(TAG, "flip_weather_generic_switch_to_view - app is NULL");
  573. DEV_CRASH();
  574. return;
  575. }
  576. View* view = app->view_loader;
  577. if(view == NULL) {
  578. FURI_LOG_E(TAG, "flip_weather_generic_switch_to_view - view is NULL");
  579. DEV_CRASH();
  580. return;
  581. }
  582. with_view_model(
  583. view,
  584. DataLoaderModel * model,
  585. {
  586. model->title = title;
  587. model->fetcher = fetcher;
  588. model->parser = parser;
  589. model->request_index = 0;
  590. model->request_count = request_count;
  591. model->back_callback = back;
  592. model->data_state = DataStateInitial;
  593. model->data_text = NULL;
  594. },
  595. true);
  596. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  597. }