Skip to content

Commit

Permalink
home: support importing .utm by drag and drop
Browse files Browse the repository at this point in the history
Fixes #3599
  • Loading branch information
osy committed Mar 6, 2023
1 parent 4120298 commit dd73b0c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Platform/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

import SwiftUI
import UniformTypeIdentifiers
#if !os(macOS)
import IQKeyboardManagerSwift
#endif
Expand Down Expand Up @@ -51,6 +52,7 @@ struct ContentView: View {
openSheetPresented = true
}
}.fileImporter(isPresented: $openSheetPresented, allowedContentTypes: [.UTM, .UTMextension], allowsMultipleSelection: true, onCompletion: selectImportedUTM)
.onDrop(of: [.fileURL], delegate: self)
.onAppear {
Task {
await data.listRefresh()
Expand Down Expand Up @@ -177,6 +179,47 @@ struct ContentView: View {
}
}

extension ContentView: DropDelegate {
func validateDrop(info: DropInfo) -> Bool {
!urlsFrom(info: info).isEmpty
}

func performDrop(info: DropInfo) -> Bool {
data.busyWorkAsync {
for url in await urlsFrom(info: info) {

try await data.importUTM(from: url)
}
}
return true
}

private func urlsFrom(info: DropInfo) -> [URL] {
let providers = info.itemProviders(for: [.fileURL])

var validURLs: [URL] = []

let group = DispatchGroup()

providers.forEach { provider in
group.enter()
_ = provider.loadDataRepresentation(forTypeIdentifier: UTType.fileURL.identifier) { (data, error) in
if let data = data {
let url = URL(dataRepresentation: data, relativeTo: nil)
if url?.pathExtension == "utm" {
validURLs.append(url!)
}
}
group.leave()
}
}

group.wait()

return validURLs
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
Expand Down

0 comments on commit dd73b0c

Please sign in to comment.