From 4a4fab030b69ec799fae42f9a444392e7be7a43a Mon Sep 17 00:00:00 2001 From: Dustin Mierau Date: Fri, 10 May 2024 15:24:50 -0700 Subject: First crack at threading news. Added read state to news items. Playing with some icon visual styles. --- Hotline/Models/Hotline.swift | 36 ++++++++++++++++++++++++++++++++---- Hotline/Models/NewsInfo.swift | 1 + 2 files changed, 33 insertions(+), 4 deletions(-) (limited to 'Hotline/Models') 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 -- cgit