Преглед изворни кода

Improvements

- Changes how add nfc and view content works
- Tweaks remove nfc to make sure things run properly and also use a different function
acegoal07 пре 1 година
родитељ
комит
06626d8519

+ 0 - 1
nfc_playlist.h

@@ -74,7 +74,6 @@ static const bool default_skip_error = false;
 
 
 #define PLAYLIST_LOCATION "/ext/apps_data/nfc_playlist/"
 #define PLAYLIST_LOCATION "/ext/apps_data/nfc_playlist/"
 #define PLAYLIST_DIR "/ext/apps_data/nfc_playlist"
 #define PLAYLIST_DIR "/ext/apps_data/nfc_playlist"
-#define PLAYLIST_VIEW_MAX_SIZE 1000
 
 
 typedef enum NfcPlaylistLedState {
 typedef enum NfcPlaylistLedState {
    NfcPlaylistLedState_Normal,
    NfcPlaylistLedState_Normal,

+ 3 - 5
scenes/nfc_playlist_scene_nfc_remove.c

@@ -76,13 +76,11 @@ bool nfc_playlist_nfc_remove_scene_on_event(void* context, SceneManagerEvent eve
                   if (current_line != selected_line) {
                   if (current_line != selected_line) {
                      furi_string_replace_all(line, "\n", "");
                      furi_string_replace_all(line, "\n", "");
                      if (furi_string_empty(tmp_str)) {
                      if (furi_string_empty(tmp_str)) {
-                        furi_string_printf(tmp_str, "%s", furi_string_get_cstr(line));
+                        furi_string_cat_printf(tmp_str, "%s", furi_string_get_cstr(line));
                      } else {
                      } else {
-                        furi_string_cat(tmp_str, "\n");
-                        furi_string_cat(tmp_str, furi_string_get_cstr(line));
+                        furi_string_cat_printf(tmp_str, "\n%s", furi_string_get_cstr(line));
                      }
                      }
                   }
                   }
-                  furi_string_reset(line);
                }
                }
 
 
                stream_clean(stream);
                stream_clean(stream);
@@ -90,10 +88,10 @@ bool nfc_playlist_nfc_remove_scene_on_event(void* context, SceneManagerEvent eve
                stream_write_string(stream, tmp_str);
                stream_write_string(stream, tmp_str);
                furi_string_free(tmp_str);
                furi_string_free(tmp_str);
                file_stream_close(stream);
                file_stream_close(stream);
-               stream_free(stream);
                nfc_playlist->settings.playlist_length--;
                nfc_playlist->settings.playlist_length--;
                selected_line = nfc_playlist->settings.playlist_length;
                selected_line = nfc_playlist->settings.playlist_length;
             }
             }
