# HG changeset patch # User Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> # Date 1612722604 -3600 # Node ID 57d22236ccbd75c0b47af630c474971c5f02c41a # Parent 5110adf17b2279f19febe8a7b9836ebe5ab9ad49 Add date to HistoryList, and sorted that list by date. The new ones come first diff -r 5110adf17b22 -r 57d22236ccbd LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed diff -r 5110adf17b22 -r 57d22236ccbd lazybear/Views/Company.swift --- a/lazybear/Views/Company.swift Sun Feb 07 17:05:18 2021 +0100 +++ b/lazybear/Views/Company.swift Sun Feb 07 19:30:04 2021 +0100 @@ -22,7 +22,8 @@ } else if viewSelected == 1 { Transactions(symbol: symbol) } - } .onAppear { saveSearch(name: name, symbol: symbol) } + } + .onAppear { saveSearch(name: name, symbol: symbol) } .navigationBarTitle(symbol, displayMode: .inline) .navigationBarItems(trailing: diff -r 5110adf17b22 -r 57d22236ccbd lazybear/Views/CompanyRow.swift --- a/lazybear/Views/CompanyRow.swift Sun Feb 07 17:05:18 2021 +0100 +++ b/lazybear/Views/CompanyRow.swift Sun Feb 07 19:30:04 2021 +0100 @@ -17,15 +17,36 @@ let symbol = company?.symbol ?? history?.symbol ?? "" NavigationLink(destination: Company(name: name, symbol: symbol)) { - VStack(alignment: .leading) { - Text(symbol.uppercased()) - .fontWeight(.semibold) + HStack { + VStack(alignment: .leading) { + Text(symbol.uppercased()) + .fontWeight(.semibold) + + Text(name.capitalized) + .font(.subheadline) + } - Text(name.capitalized) - .font(.subheadline) + if history != nil { + Spacer() + let (day, month) = formatDate() + Text("\(day) \(month)") + .font(.caption) + } } } } + + private func formatDate() -> (String, String) { + let date = history?.date ?? Date() + let calendar = Calendar.current + let components = calendar.dateComponents([.day, .month], from: date) + + let day = components.day ?? 0 // Get day number + let month = components.month ?? 0 // Get month number + let monthString = calendar.monthSymbols[month] // Get month with letters + + return (String(day), monthString) + } } struct CompanyRown_Previews: PreviewProvider { diff -r 5110adf17b22 -r 57d22236ccbd lazybear/Views/HistoryList.swift --- a/lazybear/Views/HistoryList.swift Sun Feb 07 17:05:18 2021 +0100 +++ b/lazybear/Views/HistoryList.swift Sun Feb 07 19:30:04 2021 +0100 @@ -25,7 +25,8 @@ .actionSheet(isPresented: $showingActionSheet) { alert() } } ) { - ForEach(history) { company in + let sorted = history.sorted { $0.date ?? Date() > $1.date ?? Date() } + ForEach(sorted) { company in CompanyRow(history: company) } }