Updating local files takes long. Show progress.

This commit is contained in:
ByteHamster 2022-03-16 23:51:51 +01:00
parent 3b47deb705
commit 2df8e655ac
4 changed files with 34 additions and 25 deletions

View File

@ -7,6 +7,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
import java.io.IOException; import java.io.IOException;
@ -47,9 +48,10 @@ public class LocalFeedUpdater {
static final String[] PREFERRED_FEED_IMAGE_FILENAMES = { "folder.jpg", "Folder.jpg", "folder.png", "Folder.png" }; static final String[] PREFERRED_FEED_IMAGE_FILENAMES = { "folder.jpg", "Folder.jpg", "folder.png", "Folder.png" };
public static void updateFeed(Feed feed, Context context) { public static void updateFeed(Feed feed, Context context,
@Nullable UpdaterProgressListener updaterProgressListener) {
try { try {
tryUpdateFeed(feed, context); tryUpdateFeed(feed, context, updaterProgressListener);
if (mustReportDownloadSuccessful(feed)) { if (mustReportDownloadSuccessful(feed)) {
reportSuccess(feed); reportSuccess(feed);
@ -60,7 +62,8 @@ public class LocalFeedUpdater {
} }
} }
private static void tryUpdateFeed(Feed feed, Context context) throws IOException { private static void tryUpdateFeed(Feed feed, Context context, UpdaterProgressListener updaterProgressListener)
throws IOException {
String uriString = feed.getDownload_url().replace(Feed.PREFIX_LOCAL_FOLDER, ""); String uriString = feed.getDownload_url().replace(Feed.PREFIX_LOCAL_FOLDER, "");
DocumentFile documentFolder = DocumentFile.fromTreeUri(context, Uri.parse(uriString)); DocumentFile documentFolder = DocumentFile.fromTreeUri(context, Uri.parse(uriString));
if (documentFolder == null) { if (documentFolder == null) {
@ -92,14 +95,17 @@ public class LocalFeedUpdater {
// add new files to feed and update item data // add new files to feed and update item data
List<FeedItem> newItems = feed.getItems(); List<FeedItem> newItems = feed.getItems();
for (DocumentFile f : mediaFiles) { for (int i = 0; i < mediaFiles.size(); i++) {
FeedItem oldItem = feedContainsFile(feed, f.getName()); FeedItem oldItem = feedContainsFile(feed, mediaFiles.get(i).getName());
FeedItem newItem = createFeedItem(feed, f, context); FeedItem newItem = createFeedItem(feed, mediaFiles.get(i), context);
if (oldItem == null) { if (oldItem == null) {
newItems.add(newItem); newItems.add(newItem);
} else { } else {
oldItem.updateFromOther(newItem); oldItem.updateFromOther(newItem);
} }
if (updaterProgressListener != null) {
updaterProgressListener.onLocalFileScanned(i, mediaFiles.size());
}
} }
// remove feed items without corresponding file // remove feed items without corresponding file
@ -260,4 +266,9 @@ public class LocalFeedUpdater {
// (avoid logging success again if the last update was ok) // (avoid logging success again if the last update was ok)
return !lastDownloadStatus.isSuccessful(); return !lastDownloadStatus.isSuccessful();
} }
@FunctionalInterface
public interface UpdaterProgressListener {
void onLocalFileScanned(int scanned, int totalFiles);
}
} }

View File

@ -301,7 +301,11 @@ public class DownloadService extends Service {
private void performLocalFeedRefresh(Downloader downloader, DownloadRequest request) { private void performLocalFeedRefresh(Downloader downloader, DownloadRequest request) {
try { try {
Feed feed = DBReader.getFeed(request.getFeedfileId()); Feed feed = DBReader.getFeed(request.getFeedfileId());
LocalFeedUpdater.updateFeed(feed, DownloadService.this); LocalFeedUpdater.updateFeed(feed, DownloadService.this, (scanned, totalFiles) -> {
request.setSize(totalFiles);
request.setSoFar(scanned);
request.setProgressPercent((int) (100.0 * scanned / totalFiles));
});
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -73,24 +73,18 @@ public class DownloadServiceNotification {
} }
stringBuilder.append(""); stringBuilder.append("");
DownloadRequest request = downloader.getDownloadRequest(); DownloadRequest request = downloader.getDownloadRequest();
switch (request.getFeedfileType()) {
case Feed.FEEDFILETYPE_FEED:
if (request.getTitle() != null) { if (request.getTitle() != null) {
stringBuilder.append(request.getTitle()); stringBuilder.append(request.getTitle());
} else {
stringBuilder.append(request.getSource());
} }
break; if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
case FeedMedia.FEEDFILETYPE_FEEDMEDIA: stringBuilder.append(" (").append(request.getProgressPercent()).append("%)");
if (request.getTitle() != null) { } else if (request.getSource().startsWith(Feed.PREFIX_LOCAL_FOLDER)) {
stringBuilder.append(request.getTitle()) stringBuilder.append(" (").append(request.getSoFar())
.append(" (") .append("/").append(request.getSize()).append(")");
.append(request.getProgressPercent())
.append("%)");
} }
break; if (i != downloads.size() - 1) {
default:
stringBuilder.append("Unknown: ").append(request.getFeedfileType());
}
if (i != downloads.size()) {
stringBuilder.append("\n"); stringBuilder.append("\n");
} }
} }

View File

@ -276,7 +276,7 @@ public class LocalFeedUpdaterTest {
// call method to test // call method to test
Feed feed = new Feed(FEED_URL, null); Feed feed = new Feed(FEED_URL, null);
LocalFeedUpdater.updateFeed(feed, context); LocalFeedUpdater.updateFeed(feed, context, null);
} }
} }