diff options
| author | Dustin Mierau <dustin@mierau.me> | 2024-05-10 15:24:50 -0700 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2024-05-10 15:24:50 -0700 |
| commit | 4a4fab030b69ec799fae42f9a444392e7be7a43a (patch) | |
| tree | 0db4b668f8ee5cc31527f27a240423e32bc8bc35 /Hotline/Models | |
| parent | b5c2a0f619f645412780d873f17c4333c787ae30 (diff) | |
First crack at threading news. Added read state to news items. Playing with some icon visual styles.
Diffstat (limited to 'Hotline/Models')
| -rw-r--r-- | Hotline/Models/Hotline.swift | 36 | ||||
| -rw-r--r-- | Hotline/Models/NewsInfo.swift | 1 |
2 files changed, 33 insertions, 4 deletions
diff --git a/Hotline/Models/Hotline.swift b/Hotline/Models/Hotline.swift index 598c787..48266a9 100644 --- a/Hotline/Models/Hotline.swift +++ b/Hotline/Models/Hotline.swift @@ -345,30 +345,58 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate { self?.client.sendGetNewsArticles(path: path) { [weak self] articles in // let parentNews = self?.findNews(in: self?.news ?? [], at: path) print("GENERATING NEWS") - + var newArticles: [NewsInfo] = [] for article in articles { newArticles.append(NewsInfo(hotlineNewsArticle: article)) } + let organizedNewsArticles: [NewsInfo] = self?.organizeNewsList(newArticles) ?? [] + DispatchQueue.main.async { if let parent = existingNewsItem { print("UNDER PARENT:", parent.name) - parent.children = newArticles + parent.children = organizedNewsArticles print(parent.children) } else if path.isEmpty { - self?.news = newArticles + self?.news = organizedNewsArticles } - continuation.resume(returning: newArticles) + continuation.resume(returning: organizedNewsArticles) } } } } } + func organizeNewsList(_ news: [NewsInfo]) -> [NewsInfo] { + var articleMap: [UInt: NewsInfo] = [:] + + // Create lookup table of each news item. + for article in news { + if let articleID = article.articleID { + articleMap[articleID] = article + } + } + + // Place articles under their parent. + var organized: [NewsInfo] = [] + for article in news { + if let parentID = article.parentID, + parentID != 0, + let parentArticle = articleMap[parentID] { + parentArticle.children.append(article) + } + else { + organized.append(article) + } + } + + return organized + } + @MainActor func getNewsCategories(at path: [String] = []) async -> [NewsInfo] { return await withCheckedContinuation { [weak self] continuation in self?.client.sendGetNewsCategories(path: path) { [weak self] categories in diff --git a/Hotline/Models/NewsInfo.swift b/Hotline/Models/NewsInfo.swift index f95cba1..b635138 100644 --- a/Hotline/Models/NewsInfo.swift +++ b/Hotline/Models/NewsInfo.swift @@ -24,6 +24,7 @@ enum NewsInfoType { var articleFlavors: [String]? var articleUsername: String? var articleDate: Date? + var read: Bool = false init(hotlineNewsCategory: HotlineNewsCategory) { self.categoryID = hotlineNewsCategory.id |