diff options
author | Nicholas Tay <nick@windblume.net> | 2022-03-22 14:14:02 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-03-22 14:14:20 +1100 |
commit | dac7f928d2a1ed555aeedd97ec9433e266f47cf1 (patch) | |
tree | f8fc1f1d257d55e6f29f3e49940c9ffdbe7e6e20 /foray | |
parent | d776ba9c6ec94d6622e1f759c112fd2334b7fb8b (diff) | |
download | forayios-dac7f928d2a1ed555aeedd97ec9433e266f47cf1.tar.gz forayios-dac7f928d2a1ed555aeedd97ec9433e266f47cf1.tar.bz2 forayios-dac7f928d2a1ed555aeedd97ec9433e266f47cf1.zip |
Adjust YearSection to just Int
Was a bit awkward to have it as a Date normalised to first day of the
year
Diffstat (limited to '')
-rw-r--r-- | foray/ForayItems.swift | 2 | ||||
-rw-r--r-- | foray/ForayTableViewController.swift | 16 |
2 files changed, 6 insertions, 12 deletions
diff --git a/foray/ForayItems.swift b/foray/ForayItems.swift index f1a1089..a8786ee 100644 --- a/foray/ForayItems.swift +++ b/foray/ForayItems.swift @@ -13,7 +13,7 @@ enum ItemType: String, Decodable { } struct YearSection { - var year: Date + var year: Int var items: [PenguinItem] } diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index 2b7ba65..37ac2e5 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -7,12 +7,6 @@ import UIKit -private func firstDayOfYear(date: Date) -> Date { - let calendar = Calendar.current - let components = calendar.dateComponents([.year], from: date) - return calendar.date(from: components)! -} - class ForayTableViewController: UITableViewController, ForayCoordinated { var coordinator: ForayCoordinator? @@ -49,10 +43,13 @@ class ForayTableViewController: UITableViewController, ForayCoordinated { url: "https://users.windblume.net/~nick/upload/dummy.json", onComplete: { (apiItems: [PenguinItem]) in var items = apiItems + + // Show items in chronological order within sections items.sort { (lhs, rhs) in lhs.releaseDate < rhs.releaseDate } + // Group by year sections let groups = Dictionary(grouping: apiItems) { (item) in - return firstDayOfYear(date: item.releaseDate) + return Calendar.current.component(.year, from: item.releaseDate) } self.sections = groups.map { (key, values) in return YearSection(year: key, items: values) @@ -98,10 +95,7 @@ class ForayTableViewController: UITableViewController, ForayCoordinated { } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - let section = self.sections[section] - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy" - return "Released in " + dateFormatter.string(from: section.year) + return "Released in \(self.sections[section].year)" } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |