Skip to content

Commit a71460b

Browse files
committed
Implement comparing terms function in Swift
1 parent 52a5588 commit a71460b

File tree

8 files changed

+20
-39
lines changed

8 files changed

+20
-39
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Foundation
2+
import WordPressKitObjC
3+
4+
extension RemotePost {
5+
public static func compare(otherTerms lhs: [String: [String]], withAnother rhs: [String: [String]]) -> Bool {
6+
guard lhs.count == rhs.count else { return false }
7+
8+
return lhs.mapValues { Set($0) } == rhs.mapValues { Set($0) }
9+
}
10+
}

Modules/Sources/WordPressKit/RemotePostParameters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ extension RemotePostCreateParameters {
140140
if previous.tags != tags {
141141
changes.tags = tags
142142
}
143-
if !RemotePost.compareOtherTerms(previous.otherTerms, withAnother: otherTerms) {
143+
if !RemotePost.compare(otherTerms: previous.otherTerms, withAnother: otherTerms) {
144144
changes.otherTerms = otherTerms
145145
}
146146
if Set(previous.categoryIDs) != Set(categoryIDs) {

Modules/Sources/WordPressKitObjC/RemotePost.m

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,6 @@ - (id)initWithSiteID:(NSNumber *)siteID status:(NSString *)status title:(NSStrin
2323
return self;
2424
}
2525

26-
+ (BOOL)compareOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)lhs withAnother:(NSDictionary<NSString *, NSArray<NSString *> *> *)rhs
27-
{
28-
if (lhs.count != rhs.count) {
29-
return NO;
30-
}
31-
32-
NSMutableDictionary<NSString *, NSSet<NSString *> *> *lhsNormalized = [NSMutableDictionary dictionary];
33-
for (NSString *key in lhs) {
34-
lhsNormalized[key] = [NSSet setWithArray:lhs[key]];
35-
}
36-
37-
NSMutableDictionary<NSString *, NSSet<NSString *> *> *rhsNormalized = [NSMutableDictionary dictionary];
38-
for (NSString *key in rhs) {
39-
rhsNormalized[key] = [NSSet setWithArray:rhs[key]];
40-
}
41-
42-
return [lhsNormalized isEqualToDictionary:rhsNormalized];
43-
}
44-
4526
- (NSString *)debugDescription {
4627
NSDictionary *properties = [self debugProperties];
4728
return [NSString stringWithFormat:@"<%@: %p> (%@)", NSStringFromClass([self class]), self, properties];

Modules/Sources/WordPressKitObjC/include/RemotePost.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ extern NSString * const PostStatusDeleted;
5656
@property (nonatomic, strong) NSArray *categories;
5757
@property (nonatomic, strong) NSArray *revisions;
5858
@property (nonatomic, strong) NSArray *tags;
59-
//@property (nonatomic, strong) NSArray<RemotePostTerm *> *otherTerms;
6059
@property (nonatomic, strong) NSDictionary<NSString *, NSArray<NSString *> *> *otherTerms;
6160
@property (nonatomic, strong) NSString *pathForDisplayImage;
6261
@property (nonatomic, assign) NSNumber *isStickyPost;
@@ -67,8 +66,6 @@ extern NSString * const PostStatusDeleted;
6766
*/
6867
@property (nonatomic, strong) NSArray *metadata;
6968

70-
+ (BOOL)compareOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)lhs withAnother:(NSDictionary<NSString *, NSArray<NSString *> *> *)rhs;
71-
7269
// Featured images?
7370
// Geolocation?
7471
// Attachments?

Sources/WordPressData/Objective-C/AbstractPost.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,4 @@ - (void)setParsedOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)d
365365
return [NSJSONSerialization JSONObjectWithData:self.rawOtherTerms options:0 error:nil] ?: [NSDictionary dictionary];
366366
}
367367

368-
+ (BOOL)compareOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)lhs withAnother:(NSDictionary<NSString *, NSArray<NSString *> *> *)rhs
369-
{
370-
return [RemotePost compareOtherTerms:lhs withAnother:rhs];
371-
}
372-
373368
@end

Sources/WordPressData/Objective-C/include/AbstractPost.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ typedef NS_ENUM(NSUInteger, AbstractPostRemoteStatus) {
133133
- (void)setParsedOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)data;
134134
- (NSDictionary<NSString *, NSArray<NSString *> *> *)parseOtherTerms;
135135

136-
+ (BOOL)compareOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)lhs withAnother:(NSDictionary<NSString *, NSArray<NSString *> *> *)rhs;
137-
138136
@end
139137

140138
@interface AbstractPost (CoreDataGeneratedAccessors)

Tests/WordPressKitTests/WordPressKitTests/Tests/Models/PostTermComparisonTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct PostTermComparisonTests {
99
let lhs: [String: [String]] = [:]
1010
let rhs: [String: [String]] = [:]
1111

12-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == true)
12+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == true)
1313
}
1414

1515
@Test
@@ -23,7 +23,7 @@ struct PostTermComparisonTests {
2323
"post_tag": ["tag1", "tag2"]
2424
]
2525

26-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == true)
26+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == true)
2727
}
2828

2929
@Test
@@ -37,7 +37,7 @@ struct PostTermComparisonTests {
3737
"post_tag": ["tag2", "tag1"]
3838
]
3939

40-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == true)
40+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == true)
4141
}
4242

4343
@Test
@@ -49,7 +49,7 @@ struct PostTermComparisonTests {
4949
"post_tag": ["tag1", "tag2"]
5050
]
5151

52-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == false)
52+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == false)
5353
}
5454

5555
@Test
@@ -61,7 +61,7 @@ struct PostTermComparisonTests {
6161
"category": ["1", "2", "4"]
6262
]
6363

64-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == false)
64+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == false)
6565
}
6666

6767
@Test
@@ -74,7 +74,7 @@ struct PostTermComparisonTests {
7474
"category": ["1", "2", "3"]
7575
]
7676

77-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == false)
77+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == false)
7878
}
7979

8080
@Test
@@ -86,7 +86,7 @@ struct PostTermComparisonTests {
8686
"category": ["1", "2", "3"]
8787
]
8888

89-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == true)
89+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == true)
9090
}
9191

9292
@Test
@@ -102,7 +102,7 @@ struct PostTermComparisonTests {
102102
"custom_tax": ["term1"]
103103
]
104104

105-
#expect(RemotePost.compareOtherTerms(lhs, withAnother: rhs) == true)
105+
#expect(RemotePost.compare(otherTerms: lhs, withAnother: rhs) == true)
106106
}
107107

108108
}

WordPress/Classes/ViewRelated/Post/PostSettings/PostSettings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct PostSettings: Hashable {
112112
post.featuredImage = nil
113113
}
114114

115-
if !AbstractPost.compareOtherTerms(post.parseOtherTerms(), withAnother: otherTerms) {
115+
if !RemotePost.compare(otherTerms:post.parseOtherTerms(), withAnother: otherTerms) {
116116
post.setParsedOtherTerms(otherTerms)
117117
}
118118

0 commit comments

Comments
 (0)