From 875ad869ab46306d8540488c22c3ff60c77749a1 Mon Sep 17 00:00:00 2001 From: ginsudev Date: Tue, 3 Jan 2023 18:23:25 +1100 Subject: [PATCH] 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. --- WDBFontOverwrite.xcodeproj/project.pbxproj | 8 +- WDBFontOverwrite/ContentView.ViewModel.swift | 19 ----- WDBFontOverwrite/ContentView.swift | 40 +++------- ...stomFontPickerViewControllerDelegate.swift | 80 +++++++++++++++++++ 4 files changed, 96 insertions(+), 51 deletions(-) create mode 100644 WDBFontOverwrite/WDBImportCustomFontPickerViewControllerDelegate.swift diff --git a/WDBFontOverwrite.xcodeproj/project.pbxproj b/WDBFontOverwrite.xcodeproj/project.pbxproj index 8d20a8f..0468fa7 100644 --- a/WDBFontOverwrite.xcodeproj/project.pbxproj +++ b/WDBFontOverwrite.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 4F4E64A7295F9AB600D4F04D /* ContentView.ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F4E64A6295F9AB600D4F04D /* ContentView.ViewModel.swift */; }; 4FE5EF312963E460003384EC /* NoticeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE5EF302963E460003384EC /* NoticeView.swift */; }; + 4FE5EF3329640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE5EF3229640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift */; }; C55CF774295BA37D000DE71C /* woff2_wrapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C55CF772295BA37D000DE71C /* woff2_wrapper.cpp */; }; C55CF776295BA9B1000DE71C /* BrotliPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = C55CF775295BA9B1000DE71C /* BrotliPadding.swift */; }; C55CF778295BAF42000DE71C /* libwoff2enc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C55CF76E295BA346000DE71C /* libwoff2enc.a */; }; @@ -28,6 +29,7 @@ /* Begin PBXFileReference section */ 4F4E64A6295F9AB600D4F04D /* ContentView.ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.ViewModel.swift; sourceTree = ""; }; 4FE5EF302963E460003384EC /* NoticeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeView.swift; sourceTree = ""; }; + 4FE5EF3229640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WDBImportCustomFontPickerViewControllerDelegate.swift; sourceTree = ""; }; C55CF76E295BA346000DE71C /* libwoff2enc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libwoff2enc.a; sourceTree = ""; }; C55CF76F295BA346000DE71C /* libwoff2common.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libwoff2common.a; sourceTree = ""; }; C55CF772295BA37D000DE71C /* woff2_wrapper.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = woff2_wrapper.cpp; sourceTree = ""; }; @@ -100,6 +102,7 @@ C5C9A7A02959263A00466D87 /* OverwriteFontImpl.swift */, C55CF775295BA9B1000DE71C /* BrotliPadding.swift */, 4FE5EF302963E460003384EC /* NoticeView.swift */, + 4FE5EF3229640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift */, C5C9A7962959261200466D87 /* Assets.xcassets */, C5C9A7A92959417100466D87 /* vm_unaligned_copy_switch_race.c */, C5C9A7AB2959438600466D87 /* vm_unaligned_copy_switch_race.h */, @@ -205,6 +208,7 @@ C5C9A7AA2959417100466D87 /* vm_unaligned_copy_switch_race.c in Sources */, C5999BFC295E69C200BBBE1F /* ttcpad.m in Sources */, 4FE5EF312963E460003384EC /* NoticeView.swift in Sources */, + 4FE5EF3329640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift in Sources */, C5C9A7A12959263A00466D87 /* OverwriteFontImpl.swift in Sources */, C55CF774295BA37D000DE71C /* woff2_wrapper.cpp in Sources */, 4F4E64A7295F9AB600D4F04D /* ContentView.ViewModel.swift in Sources */, @@ -355,7 +359,7 @@ "$(inherited)", "$(PROJECT_DIR)/WDBFontOverwrite", ); - MARKETING_VERSION = 1.9.0; + MARKETING_VERSION = 1.9.1; PRODUCT_BUNDLE_IDENTIFIER = com.ginsudev.WDBFontOverwrite; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; @@ -393,7 +397,7 @@ "$(inherited)", "$(PROJECT_DIR)/WDBFontOverwrite", ); - MARKETING_VERSION = 1.9.0; + MARKETING_VERSION = 1.9.1; PRODUCT_BUNDLE_IDENTIFIER = com.ginsudev.WDBFontOverwrite; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; diff --git a/WDBFontOverwrite/ContentView.ViewModel.swift b/WDBFontOverwrite/ContentView.ViewModel.swift index 3cdc370..f9a80ed 100644 --- a/WDBFontOverwrite/ContentView.ViewModel.swift +++ b/WDBFontOverwrite/ContentView.ViewModel.swift @@ -143,24 +143,5 @@ extension ContentView { notice: .keyboard ) ] - - func importSelectedFile(fromURL url: URL) { - url.startAccessingSecurityScopedResource() - - let documentDirectory = FileManager.default.urls( - for: .documentDirectory, - in: .userDomainMask - )[0] - - let targetURL = documentDirectory.appendingPathComponent(selectedCustomFont.localPath) - - let importResult = importCustomFontImpl( - fileURL: url, - targetURL: targetURL, - ttcRepackMode: importTTCRepackMode - ) - - message = importResult ?? "Successfully imported font" - } } } diff --git a/WDBFontOverwrite/ContentView.swift b/WDBFontOverwrite/ContentView.swift index 959e833..2cbd1b2 100644 --- a/WDBFontOverwrite/ContentView.swift +++ b/WDBFontOverwrite/ContentView.swift @@ -27,23 +27,13 @@ struct ContentView: View { .navigationTitle("WDBFontOverwrite") } .navigationViewStyle(.stack) - .fileImporter( - isPresented: $viewModel.importPresented, - allowedContentTypes: [ - UTType.font, - UTType( - filenameExtension: "woff2", - conformingTo: .font - )! - ]) { result in - switch result { - case .success(let url): - viewModel.importSelectedFile(fromURL: url) - case .failure(let failure): - viewModel.message = "Failed to import" - print(failure.localizedDescription) + .sheet(isPresented: $viewModel.importPresented) { + DocumentPicker( + name: viewModel.importName, + ttcRepackMode: viewModel.importTTCRepackMode) { + viewModel.message = $0 } - } + } } private var segmentControl: some View { @@ -113,17 +103,19 @@ struct ContentView: View { Section { Button { + viewModel.message = "Importing..." viewModel.importName = viewModel.selectedCustomFont.localPath viewModel.importTTCRepackMode = .woff2 - presentPicker() + viewModel.importPresented = true } label: { Text("Import custom \(viewModel.selectedCustomFont.name)") } if let alternativeTTCRepackMode = viewModel.selectedCustomFont.alternativeTTCRepackMode { Button { + viewModel.message = "Importing..." viewModel.importName = viewModel.selectedCustomFont.localPath viewModel.importTTCRepackMode = alternativeTTCRepackMode - presentPicker() + viewModel.importPresented = true } label: { Text("Import custom \(viewModel.selectedCustomFont.name) with fix for .ttc") } @@ -168,18 +160,6 @@ struct ContentView: View { Text("Originally created by [@zhuowei](https://twitter.com/zhuowei). Updated & maintained by [@GinsuDev](https://twitter.com/GinsuDev).") } } - - private func presentPicker() { - if viewModel.importPresented { - // Fixes broken fileimporter sheet not resetting binding bool on swipe down - viewModel.importPresented = false - DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: { - viewModel.importPresented = true - }) - } else { - viewModel.importPresented = true - } - } } struct ContentView_Previews: PreviewProvider { diff --git a/WDBFontOverwrite/WDBImportCustomFontPickerViewControllerDelegate.swift b/WDBFontOverwrite/WDBImportCustomFontPickerViewControllerDelegate.swift new file mode 100644 index 0000000..1c62354 --- /dev/null +++ b/WDBFontOverwrite/WDBImportCustomFontPickerViewControllerDelegate.swift @@ -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) -> 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 + ) {} +}