This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathModMailContributionLoader.swift
79 lines (68 loc) · 2.66 KB
/
ModMailContributionLoader.swift
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// InboxContributionLoader.swift
// Slide for Reddit
//
// Created by Carlos Crane on 1/22/17.
// Copyright © 2017 Haptic Apps. All rights reserved.
//
import CoreData
import Foundation
import reddift
class ModMailContributionLoader: ContributionLoader {
func reset() {
content = []
}
var color: UIColor
var unread = false
var canGetMore = true
var subreddit: String
init(_ unread: Bool, sub: String) {
self.unread = unread
self.subreddit = sub
paginator = Paginator()
content = []
color = ColorUtil.getColorForSub(sub: "")
}
var paginator: Paginator
var content: [RedditObject]
weak var delegate: ContentListingViewController?
var paging = true
func getData(reload: Bool) {
if delegate != nil {
do {
if reload {
paginator = Paginator()
}
try delegate?.session?.getModMail(paginator, unread, completion: { (result) in
switch result {
case .failure(let error):
print(error)
case .success(let listing):
if reload {
self.content = []
}
let before = self.content.count
for message in listing.children.compactMap({ $0 }) {
self.content.append(MessageObject.messageToMessageObject(message: message as! Message))
if (message as! Message).baseJson["replies"] != nil {
let json = (message as! Message).baseJson as JSONDictionary
if let j = json["replies"] as? JSONDictionary, let data = j["data"] as? JSONDictionary, let things = data["children"] as? JSONArray {
for thing in things {
self.content.append(MessageObject.messageToMessageObject(message: Message.init(json: (thing as! JSONDictionary)["data"] as! JSONDictionary)))
}
}
}
}
self.paginator = listing.paginator
self.canGetMore = self.paginator.hasMore()
DispatchQueue.main.async {
self.delegate?.doneLoading(before: before, filter: false)
}
}
})
} catch {
print(error)
}
}
}
}