Skip to content

chore: Migrate some obj-c code to Swift in WireImages - WPB-15697 #2426

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

Merged
merged 19 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c5cea4c
Convert `ZMIImageProperties` to swift
samwyndham Jan 24, 2025
17d6a98
Migrate `ImagePreprocessorTests.testThatItCanCalculateTheSizeOfAnImag…
samwyndham Jan 24, 2025
1ba5d66
Migrate `ImagePreprocessorTests.testThatItReturnsZeroSizeIfFileIsNotA…
samwyndham Jan 24, 2025
ff74ed5
Migrate `ImagePreprocessorTests.testThatItReturnsTheRotatedSizeForIma…
samwyndham Jan 24, 2025
1fefe47
Migrate `ImagePreprocessorTests.testThatItReturnsTheRotatedSizeForIma…
samwyndham Jan 24, 2025
da51070
Migrate `ImagePreprocessorTests.testThatItReturnsTheRotatedSizeForIma…
samwyndham Jan 24, 2025
e95ddab
Migrate `ImagePreprocessorTests.testThatItReturnsZeroSizeIfFileDoesNo…
samwyndham Jan 24, 2025
85b8e6a
Migrate `ImagePreprocessorTests.testThatItCanCalculateTheSizeOfAnImag…
samwyndham Jan 24, 2025
4599737
Migrate `ImagePreprocessorTests.testThatItReturnsZeroSizeIfDataIsNotA…
samwyndham Jan 24, 2025
ee96a77
Migrate `static OperationQueue.serialQueue(name:)`
samwyndham Jan 24, 2025
465a0ae
Delete `waitUntilAllOperationsAreFinishedWithTimeout`
samwyndham Jan 24, 2025
5927402
Migrate `ImageLoadOperationTests.testThatItDoesNotCrashOnInvalidData()`
samwyndham Jan 24, 2025
1ad72b5
Migrate `ImageLoadOperationTests.testThatItDoesNotLoadWhenCancelled()`
samwyndham Jan 24, 2025
e6f6238
Migrate `ImageLoadOperationTests.testThatItLoadsJPEGData()`
samwyndham Jan 24, 2025
eb06a7f
Convert WireImages tests to folder
samwyndham Jan 24, 2025
01b34e8
Migrate `ZMImageOwner`
samwyndham Jan 24, 2025
4c5b478
Lint & format
samwyndham Jan 24, 2025
aade9a5
Use `@objcMembers` in `ZMIImageProperties`
samwyndham Feb 5, 2025
b719aa0
Fix compilation errors
samwyndham Feb 5, 2025
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
38 changes: 38 additions & 0 deletions wire-ios-images/Sources/Image Processing/ImageProperties.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Wire
// Copyright (C) 2025 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//

import CoreGraphics
import Foundation

