aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Models/NewsInfo.swift
blob: caf532fd815274efbf7b660765cd7112ff60402c (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import SwiftUI

enum NewsInfoType {
  case bundle
  case category
  case article
}

@Observable class NewsInfo: Identifiable, Hashable {
  let id: UUID = UUID()

  var name: String
  var count: UInt
  let type: NewsInfoType

  var categoryID: UUID?
  var articleID: UInt?
  var parentID: UInt?

  var path: [String]
  var expanded: Bool = false
  var children: [NewsInfo] = []

  var articleFlavors: [String]?
  var articleUsername: String?
  var articleDate: Date?
  var read: Bool = false

  var expandable: Bool {
    self.type == .bundle || self.type == .category || self.children.count > 0
  }

  var lookupPath: String? {
    switch self.type {
    case .bundle, .category:
      return "/\(self.path.joined(separator: "/"))"
    case .article:
      guard let aid = self.articleID else {
        return nil
      }
      //      if let pid = self.parentID, pid != 0 {
      //        return "/\(self.path.joined(separator: "/"))/\(pid)/\(aid)"
      //      }
      return "/\(self.path.joined(separator: "/"))/\(aid)"
    }
  }

  var parentArticleLookupPath: String? {
    switch self.type {
    case .bundle, .category:
      //      if self.path.count <= 1 {
      //        return "/"
      //      }
      //      let parentPath = self.path[0..<self.path.count-1]
      //      return "/\(parentPath.joined(separator: "/"))"
      return nil
    case .article:
      guard let pid = self.parentID, pid != 0 else {
        return nil
      }
      return "/\(self.path.joined(separator: "/"))/\(pid)"
    }
  }

  init(hotlineNewsCategory: HotlineNewsCategory) {
    self.categoryID = hotlineNewsCategory.id
    self.articleID = nil
    self.parentID = 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.parentID = hotlineNewsArticle.parentID == 0 ? nil : UInt(hotlineNewsArticle.parentID)
    print(hotlineNewsArticle.parentID)
    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
  }
}