changeset 128:57d22236ccbd

Add date to HistoryList, and sorted that list by date. The new ones come first
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 07 Feb 2021 19:30:04 +0100
parents 5110adf17b22
children 4ce0ff2a38ae
files LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Views/Company.swift lazybear/Views/CompanyRow.swift lazybear/Views/HistoryList.swift
diffstat 4 files changed, 30 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- 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:
--- 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 {
--- 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)
                 }
             }