-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReaderBlockingHelper.swift
More file actions
58 lines (49 loc) · 2.64 KB
/
Copy pathReaderBlockingHelper.swift
File metadata and controls
58 lines (49 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Foundation
import WordPressData
struct ReaderBlockingHelper {
func blockSite(forPost post: ReaderPost, context: NSManagedObjectContext = ContextManager.shared.mainContext) {
postSiteBlockingWillBeginNotification(post)
ReaderBlockSiteAction(asBlocked: true).execute(with: post, context: context, completion: {
ReaderHelpers.dispatchSiteBlockedMessage(post: post, success: true)
postSiteBlockingDidFinish(post)
}, failure: { error in
ReaderHelpers.dispatchSiteBlockedMessage(post: post, success: false)
postSiteBlockingDidFail(post, error: error)
})
}
func blockUser(forPost post: ReaderPost, context: NSManagedObjectContext = ContextManager.shared.mainContext) {
postUserBlockingWillBeginNotification(post)
ReaderBlockUserAction(context: context).execute(with: post, blocked: true) { result in
switch result {
case .success:
ReaderHelpers.dispatchUserBlockedMessage(post: post, success: true)
case .failure:
ReaderHelpers.dispatchUserBlockedMessage(post: post, success: false)
}
postUserBlockingDidFinishNotification(post, result: result)
}
}
// MARK: Helpers
private func postSiteBlockingWillBeginNotification(_ post: ReaderPost) {
NotificationCenter.default.post(name: .ReaderSiteBlockingWillBegin, object: nil, userInfo: [ReaderNotificationKeys.post: post])
}
/// Notify Reader Cards Stream so the post card is updated.
private func postSiteBlockingDidFinish(_ post: ReaderPost) {
NotificationCenter.default.post(name: .ReaderSiteBlocked, object: nil, userInfo: [ReaderNotificationKeys.post: post])
}
private func postSiteBlockingDidFail(_ post: ReaderPost, error: Error?) {
var userInfo: [String: Any] = [ReaderNotificationKeys.post: post]
if let error {
userInfo[ReaderNotificationKeys.error] = error
}
NotificationCenter.default.post(name: .ReaderSiteBlockingFailed, object: nil, userInfo: userInfo)
}
private func postUserBlockingWillBeginNotification(_ post: ReaderPost) {
NotificationCenter.default.post(name: .ReaderUserBlockingWillBegin, object: nil, userInfo: [ReaderNotificationKeys.post: post])
}
private func postUserBlockingDidFinishNotification(_ post: ReaderPost, result: Result<Void, Error>) {
let center = NotificationCenter.default
let userInfo: [String: Any] = [ReaderNotificationKeys.post: post, ReaderNotificationKeys.result: result]
center.post(name: .ReaderUserBlockingDidEnd, object: nil, userInfo: userInfo)
}
}