aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/News/NewsView.swift
blob: 00b65553e184e46f16f7a1b4d8a08c14abb4825d (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import MarkdownUI
import SplitView
import SwiftUI

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

  @State private var selection: NewsInfo?
  @State private var articleText: String?
  @State private var splitHidden: SideHolder = SideHolder(.bottom)
  @State private var splitFraction = FractionHolder.usingUserDefaults(0.25, key: "News Split Fraction")
  @State private var editorOpen: Bool = false
  @State private var replyOpen: Bool = false
  @State private var loading: Bool = false

  var body: some View {
    Group {
      if model.serverVersion < 151 {
        disabledNewsView
      }
      else if !model.newsLoaded {
        loadingIndicator
      }
      else if model.news.isEmpty {
        emptyNewsView
      }
      else {
        NavigationStack {
          VSplit(
            top: {
              newsBrowser
            },
            bottom: {
              articleViewer
            }
          )
          .fraction(splitFraction)
          .constraints(minPFraction: 0.1, minSFraction: 0.3)
          .hide(splitHidden)
          .styling(
            color: colorScheme == .dark ? .black : Splitter.defaultColor, inset: 0,
            visibleThickness: 0.5, invisibleThickness: 5, hideSplitter: true
          )
          .frame(maxWidth: .infinity, maxHeight: .infinity)
          .background(Color(nsColor: .textBackgroundColor))
        }
      }
    }
    .task {
      if !model.newsLoaded {
        loading = true
        try? await model.getNewsList()
        loading = false
      }
    }
    .sheet(isPresented: $editorOpen) {
    } content: {
      if let selection = selection {
        switch selection.type {
        case .article, .category:
          NewsEditorView(
            editorTitle: selection.path.last ?? "New Post", isReply: false, path: selection.path,
            parentID: 0)
        default:
          EmptyView()
        }
      } else {
        EmptyView()
      }
    }
    .sheet(isPresented: $replyOpen) {
    } content: {
      if let selection = selection, selection.type == .article {
        NewsEditorView(
          editorTitle: "Reply to \(selection.articleUsername ?? "Post")", isReply: true,
          path: selection.path, parentID: UInt32(selection.articleID!),
          title: selection.name.replyToString())
      } else {
        EmptyView()
      }
    }
    .toolbar {
      ToolbarItem {
        Button {
          if selection?.type == .category || selection?.type == .article {
            editorOpen = true
          }
        } label: {
          Image(systemName: "square.and.pencil")
        }
        .help("New post under current topic")
        .disabled(selection?.type != .category && selection?.type != .article)
      }
      
      ToolbarItem {
        Button {
          if selection?.type == .article {
            replyOpen = true
          }
        } label: {
          Image(systemName: "arrowshape.turn.up.left")
        }
        .help("Reply to selected post")
        .disabled(selection?.type != .article)
      }
      
      if #available(macOS 26.0, *) {
        ToolbarSpacer(.fixed)
      }
      
      ToolbarItem {
        Button {
//          if selection?.type == .category || selection?.type == .article {
//            editorOpen = true
//          }
        } label: {
          Image(systemName: "newspaper")
        }
        .help("Create a new topic")
      }
      
      ToolbarItem {
        Button {
//          if selection?.type == .category || selection?.type == .article {
//            editorOpen = true
//          }
        } label: {
          Image(systemName: "tray")
        }
        .help("Create a new category")
      }
      
      ToolbarItem {
        Button {
          loading = true
          if let selectionPath = selection?.path {
            Task {
              try? await model.getNewsList(at: selectionPath)
              loading = false
            }
          } else {
            Task {
              try? await model.getNewsList()
              loading = false
            }
          }
        } label: {
          Image(systemName: "arrow.clockwise")
        }
        .help("Reload selected topic")
        .disabled(loading)
      }
    }
  }
  
  private var disabledNewsView: some View {
    ContentUnavailableView {
      Label("No News", systemImage: "newspaper")
    } description: {
      Text("This server has turned off newsgroups")
    }
  }
  
  private var emptyNewsView: some View {
    ContentUnavailableView {
      Label("No News", systemImage: "newspaper")
    } description: {
      Text("This server has no newsgroups")
    }
  }
  
  private var newsBrowser: some View {
    List(model.news, id: \.self, selection: $selection) { newsItem in
      NewsItemView(news: newsItem, depth: 0).tag(newsItem.id)
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .environment(\.defaultMinListRowHeight, 28)
    .listStyle(.inset)
    .alternatingRowBackgrounds(.enabled)
    .contextMenu(forSelectionType: NewsInfo.self) { items in
      let selectedItem = items.first

      Button {
        if selectedItem?.type == .article {
          replyOpen = true
        }
      } label: {
        Label(
          "Reply to \(selectedItem?.articleUsername ?? "Post")",
          systemImage: "arrowshape.turn.up.left")
      }
      .disabled(selectedItem == nil || selectedItem?.type != .article)

    } primaryAction: { items in
      guard let clickedNews = items.first else {
        return
      }

      self.selection = clickedNews
      if clickedNews.type == .bundle || clickedNews.type == .category
        || clickedNews.children.count > 0
      {
        clickedNews.expanded.toggle()
      }
    }
    .onChange(of: selection) {
      self.articleText = nil
      if let article = selection, article.type == .article {
        article.read = true
        if let articleFlavor = article.articleFlavors?.first,
          let articleID = article.articleID
        {
          Task {
            if let articleText = try? await self.model.getNewsArticle(id: articleID, at: article.path, flavor: articleFlavor) {
              self.articleText = articleText
            }
          }
          if self.splitHidden.side != nil {
            withAnimation(.easeOut(duration: 0.15)) {
              self.splitHidden.side = nil
            }
          }

        }
      } else {
        if self.splitHidden.side != .bottom {
          withAnimation(.easeOut(duration: 0.25)) {
            self.splitHidden.side = .bottom
          }
        }
      }
    }
    .onKeyPress(.rightArrow) {
      if let s = selection, s.expandable {
        s.expanded = true
        return .handled
      }
      return .ignored
    }
    .onKeyPress(.leftArrow) {
      if let s = selection, s.expandable {
        s.expanded = false
        return .handled
      }
      return .ignored
    }
  }
  
  private var loadingIndicator: some View {
    VStack {
      HStack {
        ProgressView {
          Text("Loading Newsgroups")
        }
        .controlSize(.regular)
      }
    }
    .frame(maxWidth: .infinity)
  }
  
  private var articleViewer: some View {
    ScrollView {
      VStack(alignment: .leading, spacing: 0) {
        if let selection = selection, selection.type == .article {
          if let poster = selection.articleUsername, let postDate = selection.articleDate {
            HStack(alignment: .firstTextBaseline) {
              Text(poster)
                .foregroundStyle(.secondary)
                .lineLimit(1)
                .truncationMode(.tail)
                .textSelection(.enabled)
                .padding(.bottom, 16)
              Spacer()
              Text("\(NewsItemView.dateFormatter.string(from: postDate))")
                .foregroundStyle(.secondary)
                .lineLimit(1)
                .truncationMode(.tail)
                .textSelection(.enabled)
                .padding(.bottom, 16)
            }
          }

          Divider()

          Text(selection.name).font(.title)
            .textSelection(.enabled)
            .padding(.bottom, 8)
            .padding(.top, 16)

          if let newsText = self.articleText {
            Markdown(newsText)
              .markdownTheme(.basic)
              .textSelection(.enabled)
              .lineSpacing(6)
              .padding(.top, 16)
          }
        }
        else {
          EmptyView()
//          HStack(alignment: .center) {
//            Spacer()
//            HStack(alignment: .center, spacing: 8) {
//              Text("Select a news article to read")
//                .foregroundStyle(.tertiary)
//                .font(.subheadline)
//            }
//            Spacer()
//          }
//          .padding()
//          .padding(.top, 48)
        }
      }
      .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
      .padding()
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .transition(.move(edge: .bottom))
  }
}

#Preview {
  NewsView()
    .environment(HotlineState())
}