-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert back to old document picker implementation.
Reverted back to old document picker implementation, but fixed a bug where the picker would be deinited while using and use modern concurrency techniques.
- Loading branch information
Showing
4 changed files
with
96 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
WDBFontOverwrite/WDBImportCustomFontPickerViewControllerDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import SwiftUI | ||
import UniformTypeIdentifiers | ||
|
||
class WDBImportCustomFontPickerViewControllerDelegate: NSObject, UIDocumentPickerDelegate { | ||
let name: String | ||
let ttcRepackMode: TTCRepackMode | ||
let completion: (String) -> Void | ||
|
||
init(name: String, ttcRepackMode: TTCRepackMode, completion: @escaping (String) -> Void) { | ||
self.name = name | ||
self.ttcRepackMode = ttcRepackMode | ||
self.completion = completion | ||
} | ||
|
||
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { | ||
guard urls.count == 1 else { | ||
completion("import one file at a time") | ||
return | ||
} | ||
|
||
Task(priority: .background) { | ||
let fileURL = urls[0] | ||
let documentDirectory = FileManager.default.urls( | ||
for: .documentDirectory, | ||
in: .userDomainMask | ||
)[0] | ||
let targetURL = documentDirectory.appendingPathComponent(self.name) | ||
let success = importCustomFontImpl( | ||
fileURL: fileURL, | ||
targetURL: targetURL, | ||
ttcRepackMode: self.ttcRepackMode | ||
) | ||
await MainActor.run { [weak self] in | ||
self?.completion(success ?? "Imported") | ||
} | ||
} | ||
} | ||
|
||
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { | ||
completion("Cancelled") | ||
} | ||
} | ||
|
||
// https://capps.tech/blog/read-files-with-documentpicker-in-swiftui | ||
struct DocumentPicker: UIViewControllerRepresentable { | ||
var name: String | ||
var ttcRepackMode: TTCRepackMode | ||
var completion: (String) -> Void | ||
|
||
func makeCoordinator() -> WDBImportCustomFontPickerViewControllerDelegate { | ||
return WDBImportCustomFontPickerViewControllerDelegate( | ||
name: name, | ||
ttcRepackMode: ttcRepackMode, | ||
completion: completion | ||
) | ||
} | ||
|
||
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) -> UIDocumentPickerViewController { | ||
print("make ui view controller?") | ||
|
||
let pickerViewController = UIDocumentPickerViewController( | ||
forOpeningContentTypes: [ | ||
UTType.font, | ||
UTType( | ||
filenameExtension: "woff2", | ||
conformingTo: .font | ||
)!, | ||
], | ||
asCopy: true | ||
) | ||
|
||
pickerViewController.delegate = context.coordinator | ||
return pickerViewController | ||
} | ||
|
||
func updateUIViewController( | ||
_ uiViewController: UIDocumentPickerViewController, | ||
context: UIViewControllerRepresentableContext<DocumentPicker> | ||
) {} | ||
} |