Skip to content

Commit bacb2a2

Browse files
Merge pull request #423 from woocommerce/issue/19-notification-subtype
Filtering WooCommerce Notifications
2 parents d70fbfa + 727037a commit bacb2a2

File tree

9 files changed

+43
-5
lines changed

9 files changed

+43
-5
lines changed

Networking/Networking/Model/Note.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ public struct Note {
3838
///
3939
public let kind: Kind
4040

41+
/// Notification.Subtype expressed as a Swift Native enum.
42+
///
43+
public let subkind: Subkind?
44+
4145
/// Notification Type.
4246
///
4347
public let type: String?
4448

49+
/// Notification Subtype.
50+
///
51+
public let subtype: String?
52+
4553
/// Associated Resource's URL.
4654
///
4755
public let url: String?
@@ -96,6 +104,7 @@ public struct Note {
96104
noticon: String?,
97105
timestamp: String,
98106
type: String,
107+
subtype: String?,
99108
url: String?,
100109
title: String?,
101110
subject: Data,
@@ -111,7 +120,9 @@ public struct Note {
111120
self.timestamp = timestamp
112121
self.timestampAsDate = DateFormatter.Defaults.iso8601.date(from: timestamp) ?? Date()
113122
self.type = type
123+
self.subtype = subtype
114124
self.kind = Kind(rawValue: type) ?? .unknown
125+
self.subkind = subtype.flatMap { Subkind(rawValue: $0) }
115126
self.url = url
116127
self.title = title
117128

@@ -148,6 +159,7 @@ extension Note: Decodable {
148159
let noticon = container.failsafeDecodeIfPresent(String.self, forKey: .noticon)
149160
let timestamp = container.failsafeDecodeIfPresent(stringForKey: .timestamp) ?? String()
150161
let type = container.failsafeDecodeIfPresent(String.self, forKey: .type) ?? String()
162+
let subtype = container.failsafeDecodeIfPresent(String.self, forKey: .subtype)
151163
let url = container.failsafeDecodeIfPresent(String.self, forKey: .url)
152164
let title = container.failsafeDecodeIfPresent(String.self, forKey: .title)
153165

@@ -170,6 +182,7 @@ extension Note: Decodable {
170182
noticon: noticon,
171183
timestamp: timestamp,
172184
type: type,
185+
subtype: subtype,
173186
url: url,
174187
title: title,
175188
subject: subjectAsData,
@@ -194,6 +207,7 @@ extension Note {
194207
case noticon
195208
case timestamp
196209
case type
210+
case subtype
197211
case url
198212
case title
199213
case subject
@@ -216,4 +230,10 @@ extension Note {
216230
case user
217231
case unknown
218232
}
233+
234+
/// Known Notification Subkind(s)
235+
///
236+
public enum Subkind: String {
237+
case storeReview = "store_review"
238+
}
219239
}

Networking/NetworkingTests/Mapper/NoteListMapperTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ class NoteListMapperTests: XCTestCase {
250250
let commentNote = sampleNotes.first(where: { $0.kind == .comment })
251251
XCTAssertEqual(commentNote?.body[1].kind, .comment)
252252
}
253+
254+
255+
/// Verifies that the Notification's subtype is properly parsed.
256+
///
257+
func testStoreReviewSubtypeIsProperlyParsed() {
258+
let storeReview = sampleNotes.first(where: { $0.noteId == 100009 })
259+
XCTAssertEqual(storeReview?.subtype, "store_review")
260+
XCTAssertEqual(storeReview?.subkind, .storeReview)
261+
}
253262
}
254263

255264

Networking/NetworkingTests/Responses/notifications-load-all.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@
12281228
"id": 100009,
12291229
"note_hash": 987654,
12301230
"type": "comment",
1231+
"subtype": "store_review",
12311232
"read": 1,
12321233
"noticon": "\uf300",
12331234
"timestamp": "2018-10-19T12:56:47+00:00",

Storage/Storage/Model/MIGRATIONS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
This file documents changes in the WCiOS Storage data model. Please explain any changes to the data model as well as any custom migrations.
44

55
## Model 6
6+
- @jleandroperez 2018-11-12
7+
- New `Note.subtype` property (optional type)
8+
69
- @thuycopeland 2018-11-8
710
- Added new attribute: `isJetpackInstalled`, to site entity
811
- Added new attribute: `plan`, to site entity

Storage/Storage/Model/Note+CoreDataProperties.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extension Note {
1515
@NSManaged public var noticon: String?
1616
@NSManaged public var timestamp: String?
1717
@NSManaged public var type: String?
18+
@NSManaged public var subtype: String?
1819
@NSManaged public var url: String?
1920
@NSManaged public var title: String?
2021
@NSManaged public var subject: Data?

Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 6.xcdatamodel/contents

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14460.32" systemVersion="18A391" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
2+
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14460.32" systemVersion="18B75" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
33
<entity name="Account" representedClassName="Account" syncable="YES">
44
<attribute name="displayName" optional="YES" attributeType="String" syncable="YES"/>
55
<attribute name="email" optional="YES" attributeType="String" syncable="YES"/>
@@ -17,6 +17,7 @@
1717
<attribute name="noticon" optional="YES" attributeType="String" syncable="YES"/>
1818
<attribute name="read" attributeType="Boolean" usesScalarValueType="YES" syncable="YES"/>
1919
<attribute name="subject" optional="YES" attributeType="Binary" syncable="YES"/>
20+
<attribute name="subtype" optional="YES" attributeType="String" syncable="YES"/>
2021
<attribute name="timestamp" attributeType="String" syncable="YES"/>
2122
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/>
2223
<attribute name="type" optional="YES" attributeType="String" syncable="YES"/>
@@ -175,7 +176,7 @@
175176
</entity>
176177
<elements>
177178
<element name="Account" positionX="-200.9765625" positionY="63.5625" width="128" height="120"/>
178-
<element name="Note" positionX="-162" positionY="180" width="128" height="240"/>
179+
<element name="Note" positionX="-162" positionY="180" width="128" height="255"/>
179180
<element name="Order" positionX="-20" positionY="27" width="128" height="690"/>
180181
<element name="OrderCoupon" positionX="-206.01953125" positionY="379.74609375" width="128" height="120"/>
181182
<element name="OrderItem" positionX="-364.890625" positionY="379.453125" width="128" height="225"/>

WooCommerce/Classes/ViewRelated/Notifications/NotificationsViewController.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ class NotificationsViewController: UIViewController {
2222
return ResultsController<StorageNote>(storageManager: storageManager, sectionNameKeyPath: "normalizedAgeAsString", matching: filter, sortedBy: [descriptor])
2323
}()
2424

25-
/// Store Notifications CoreData Filter. IMPORTANT!! This is CLEARLY a quick hack (we can't filter based on the title!)
26-
/// TODO: Remove ASAP as soon as the (pending) backend PR is merged
25+
/// Store Notifications CoreData Filter.
2726
///
2827
private var filter: NSPredicate {
29-
return NSPredicate(format: "type == %@ OR title == 'Product Review'", Note.Kind.storeOrder.rawValue)
28+
return NSPredicate(format: "type == %@ OR subtype == %@", Note.Kind.storeOrder.rawValue, Note.Subkind.storeReview.rawValue)
3029
}
3130

3231
/// Pull To Refresh Support.

Yosemite/Yosemite/Model/Storage/Note+ReadOnlyConvertible.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extension Storage.Note: ReadOnlyConvertible {
1717
url = note.url
1818
title = note.title
1919
type = note.type
20+
subtype = note.subtype
2021
subject = note.subjectAsData
2122
header = note.headerAsData
2223
body = note.bodyAsData
@@ -33,6 +34,7 @@ extension Storage.Note: ReadOnlyConvertible {
3334
noticon: noticon,
3435
timestamp: timestamp ?? "",
3536
type: type ?? "",
37+
subtype: subtype,
3638
url: url,
3739
title: title,
3840
subject: subject ?? Data(),

Yosemite/YosemiteTests/Stores/NotificationStoreTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ private extension NotificationStoreTests {
283283
noticon: "\u{f408}",
284284
timestamp: "2018-10-22T18:51:33+00:00",
285285
type: "comment_like",
286+
subtype: nil,
286287
url: "https:\\someurl.sometld",
287288
title: "3 Likes",
288289
subject: Data(),
@@ -299,6 +300,7 @@ private extension NotificationStoreTests {
299300
noticon: "\u{f408}",
300301
timestamp: "2018-10-22T18:51:33+00:00",
301302
type: "comment_like",
303+
subtype: nil,
302304
url: "https:\\someurl.sometld",
303305
title: "3 Likes",
304306
subject: Data(),

0 commit comments

Comments
 (0)