aboutsummaryrefslogtreecommitdiff
path: root/Hotline/iOS/NewsView.swift
blob: 72dd0341242a11da3500ecde894c42403697f6c8 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import SwiftUI
import UniformTypeIdentifiers

struct NewsItemView: View {
  @Environment(Hotline.self) private var model: Hotline
  @Environment(NewsItemSelection.self) private var selectedArticle: NewsItemSelection

  let news: NewsInfo

  static var dateFormatter: DateFormatter = {
    var formatter = DateFormatter()
    formatter.dateFormat = "MM/dd/yy, h:mm a"
    formatter.timeZone = .gmt
    return formatter
  }()

  static var relativeDateFormatter: RelativeDateTimeFormatter = {
    var formatter = RelativeDateTimeFormatter()
    formatter.unitsStyle = .abbreviated
    formatter.dateTimeStyle = .numeric
    formatter.formattingContext = .listItem
    return formatter
  }()

  @State var expanded = false

  var body: some View {
    if news.count > 0 {
      DisclosureGroup(isExpanded: $expanded) {
        ForEach(news.children) { childNews in
          NewsItemView(news: childNews)
            .frame(height: 38)
            .environment(self.selectedArticle)
        }
      } label: {
        HStack {
          Text(news.name)
            .fontWeight(.bold)
            .lineLimit(1)
            .truncationMode(.tail)
          Spacer()
          if news.count > 0 {
            Text("\(news.count)")
              .foregroundStyle(.secondary)
          }
        }
      }
      .onChange(of: expanded) {
        if !expanded {
          return
        }

        Task {
          await model.getNewsList(at: news.path)
        }
      }
    } else {
      HStack(alignment: .firstTextBaseline) {
        Text(Image(systemName: "quote.opening"))
        Text(news.name)
          .lineLimit(1)
          .truncationMode(.tail)
        Spacer()
        if news.count > 0 {
          Text("\(news.count)")
            .lineLimit(1)
            .foregroundStyle(.secondary)
        } else if let postAuthor = news.articleUsername {
          //          Text("\(NewsItemView.relativeDateFormatter.localizedString(for: postDate, relativeTo: Date.now))")
          Text(postAuthor)
            .foregroundStyle(.secondary)
            .truncationMode(.tail)
            //            .frame(maxWidth: 50)
            .font(.caption)
            .lineLimit(1)
        }
      }
      .onTapGesture {
        if news.type == .article {
          print("SELECTED", news.name)
          selectedArticle.selectedArticle = news
        }
      }
    }
  }
}

@Observable
class NewsItemSelection: Equatable {
  var selectedArticle: NewsInfo? = nil

  static func == (lhs: NewsItemSelection, rhs: NewsItemSelection) -> Bool {
    return lhs.selectedArticle == rhs.selectedArticle
  }
}

struct NewsView: View {
  @Environment(Hotline.self) private var model: Hotline
  @Environment(\.colorScheme) var colorScheme

  @State private var fetched = false
  @State private var selectedCategory: NewsInfo? = nil
  @State private var topListHeight: CGFloat = 280
  @State private var dividerHeight: CGFloat = 30

  @State private var articleSelection = NewsItemSelection()
  @State private var articleText = ""

  //  @State private var selectedArticleID: UInt?

  var articleList: some View {
    VStack(spacing: 0) {
      if model.news.count == 0 {
        Text("No News Available")
          .font(.headline)
          .opacity(0.3)
      } else {
        List(model.news) { category in
          NewsItemView(news: category)
            .frame(height: 38)
            .environment(self.articleSelection)
        }
        .scrollBounceBehavior(.basedOnSize)
      }
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Color(uiColor: .systemGroupedBackground))

    //    .listStyle(.plain)
  }

  var body: some View {
    NavigationStack {
      VStack(spacing: 0) {
        articleList
          .frame(height: topListHeight)
          .frame(minHeight: topListHeight)
          .onChange(of: self.articleSelection.selectedArticle) {
            self.articleText = ""
            if let article = self.articleSelection.selectedArticle,
              let articleFlavor = article.articleFlavors?.first,
              let articleID = article.articleID
            {
              Task {
                if let articleText = await self.model.getNewsArticle(
                  id: articleID, at: article.path, flavor: articleFlavor)
                {
                  self.articleText = articleText
                }
              }
            }
            //            print("SELECTED ARTICLE", articleSelection.selectedArticle?.name)
          }

        // Movable Divider
        VStack(alignment: .center) {
          Divider()
          Spacer()
          HStack(alignment: .center) {
            Rectangle()
              .fill(.tertiary)
              .frame(width: 50, height: 6, alignment: .center)
              .cornerRadius(10)
          }
          Spacer()
        }
        .background(
          colorScheme == .dark ? Color(white: 0.1) : Color(uiColor: UIColor.systemBackground)
        )
        .frame(maxWidth: .infinity)
        .frame(height: dividerHeight)
        .gesture(
          DragGesture()
            .onChanged { gesture in
              let delta = gesture.translation.height
              topListHeight = max(min(topListHeight + delta, 500), 50)
            }
        )

        // Reader View
        ScrollView(.vertical) {
          VStack(alignment: .leading) {

            if let news = self.articleSelection.selectedArticle {
              if let poster = news.articleUsername, let postDate = news.articleDate {
                HStack(alignment: .firstTextBaseline) {
                  Text(poster)
                    .foregroundStyle(.secondary)
                    .font(.subheadline)
                    .lineLimit(1)
                    .truncationMode(.tail)
                    .textSelection(.enabled)
                  //                    .padding()
                  Spacer()
                  Text("\(NewsItemView.dateFormatter.string(from: postDate))")
                    .foregroundStyle(.secondary)
                    .font(.subheadline)
                    .lineLimit(1)
                    .textSelection(.enabled)
                }
                .padding(.bottom, 8)

                Divider()
                  .padding(.bottom, 16)
              }

              Text(news.name).font(.title3).fontWeight(.medium)
                .textSelection(.enabled)
                .padding(.bottom, 8)
            }

            Text(self.articleText)
              .multilineTextAlignment(.leading)
              .textSelection(.enabled)
          }
          .frame(maxWidth: .infinity)
          .padding()
        }
        .scrollBounceBehavior(.basedOnSize)
        .background(
          colorScheme == .dark ? Color(white: 0.1) : Color(uiColor: UIColor.systemBackground))
      }
      .task {
        if !fetched {
          await model.getNewsList()
          fetched = true
        }
      }
      .navigationBarTitleDisplayMode(.inline)
      .toolbar {
        ToolbarItem(placement: .principal) {
          Text(model.serverTitle)
            .font(.headline)
        }
        ToolbarItem(placement: .navigationBarLeading) {
          Button {
            model.disconnect()
          } label: {
            Text(Image(systemName: "xmark.circle.fill"))
              .symbolRenderingMode(.hierarchical)
              .font(.title2)
              .foregroundColor(.secondary)
          }
        }
      }
    }
  }
}

#Preview {
  MessageBoardView()
    .environment(Hotline(trackerClient: HotlineTrackerClient(), client: HotlineClient()))
}