nfc_playlist_scene_nfc_move_item.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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(
  71. nfc_playlist->view_dispatcher, NfcPlaylistView_VariableItemList);
  72. }
  73. bool nfc_playlist_nfc_move_item_scene_on_event(void* context, SceneManagerEvent event) {
  74. NfcPlaylist* nfc_playlist = context;
  75. bool consumed = false;
  76. if(event.type == SceneManagerEventTypeCustom) {
  77. switch(event.event) {
  78. case NfcPlaylistNfcMoveItem_MoveItem: {
  79. Storage* storage = furi_record_open(RECORD_STORAGE);
  80. Stream* stream = file_stream_alloc(storage);
  81. if(file_stream_open(
  82. stream,
  83. furi_string_get_cstr(nfc_playlist->settings.playlist_path),
  84. FSAM_READ_WRITE,
  85. FSOM_OPEN_EXISTING)) {
  86. FuriString* tmp_target_str = furi_string_alloc();
  87. FuriString* line = furi_string_alloc();
  88. uint8_t counter = 0;
  89. while(stream_read_line(stream, line)) {
  90. counter++;
  91. if(counter == selected_target) {
  92. furi_string_trim(line);
  93. furi_string_cat_printf(tmp_target_str, "%s", furi_string_get_cstr(line));
  94. stream_rewind(stream);
  95. counter = 0;
  96. break;
  97. }
  98. }
  99. FuriString* tmp_new_order_str = furi_string_alloc();
  100. while(stream_read_line(stream, line)) {
  101. counter++;
  102. if(counter == selected_target) {
  103. continue;
  104. }
  105. if(!furi_string_empty(tmp_new_order_str)) {
  106. furi_string_cat_printf(tmp_new_order_str, "%s", "\n");
  107. }
  108. furi_string_trim(line);
  109. if(counter == selected_destination) {
  110. if(counter == 1) {
  111. furi_string_cat_printf(
  112. tmp_new_order_str,
  113. "%s\n%s",
  114. furi_string_get_cstr(tmp_target_str),
  115. furi_string_get_cstr(line));
  116. } else {
  117. furi_string_cat_printf(
  118. tmp_new_order_str,
  119. "%s\n%s",
  120. furi_string_get_cstr(line),
  121. furi_string_get_cstr(tmp_target_str));
  122. }
  123. furi_string_free(tmp_target_str);
  124. } else {
  125. furi_string_cat_printf(
  126. tmp_new_order_str, "%s", furi_string_get_cstr(line));
  127. }
  128. }
  129. furi_string_free(line);
  130. stream_clean(stream);
  131. stream_write_string(stream, tmp_new_order_str);
  132. furi_string_free(tmp_new_order_str);
  133. file_stream_close(stream);
  134. }
  135. stream_free(stream);
  136. furi_record_close(RECORD_STORAGE);
  137. consumed = true;
  138. break;
  139. }
  140. default:
  141. break;
  142. }
  143. }
  144. return consumed;
  145. }
  146. void nfc_playlist_nfc_move_item_scene_on_exit(void* context) {
  147. NfcPlaylist* nfc_playlist = context;
  148. variable_item_list_reset(nfc_playlist->variable_item_list);
  149. }