[ios] add DocumentPicker and FileType

Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
This commit is contained in:
Kiryl Kaveryn 2024-06-06 19:55:03 +04:00 committed by Alexander Borsuk
parent ba7d55aa97
commit 577c6cc37f
4 changed files with 67 additions and 10 deletions

View file

@ -13,16 +13,6 @@ enum WritingResult {
typealias VoidResultCompletionHandler = (VoidResult) -> Void
typealias WritingResultCompletionHandler = (WritingResult) -> Void
// TODO: Remove this type and use custom UTTypeIdentifier that is registered into the Info.plist after updating to the iOS >= 14.0.
struct FileType {
let fileExtension: String
let typeIdentifier: String
}
extension FileType {
static let kml = FileType(fileExtension: "kml", typeIdentifier: "com.google.earth.kml")
}
let kTrashDirectoryName = ".Trash"
private let kBookmarksDirectoryName = "bookmarks"
private let kICloudSynchronizationDidChangeEnabledStateNotificationName = "iCloudSynchronizationDidChangeEnabledStateNotification"

View file

@ -471,6 +471,7 @@
ED1263AB2B6F99F900AD99F3 /* UIView+AddSeparator.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED1263AA2B6F99F900AD99F3 /* UIView+AddSeparator.swift */; };
ED1ADA332BC6B1B40029209F /* CarPlayServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED1ADA322BC6B1B40029209F /* CarPlayServiceTests.swift */; };
ED3EAC202B03C88100220A4A /* BottomTabBarButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED3EAC1F2B03C88100220A4A /* BottomTabBarButton.swift */; };
ED43B8BD2C12063500D07BAA /* DocumentPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED43B8BC2C12063500D07BAA /* DocumentPicker.swift */; };
ED63CEB92BDF8F9D006155C4 /* SettingsTableViewiCloudSwitchCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED63CEB62BDF8F9C006155C4 /* SettingsTableViewiCloudSwitchCell.swift */; };
ED79A5AB2BD7AA9C00952D1F /* LoadingOverlayViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED79A5AA2BD7AA9C00952D1F /* LoadingOverlayViewController.swift */; };
ED79A5AD2BD7BA0F00952D1F /* UIApplication+LoadingOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED79A5AC2BD7BA0F00952D1F /* UIApplication+LoadingOverlay.swift */; };
@ -480,6 +481,7 @@
ED79A5D62BDF8D6100952D1F /* iCloudDocumentsDirectoryMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED79A5CE2BDF8D6100952D1F /* iCloudDocumentsDirectoryMonitor.swift */; };
ED79A5D72BDF8D6100952D1F /* SynchronizationStateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED79A5CF2BDF8D6100952D1F /* SynchronizationStateManager.swift */; };
ED79A5D82BDF8D6100952D1F /* DefaultLocalDirectoryMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED79A5D02BDF8D6100952D1F /* DefaultLocalDirectoryMonitor.swift */; };
ED7CCC4F2C1362E300E2A737 /* FileType.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED7CCC4E2C1362E300E2A737 /* FileType.swift */; };
ED9966802B94FBC20083CE55 /* ColorPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED99667D2B94FBC20083CE55 /* ColorPicker.swift */; };
EDBD68072B625724005DD151 /* LocationServicesDisabledAlert.xib in Resources */ = {isa = PBXBuildFile; fileRef = EDBD68062B625724005DD151 /* LocationServicesDisabledAlert.xib */; };
EDBD680B2B62572E005DD151 /* LocationServicesDisabledAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBD680A2B62572E005DD151 /* LocationServicesDisabledAlert.swift */; };
@ -1377,6 +1379,7 @@
ED1263AA2B6F99F900AD99F3 /* UIView+AddSeparator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+AddSeparator.swift"; sourceTree = "<group>"; };
ED1ADA322BC6B1B40029209F /* CarPlayServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarPlayServiceTests.swift; sourceTree = "<group>"; };
ED3EAC1F2B03C88100220A4A /* BottomTabBarButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BottomTabBarButton.swift; sourceTree = "<group>"; };
ED43B8BC2C12063500D07BAA /* DocumentPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentPicker.swift; sourceTree = "<group>"; };
ED48BBB817C2B1E2003E7E92 /* CircleView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleView.h; sourceTree = "<group>"; };
ED48BBB917C2B1E2003E7E92 /* CircleView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CircleView.m; sourceTree = "<group>"; };
ED63CEB62BDF8F9C006155C4 /* SettingsTableViewiCloudSwitchCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsTableViewiCloudSwitchCell.swift; sourceTree = "<group>"; };
@ -1388,6 +1391,7 @@
ED79A5CE2BDF8D6100952D1F /* iCloudDocumentsDirectoryMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = iCloudDocumentsDirectoryMonitor.swift; sourceTree = "<group>"; };
ED79A5CF2BDF8D6100952D1F /* SynchronizationStateManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SynchronizationStateManager.swift; sourceTree = "<group>"; };
ED79A5D02BDF8D6100952D1F /* DefaultLocalDirectoryMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultLocalDirectoryMonitor.swift; sourceTree = "<group>"; };
ED7CCC4E2C1362E300E2A737 /* FileType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileType.swift; sourceTree = "<group>"; };
ED99667D2B94FBC20083CE55 /* ColorPicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPicker.swift; sourceTree = "<group>"; };
EDBD68062B625724005DD151 /* LocationServicesDisabledAlert.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LocationServicesDisabledAlert.xib; sourceTree = "<group>"; };
EDBD680A2B62572E005DD151 /* LocationServicesDisabledAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationServicesDisabledAlert.swift; sourceTree = "<group>"; };
@ -3049,6 +3053,15 @@
path = Tests;
sourceTree = "<group>";
};
ED43B8B92C12061600D07BAA /* DocumentPicker */ = {
isa = PBXGroup;
children = (
ED7CCC4E2C1362E300E2A737 /* FileType.swift */,
ED43B8BC2C12063500D07BAA /* DocumentPicker.swift */,
);
path = DocumentPicker;
sourceTree = "<group>";
};
ED79A5A92BD7AA7500952D1F /* LoadingOverlay */ = {
isa = PBXGroup;
children = (
@ -3322,6 +3335,7 @@
F6E2FBFB1E097B9F0083EBEC /* UI */ = {
isa = PBXGroup;
children = (
ED43B8B92C12061600D07BAA /* DocumentPicker */,
ED99667C2B94FBC20083CE55 /* ColorPicker */,
F69018B51E9E5FEB00B3C10B /* Autoupdate */,
34E7760D1F14B165003040B3 /* AvailableArea */,
@ -4296,6 +4310,7 @@
34D3B0181E389D05004100F9 /* EditorAdditionalNamePlaceholderTableViewCell.swift in Sources */,
993DF12223F6BDB100AC231A /* UINavigationItemRenderer.swift in Sources */,
993DF12B23F6BDB100AC231A /* StyleManager.swift in Sources */,
ED43B8BD2C12063500D07BAA /* DocumentPicker.swift in Sources */,
470E1674252AD7F2002D201A /* BookmarksListInfoViewController.swift in Sources */,
47B9065521C7FA400079C85E /* NSString+MD5.m in Sources */,
CDB4D5022231412900104869 /* SettingsTemplateBuilder.swift in Sources */,
@ -4488,6 +4503,7 @@
34AB66621FC5AA330078E451 /* TransportTransitSeparator.swift in Sources */,
CDCA2743223F8D1E00167D87 /* ListItemInfo.swift in Sources */,
993DF11F23F6BDB100AC231A /* UITableViewCellRenderer.swift in Sources */,
ED7CCC4F2C1362E300E2A737 /* FileType.swift in Sources */,
4767CDA820AB401000BD8166 /* LinkTextView.swift in Sources */,
34763EE71F2F392300F4D2D3 /* MWMTextToSpeech.mm in Sources */,
998927402449ECC200260CE2 /* BottomMenuItemCell.swift in Sources */,

View file

@ -0,0 +1,29 @@
typealias URLsCompletionHandler = ([URL]) -> Void
final class DocumentPicker: NSObject {
static let shared = DocumentPicker()
private var completionHandler: URLsCompletionHandler?
func present(from rootViewController: UIViewController,
fileTypes: [FileType] = [.kml, .kmz, .gpx],
completionHandler: @escaping URLsCompletionHandler) {
self.completionHandler = completionHandler
let documentPickerViewController: UIDocumentPickerViewController
if #available(iOS 14.0, *) {
documentPickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: fileTypes.map(\.utType))
} else {
documentPickerViewController = UIDocumentPickerViewController(documentTypes: fileTypes.map(\.typeIdentifier), in: .import)
}
documentPickerViewController.delegate = self
// TODO: Enable multiple selection when the multiple files parsing support will be added to the bookmark_manager.
documentPickerViewController.allowsMultipleSelection = false
rootViewController.present(documentPickerViewController, animated: true)
}
}
// MARK: - UIDocumentPickerDelegate
extension DocumentPicker: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
completionHandler?(urls)
}
}

View file

@ -0,0 +1,22 @@
import UniformTypeIdentifiers
// TODO: Remove this type-wrapper and use custom UTTypeIdentifier that is registered into the Info.plist after updating to the iOS >= 14.0.
struct FileType {
let fileExtension: String
let typeIdentifier: String
}
extension FileType {
static let kml = FileType(fileExtension: "kml", typeIdentifier: "com.google.earth.kml")
static let kmz = FileType(fileExtension: "kmz", typeIdentifier: "com.google.earth.kmz")
static let gpx = FileType(fileExtension: "gpx", typeIdentifier: "com.topografix.gpx")
}
// MARK: - FileType + UTType
extension FileType {
@available(iOS 14.0, *)
var utType: UTType {
UTType(filenameExtension: fileExtension)!
}
}