Skip to content

Commit

Permalink
refactor: Modernize some ObjC code (#1472)
Browse files Browse the repository at this point in the history
* chore: Minor ObjC API cleanups

* refactor: Improve & document CDVConfigParser class
  • Loading branch information
dpogue authored Aug 23, 2024
1 parent f3339a9 commit ab78439
Show file tree
Hide file tree
Showing 36 changed files with 359 additions and 260 deletions.
4 changes: 2 additions & 2 deletions CordovaLib/Classes/Private/CDVCommandDelegateImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

@import UIKit;
#import <UIKit/UIKit.h>
#import <Cordova/CDVCommandDelegate.h>

@class CDVViewController;
Expand All @@ -31,6 +31,6 @@
__weak CDVCommandQueue* _commandQueue;
BOOL _delayResponses;
}
- (id)initWithViewController:(CDVViewController*)viewController;
- (instancetype)initWithViewController:(CDVViewController *)viewController;
- (void)flushCommandQueueWithDelayedJs;
@end
4 changes: 1 addition & 3 deletions CordovaLib/Classes/Private/CDVCommandDelegateImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one

@implementation CDVCommandDelegateImpl

@synthesize urlTransformer;

- (id)initWithViewController:(CDVViewController*)viewController
- (instancetype)initWithViewController:(CDVViewController *)viewController
{
self = [super init];
if (self != nil) {
Expand Down
2 changes: 1 addition & 1 deletion CordovaLib/Classes/Private/CDVJSON_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

@import Foundation;
#import <Foundation/Foundation.h>

@interface NSArray (CDVJSONSerializingPrivate)
- (NSString*)cdv_JSONString;
Expand Down
2 changes: 0 additions & 2 deletions CordovaLib/Classes/Private/CDVJSON_private.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Licensed to the Apache Software Foundation (ASF) under one
under the License.
*/

@import Foundation;

#import "CDVJSON_private.h"

@implementation NSArray (CDVJSONSerializingPrivate)
Expand Down
2 changes: 2 additions & 0 deletions CordovaLib/Classes/Private/CDVPlugin+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
under the License.
*/

#import <Cordova/CDVPlugin.h>

@interface CDVPlugin (Private)

- (instancetype)initWithWebViewEngine:(id <CDVWebViewEngineProtocol>)theWebViewEngine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ - (void)applyLongPressFix
{
// You can't suppress 3D Touch and still have regular longpress,
// so if this is false, let's not consider the 3D Touch setting at all.
if (![self.commandDelegate.settings objectForKey:@"suppresseslongpressgesture"] ||
![[self.commandDelegate.settings objectForKey:@"suppresseslongpressgesture"] boolValue]) {
if (![self.commandDelegate.settings cordovaBoolSettingForKey:@"SuppressesLongPressGesture" defaultValue:NO]) {
return;
}

Expand All @@ -41,8 +40,7 @@ - (void)applyLongPressFix

// 0.45 is ok for 'regular longpress', 0.05-0.08 is required for '3D Touch longpress',
// but since this will also kill onclick handlers (not ontouchend) it's optional.
if ([self.commandDelegate.settings objectForKey:@"suppresses3dtouchgesture"] &&
[[self.commandDelegate.settings objectForKey:@"suppresses3dtouchgesture"] boolValue]) {
if ([self.commandDelegate.settings cordovaBoolSettingForKey:@"Suppresses3DTouchGesture" defaultValue:NO]) {
self.lpgr.minimumPressDuration = 0.15f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import "CDVHandleOpenURL.h"
#import <Cordova/CDV.h>

@implementation CDVHandleOpenURL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import "CDVIntentAndNavigationFilter.h"
#import <Cordova/CDV.h>
#import <Cordova/CDVConfigParser.h>

@interface CDVIntentAndNavigationFilter ()

Expand Down Expand Up @@ -74,9 +74,7 @@ - (void)parser:(NSXMLParser*)parser parseErrorOccurred:(NSError*)parseError

- (void)pluginInitialize
{
if ([self.viewController isKindOfClass:[CDVViewController class]]) {
[(CDVViewController*)self.viewController parseSettingsWithParser:self];
}
[CDVConfigParser parseConfigFile:self.viewController.configFilePath withDelegate:self];
}

+ (CDVIntentAndNavigationFilterValue) filterUrl:(NSURL*)url allowIntentsList:(CDVAllowList*)allowIntentsList navigationsAllowList:(CDVAllowList*)navigationsAllowList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import "CDVLaunchScreen.h"
#import <Cordova/CDVViewController.h>

@implementation CDVLaunchScreen

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

@import WebKit;
#import <Cordova/CDV.h>
#import <WebKit/WebKit.h>
#import <Cordova/CDVPlugin.h>

@interface CDVWebViewEngine : CDVPlugin <CDVWebViewEngineProtocol, WKScriptMessageHandler, WKNavigationDelegate>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

@import WebKit;
#import <WebKit/WebKit.h>

@interface CDVWebViewUIDelegate : NSObject <WKUIDelegate>
{
Expand Down
7 changes: 6 additions & 1 deletion CordovaLib/Classes/Public/CDVCommandQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ - (BOOL)currentlyExecuting
return _startExecutionTime > 0;
}

- (id)initWithViewController:(CDVViewController*)viewController
- (instancetype)init
{
return [self initWithViewController:nil];
}

- (instancetype)initWithViewController:(CDVViewController *)viewController
{
self = [super init];
if (self != nil) {
Expand Down
48 changes: 38 additions & 10 deletions CordovaLib/Classes/Public/CDVConfigParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,47 @@ Licensed to the Apache Software Foundation (ASF) under one
#import <Cordova/CDVConfigParser.h>

@interface CDVConfigParser ()
{
NSString *featureName;
}

@property (nonatomic, readwrite, strong) NSMutableDictionary* pluginsDict;
@property (nonatomic, readwrite, strong) NSMutableDictionary* settings;
@property (nonatomic, readwrite, strong) NSMutableArray* startupPluginNames;
@property (nonatomic, readwrite, strong) NSString* startPage;
@property (nonatomic, readwrite, strong) NSMutableDictionary *pluginsDict;
@property (nonatomic, readwrite, strong) NSMutableDictionary *settings;
@property (nonatomic, readwrite, strong) NSMutableArray *startupPluginNames;
@property (nonatomic, readwrite, strong) NSString *startPage;

@end

@implementation CDVConfigParser

@synthesize pluginsDict, settings, startPage, startupPluginNames;
@synthesize pluginsDict;
@synthesize settings;
@synthesize startPage;
@synthesize startupPluginNames;

+ (instancetype)parseConfigFile:(NSURL *)filePath
{
CDVConfigParser* delegate = [[CDVConfigParser alloc] init];
[CDVConfigParser parseConfigFile:filePath withDelegate:delegate];
return delegate;
}

+ (BOOL)parseConfigFile:(NSURL *)filePath withDelegate:(id <NSXMLParserDelegate>)delegate
{
NSXMLParser *configParser = [[NSXMLParser alloc] initWithContentsOfURL:filePath];

if (configParser == nil) {
NSLog(@"Failed to initialize XML parser.");
return NO;
}

[configParser setDelegate:delegate];
[configParser parse];

return YES;
}

- (id)init
- (instancetype)init
{
self = [super init];
if (self != nil) {
Expand All @@ -44,14 +72,14 @@ - (id)init
return self;
}

- (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"preference"]) {
settings[[attributeDict[@"name"] lowercaseString]] = attributeDict[@"value"];
} else if ([elementName isEqualToString:@"feature"]) { // store feature name to use with correct parameter set
featureName = [attributeDict[@"name"] lowercaseString];
} else if ((featureName != nil) && [elementName isEqualToString:@"param"]) {
NSString* paramName = [attributeDict[@"name"] lowercaseString];
NSString *paramName = [attributeDict[@"name"] lowercaseString];
id value = attributeDict[@"value"];
if ([paramName isEqualToString:@"ios-package"]) {
pluginsDict[featureName] = value;
Expand All @@ -66,14 +94,14 @@ - (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName names
}
}

- (void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
{
if ([elementName isEqualToString:@"feature"]) { // no longer handling a feature so release
featureName = nil;
}
}

- (void)parser:(NSXMLParser*)parser parseErrorOccurred:(NSError*)parseError
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSAssert(NO, @"config.xml parse error line %ld col %ld", (long)[parser lineNumber], (long)[parser columnNumber]);
}
Expand Down
17 changes: 11 additions & 6 deletions CordovaLib/Classes/Public/CDVInvokedUrlCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ @implementation CDVInvokedUrlCommand
@synthesize className = _className;
@synthesize methodName = _methodName;

+ (CDVInvokedUrlCommand*)commandFromJson:(NSArray*)jsonEntry
+ (instancetype)commandFromJson:(NSArray *)jsonEntry
{
return [[CDVInvokedUrlCommand alloc] initFromJson:jsonEntry];
}

- (id)initFromJson:(NSArray*)jsonEntry
- (instancetype)init
{
return [self initWithArguments:nil callbackId:nil className:nil methodName:nil];
}

- (instancetype)initFromJson:(NSArray *)jsonEntry
{
id tmp = [jsonEntry objectAtIndex:0];
NSString* callbackId = tmp == [NSNull null] ? nil : tmp;
Expand All @@ -46,10 +51,10 @@ - (id)initFromJson:(NSArray*)jsonEntry
methodName:methodName];
}

- (id)initWithArguments:(NSArray*)arguments
callbackId:(NSString*)callbackId
className:(NSString*)className
methodName:(NSString*)methodName
- (instancetype)initWithArguments:(NSArray *)arguments
callbackId:(NSString *)callbackId
className:(NSString *)className
methodName:(NSString *)methodName
{
self = [super init];
if (self != nil) {
Expand Down
24 changes: 12 additions & 12 deletions CordovaLib/Classes/Public/CDVPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import <Cordova/CDVPlugin.h>
#import "CDVPlugin+Private.h"
#import <Cordova/CDVPlugin+Resources.h>
#import "CDVPlugin+Private.h"
#import <Cordova/CDVViewController.h>
#include <objc/message.h>

Expand All @@ -40,17 +40,17 @@ - (UIScrollView*)scrollView

@end

NSString* const CDVPageDidLoadNotification = @"CDVPageDidLoadNotification";
NSString* const CDVPluginHandleOpenURLNotification = @"CDVPluginHandleOpenURLNotification";
NSString* const CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification = @"CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification";
NSString* const CDVPluginResetNotification = @"CDVPluginResetNotification";
NSString* const CDVViewWillAppearNotification = @"CDVViewWillAppearNotification";
NSString* const CDVViewDidAppearNotification = @"CDVViewDidAppearNotification";
NSString* const CDVViewWillDisappearNotification = @"CDVViewWillDisappearNotification";
NSString* const CDVViewDidDisappearNotification = @"CDVViewDidDisappearNotification";
NSString* const CDVViewWillLayoutSubviewsNotification = @"CDVViewWillLayoutSubviewsNotification";
NSString* const CDVViewDidLayoutSubviewsNotification = @"CDVViewDidLayoutSubviewsNotification";
NSString* const CDVViewWillTransitionToSizeNotification = @"CDVViewWillTransitionToSizeNotification";
const NSNotificationName CDVPageDidLoadNotification = @"CDVPageDidLoadNotification";
const NSNotificationName CDVPluginHandleOpenURLNotification = @"CDVPluginHandleOpenURLNotification";
const NSNotificationName CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification = @"CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification";
const NSNotificationName CDVPluginResetNotification = @"CDVPluginResetNotification";
const NSNotificationName CDVViewWillAppearNotification = @"CDVViewWillAppearNotification";
const NSNotificationName CDVViewDidAppearNotification = @"CDVViewDidAppearNotification";
const NSNotificationName CDVViewWillDisappearNotification = @"CDVViewWillDisappearNotification";
const NSNotificationName CDVViewDidDisappearNotification = @"CDVViewDidDisappearNotification";
const NSNotificationName CDVViewWillLayoutSubviewsNotification = @"CDVViewWillLayoutSubviewsNotification";
const NSNotificationName CDVViewDidLayoutSubviewsNotification = @"CDVViewDidLayoutSubviewsNotification";
const NSNotificationName CDVViewWillTransitionToSizeNotification = @"CDVViewWillTransitionToSizeNotification";

@interface CDVPlugin ()

Expand Down
Loading

0 comments on commit ab78439

Please sign in to comment.