-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHomeNewsCellItem.swift
More file actions
executable file
·45 lines (38 loc) · 1.47 KB
/
HomeNewsCellItem.swift
File metadata and controls
executable file
·45 lines (38 loc) · 1.47 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
//
// HomeNewsCellItem.swift
// PennMobile
//
// Created by Josh Doman on 2/7/19.
// Copyright © 2019 PennLabs. All rights reserved.
//
import Foundation
final class HomeNewsCellItem: HomeCellItem {
static var jsonKey = "news"
static var associatedCell: ModularTableViewCell.Type = HomeNewsCell.self
let article: NewsArticle
var showSubtitle = true
init(for article: NewsArticle) {
self.article = article
}
static func getHomeCellItem(_ completion: @escaping (([HomeCellItem]) -> Void)) {
let task = URLSession.shared.dataTask(with: URL(string: "https://labs-graphql-295919.ue.r.appspot.com/graphql?query=%7BlabsArticle%7Bslug,headline,abstract,published_at,authors%7Bname%7D,dominantMedia%7BimageUrl,authors%7Bname%7D%7D,tag,content%7D%7D")!) { data, _, _ in
guard let data = data else { completion([]); return }
if let article = try? JSONDecoder().decode(NewsArticle.self, from: data) {
completion([HomeNewsCellItem(for: article)])
} else {
completion([])
}
}
task.resume()
}
func equals(item: ModularTableViewItem) -> Bool {
guard let item = item as? HomeNewsCellItem else { return false }
return article.data.labsArticle.headline == item.article.data.labsArticle.headline
}
}
// MARK: - Logging ID
extension HomeNewsCellItem: LoggingIdentifiable {
var id: String {
return article.data.labsArticle.slug
}
}