Skip to content

Commit

Permalink
Revert back to old document picker implementation.
Browse files Browse the repository at this point in the history
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
ginsudev committed Jan 3, 2023
1 parent 542e58c commit 875ad86
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 51 deletions.
8 changes: 6 additions & 2 deletions WDBFontOverwrite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -28,6 +29,7 @@
/* Begin PBXFileReference section */
4F4E64A6295F9AB600D4F04D /* ContentView.ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.ViewModel.swift; sourceTree = "<group>"; };
4FE5EF302963E460003384EC /* NoticeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeView.swift; sourceTree = "<group>"; };
4FE5EF3229640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WDBImportCustomFontPickerViewControllerDelegate.swift; sourceTree = "<group>"; };
C55CF76E295BA346000DE71C /* libwoff2enc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libwoff2enc.a; sourceTree = "<group>"; };
C55CF76F295BA346000DE71C /* libwoff2common.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libwoff2common.a; sourceTree = "<group>"; };
C55CF772295BA37D000DE71C /* woff2_wrapper.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = woff2_wrapper.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 0 additions & 19 deletions WDBFontOverwrite/ContentView.ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
40 changes: 10 additions & 30 deletions WDBFontOverwrite/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
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>
) {}
}

0 comments on commit 875ad86

Please sign in to comment.