Basic objc-bridging

This commit is contained in:
Yannick Heinrich 2022-06-08 12:07:50 +02:00
parent 962acabf76
commit 82c751a850
11 changed files with 257 additions and 2 deletions

View File

@ -9,15 +9,20 @@ let package = Package(
.library(
name: "StrasbourgParkAPI",
targets: ["StrasbourgParkAPI"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
],
targets: [
.target(name: "StrasbourgParkAPIObjc"),
.target(name: "StrasbourgParkAPIObjcPrivate", dependencies: [.target(name:"StrasbourgParkAPIObjc")]),
.target(
name: "StrasbourgParkAPI",
dependencies: [.product(name: "Logging", package: "swift-log")]),
dependencies: [
.product(name: "Logging", package: "swift-log"),
.target(name: "StrasbourgParkAPIObjc"),
.target(name: "StrasbourgParkAPIObjcPrivate")
]),
.testTarget(
name: "StrasbourgParkAPITests",
dependencies: ["StrasbourgParkAPI"],

View File

@ -8,6 +8,8 @@
import Foundation
import CoreLocation
import StrasbourgParkAPIObjc
import StrasbourgParkAPIObjcPrivate
@propertyWrapper struct FailableDecodable<T: Decodable>: Decodable {
let wrappedValue: T?
@ -227,6 +229,16 @@ public enum Either<L, R>: Decodable where L: Decodable, R: Decodable {
/// The status of the parking
public struct StatusOpenData: Decodable {
fileprivate init(id: String, name: String, etat: Int, free: UInt, total: UInt, description: String, usersInfo: Either<String, Int>?) {
self.id = id
self.name = name
self.etat = etat
self.free = free
self.total = total
self.description = description
self.usersInfo = usersInfo
}
/// The reference of the resource on the server
public let id: String
/// The name of the parking
@ -265,4 +277,60 @@ public struct StatusOpenData: Decodable {
self.etat = try container.decode(Int.self, forKey: .etat)
self.usersInfo = try container.decodeIfPresent(Either<String, Int>.self, forKey: .usersInfo)
}
}
// MARK: - Bridging
extension StatusOpenData: _ObjectiveCBridgeable {
public func _bridgeToObjectiveC() -> SPParkingStatus {
let userInfo: Any?
switch self.usersInfo {
case .left(let string):
userInfo = string as Any
case .right(let val):
userInfo = val as Any
default:
userInfo = nil
}
let value = SPParkingStatus(idenfifier: self.id, name: self.name, etat: self.etat, free: self.free, total: self.total, description: self.description, usersInfo: userInfo)
return value
}
public static func _forceBridgeFromObjectiveC(_ source: SPParkingStatus, result: inout StatusOpenData?) {
let usersInfo: Either<String, Int>?
switch source.usersInfo {
case let x as NSNumber:
usersInfo = .right(x.intValue)
case let x as String:
usersInfo = .left(x)
default:
usersInfo = nil
}
result = StatusOpenData(id: source.identifier,
name: source.name,
etat: source.etat,
free: source.free,
total: source.total,
description: source.parkingDescription,
usersInfo: usersInfo)
}
public static func _conditionallyBridgeFromObjectiveC(_ source: SPParkingStatus, result: inout StatusOpenData?) -> Bool {
_forceBridgeFromObjectiveC(source, result: &result)
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: SPParkingStatus?) -> StatusOpenData {
var result: StatusOpenData!
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
public typealias _ObjectiveCType = SPParkingStatus
}

View File

@ -20,6 +20,7 @@ public enum ParkingAPIClientError: Error {
/// An http client to query
/// data from the server
@objc(SPParkingAPIClient)
public final class ParkingAPIClient: NSObject, URLSessionDelegate {
private var session: URLSession!
@ -59,6 +60,7 @@ public final class ParkingAPIClient: NSObject, URLSessionDelegate {
#endif
}
// MARK: - Callback closures APIs(OBJC)
// MARK: - Callback closures APIs
/// Retrieve the parkings' locations with the legacy API
/// - Parameter completion: The result when the request completed

View File

@ -0,0 +1,12 @@
//
// SPParkingLocation.m
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import "SPParkingLocation.h"
@implementation SPParkingLocation
@end

View File

@ -0,0 +1,12 @@
//
// SPParkingStatus.m
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import "SPParkingStatus.h"
@implementation SPParkingStatus
@synthesize identifier = _identidfier, name = _name, etat = _etat, free = _free, total = _total, parkingDescription = _parkingDescription, usersInfo = _usersInfo;
@end

View File

@ -0,0 +1,19 @@
//
// SPParkingLocation.h
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SPParkingLocation : NSObject
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,37 @@
//
// SPParkingStatus.h
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SPParkingStatus : NSObject
/// The reference of the resource on the server
@property(nonatomic, copy, readonly) NSString *identifier;
/// The name of the parking
@property(nonatomic, copy, readonly) NSString *name;
/// The state of the parking
@property(nonatomic, assign, readonly) NSInteger etat;
/// The total of available free slots
@property(nonatomic, assign, readonly) NSUInteger free;
/// The total capacity of the parking
@property(nonatomic, assign, readonly) NSUInteger total;
/// Some description on the parking
@property(nonatomic, copy, readonly) NSString *parkingDescription;
/// Some information available to the users. Either one NSString or NSNumber instance
@property(nullable, nonatomic, copy, readonly) id usersInfo;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,12 @@
//
// SPParkingLocation+Private.m
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import "SPParkingLocation+Private.h"
@implementation SPParkingLocation (Private)
@end

View File

@ -0,0 +1,16 @@
//
// SPParkingLocation+Private.h
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import "SPParkingLocation.h"
NS_ASSUME_NONNULL_BEGIN
@interface SPParkingLocation (Private)
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,31 @@
//
// SPParkingStatus+Private.h
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import "SPParkingStatus.h"
NS_ASSUME_NONNULL_BEGIN
@interface SPParkingStatus (Private)
@property(nonatomic, copy, readwrite) NSString *identifier;
@property(nonatomic, copy, readwrite) NSString *name;
@property(nonatomic, assign, readwrite) NSInteger etat;
@property(nonatomic, assign, readwrite) NSUInteger free;
@property(nonatomic, assign, readwrite) NSUInteger total;
@property(nonatomic, copy, readwrite) NSString *parkingDescription;
@property(nullable, nonatomic, copy, readwrite) id usersInfo;
-(instancetype) initWithIdenfifier:(NSString*) identifier
name:(NSString*) name
etat:(NSInteger) etat
free:(NSUInteger) free
total:(NSUInteger) total
description:(NSString *) description
usersInfo:(nullable id) usersInfo;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,41 @@
//
// SPParkingStatus+Private.m
//
//
// Created by Heinrich Yannick on 08/06/2022.
//
#import "SPParkingStatus+Private.h"
@implementation SPParkingStatus (Private)
@dynamic identifier;
@dynamic name;
@dynamic etat;
@dynamic free;
@dynamic total;
@dynamic parkingDescription;
@dynamic usersInfo;
- (instancetype)initWithIdenfifier:(NSString *)identifier
name:(NSString *)name
etat:(NSInteger)etat
free:(NSUInteger)free
total:(NSUInteger)total
description:(NSString *)description
usersInfo:(id)usersInfo {
self = [super init];
if (self) {
self.identifier = identifier;
self.name = name;
self.etat = etat;
self.free = free;
self.total = total;
self.parkingDescription = description;
self.usersInfo = usersInfo;
}
return self;
}
@end