aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/News/NewsItemView.swift
blob: fc20e616d7e098b27a4d72ac862b894b40a841fd (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import SwiftUI

struct NewsItemView: View {
  @Environment(Hotline.self) private var model: Hotline
  
  var news: NewsInfo
  let depth: Int
  
  static var dateFormatter: DateFormatter = {
    var dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .short
    dateFormatter.timeZone = .gmt
    return dateFormatter
  }()
  
  static var relativeDateFormatter: RelativeDateTimeFormatter = {
    var formatter = RelativeDateTimeFormatter()
    formatter.unitsStyle = .full
    formatter.dateTimeStyle = .named
    formatter.formattingContext = .listItem
    return formatter
  }()
  
  var body: some View {
    HStack(alignment: .center, spacing: 0) {
      if news.expandable {
        Button {
          news.expanded.toggle()
        } label: {
          Text(Image(systemName: news.expanded ? "chevron.down" : "chevron.right"))
            .bold()
            .font(.system(size: 10))
            .opacity(0.5)
            .frame(alignment: .center)
        }
        .buttonStyle(.plain)
        .frame(width: 10)
        .padding(.leading, 4)
        .padding(.trailing, 8)
      }
      else {
        Spacer()
          .frame(width: 10)
          .padding(.leading, 4)
          .padding(.trailing, 8)
      }
      
      // Tree indent
      Spacer()
        .frame(width: (CGFloat(depth) * 22))
      
      switch news.type {
      case .category:
        Image("News Category")
          .resizable()
          .frame(width: 16, height: 16, alignment: .center)
          .padding(.trailing, 6)
      case .bundle:
        Image("News Bundle")
          .resizable()
          .frame(width: 16, height: 16, alignment: .center)
          .padding(.trailing, 6)
      case .article:
        EmptyView()
      }
      
      Text(news.name)
        .fontWeight((news.type == .bundle || news.type == .category || !news.read) ? .semibold : .regular)
        .lineLimit(1)
        .truncationMode(.tail)
      
      if news.type == .article && news.articleUsername != nil {
        Text(news.articleUsername!)
          .foregroundStyle(.secondary)
          .lineLimit(1)
          .padding(.leading, 8)
      }
      
      Spacer()
      
      if news.type == .category && news.count > 0 {
        Text("^[\(news.count) Post](inflect: true)")
          .lineLimit(1)
          .foregroundStyle(.secondary)
          .padding(.trailing, 8)
      }
      else if news.type == .bundle && news.count > 0 {
        Text("^[\(news.count) Category](inflect: true)")
          .lineLimit(1)
          .foregroundStyle(.secondary)
          .padding(.trailing, 8)
      }
//      if news.type == .bundle {
//        Text("\(news.count)")
//          .lineLimit(1)
//          .foregroundStyle(.tertiary)
//          .padding(.trailing, 8)
        
//        ZStack {
//          Text("^[\(news.count) \(news.type == .bundle ? "Category" : "Post")](inflect: true)")
//          Text("\(news.count)")
//            .foregroundStyle(.clear)
////            .font(.caption)
//            .lineLimit(1)
//            .padding([.leading, .trailing], 8)
//            .padding([.top, .bottom], 2)
//            .background(.secondary)
//            .clipShape(Capsule())
//          
//          Text("\(news.count)")
//            .foregroundStyle(.white)
////            .font(.caption)
//            .lineLimit(1)
//            .padding([.leading, .trailing], 8)
//            .padding([.top, .bottom], 2)
//            .blendMode(.destinationOut)
//        }
//        .drawingGroup(opaque: false)
//      }
      else if news.type == .article && news.articleUsername != nil {
        if let d = news.articleDate {
          Text(NewsItemView.relativeDateFormatter.localizedString(for: d, relativeTo: Date.now))
            .lineLimit(1)
            .foregroundStyle(.secondary)
            .padding(.trailing, 8)
        }
      }
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .onChange(of: news.expanded) {
      guard news.expanded, news.type == .bundle || news.type == .category else {
        return
      }
      
      Task {
        await model.getNewsList(at: news.path)
      }
    }
    
    if news.expanded {
      ForEach(news.children.reversed(), id: \.self) { childNews in
        NewsItemView(news: childNews, depth: self.depth + 1).tag(childNews.id)
      }
    }
  }
}

#Preview {
  NewsItemView(news: NewsInfo(hotlineNewsArticle: HotlineNewsArticle(id: 0, parentID: 0, flags: 0, title: "Title", username: "username", date: Date.now, flavors: [("", 1)], path: ["Guest"])), depth: 0)
    .environment(Hotline(trackerClient: HotlineTrackerClient(), client: HotlineClient()))
}