Skip to content

Commit

Permalink
refactor: Stop using computed properties for DateSection
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Mar 30, 2023
1 parent a3f662c commit 527c303
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions Mail/Views/Thread List/ThreadListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,40 @@ class DateSection: Identifiable {
return .init(start: date.startOfMonth, end: date.endOfMonth)
}
}
}

var id: DateInterval { referenceDate.dateInterval }

var title: String {
switch referenceDate {
case .today:
return MailResourcesStrings.Localizable.threadListSectionToday
case .yesterday:
return MailResourcesStrings.Localizable.messageDetailsYesterday
case .thisWeek:
return MailResourcesStrings.Localizable.threadListSectionThisWeek
case .lastWeek:
return MailResourcesStrings.Localizable.threadListSectionLastWeek
case .thisMonth:
return MailResourcesStrings.Localizable.threadListSectionThisMonth
case .older(let date):
var formatStyle = Date.FormatStyle.dateTime.month(.wide)
if !Calendar.current.isDate(date, equalTo: .now, toGranularity: .year) {
formatStyle = formatStyle.year()
public var title: String {
switch self {
case .today:
return MailResourcesStrings.Localizable.threadListSectionToday
case .yesterday:
return MailResourcesStrings.Localizable.messageDetailsYesterday
case .thisWeek:
return MailResourcesStrings.Localizable.threadListSectionThisWeek
case .lastWeek:
return MailResourcesStrings.Localizable.threadListSectionLastWeek
case .thisMonth:
return MailResourcesStrings.Localizable.threadListSectionThisMonth
case .older(let date):
var formatStyle = Date.FormatStyle.dateTime.month(.wide)
if !Calendar.current.isDate(date, equalTo: .now, toGranularity: .year) {
formatStyle = formatStyle.year()
}
return date.formatted(formatStyle).capitalized
}
return date.formatted(formatStyle).capitalized
}
}

let id: DateInterval
let title: String
var threads = [Thread]()

private let referenceDate: ReferenceDate

init(thread: Thread) {
let sections: [ReferenceDate] = [.today, .yesterday, .thisWeek, .lastWeek, .thisMonth]
referenceDate = sections.first { $0.dateInterval.contains(thread.date) } ?? .older(thread.date)
id = referenceDate.dateInterval
title = referenceDate.title
}

func threadBelongsToSection(thread: Thread) -> Bool {
Expand Down

0 comments on commit 527c303

Please sign in to comment.