Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios_platform_images] migrate objC to swift #4847

Merged
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ad9ed35
[ios_platform_images] migrate objC to swift
Mairramer Sep 5, 2023
e5545bc
pump version
Mairramer Sep 5, 2023
cf21182
added some tests
Mairramer Sep 6, 2023
008d5d3
update tests
Mairramer Sep 8, 2023
e422e78
Merge branch 'main' into ios_platform_images_swift_migration
Mairramer Sep 8, 2023
b0122cf
fix format
Mairramer Sep 8, 2023
54b9b92
Merge branch 'main' into ios_platform_images_swift_migration
Mairramer Sep 9, 2023
6c29139
Merge branch 'main' into ios_platform_images_swift_migration
Mairramer Sep 10, 2023
f3ce031
improve code
Mairramer Sep 11, 2023
100202a
rollback comment
Mairramer Sep 11, 2023
97dc083
pump version
Mairramer Sep 11, 2023
1a28d20
Merge branch 'main' into ios_platform_images_swift_migration
Mairramer Sep 13, 2023
8da12bd
rollback implementation
Mairramer Sep 13, 2023
50ce448
rollback original logic
Mairramer Sep 14, 2023
59d7b6d
fix logic
Mairramer Sep 14, 2023
17a369e
rollback logic
Mairramer Sep 14, 2023
cfbf55f
Merge branch 'main' into ios_platform_images_swift_migration
Mairramer Sep 14, 2023
101c190
fix test
Mairramer Sep 14, 2023
1d2969b
fix test
Mairramer Sep 14, 2023
372ce11
fix scaled image and migrate tests
Mairramer Oct 13, 2023
523a277
Merge remote-tracking branch 'origin/main' into ios_platform_images_s…
Mairramer Oct 13, 2023
aef7985
migrate pigeon to swift
Mairramer Oct 13, 2023
78cbc82
format code
Mairramer Oct 13, 2023
45d185d
fix build script
Mairramer Oct 13, 2023
a9a4879
improves
Mairramer Oct 13, 2023
364be83
update readme
Mairramer Oct 13, 2023
aae7a29
rollback objC test
Mairramer Oct 17, 2023
cbbc208
fix test import
Mairramer Oct 17, 2023
ac54592
improves
Mairramer Oct 17, 2023
3e66718
improve test
Mairramer Oct 17, 2023
71f2589
fix test
Mairramer Oct 17, 2023
e2d9a9a
Merge branch 'main' into ios_platform_images_swift_migration
Mairramer Oct 17, 2023
3324ccd
rollback
Mairramer Oct 17, 2023
e370d61
update readme
Mairramer Oct 17, 2023
b25d574
Revert whitespace change in podfile
stuartmorgan Oct 20, 2023
5d03976
TODO style fix
stuartmorgan Oct 20, 2023
eaf7c90
Re-add DEFINES_MODULE
stuartmorgan Oct 20, 2023
139e125
Merge branch 'main' into ios_platform_images_swift_migration
stuartmorgan Oct 20, 2023
6ea1fc0
Merge branch 'main' into ios_platform_images_swift_migration
Mairramer Oct 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ios_platform_images/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <[email protected]>
Anton Borries <[email protected]>
Alex Li <[email protected]>
Rahul Raj <[email protected]>
Mairramer <[email protected]>
4 changes: 4 additions & 0 deletions packages/ios_platform_images/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.3

* Migrates to a Swift implementation.

## 0.2.2+3

* Converts platform communication to Pigeon.
Expand Down
12 changes: 6 additions & 6 deletions packages/ios_platform_images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ Widget build(BuildContext context) {
}
```

`IosPlatformImages.load` functions like [[UIImage imageNamed:]](https://developer.apple.com/documentation/uikit/uiimage/1624146-imagenamed).
`IosPlatformImages.load` works similarly to [`UIImage(named:)`](https://developer.apple.com/documentation/uikit/uiimage/1624146-imagenamed).

### Flutter->iOS Example

```objc
#import <ios_platform_images/UIImage+ios_platform_images.h>
```swift
import ios_platform_images

static UIImageView* MakeImage() {
UIImage* image = [UIImage flutterImageWithName:@"assets/foo.png"];
return [[UIImageView alloc] initWithImage:image];
func makeImage() -> UIImageView {
let image = flutterImageWithName(withName: "assets/foo.png")
return UIImageView(image: image)
}
```
2 changes: 1 addition & 1 deletion packages/ios_platform_images/example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_ios_podfile_setup
target 'Runner' do
use_frameworks!
use_modular_headers!
Mairramer marked this conversation as resolved.
Show resolved Hide resolved

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
target 'RunnerTests' do
inherit! :search_paths
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
Mairramer marked this conversation as resolved.
Show resolved Hide resolved
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F76AC1BD266713D00040C8BC"
BuildableName = "RunnerTests.xctest"
BlueprintName = "RunnerTests"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import UIKit
import Flutter
import UIKit

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 Flutter
import XCTest

