aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Models/NewsInfo.swift
blob: daab71f9bdb2770dabb9a2de211efcf46ed6b816 (plain)
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
import SwiftUI

enum NewsInfoType {
  case bundle
  case category
  case article
}

@Observable class NewsInfo: Identifiable, Hashable {
  let id: UUID = UUID()
  
  let name: String
  let count: UInt
  let type: NewsInfoType
  
  let categoryID: UUID?
  let articleID: UInt?
  
  let path: [String]
  var expanded: Bool = false
  var children: [NewsInfo] = []
  
  var articleFlavors: [String]?
  var articleUsername: String?
  var articleDate: Date?
  
  init(hotlineNewsCategory: HotlineNewsCategory) {
    self.categoryID = hotlineNewsCategory.id
    self.articleID = nil
    self.name = hotlineNewsCategory.name
    self.count = UInt(hotlineNewsCategory.count)
    self.path = hotlineNewsCategory.path
    
    if hotlineNewsCategory.type == 2 {
      self.type = .bundle
    }
    else {
      self.type = .category
    }
  }
  
  init(hotlineNewsArticle: HotlineNewsArticle) {
    self.articleID = UInt(hotlineNewsArticle.id)
    self.categoryID = nil
    self.name = hotlineNewsArticle.title
    self.count = 0
//    self.count = UInt(hotlineNewsArticle.count)
    self.path = hotlineNewsArticle.path
    self.type = .article
    
    self.articleFlavors = hotlineNewsArticle.flavors.map { $0.0 }
    self.articleUsername = hotlineNewsArticle.username
    self.articleDate = hotlineNewsArticle.date
  }
  
  func hash(into hasher: inout Hasher) {
    hasher.combine(self.id)
  }
  
  static func == (lhs: NewsInfo, rhs: NewsInfo) -> Bool {
    return lhs.id == rhs.id
  }
}