nfc_playlist_scene_nfc_move_item 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // This is currently not working so its not included at the moment
  2. #include "../nfc_playlist.h"
  3. typedef enum {
  4. NfcPlaylistNfcMoveItem_TargetSelector,
  5. NfcPlaylistNfcMoveItem_DestinationSelector,
  6. NfcPlaylistNfcMoveItem_MoveItem
  7. } NfcPlaylistNfcMoveItemMenuSelection;
  8. uint8_t selected_target;
  9. uint8_t selected_destination;
  10. void nfc_playlist_nfc_move_item_menu_callback(void* context, uint32_t index) {
  11. NfcPlaylist* nfc_playlist = context;
  12. scene_manager_handle_custom_event(nfc_playlist->scene_manager, index);
  13. }
  14. void nfc_playlist_nfc_move_item_options_change_callback(VariableItem* item) {
  15. NfcPlaylist* nfc_playlist = variable_item_get_context(item);
  16. uint8_t current_option = variable_item_list_get_selected_item_index(nfc_playlist->variable_item_list);
  17. uint8_t option_value_index = variable_item_get_current_value_index(item);
  18. switch(current_option) {
  19. case NfcPlaylistNfcMoveItem_TargetSelector: {
  20. selected_target = option_value_index + 1;
  21. FuriString* tmp_str = furi_string_alloc_printf("%d", selected_target);
  22. variable_item_set_current_value_text(item, furi_string_get_cstr(tmp_str));
  23. furi_string_free(tmp_str);
  24. break;
  25. }
  26. case NfcPlaylistNfcMoveItem_DestinationSelector: {
  27. selected_destination = option_value_index + 1;
  28. FuriString* tmp_str = furi_string_alloc_printf("%d", selected_destination);
  29. variable_item_set_current_value_text(item, furi_string_get_cstr(tmp_str));
  30. furi_string_free(tmp_str);
  31. break;
  32. }
  33. default:
  34. break;
  35. }
  36. variable_item_set_locked(variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistNfcMoveItem_MoveItem), (selected_target == selected_destination), "Target\nand\nDestination\nare the same");
  37. }
  38. void nfc_playlist_nfc_move_item_scene_on_enter(void* context) {
  39. NfcPlaylist* nfc_playlist = context;
  40. selected_target = 0;
  41. selected_destination = 0;
  42. variable_item_list_set_header(nfc_playlist->variable_item_list, "Move NFC Item");
  43. VariableItem* target_selector = variable_item_list_add(
  44. nfc_playlist->variable_item_list,
  45. "Select Target",
  46. nfc_playlist->settings.playlist_length,
  47. nfc_playlist_nfc_move_item_options_change_callback,
  48. nfc_playlist);
  49. variable_item_set_current_value_index(target_selector, 0);
  50. variable_item_set_current_value_text(target_selector, "1");
  51. VariableItem* destination_selector = variable_item_list_add(
  52. nfc_playlist->variable_item_list,
  53. "Select Destination",
  54. nfc_playlist->settings.playlist_length,
  55. nfc_playlist_nfc_move_item_options_change_callback,
  56. nfc_playlist);
  57. variable_item_set_current_value_index(destination_selector, 0);
  58. variable_item_set_current_value_text(destination_selector, "1");
  59. VariableItem* move_button = variable_item_list_add(nfc_playlist->variable_item_list, "Move Item", 0, NULL, NULL);
  60. variable_item_set_locked(move_button, (selected_target == selected_destination), "Target\nand\nDestination\nare the same");
  61. variable_item_list_set_enter_callback(nfc_playlist->variable_item_list, nfc_playlist_nfc_move_item_menu_callback, nfc_playlist);
  62. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_VariableItemList);
  63. }
  64. bool nfc_playlist_nfc_move_item_scene_on_event(void* context, SceneManagerEvent event) {
  65. NfcPlaylist* nfc_playlist = context;
  66. UNUSED(nfc_playlist);
  67. bool consumed = false;
  68. if(event.type == SceneManagerEventTypeCustom) {
  69. switch(event.event) {
  70. case NfcPlaylistNfcMoveItem_MoveItem: {
  71. Storage* storage = furi_record_open(RECORD_STORAGE);
  72. Stream* stream = file_stream_alloc(storage);
  73. if(file_stream_open(stream, furi_string_get_cstr(nfc_playlist->settings.playlist_path), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  74. int counter = 0;
  75. FuriString* tmp_target_line = furi_string_alloc();
  76. FuriString* line = furi_string_alloc();
  77. while(stream_read_line(stream, line)) {
  78. counter++;
  79. if (counter == selected_target) {
  80. furi_string_set(tmp_target_line, line);
  81. }
  82. }
  83. furi_string_reset(line);
  84. stream_rewind(stream);
  85. counter = 0;
  86. FuriString* tmp_new_playlist_order = furi_string_alloc();
  87. while(stream_read_line(stream, line)) {
  88. counter++;
  89. if(counter == selected_target) {
  90. continue;
  91. }
  92. furi_string_trim(line);
  93. if(!furi_string_empty(tmp_new_playlist_order)) {
  94. furi_string_cat_printf(tmp_new_playlist_order, "\n");
  95. }
  96. if(counter == selected_destination) {
  97. furi_string_cat_printf(tmp_new_playlist_order, "%s\n%s", furi_string_get_cstr(tmp_target_line), furi_string_get_cstr(line));
  98. } else {
  99. furi_string_cat_printf(tmp_new_playlist_order, "%s", furi_string_get_cstr(line));
  100. }
  101. }
  102. FURI_LOG_RAW_I("New playlist order: %s\n", furi_string_get_cstr(tmp_new_playlist_order));
  103. stream_clean(stream);
  104. stream_write_string(stream, tmp_new_playlist_order);
  105. file_stream_close(stream);
  106. furi_string_free(tmp_new_playlist_order);
  107. furi_string_free(tmp_target_line);
  108. furi_string_free(line);
  109. }
  110. stream_free(stream);
  111. furi_record_close(RECORD_STORAGE);
  112. consumed = true;
  113. break;
  114. }
  115. default:
  116. break;
  117. }
  118. }
  119. return consumed;
  120. }
  121. void nfc_playlist_nfc_move_item_scene_on_exit(void* context) {
  122. NfcPlaylist* nfc_playlist = context;
  123. variable_item_list_reset(nfc_playlist->variable_item_list);
  124. }