Determine elements for news section

This commit is contained in:
Christoph Hagen 2022-12-01 15:39:39 +01:00
parent 92d832dc44
commit 6e24c27fdc
3 changed files with 41 additions and 1 deletions

View File

@ -126,6 +126,12 @@ struct Element {
*/ */
let headerType: HeaderType let headerType: HeaderType
/**
Indicate that the overview section should contain a `Newest Content` section before the other sections.
- Note: If not specified, this property defaults to `false`
*/
let showMostRecentSection: Bool
/** /**
The localized metadata for each language. The localized metadata for each language.
*/ */
@ -318,6 +324,17 @@ extension Element {
} }
} }
func mostRecentElements(_ count: Int) -> [Element] {
guard self.containsElements else {
return [self]
}
let all = shownItems
.reduce(into: [Element]()) { $0 += $1.mostRecentElements(count) }
.filter { $0.date != nil }
.sorted { $0.date! > $1.date! }
return Array(all.prefix(count))
}
var sortedItems: [Element] { var sortedItems: [Element] {
if useManualSorting { if useManualSorting {
return shownItems.sorted { $0.sortIndex! < $1.sortIndex! } return shownItems.sorted { $0.sortIndex! < $1.sortIndex! }

View File

@ -125,6 +125,12 @@ struct GenericMetadata {
*/ */
let headerType: String? let headerType: String?
/**
Indicate that the overview section should contain a `Newest Content` section before the other sections.
- Note: If not specified, this property defaults to `false`
*/
let showMostRecentSection: Bool?
/** /**
The localized metadata for each language. The localized metadata for each language.
*/ */
@ -150,6 +156,7 @@ extension GenericMetadata: Codable {
.useManualSorting, .useManualSorting,
.overviewItemCount, .overviewItemCount,
.headerType, .headerType,
.showMostRecentSection,
.languages, .languages,
] ]
} }
@ -221,6 +228,7 @@ extension GenericMetadata {
useManualSorting: false, useManualSorting: false,
overviewItemCount: 6, overviewItemCount: 6,
headerType: "left", headerType: "left",
showMostRecentSection: false,
languages: [.full]) languages: [.full])
} }
} }

View File

@ -15,6 +15,21 @@ struct OverviewSectionGenerator {
} }
func generate(sections: [Element], in parent: Element, language: String, sectionItemCount: Int) -> String { func generate(sections: [Element], in parent: Element, language: String, sectionItemCount: Int) -> String {
let content = sectionsContent(sections, in: parent, language: language, sectionItemCount: sectionItemCount)
if parent.showMostRecentSection {
let news = newsSectionContent(for: parent, language: language, sectionItemCount: sectionItemCount)
return news + "\n" + content
} else {
return content
}
}
private func newsSectionContent(for element: Element, language: String, sectionItemCount: Int) -> String {
let shownElements = element.mostRecentElements(sectionItemCount)
return ""
}
private func sectionsContent(_ sections: [Element], in parent: Element, language: String, sectionItemCount: Int) -> String {
sections.map { section in sections.map { section in
let metadata = section.localized(for: language) let metadata = section.localized(for: language)
let fullUrl = section.fullPageUrl(for: language) let fullUrl = section.fullPageUrl(for: language)