Skip to content

Commit

Permalink
[file_selector] Convert iOS to Swift and SPM (#6755)
Browse files Browse the repository at this point in the history
Converts the plugin and its tests to Swift, and re-adds the SPM support that was reverted due to modulemap issues.

In order to avoid the ugliness and loss of type saftey of using associated objects in the Swift version, this replaces that with a bridge object that serves as a delegate instead of the plugin, and manages its own lifetime in coordination with the plugin.

While this is one PR, the conversion was done in individual steps, each of which is a commit:
- Rewrite just the tests in Swift, with no implementation changes, ensuring that everything still passed.
- Rewrite the implementation in Swift, changing the tests only as necessary for the structural changes to the implementation due to the removal of associated objects
- Re-add SPM

The changes in the generated Dart files are just due to updating to the latest version of Pigeon (to avoid writing the Swift implementation against an older version of the Swift API, and then having to update again later for breaking changes).

Part of flutter/flutter#119015
Fixes flutter/flutter#146903
  • Loading branch information
stuartmorgan authored May 23, 2024
1 parent 66530f2 commit df16d4e
Show file tree
Hide file tree
Showing 24 changed files with 487 additions and 474 deletions.
5 changes: 5 additions & 0 deletions packages/file_selector/file_selector_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.3

* Converts implementation to Swift.
* Re-adds Swift Package Manager compatibility.

## 0.5.2+1

* Temporarily remove Swift Package Manager compatibility to resolve issues with Cocoapods builds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
21160A929DC757957DE39F1E /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 000792269CB6B9FE88AC567C /* Pods_Runner.framework */; };
337EF9CE2BF7945F0079FB1A /* FileSelectorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 337EF9CD2BF7945F0079FB1A /* FileSelectorTests.swift */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
6165A2F80DFA224EAF50A1D5 /* Pods_RunnerTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AC3841659BF3693FAC5A2F8F /* Pods_RunnerTests.framework */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
C71AE4C8281C6B6B0086307A /* FileSelectorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C71AE4C5281C6B530086307A /* FileSelectorTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -45,6 +45,7 @@
000792269CB6B9FE88AC567C /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
337EF9CD2BF7945F0079FB1A /* FileSelectorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSelectorTests.swift; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
4A27CC0DB4EF6669B637A1E8 /* Pods-RunnerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.debug.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.debug.xcconfig"; sourceTree = "<group>"; };
5667547C6832727A744371E2 /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };
Expand All @@ -63,7 +64,6 @@
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AC3841659BF3693FAC5A2F8F /* Pods_RunnerTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RunnerTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C71AE4B6281C6A090086307A /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
C71AE4C5281C6B530086307A /* FileSelectorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FileSelectorTests.m; sourceTree = "<group>"; };
F818CE2D7CDF8AFF94707327 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -150,7 +150,7 @@
C71AE4C4281C6B370086307A /* RunnerTests */ = {
isa = PBXGroup;
children = (
C71AE4C5281C6B530086307A /* FileSelectorTests.m */,
337EF9CD2BF7945F0079FB1A /* FileSelectorTests.swift */,
);
path = RunnerTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -223,6 +223,7 @@
};
C71AE4B5281C6A090086307A = {
CreatedOnToolsVersion = 13.1;
LastSwiftMigration = 1510;
TestTargetID = 97C146ED1CF9000F007C117D;
};
};
Expand Down Expand Up @@ -376,7 +377,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C71AE4C8281C6B6B0086307A /* FileSelectorTests.m in Sources */,
337EF9CE2BF7945F0079FB1A /* FileSelectorTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -636,6 +637,7 @@
baseConfigurationReference = 4A27CC0DB4EF6669B637A1E8 /* Pods-RunnerTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -647,6 +649,8 @@
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.FileSelectorTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/Runner";
};
name = Debug;
Expand All @@ -659,6 +663,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
Expand All @@ -678,6 +683,7 @@
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.FileSelectorTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/Runner";
};
Expand All @@ -691,6 +697,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
Expand All @@ -710,6 +717,7 @@
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.FileSelectorTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/Runner";
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Prepare Flutter Framework Script"
scriptText = "/bin/sh &quot;$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh&quot; prepare&#10;">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "97C146ED1CF9000F007C117D"
BuildableName = "Runner.app"
BlueprintName = "Runner"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PreActions>
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Flutter
import UIKit

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import XCTest

@testable import file_selector_ios

final class TestViewPresenter: ViewPresenter {
public var presentedController: UIViewController?

func present(
_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)? = nil
) {
presentedController = viewControllerToPresent
}
}

class FileSelectorTests: XCTestCase {
func testPickerPresents() throws {
let plugin = FileSelectorPlugin()
let picker = UIDocumentPickerViewController(documentTypes: [], in: UIDocumentPickerMode.import)
let presenter = TestViewPresenter()
plugin.documentPickerViewControllerOverride = picker
plugin.viewPresenterOverride = presenter

plugin.openFile(
config: FileSelectorConfig(utis: [], allowMultiSelection: false)
) { _ in }

XCTAssertEqual(plugin.pendingCompletions.count, 1)
XCTAssertTrue(picker.delegate === plugin.pendingCompletions.first)
XCTAssertTrue(presenter.presentedController === picker)
}

func testReturnsPickedFiles() throws {
let plugin = FileSelectorPlugin()
let picker = UIDocumentPickerViewController(documentTypes: [], in: UIDocumentPickerMode.import)
plugin.documentPickerViewControllerOverride = picker
plugin.viewPresenterOverride = TestViewPresenter()
let completionWasCalled = expectation(description: "completion")

plugin.openFile(
config: FileSelectorConfig(utis: [], allowMultiSelection: false)
) { result in
switch result {
case .success(let paths):
XCTAssertEqual(paths, ["/file1.txt", "/file2.txt"])
case .failure(let error):
XCTFail("\(error)")
}
completionWasCalled.fulfill()
}
plugin.pendingCompletions.first!.documentPicker(
picker,
didPickDocumentsAt: [URL(string: "file:///file1.txt")!, URL(string: "file:///file2.txt")!])

waitForExpectations(timeout: 30.0)
XCTAssertTrue(plugin.pendingCompletions.isEmpty)
}

func testCancellingPickerReturnsEmptyList() throws {
let plugin = FileSelectorPlugin()
let picker = UIDocumentPickerViewController(documentTypes: [], in: UIDocumentPickerMode.import)
plugin.documentPickerViewControllerOverride = picker
plugin.viewPresenterOverride = TestViewPresenter()
let completionWasCalled = expectation(description: "completion")

plugin.openFile(
config: FileSelectorConfig(utis: [], allowMultiSelection: false)
) { result in
switch result {
case .success(let paths):
XCTAssertEqual(paths.count, 0)
case .failure(let error):
XCTFail("\(error)")
}
completionWasCalled.fulfill()
}
plugin.pendingCompletions.first!.documentPickerWasCancelled(picker)

waitForExpectations(timeout: 30.0)
XCTAssertTrue(plugin.pendingCompletions.isEmpty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ Displays the native iOS document picker.
s.license = { :type => 'BSD', :file => '../LICENSE' }
s.author = { 'Flutter Dev Team' => '[email protected]' }
s.source = { :http => 'https://github.com/flutter/packages/tree/main/packages/file_selector/file_selector_ios' }
s.source_files = 'file_selector_ios/Sources/file_selector_ios/**/*.{h,m}'
s.module_map = 'file_selector_ios/Sources/file_selector_ios/include/cocoapods_file_selector_ios.modulemap'
s.source_files = 'file_selector_ios/Sources/file_selector_ios/**/*.swift'
s.dependency 'Flutter'
s.platform = :ios, '12.0'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.swift_version = '5.0'
s.xcconfig = {
'LIBRARY_SEARCH_PATHS' => '$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)/ $(SDKROOT)/usr/lib/swift',
'LD_RUNPATH_SEARCH_PATHS' => '/usr/lib/swift',
}
s.resource_bundles = {'file_selector_ios_privacy' => ['file_selector_ios/Sources/file_selector_ios/Resources/PrivacyInfo.xcprivacy']}
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import PackageDescription

let package = Package(
name: "file_selector_ios",
platforms: [
.iOS("12.0")
],
products: [
.library(name: "file-selector-ios", targets: ["file_selector_ios"])
],
dependencies: [],
targets: [
.target(
name: "file_selector_ios",
dependencies: [],
resources: [
.process("Resources")
],
cSettings: [
.headerSearchPath("include/file_selector_ios")
]
)
]
)
Loading

0 comments on commit df16d4e

Please sign in to comment.