nfc_playlist_scene_nfc_remove.c 5.4 KB

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