Merge pull request #5792 from ByteHamster/local-update-progress

Updating local files takes long. Show progress.
This commit is contained in:
ByteHamster 2022-03-19 20:56:40 +01:00 committed by GitHub
commit a7615405bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 25 deletions

View File

@ -7,6 +7,7 @@ import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.documentfile.provider.DocumentFile;
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" };
public static void updateFeed(Feed feed, Context context) {
public static void updateFeed(Feed feed, Context context,
@Nullable UpdaterProgressListener updaterProgressListener) {
try {
tryUpdateFeed(feed, context);
tryUpdateFeed(feed, context, updaterProgressListener);
if (mustReportDownloadSuccessful(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, "");
DocumentFile documentFolder = DocumentFile.fromTreeUri(context, Uri.parse(uriString));
if (documentFolder == null) {
@ -92,14 +95,17 @@ public class LocalFeedUpdater {
// add new files to feed and update item data
List<FeedItem> newItems = feed.getItems();
for (DocumentFile f : mediaFiles) {
FeedItem oldItem = feedContainsFile(feed, f.getName());
FeedItem newItem = createFeedItem(feed, f, context);
for (int i = 0; i < mediaFiles.size(); i++) {
FeedItem oldItem = feedContainsFile(feed, mediaFiles.get(i).getName());
FeedItem newItem = createFeedItem(feed, mediaFiles.get(i), context);
if (oldItem == null) {
newItems.add(newItem);
} else {
oldItem.updateFromOther(newItem);
}
if (updaterProgressListener != null) {
updaterProgressListener.onLocalFileScanned(i, mediaFiles.size());
}
}
// remove feed items without corresponding file
@ -260,4 +266,9 @@ public class LocalFeedUpdater {
// (avoid logging success again if the last update was ok)
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) {
try {
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) {
e.printStackTrace();
}

View File

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

View File

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