Skip to content

Commit

Permalink
Merge pull request #1134 from TortugaPower/fix-import-picker
Browse files Browse the repository at this point in the history
Fix crash when importing files after some delay
  • Loading branch information
GianniCarlo authored Apr 26, 2024
2 parents 54ef0f0 + 2db4aa8 commit 8b307f7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
11 changes: 10 additions & 1 deletion BookPlayer/Coordinators/LibraryListCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,17 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat
}

func processFiles(urls: [URL]) {
let temporaryDirectoryPath = FileManager.default.temporaryDirectory.absoluteString
let documentsFolder = DataManager.getDocumentsFolderURL()

for url in urls {
self.importManager.process(url)
/// At some point (iOS 17?), the OS stopped sending the picked files to the Documents/Inbox folder, instead
/// it's now sent to a temp folder that can't be relied on to keep the file existing until the import is finished
if url.absoluteString.contains(temporaryDirectoryPath) {
try! FileManager.default.moveItem(at: url, to: documentsFolder)
} else {
importManager.process(url)
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion BookPlayer/Library/ItemList Screen/ItemListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,20 @@ class ItemListViewModel: ViewModelProtocol {
// MARK: - Import related functions
extension ItemListViewModel {
func handleNewFiles(_ urls: [URL]) {
let temporaryDirectoryPath = FileManager.default.temporaryDirectory.absoluteString
let documentsFolder = DataManager.getDocumentsFolderURL()

for url in urls {
importManager.process(url)
/// At some point (iOS 17?), the OS stopped sending the picked files to the Documents/Inbox folder, instead
/// it's now sent to a temp folder that can't be relied on to keep the file existing until the import is finished
if url.absoluteString.contains(temporaryDirectoryPath) {
let destinationURL = documentsFolder.appendingPathComponent(url.lastPathComponent)
if !FileManager.default.fileExists(atPath: destinationURL.path) {
try! FileManager.default.copyItem(at: url, to: destinationURL)
}
} else {
importManager.process(url)
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions BookPlayer/PrivacyInfo.xcprivacy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array>
<dict>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>3B52.1</string>
</array>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryFileTimestamp</string>
</dict>
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryDiskSpace</string>
Expand Down

0 comments on commit 8b307f7

Please sign in to comment.