From 66c454c1b56951838f9d689c1dab95580b326cfb Mon Sep 17 00:00:00 2001 From: Livid Date: Wed, 7 Feb 2024 02:40:10 -0800 Subject: [PATCH] Fix rebuild --- Planet/Entities/MyArticleModel+Save.swift | 8 ++- .../Entities/MyArticleModel+SavePublic.swift | 20 ++++++-- Planet/Entities/MyPlanetModel.swift | 51 +++++++++++++------ .../Views/Components/GIFIndicatorView.swift | 22 ++++++++ Planet/Views/My/MyArticleGridView.swift | 5 ++ Planet/versioning.xcconfig | 2 +- 6 files changed, 86 insertions(+), 22 deletions(-) diff --git a/Planet/Entities/MyArticleModel+Save.swift b/Planet/Entities/MyArticleModel+Save.swift index abd874a6..2d2d0977 100644 --- a/Planet/Entities/MyArticleModel+Save.swift +++ b/Planet/Entities/MyArticleModel+Save.swift @@ -45,7 +45,7 @@ extension MyArticleModel { // MARK: - Save to Public/:planet_id/:article_id/index.html /// Save the article into UUID/index.html along with its attachments. - func savePublic() throws { + func savePublic(usingTasks: Bool = false) throws { let started: Date = Date() var marks: OrderedDictionary = ["Started": started] @@ -79,7 +79,7 @@ extension MyArticleModel { // MARK: - Render Markdown - try processArticleHTML() + try processArticleHTML(usingTasks: usingTasks) marks.recordEvent("ArticleHTML", for: self.title) // MARK: - Hero Grid @@ -317,6 +317,10 @@ extension MyArticleModel { debugPrint("HeroImage: candidate size: \(image.size)") if image.size.width >= 600 && image.size.height >= 400 { debugPrint("HeroImage: \(item)") + DispatchQueue.main.async { + self.heroImage = firstImage + try? self.save() + } return item } } diff --git a/Planet/Entities/MyArticleModel+SavePublic.swift b/Planet/Entities/MyArticleModel+SavePublic.swift index 574bd498..13a55c43 100644 --- a/Planet/Entities/MyArticleModel+SavePublic.swift +++ b/Planet/Entities/MyArticleModel+SavePublic.swift @@ -224,18 +224,30 @@ extension MyArticleModel { } /// Render article HTML - func processArticleHTML() throws { + func processArticleHTML(usingTasks: Bool = false) throws { guard let template = planet.template else { throw PlanetError.MissingTemplateError } - Task(priority: .userInitiated) { + if (usingTasks) { + Task(priority: .userInitiated) { + let articleHTML = try template.render(article: self) + try articleHTML.data(using: .utf8)?.write(to: publicIndexPath) + debugPrint("HTML for \(self.title) saved to \(publicIndexPath.path)") + } + + Task(priority: .userInitiated) { + if template.hasSimpleHTML { + let simpleHTML = try template.render(article: self, forSimpleHTML: true) + try simpleHTML.data(using: .utf8)?.write(to: publicSimplePath) + debugPrint("Simple HTML for \(self.title) saved to \(publicSimplePath.path)") + } + } + } else { let articleHTML = try template.render(article: self) try articleHTML.data(using: .utf8)?.write(to: publicIndexPath) debugPrint("HTML for \(self.title) saved to \(publicIndexPath.path)") - } - Task(priority: .userInitiated) { if template.hasSimpleHTML { let simpleHTML = try template.render(article: self, forSimpleHTML: true) try simpleHTML.data(using: .utf8)?.write(to: publicSimplePath) diff --git a/Planet/Entities/MyPlanetModel.swift b/Planet/Entities/MyPlanetModel.swift index 7f82043f..1c6d3524 100644 --- a/Planet/Entities/MyPlanetModel.swift +++ b/Planet/Entities/MyPlanetModel.swift @@ -1399,6 +1399,15 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl let hasPodcastCoverArt = FileManager.default.fileExists( atPath: publicPodcastCoverArtPath.path ) + + // MARK: - Render RSS and podcast RSS + renderRSS(podcastOnly: false) + + if publicPlanet.hasAudioContent() { + renderRSS(podcastOnly: true) + } + reduceRebuildTasks() + // MARK: - Render index.html and pages let itemsPerPage = template.idealItemsPerPage ?? 10 let generateIndexPagination = template.generateIndexPagination ?? false @@ -1458,6 +1467,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl let indexHTML = try template.renderIndex(context: pageContext) try indexHTML.data(using: .utf8)?.write(to: publicIndexPath) } + reduceRebuildTasks() // MARK: - Render tags if let generateTagPages = template.generateTagPages, generateTagPages { @@ -1520,6 +1530,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl else { debugPrint("Skip generating tags for planet \(name)") } + reduceRebuildTasks() // MARK: - Render archive.html if let generateArchive = template.generateArchive, generateArchive { @@ -1556,13 +1567,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl else { debugPrint("Skip generating archive for planet \(name)") } - - // MARK: - Render RSS and podcast RSS - renderRSS(podcastOnly: false) - - if publicPlanet.hasAudioContent() { - renderRSS(podcastOnly: true) - } + reduceRebuildTasks() // MARK: - Save planet.json let info = try JSONEncoder.shared.encode(publicPlanet) @@ -1890,7 +1895,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl // heaviest task is generating thumbnails - // try self.articles.forEach { try $0.savePublic() } + // try self.articles.forEach { try $0.savePublic(usingTasks: true) } do { // split the articles into groups @@ -1904,7 +1909,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl DispatchQueue.concurrentPerform(iterations: articleGroup.count) { index in group.enter() do { - try articleGroup[index].savePublic() + try articleGroup[index].savePublic(usingTasks: true) group.leave() } catch { @@ -1919,7 +1924,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl try await withThrowingTaskGroup(of: Void.self) { group in for article in articleGroup { group.addTask(priority: .high) { - try article.savePublic() + try article.savePublic(usingTasks: true) } } try await group.waitForAll() @@ -1956,6 +1961,25 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl await sendNotificationForRebuild() } + func quickRebuildTaskCount() -> Int { + let copyTemplateAssetsTask = 1 + let pagesTask = 1 + let tagsTask = 1 + let archiveTask = 1 + let rssTask = 1 + return copyTemplateAssetsTask + pagesTask + tagsTask + archiveTask + rssTask + } + + func reduceRebuildTasks() { + Task { @MainActor in + let before = PlanetStore.shared.rebuildTasks + PlanetStore.shared.rebuildTasks -= 1 + let after = PlanetStore.shared.rebuildTasks + debugPrint("Rebuild tasks reduced from \(before) to \(after) at \(Date())") + NotificationCenter.default.post(name: .myArticleBuilt, object:nil) + } + } + func quickRebuild() async throws { let started = Date() await MainActor.run { @@ -1969,14 +1993,11 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl } Task { @MainActor in PlanetStore.shared.isRebuilding = true - PlanetStore.shared.rebuildTasks = 2 + PlanetStore.shared.rebuildTasks = quickRebuildTaskCount() PlanetStatusManager.shared.updateStatus() } try self.copyTemplateAssets() - Task { @MainActor in - PlanetStore.shared.rebuildTasks = 1 - NotificationCenter.default.post(name: .myArticleBuilt, object:nil) - } + reduceRebuildTasks() try await self.savePublic() await MainActor.run { self.isRebuilding = false diff --git a/Planet/Views/Components/GIFIndicatorView.swift b/Planet/Views/Components/GIFIndicatorView.swift index 37bcc339..8b9d7bec 100644 --- a/Planet/Views/Components/GIFIndicatorView.swift +++ b/Planet/Views/Components/GIFIndicatorView.swift @@ -28,3 +28,25 @@ struct GIFIndicatorView: View { } } } + + +struct VideoIndicatorView: View { + var body: some View { + VStack { + Spacer() + HStack { + Text("MP4") + .font(.system(size: 10, weight: .semibold, design: .monospaced)) + .foregroundColor(.white.opacity(0.85)) + .padding(.horizontal, 4) + .padding(.vertical, 2) + .background(Color.secondary.opacity(0.75)) + .cornerRadius(4) + .multilineTextAlignment(.center) + Spacer() + } + .padding(.leading, 4) + .padding(.bottom, 4) + } + } +} diff --git a/Planet/Views/My/MyArticleGridView.swift b/Planet/Views/My/MyArticleGridView.swift index 34967272..193cca05 100644 --- a/Planet/Views/My/MyArticleGridView.swift +++ b/Planet/Views/My/MyArticleGridView.swift @@ -186,6 +186,11 @@ struct MyArticleGridView: View { .shadow(color: Color.black.opacity(0.1), radius: 2, x: 0, y: 1) if article.hasGIF { GIFIndicatorView() + } + else if article.hasVideo { + VideoIndicatorView() + } else { + } if article.attachmentURLs.count > 1 { GroupIndicatorView() diff --git a/Planet/versioning.xcconfig b/Planet/versioning.xcconfig index 9e2ccd04..dd3aee61 100644 --- a/Planet/versioning.xcconfig +++ b/Planet/versioning.xcconfig @@ -1 +1 @@ -CURRENT_PROJECT_VERSION = 1858 +CURRENT_PROJECT_VERSION = 1859