@objc
public class ZMIImageProperties: NSObject {
@objc public let size: CGSize
@objc public let length: UInt
@objc public let mimeType: String

@objc
public init(size: CGSize, length: UInt, mimeType: String) {
self.size = size
self.length = length
self.mimeType = mimeType
}

public override var description: String {
"{ length: \(length), type: \(mimeType), size: (\(size.width), \(size.height)}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import <WireImages/ZMImageDownsampleOperation.h>

@protocol ZMAssetsPreprocessorDelegate;
@protocol ZMImageOwner;


@protocol ZMAssetsPreprocessor <NSObject>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#import "ZMImageLoadOperation.h"
#import "ZMImageOwner.h"
#import "ZMImageDownsampleOperation.h"

#import <WireImages/WireImages-Swift.h>

@implementation ZMAssetsPreprocessor

Expand Down
44 changes: 0 additions & 44 deletions wire-ios-images/Sources/Image Processing/ZMIImageProperties.m

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
@import Foundation;

#import <WireImages/ZMImageOwner.h>
#import <WireImages/ZMIImageProperties.h>

@class ZMImageLoadOperation;
@class ZMIImageProperties;


typedef NS_ENUM (int16_t, ZMImageDownsampleType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@import Foundation;
@import CoreGraphics;

#import <WireImages/ZMIImageProperties.h>
@class ZMIImageProperties;

@interface ZMImageLoadOperation : NSOperation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@import WireSystem;

#import "ZMImageLoadOperation.h"

#import <WireImages/WireImages-Swift.h>

@interface ZMImageLoadOperation ()
{
Expand Down Expand Up @@ -108,11 +108,7 @@ - (void)main;
CGSize size = [self imageSizeFromSourceImage];
NSUInteger length = (NSUInteger) [(NSNumber *)self.sourceImageProperties[(__bridge id) kCGImagePropertyFileSize] intValue];
NSString *mimeType = (__bridge id) CGImageSourceGetType(_source);
self.computedImageProperties = [ZMIImageProperties
imagePropertiesWithSize:size
length:length
mimeType:mimeType
];
self.computedImageProperties = [[ZMIImageProperties alloc] initWithSize:size length:length mimeType:mimeType];
}

- (int)tiffOrientation;
Expand Down
11 changes: 0 additions & 11 deletions wire-ios-images/Sources/Image Processing/ZMImageOwner.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

@import Foundation;
@import CoreGraphics;
#import "ZMIImageProperties.h"

typedef NS_ENUM(NSUInteger, ZMImageFormat) {
ZMImageFormatInvalid = 0,
Expand All @@ -27,13 +26,3 @@ typedef NS_ENUM(NSUInteger, ZMImageFormat) {
ZMImageFormatOriginal,
ZMImageFormatProfile
};

@protocol ZMImageOwner <NSObject>

/// The image formats that this @c ZMImageOwner wants preprocessed. Order of formats determines order in which data is preprocessed
- (nonnull NSOrderedSet *)requiredImageFormats;

- (nullable NSData *)originalImageData;

@end

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
// along with this program. If not, see http://www.gnu.org/licenses/.
//

@import CoreGraphics;
@import Foundation;
import Foundation

@interface ZMIImageProperties : NSObject
@objc
public protocol ZMImageOwner: NSObjectProtocol {

@property (nonatomic, readonly) CGSize size;
@property (nonatomic, readonly) NSUInteger length;
@property (nonatomic, readonly, copy) NSString *mimeType;
/// The image formats that this ``ZMImageOwner`` wants preprocessed. Order of formats determines order in which data
/// is preprocessed

+ (instancetype)imagePropertiesWithSize:(CGSize)size length:(NSUInteger)length mimeType:(NSString *)type;
@objc
func requiredImageFormats() -> NSOrderedSet

@end
@objc
func originalImageData() -> Data?

}
1 change: 0 additions & 1 deletion wire-ios-images/Sources/WireImages.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ FOUNDATION_EXPORT const unsigned char WireImagesVersionString[];
#import <WireImages/ZMImageLoadOperation.h>
#import <WireImages/ZMAssetsPreprocessor.h>
#import <WireImages/ZMImagePreprocessor.h>
#import <WireImages/ZMIImageProperties.h>
79 changes: 79 additions & 0 deletions wire-ios-images/Tests/ImageLoadOperationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// Wire
// Copyright (C) 2025 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//

import UniformTypeIdentifiers
import WireTesting
import XCTest

@testable import WireImages

final class ImageLoadOperationTests: ZMTBaseTest {

func testThatItLoadsJPEGData() throws {
// given
let imageData = try XCTUnwrap(data(forResource: "unsplash_medium_exif_2", extension: "jpg"))
let sut = try XCTUnwrap(ZMImageLoadOperation(imageData: imageData))
expectation(for: NSPredicate(format: "isFinished == YES"), evaluatedWith: sut, handler: nil)

// when
sut.start()
waitForExpectations(timeout: 5)

// then
let tiffDictionary = try XCTUnwrap(
sut.sourceImageProperties[kCGImagePropertyTIFFDictionary] as? [AnyHashable: Any]
)
XCTAssertNotNil(sut.cgImage)
XCTAssertEqual(tiffDictionary[kCGImagePropertyTIFFOrientation] as? Int, 2)
XCTAssertEqual(sut.sourceImageProperties[kCGImagePropertyOrientation] as? Int, 2)
XCTAssertEqual(sut.sourceImageProperties[kCGImagePropertyPixelHeight] as? Double, 346)
XCTAssertEqual(sut.sourceImageProperties[kCGImagePropertyPixelWidth] as? Double, 531)
XCTAssertEqual(sut.originalImageData, imageData)
XCTAssertEqual(sut.computedImageProperties.mimeType, UTType.jpeg.identifier)
XCTAssertEqual(sut.tiffOrientation, 2)
XCTAssertEqual(sut.computedImageProperties.size, CGSize(width: 531, height: 346))
}

func testThatItDoesNotLoadWhenCancelled() throws {
// given
let imageData = try XCTUnwrap(data(forResource: "unsplash_medium", extension: "jpg"))
let sut = try XCTUnwrap(ZMImageLoadOperation(imageData: imageData))
sut.cancel()
expectation(for: NSPredicate(format: "isFinished == YES"), evaluatedWith: sut, handler: nil)

// when
sut.start()
waitForExpectations(timeout: 5)

// then
XCTAssertNil(sut.cgImage)
XCTAssertNil(sut.sourceImageProperties)
}

func testThatItDoesNotCrashOnInvalidData() throws {
// given
let imageData = try XCTUnwrap(data(forResource: "Lorem Ipsum", extension: "txt"))
let sut = try XCTUnwrap(ZMImageLoadOperation(imageData: imageData))
expectation(for: NSPredicate(format: "isFinished == YES"), evaluatedWith: sut, handler: nil)

// when
sut.start()
waitForExpectations(timeout: 5)
}

}
Loading
Loading