nfc_playlist_scene_nfc_move_item.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 =
  16. 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(
  37. variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistNfcMoveItem_MoveItem),
  38. (selected_target == selected_destination),
  39. "Target\nand\nDestination\nare the same");
  40. }
  41. void nfc_playlist_nfc_move_item_scene_on_enter(void* context) {
  42. NfcPlaylist* nfc_playlist = context;
  43. selected_target = 1;
  44. selected_destination = 1;
  45. variable_item_list_set_header(nfc_playlist->variable_item_list, "Move NFC Item");
  46. VariableItem* target_selector = variable_item_list_add(
  47. nfc_playlist->variable_item_list,
  48. "Select Target",
  49. nfc_playlist->settings.playlist_length,
  50. nfc_playlist_nfc_move_item_options_change_callback,
  51. nfc_playlist);
  52. variable_item_set_current_value_index(target_selector, 0);
  53. variable_item_set_current_value_text(target_selector, "1");
  54. VariableItem* destination_selector = variable_item_list_add(
  55. nfc_playlist->variable_item_list,
  56. "Select Destination",
  57. nfc_playlist->settings.playlist_length,
  58. nfc_playlist_nfc_move_item_options_change_callback,
  59. nfc_playlist);
  60. variable_item_set_current_value_index(destination_selector, 0);
  61. variable_item_set_current_value_text(destination_selector, "1");
  62. VariableItem* move_button =
  63. variable_item_list_add(nfc_playlist->variable_item_list, "Move Item", 0, NULL, NULL);
  64. variable_item_set_locked(
  65. move_button,
  66. (selected_target == selected_destination),
  67. "Target\nand\nDestination\nare the same");
  68. variable_item_list_set_enter_callback(
  69. nfc_playlist->variable_item_list, nfc_playlist_nfc_move_item_menu_callback, nfc_playlist);
  70. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_VariableItemList);
  71. }
  72. bool nfc_playlist_nfc_move_item_scene_on_event(void* context, SceneManagerEvent event) {
  73. NfcPlaylist* nfc_playlist = context;
  74. bool consumed = false;
  75. if(event.type == SceneManagerEventTypeCustom) {
  76. switch(event.event) {
  77. case NfcPlaylistNfcMoveItem_MoveItem: {
  78. Storage* storage = furi_record_open(RECORD_STORAGE);
  79. Stream* stream = file_stream_alloc(storage);
  80. if(file_stream_open(
  81. stream,
  82. furi_string_get_cstr(nfc_playlist->settings.playlist_path),
  83. FSAM_READ_WRITE,
  84. FSOM_OPEN_EXISTING)) {
  85. FuriString* tmp_target_str = furi_string_alloc();
  86. FuriString* line = furi_string_alloc();
  87. uint8_t counter = 0;
  88. while(stream_read_line(stream, line)) {
  89. counter++;
  90. if(counter == selected_target) {
  91. furi_string_trim(line);
  92. furi_string_cat(tmp_target_str, line);
  93. stream_rewind(stream);
  94. counter = 0;
  95. break;
  96. }
  97. }
  98. FuriString* tmp_new_order_str = furi_string_alloc();
  99. while(stream_read_line(stream, line)) {
  100. counter++;
  101. if(counter == selected_target) {
  102. continue;
  103. }
  104. if(!furi_string_empty(tmp_new_order_str)) {
  105. furi_string_cat(tmp_new_order_str, "\n");
  106. }
  107. furi_string_trim(line);
  108. if(counter == selected_destination) {
  109. if(counter == 1) {
  110. furi_string_cat_printf(
  111. tmp_new_order_str,
  112. "%s\n%s",
  113. furi_string_get_cstr(tmp_target_str),
  114. furi_string_get_cstr(line));
  115. } else {
  116. furi_string_cat_printf(
  117. tmp_new_order_str,
  118. "%s\n%s",
  119. furi_string_get_cstr(line),
  120. furi_string_get_cstr(tmp_target_str));
  121. }
  122. furi_string_free(tmp_target_str);
  123. } else {
  124. furi_string_cat(tmp_new_order_str, furi_string_get_cstr(line));
  125. }
  126. }
  127. furi_string_free(line);
  128. stream_clean(stream);
  129. stream_write_string(stream, tmp_new_order_str);
  130. furi_string_free(tmp_new_order_str);
  131. file_stream_close(stream);
  132. }
  133. stream_free(stream);
  134. furi_record_close(RECORD_STORAGE);
  135. consumed = true;
  136. break;
  137. }
  138. default:
  139. break;
  140. }
  141. }
  142. return consumed;
  143. }
  144. void nfc_playlist_nfc_move_item_scene_on_exit(void* context) {
  145. NfcPlaylist* nfc_playlist = context;
  146. variable_item_list_reset(nfc_playlist->variable_item_list);
  147. }