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