From 5499d0bac765242fe8a9b6d2dee8fd896d40f4c6 Mon Sep 17 00:00:00 2001 From: Filip Stojanovski Date: Fri, 6 Dec 2024 10:34:05 +0100 Subject: [PATCH 1/3] Modify addRequirement method to simplify adding requirements. --- .../ObjCConfigurationController.m | 27 ++++++---------- .../ConfigurationController.swift | 13 ++------ .../ObjCConfigurationViewController.m | 24 +++++--------- .../ConfigurationViewController.swift | 13 ++------ README.md | 14 ++------- .../PoVDataTypes/PoVRequestOptions.swift | 31 ++++++++++++++++--- 6 files changed, 52 insertions(+), 70 deletions(-) diff --git a/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m b/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m index 237380f..d4a6ff8 100644 --- a/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m +++ b/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m @@ -44,28 +44,19 @@ - (void)checkAppVersion NSURL *princeOfVersionsURL = [NSURL URLWithString:Constant.princeOfVersionsURL]; PoVRequestOptions *options = [PoVRequestOptions new]; - [options addRequirementWithKey:@"region" requirementCheck:^BOOL (id value) { - - // Check OS localisation - - if (![value isKindOfClass:[NSString class]]) { - return NO; - } - - return [(NSString *)value isEqualToString:@"hr"]; + [options addRequirementWithKey:@"region" + ofType:[NSString class] + requirementCheck:^BOOL(NSString *value) { + return [value isEqualToString:@"hr"]; }]; - [options addRequirementWithKey:@"bluetooth" requirementCheck:^BOOL (id value) { - - // Check device bluetooth version - - if (![value isKindOfClass:[NSString class]]) { - return NO; - } - - return [(NSString *)value hasPrefix:@"5"]; + [options addRequirementWithKey:@"bluetooth" + ofType:[NSString class] + requirementCheck:^BOOL(NSString *value) { + return [value hasPrefix:@"5"]; }]; + __weak __typeof(self) weakSelf = self; [PrinceOfVersions checkForUpdatesFromURL:princeOfVersionsURL options:options completion:^(UpdateResponse *updateResponse) { [weakSelf fillUIWithInfoResponse:updateResponse.result]; diff --git a/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/Swift Implementation/ConfigurationController.swift b/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/Swift Implementation/ConfigurationController.swift index 3c56139..a641987 100644 --- a/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/Swift Implementation/ConfigurationController.swift +++ b/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/Swift Implementation/ConfigurationController.swift @@ -42,17 +42,10 @@ private extension ConfigurationController { let options = PoVRequestOptions() - options.addRequirement(key: "region") { (value) -> Bool in - guard let value = value as? String else { return false } - // Check OS localisation - return value == "hr" - } + options.addRequirement(key: "region", ofType: String.self) { $0.starts(with: "hr") } + + options.addRequirement(key: "bluetooth", ofType: String.self) { $0.starts(with: "5") } - options.addRequirement(key: "bluetooth") { (value) -> Bool in - guard let value = value as? String else { return false } - // Check device bluetooth version - return value.starts(with: "5") - } let princeOfVersionsURL = URL(string: Constants.princeOfVersionsURL)! diff --git a/PrinceOfVersionsSample/PrinceOfVersionsIosSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationViewController.m b/PrinceOfVersionsSample/PrinceOfVersionsIosSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationViewController.m index 17633ad..b9c2f40 100644 --- a/PrinceOfVersionsSample/PrinceOfVersionsIosSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationViewController.m +++ b/PrinceOfVersionsSample/PrinceOfVersionsIosSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationViewController.m @@ -45,26 +45,18 @@ - (void)checkAppVersion NSURL *princeOfVersionsURL = [NSURL URLWithString:Constant.princeOfVersionsURL]; PoVRequestOptions *options = [PoVRequestOptions new]; - [options addRequirementWithKey:@"region" requirementCheck:^BOOL (id value) { - + [options addRequirementWithKey:@"region" + ofType:[NSString class] + requirementCheck:^BOOL(NSString *value) { // Check OS localisation - - if (![value isKindOfClass:[NSString class]]) { - return NO; - } - - return [(NSString *)value isEqualToString:@"hr"]; + return [value isEqualToString:@"hr"]; }]; - [options addRequirementWithKey:@"bluetooth" requirementCheck:^BOOL (id value) { - + [options addRequirementWithKey:@"bluetooth" + ofType:[NSString class] + requirementCheck:^BOOL(NSString *value) { // Check device bluetooth version - - if (![value isKindOfClass:[NSString class]]) { - return NO; - } - - return [(NSString *)value hasPrefix:@"5"]; + return [value hasPrefix:@"5"]; }]; __weak __typeof(self) weakSelf = self; diff --git a/PrinceOfVersionsSample/PrinceOfVersionsIosSample/Swift Implementation/ConfigurationViewController.swift b/PrinceOfVersionsSample/PrinceOfVersionsIosSample/Swift Implementation/ConfigurationViewController.swift index d7f4d59..25a0b00 100644 --- a/PrinceOfVersionsSample/PrinceOfVersionsIosSample/Swift Implementation/ConfigurationViewController.swift +++ b/PrinceOfVersionsSample/PrinceOfVersionsIosSample/Swift Implementation/ConfigurationViewController.swift @@ -44,17 +44,8 @@ private extension ConfigurationViewController { let options = PoVRequestOptions() - options.addRequirement(key: "region") { (value) -> Bool in - guard let value = value as? String else { return false } - // Check OS localisation - return value == "hr" - } - - options.addRequirement(key: "bluetooth") { (value) -> Bool in - guard let value = value as? String else { return false } - // Check device bluetooth version - return value.starts(with: "5") - } + options.addRequirement(key: "region", ofType: String.self) { $0.starts(with: "hr") } + options.addRequirement(key: "bluetooth", ofType: String.self) { $0.starts(with: "5") } let princeOfVersionsURL = URL(string: Constants.princeOfVersionsURL)! diff --git a/README.md b/README.md index 3a79880..d316372 100644 --- a/README.md +++ b/README.md @@ -118,17 +118,9 @@ Here is the example of how to add requirement check closures. ```swift let options = PoVRequestOptions() - options.addRequirement(key: "region") { (value) -> Bool in - guard let value = value as? String else { return false } - // Check OS localisation - return value == "hr" - } - - options.addRequirement(key: "bluetooth") { (value) -> Bool in - guard let value = value as? String else { return false } - // Check device bluetooth version - return value.starts(with: "5") - } + options.addRequirement(key: "region", ofType: String.self) { $0.starts(with: "hr") } + + options.addRequirement(key: "bluetooth", ofType: String.self) { $0.starts(with: "5") } let princeOfVersionsURL = URL(string: "https://pastebin.com/raw/0MfYmWGu")! diff --git a/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift b/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift index dca69da..3426392 100644 --- a/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift +++ b/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift @@ -31,16 +31,39 @@ public class PoVRequestOptions: NSObject { // MARK: - Public methods /** - Adds requirement check for configuration. + Adds requirement check for configuration. Use this method to add custom requirement by which configuration must comply with. - parameter key: String that matches key in requirements array in JSON with `requirementsCheck` parameter, - - parameter requirementCheck: A block used to check if a configuration meets requirement. This block returns `true` if configuration meets the requirement, and takes the any value as input parameter by which . + - parameter type: The expected type of the value. + - parameter requirementCheck: A block used to check if a configuration meets the requirement. This block returns `true` if the configuration meets the requirement, and takes the typed value as input. */ - public func addRequirement(key: String, requirementCheck: @escaping ((Any) -> Bool)) { - userRequirements.updateValue(requirementCheck, forKey: key) + public func addRequirement(key: String, ofType type: T.Type, requirementCheck: @escaping (T) -> Bool) { + userRequirements.updateValue({ value in + guard let typedValue = value as? T else { return false } + return requirementCheck(typedValue) + }, forKey: key) + } + + /** + Adds requirement check for configuration (Objective-C compatible). + + Use this method to add a custom requirement by which configuration must comply with. + + - parameter key: String that matches the key in the requirements array in JSON with the `requirementCheck` parameter. + - parameter type: The expected class of the value (e.g., `NSString.class`). + - parameter requirementCheck: A block used to check if a configuration meets the requirement. This block returns `true` if the configuration meets the requirement, and takes the value as input. + + This method is designed for Objective-C compatibility and uses runtime type checking (`isKindOfClass:`) to validate the value. + */ + @objc(addRequirementWithKey:ofType:requirementCheck:) + public func addRequirementWithKey(key: String, ofType type: AnyClass, requirementCheck: @escaping (Any) -> Bool) { + userRequirements.updateValue({ value in + guard let value = value as? NSObject, value.isKind(of: type) else { return false } + return requirementCheck(value) + }, forKey: key) } } From a1ca9c958628cdc98bbc0d777c469d5cb6a4b6a0 Mon Sep 17 00:00:00 2001 From: Filip Stojanovski Date: Fri, 6 Dec 2024 10:35:57 +0100 Subject: [PATCH 2/3] Remove empty line. --- .../ConfigurationViewController/ObjCConfigurationController.m | 1 - 1 file changed, 1 deletion(-) diff --git a/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m b/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m index d4a6ff8..c9b5312 100644 --- a/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m +++ b/PrinceOfVersionsMacSample/PrinceOfVersionsMacSample/ObjectiveC Implementation/ConfigurationViewController/ObjCConfigurationController.m @@ -56,7 +56,6 @@ - (void)checkAppVersion return [value hasPrefix:@"5"]; }]; - __weak __typeof(self) weakSelf = self; [PrinceOfVersions checkForUpdatesFromURL:princeOfVersionsURL options:options completion:^(UpdateResponse *updateResponse) { [weakSelf fillUIWithInfoResponse:updateResponse.result]; From e5caa4a15fccd617b931dd984723ea6d26d14d25 Mon Sep 17 00:00:00 2001 From: Filip Stojanovski Date: Fri, 13 Dec 2024 10:36:35 +0100 Subject: [PATCH 3/3] Make addRequirementWithKey available only in ObjC. --- Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift b/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift index 3426392..0f8e9d4 100644 --- a/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift +++ b/Sources/PrinceOfVersions/PoVDataTypes/PoVRequestOptions.swift @@ -58,6 +58,7 @@ public class PoVRequestOptions: NSObject { This method is designed for Objective-C compatibility and uses runtime type checking (`isKindOfClass:`) to validate the value. */ + @available(swift, obsoleted: 1.0, message: "Use the generic addRequirement(key:ofType:requirementCheck:) method in Swift.") @objc(addRequirementWithKey:ofType:requirementCheck:) public func addRequirementWithKey(key: String, ofType type: AnyClass, requirementCheck: @escaping (Any) -> Bool) { userRequirements.updateValue({ value in