From 33f7d4963901450049a1cd39f54492b2ca7815cb Mon Sep 17 00:00:00 2001 From: Mannith Narayan Date: Sun, 29 Oct 2023 06:57:12 +1100 Subject: [PATCH] Feature works. Have to add comments --- .../local/bookmark/BookmarkFragment.java | 6 +- .../local/bookmark/BookmarkImportService.java | 57 +++++++++---------- .../local/playlist/LocalPlaylistManager.java | 1 + 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java index 46a90a8d6..630ed4650 100644 --- a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java @@ -127,11 +127,9 @@ public final class BookmarkFragment extends BaseLocalListFragment streams = new ArrayList<>(); + List streams; public BookmarkImportService(final Uri textFileUri, - final RemotePlaylistManager remotePlaylistManager, - final LocalPlaylistManager localPlaylistManager, - final CompositeDisposable compositeDisposable) { + final LocalPlaylistManager localPlaylistManager) { this.textFileUri = textFileUri; - this.remotePlaylistManager = remotePlaylistManager; this.localPlaylistManager = localPlaylistManager; - this.disposable = compositeDisposable; } public void importBookmarks(final Activity activity) { readTextFile(activity); } public void readTextFile(final Activity activity) { - int count = 0; if (textFileUri != null) { try { final InputStream inputStream = @@ -62,19 +55,14 @@ public class BookmarkImportService { final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; - + streams = new ArrayList<>(); while ((line = reader.readLine()) != null) { + readerLineCount++; handleUrl(activity, line); - if (count == 0) { - //cannot create an empty playlist - createNewPlayListWithOneEntry(); - count++; - } else { - addEntries(); - } } reader.close(); inputStream.close(); + } else { Toast.makeText(activity, "File is empty", LENGTH_SHORT).show(); } @@ -83,13 +71,7 @@ public class BookmarkImportService { } } } - public void createNewPlayListWithOneEntry() { - System.out.println("LOL"); - } - public void addEntries() { - System.out.println("LOL"); - } - public String handleUrl(final Activity activity, final String url) { + public boolean handleUrl(final Activity activity, final String url) { final StreamingService service; final StreamingService.LinkType linkType; try { @@ -101,7 +83,7 @@ public class BookmarkImportService { try { cleanUrl = factory.getUrl(factory.getId(url)); } catch (final ParsingException e) { - return "parsingException"; + return false; } final Single single = ExtractorHelper.getStreamInfo(service.getServiceId(), cleanUrl, false); @@ -112,17 +94,30 @@ public class BookmarkImportService { // Blocking network call final StreamInfo streamInfo = single.blockingGet(); final StreamEntity streamEntity = new StreamEntity(streamInfo); - // Update UI + addedByBackgroundThreadCount++; + // Update the streams list. activity.runOnUiThread(() -> { streams.add(streamEntity); + + if (addedByBackgroundThreadCount == readerLineCount) { + //All background threads done. + //Add playlist + final Maybe> playlistIds = + localPlaylistManager.createPlaylist("Sample", streams); + playlistIds.subscribe(list -> { + //this is to make the fragment fetch data from the database + //I could not find another way to do this. + }); + Toast.makeText(activity, "Playlist added", LENGTH_SHORT).show(); + } }); }); ((ExecutorService) executor).shutdown(); } } } catch (final ExtractionException e) { - return "false1"; + return false; } - return "false2"; + return false; } } diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java index 27c148561..35058d471 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java @@ -30,6 +30,7 @@ public class LocalPlaylistManager { private final PlaylistDAO playlistTable; private final PlaylistStreamDAO playlistStreamTable; + public LocalPlaylistManager(final AppDatabase db) { database = db; streamTable = db.streamDAO();