+            stream_free(stream);
             furi_record_close(RECORD_STORAGE);
             furi_record_close(RECORD_STORAGE);
 
 
             if (selected_line == 0) {
             if (selected_line == 0) {

+ 15 - 17
scenes/nfc_playlist_scene_nfc_select.c

@@ -4,32 +4,30 @@ void nfc_playlist_nfc_select_menu_callback(void* context) {
    NfcPlaylist* nfc_playlist = context;
    NfcPlaylist* nfc_playlist = context;
 
 
    Storage* storage = furi_record_open(RECORD_STORAGE);
    Storage* storage = furi_record_open(RECORD_STORAGE);
-   File* file = storage_file_alloc(storage);
+   Stream* stream = file_stream_alloc(storage);
 
 
-   if (storage_file_open(file, furi_string_get_cstr(nfc_playlist->settings.file_path), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
-      uint8_t buffer[PLAYLIST_VIEW_MAX_SIZE];
-      uint16_t read_count = storage_file_read(file, buffer, PLAYLIST_VIEW_MAX_SIZE);
-      FuriString* playlist_content = furi_string_alloc();
-
-      for(uint16_t i = 0; i < read_count; i++) {
-         furi_string_push_back(playlist_content, buffer[i]);
+   if (file_stream_open(stream, furi_string_get_cstr(nfc_playlist->settings.file_path), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
+      FuriString* line = furi_string_alloc();
+      FuriString* tmp_str = furi_string_alloc();
+      while(stream_read_line(stream, line)) {
+         furi_string_cat_printf(tmp_str, "%s", furi_string_get_cstr(line));
       }
       }
 
 
-      if (read_count > 0) {
-         furi_string_printf(playlist_content, "\n%s", furi_string_get_cstr(nfc_playlist->file_browser_output));
+      if (!furi_string_empty(tmp_str)) {
+         furi_string_cat_printf(tmp_str, "\n%s", furi_string_get_cstr(nfc_playlist->file_browser_output));
       } else {
       } else {
-         furi_string_printf(playlist_content, "%s", furi_string_get_cstr(nfc_playlist->file_browser_output));
+         furi_string_printf(tmp_str, "%s", furi_string_get_cstr(nfc_playlist->file_browser_output));
       }
       }
 
 
-      storage_file_write(file, furi_string_get_cstr(playlist_content), sizeof(char) * furi_string_utf8_length(playlist_content));
-
+      stream_clean(stream);
+      furi_string_free(line);
+      stream_write_string(stream, tmp_str);
+      file_stream_close(stream);
+      furi_string_free(tmp_str);
       nfc_playlist->settings.playlist_length++;
       nfc_playlist->settings.playlist_length++;
-
-      furi_string_free(playlist_content);
-      storage_file_close(file);
    }
    }
 
 
-   storage_file_free(file);
+   stream_free(stream);
    furi_record_close(RECORD_STORAGE);
    furi_record_close(RECORD_STORAGE);
    furi_string_reset(nfc_playlist->file_browser_output);
    furi_string_reset(nfc_playlist->file_browser_output);
 
 

+ 13 - 11
scenes/nfc_playlist_scene_view_playlist_content.c

@@ -4,27 +4,29 @@ void nfc_playlist_view_playlist_content_scene_on_enter(void* context) {
    NfcPlaylist* nfc_playlist = context;
    NfcPlaylist* nfc_playlist = context;
 
 
    Storage* storage = furi_record_open(RECORD_STORAGE);
    Storage* storage = furi_record_open(RECORD_STORAGE);
-   File* file = storage_file_alloc(storage);
+   Stream* stream = file_stream_alloc(storage);
 
 
-   if (storage_file_open(file, furi_string_get_cstr(nfc_playlist->settings.file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
-      uint8_t buffer[PLAYLIST_VIEW_MAX_SIZE];
-      uint16_t read_count = storage_file_read(file, buffer, PLAYLIST_VIEW_MAX_SIZE);
-      FuriString* playlist_content = furi_string_alloc();
+   if (file_stream_open(stream, furi_string_get_cstr(nfc_playlist->settings.file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
+      FuriString* line = furi_string_alloc();
+      FuriString* tmp_str = furi_string_alloc();
 
 
-      for(uint16_t i = 0; i < read_count; i++) {
-         furi_string_push_back(playlist_content, buffer[i]);
+      while(stream_read_line(stream, line)) {
+         furi_string_cat_printf(tmp_str, "%s", furi_string_get_cstr(line));
       }
       }
 
 
-      widget_add_text_scroll_element(nfc_playlist->widget, 4, 4, 124, 60, furi_string_get_cstr(playlist_content));
+      stream_clean(stream);
+      furi_string_free(line);
+      file_stream_close(stream);
+
+      widget_add_text_scroll_element(nfc_playlist->widget, 4, 4, 124, 60, furi_string_get_cstr(tmp_str));
       widget_add_frame_element(nfc_playlist->widget, 0, 0, 128, 64, 0);
       widget_add_frame_element(nfc_playlist->widget, 0, 0, 128, 64, 0);
 
 
-      furi_string_free(playlist_content);
-      storage_file_close(file);
+      furi_string_free(tmp_str);
    } else {
    } else {
       widget_add_text_box_element(nfc_playlist->widget, 0, 0, 128, 64, AlignCenter, AlignCenter, "\eFailed to open playlist\n\nPress back\e", false);
       widget_add_text_box_element(nfc_playlist->widget, 0, 0, 128, 64, AlignCenter, AlignCenter, "\eFailed to open playlist\n\nPress back\e", false);
    }
    }
 
 
-   storage_file_free(file);
+   stream_free(stream);
    furi_record_close(RECORD_STORAGE);
    furi_record_close(RECORD_STORAGE);
 
 
    view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Widget);
    view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Widget);