@testable import ios_platform_images

class IosPlatformImagesTests: XCTestCase {
let plugin = IosPlatformImagesPlugin()

func testLoadImage() {
let assetName = "flutter"
let imageData = plugin.loadImage(name: assetName)

XCTAssertNotNil(imageData)
XCTAssertNotNil(imageData?.data)
}

func testLoadImageNotFound() {
let assetName = "notFound"
let imageData = plugin.loadImage(name: assetName)

Mairramer marked this conversation as resolved.
Show resolved Hide resolved
XCTAssertNil(imageData)
}

func testResolveURL() {
let resourceName = "textfile"
do {
let url = try plugin.resolveUrl(resourceName: resourceName, extension: nil)
XCTAssertNotNil(url)
XCTAssertTrue(url?.contains(resourceName) ?? false)
} catch {
XCTFail("Error while resolving URL: \(error)")
}
}

func testResolveURLNotFound() {
do {
let url = try plugin.resolveUrl(resourceName: "notFound", extension: nil)
XCTAssertNil(url)
} catch {
XCTFail("Error while resolving URL: \(error)")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

@import ios_platform_images;
@import XCTest;
#import "IosPlatformImagesPlugin-Swift.h"

// The tests test the UIImage extension which is a public API intended for use from native code
// outside of the plugin (see package README). Any change that requires changing existing tests
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 Flutter
import Foundation

public final class IosPlatformImagesPlugin: NSObject, FlutterPlugin, PlatformImagesApi {
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = IosPlatformImagesPlugin()
let messenger = registrar.messenger()
PlatformImagesApiSetup.setUp(binaryMessenger: messenger, api: instance)
}

func loadImage(name: String) -> PlatformImageData? {
guard let image = UIImage(named: name),
let data = image.pngData()
else {
return nil
}

return PlatformImageData(
data: FlutterStandardTypedData(bytes: data), scale: Double(image.scale))
}

func resolveUrl(resourceName: String, extension: String?) throws -> String? {
guard
let url = Bundle.main.url(
forResource: resourceName,
withExtension: `extension`)
else {
return nil
}

return url.absoluteString
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 Flutter
import Foundation
import UIKit

@objc extension UIImage {
/// Loads a UIImage from the embedded Flutter project's assets.
///
/// This method loads the Flutter asset that is appropriate for the current
/// screen. If you are on a 2x retina device where usually `UIImage` would be
/// loading `@2x` assets, it will attempt to load the `2.0x` variant. It will
/// load the standard image if it can't find the `2.0x` variant.
///
/// For example, if your Flutter project's `pubspec.yaml` lists "assets/foo.png"
/// and "assets/2.0x/foo.png", calling
/// `[UIImage flutterImageWithName:@"assets/foo.png"]` will load
/// "assets/2.0x/foo.png".
///
/// See also https://flutter.dev/docs/development/ui/assets-and-images
///
/// Note: We don't yet support images from package dependencies (ex.
/// `AssetImage('icons/heart.png', package: 'my_icons')`).
public static func flutterImageWithName(_ name: String) -> UIImage? {
let filename = (name as NSString).lastPathComponent
let path = (name as NSString).deletingLastPathComponent
Mairramer marked this conversation as resolved.
Show resolved Hide resolved

for screenScale in stride(from: Int(UIScreen.main.scale), to: 1, by: -1) {
//TODO(hellohuanlin): This should be fixed, because it's path uses double slash.
let key = FlutterDartProject.lookupKey(forAsset: "\(path)/\(screenScale).0x/\(filename)")
if let image = UIImage(named: key, in: Bundle.main, compatibleWith: nil) {
return image
}
}

let key = FlutterDartProject.lookupKey(forAsset: name)
return UIImage(named: key, in: Bundle.main, compatibleWith: nil)
}
Mairramer marked this conversation as resolved.
Show resolved Hide resolved
}
47 changes: 0 additions & 47 deletions packages/ios_platform_images/ios/Classes/messages.g.h

This file was deleted.

Loading