Skip to content

Commit

Permalink
Fix rendering download status for items
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Dec 12, 2024
1 parent 613071d commit ac0867f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Shared/Services/LibraryService+Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public protocol LibrarySyncProtocol {

/// Fetch all items and folders inside a given folder (Used for newly imported folders)
func getAllNestedItems(inside relativePath: String) -> [SyncableItem]?
/// Get max items count inside the specified path
func getMaxItemsCount(at relativePath: String?) -> Int

/// Get all stored bookmarks of the specified type for a book
func getBookmarks(of type: BookmarkType, relativePath: String) -> [SimpleBookmark]?
Expand Down
34 changes: 18 additions & 16 deletions Shared/Services/Sync/SyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,11 @@ public final class SyncService: SyncServiceProtocol, BPLogger {
public func downloadRemoteFiles(for item: SimpleLibraryItem) async throws {
let remoteURLs = try await getRemoteFileURLs(of: item.relativePath, type: item.type)

let processedFolderURL = DataManager.getProcessedFolderURL()
let folderURLs = remoteURLs.filter({ $0.type != .book })

/// Handle throwable items first
if !folderURLs.isEmpty {
let processedFolderURL = DataManager.getProcessedFolderURL()

for remoteURL in folderURLs {
let fileURL = processedFolderURL.appendingPathComponent(remoteURL.relativePath)
try DataManager.createBackingFolderIfNeeded(fileURL)
Expand All @@ -410,6 +409,10 @@ public final class SyncService: SyncServiceProtocol, BPLogger {
var tasks = [URLSessionTask]()

for remoteURL in bookURLs {
let localURL = processedFolderURL.appendingPathComponent(remoteURL.relativePath)

guard !FileManager.default.fileExists(atPath: localURL.path) else { continue }

let task = await provider.client.download(
url: remoteURL.url,
taskDescription: remoteURL.relativePath,
Expand Down Expand Up @@ -712,21 +715,20 @@ extension SyncService {

let fileURL = item.fileURL

if item.type == .bound || item.type == .folder,
let enumerator = FileManager.default.enumerator(
at: fileURL,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants]
),
enumerator.nextObject() == nil
{
return .notDownloaded
}
switch item.type {
case .book:
return FileManager.default.fileExists(atPath: fileURL.path) ? .downloaded : .notDownloaded
case .folder, .bound:
guard
let enumerator = FileManager.default.enumerator(
at: fileURL,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants]
)
else { return .notDownloaded }

if FileManager.default.fileExists(atPath: fileURL.path) {
return .downloaded
return libraryService.getMaxItemsCount(at: item.relativePath) == enumerator.allObjects.count
? .downloaded : .notDownloaded
}

return .notDownloaded
}
}

0 comments on commit ac0867f

Please sign in to comment.