nfc_playlist_scene_nfc_move_item.c 5.9 KB

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