nfc_playlist_scene_nfc_remove.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "../nfc_playlist.h"
  2. typedef enum {
  3. NfcPlaylistSettings_LineSelector,
  4. NfcPlaylistSettings_RemoveLine
  5. } NfcPlaylistSettingsMenuSelection;
  6. uint8_t selected_line;
  7. void nfc_playlist_nfc_remove_menu_callback(void* context, uint32_t index) {
  8. NfcPlaylist* nfc_playlist = context;
  9. scene_manager_handle_custom_event(nfc_playlist->scene_manager, index);
  10. }
  11. void nfc_playlist_nfc_remove_options_change_callback(VariableItem* item) {
  12. NfcPlaylist* nfc_playlist = variable_item_get_context(item);
  13. uint8_t current_option = variable_item_list_get_selected_item_index(nfc_playlist->variable_item_list);
  14. uint8_t option_value_index = variable_item_get_current_value_index(item);
  15. switch(current_option) {
  16. case NfcPlaylistSettings_LineSelector: {
  17. selected_line = option_value_index + 1;
  18. FuriString* tmp_str = furi_string_alloc_printf("%d", selected_line);
  19. variable_item_set_current_value_text(item, furi_string_get_cstr(tmp_str));
  20. furi_string_free(tmp_str);
  21. break;
  22. }
  23. default:
  24. break;
  25. }
  26. }
  27. void nfc_playlist_nfc_remove_scene_on_enter(void* context) {
  28. NfcPlaylist* nfc_playlist = context;
  29. selected_line = nfc_playlist->settings.playlist_length;
  30. variable_item_list_set_header(nfc_playlist->variable_item_list, "Settings");
  31. VariableItem* Line_selector = variable_item_list_add(
  32. nfc_playlist->variable_item_list,
  33. "Select Line",
  34. nfc_playlist->settings.playlist_length,
  35. nfc_playlist_nfc_remove_options_change_callback,
  36. nfc_playlist);
  37. variable_item_set_current_value_index(Line_selector, nfc_playlist->settings.playlist_length - 1);
  38. FuriString* tmp_str = furi_string_alloc_printf("%d", selected_line);
  39. variable_item_set_current_value_text(Line_selector, furi_string_get_cstr(tmp_str));
  40. furi_string_free(tmp_str);
  41. variable_item_set_locked(Line_selector, nfc_playlist->settings.playlist_length == 0 ? true : false, "Playlist\nis empty");
  42. variable_item_list_add(nfc_playlist->variable_item_list, "Remove Line", 0, NULL, NULL);
  43. variable_item_set_locked(variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_RemoveLine), nfc_playlist->settings.playlist_length == 0 ? true : false, "Playlist\nis empty");
  44. variable_item_list_set_enter_callback(nfc_playlist->variable_item_list, nfc_playlist_nfc_remove_menu_callback, nfc_playlist);
  45. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_VariableItemList);
  46. }
  47. bool nfc_playlist_nfc_remove_scene_on_event(void* context, SceneManagerEvent event) {
  48. NfcPlaylist* nfc_playlist = context;
  49. bool consumed = false;
  50. if (event.type == SceneManagerEventTypeCustom) {
  51. switch(event.event) {
  52. case NfcPlaylistSettings_RemoveLine:
  53. Storage* storage = furi_record_open(RECORD_STORAGE);
  54. Stream* stream = file_stream_alloc(storage);
  55. if (file_stream_open(stream, furi_string_get_cstr(nfc_playlist->settings.playlist_path), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  56. FuriString* line = furi_string_alloc();
  57. FuriString* tmp_str = furi_string_alloc();
  58. uint8_t current_line = 0;
  59. while(stream_read_line(stream, line)) {
  60. current_line++;
  61. if (current_line != selected_line) {
  62. furi_string_replace_all(line, "\n", "");
  63. if (furi_string_empty(tmp_str)) {
  64. furi_string_cat_printf(tmp_str, "%s", furi_string_get_cstr(line));
  65. } else {
  66. furi_string_cat_printf(tmp_str, "\n%s", furi_string_get_cstr(line));
  67. }
  68. }
  69. }
  70. furi_string_free(line);
  71. stream_clean(stream);
  72. stream_write_string(stream, tmp_str);
  73. furi_string_free(tmp_str);
  74. file_stream_close(stream);
  75. nfc_playlist->settings.playlist_length--;
  76. selected_line = nfc_playlist->settings.playlist_length;
  77. }
  78. stream_free(stream);
  79. furi_record_close(RECORD_STORAGE);
  80. if (selected_line == 0) {
  81. scene_manager_previous_scene(nfc_playlist->scene_manager);
  82. } else {
  83. VariableItem* Line_selector = variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_LineSelector);
  84. variable_item_set_values_count(Line_selector, nfc_playlist->settings.playlist_length);
  85. variable_item_set_current_value_index(Line_selector, selected_line - 1);
  86. FuriString* tmp_str = furi_string_alloc_printf("%d", selected_line);
  87. variable_item_set_current_value_text(Line_selector, furi_string_get_cstr(tmp_str));
  88. furi_string_free(tmp_str);
  89. }
  90. consumed = true;
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. return consumed;
  97. }
  98. void nfc_playlist_nfc_remove_scene_on_exit(void* context) {
  99. NfcPlaylist* nfc_playlist = context;
  100. variable_item_list_reset(nfc_playlist->variable_item_list);
  101. }