nfc_playlist_scene_nfc_move_item.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. furi_string_reset(line);
  81. stream_rewind(stream);
  82. counter = 0;
  83. break;
  84. }
  85. }
  86. FuriString* tmp_new_order_str = furi_string_alloc();
  87. while(stream_read_line(stream, line)) {
  88. counter++;
  89. if(counter == selected_target) {
  90. continue;
  91. }
  92. if(!furi_string_empty(tmp_new_order_str)) {
  93. furi_string_cat_printf(tmp_new_order_str, "%s", "\n");
  94. }
  95. furi_string_trim(line);
  96. if(counter == selected_destination) {
  97. if(counter == 1) {
  98. furi_string_cat_printf(tmp_new_order_str, "%s\n%s", furi_string_get_cstr(tmp_target_str), furi_string_get_cstr(line));
  99. furi_string_reset(tmp_target_str);
  100. } else {
  101. furi_string_cat_printf(tmp_new_order_str, "%s\n%s", furi_string_get_cstr(line), furi_string_get_cstr(tmp_target_str));
  102. furi_string_reset(tmp_target_str);
  103. }
  104. } else {
  105. furi_string_cat_printf(tmp_new_order_str, "%s", furi_string_get_cstr(line));
  106. }
  107. }
  108. furi_string_free(line);
  109. stream_clean(stream);
  110. stream_write_string(stream, tmp_new_order_str);
  111. furi_string_free(tmp_new_order_str);
  112. file_stream_close(stream);
  113. }
  114. stream_free(stream);
  115. furi_record_close(RECORD_STORAGE);
  116. consumed = true;
  117. break;
  118. }
  119. default:
  120. break;
  121. }
  122. }
  123. return consumed;
  124. }
  125. void nfc_playlist_nfc_move_item_scene_on_exit(void* context) {
  126. NfcPlaylist* nfc_playlist = context;
  127. variable_item_list_reset(nfc_playlist->variable_item_list);
  128. }