gemenon

[ACTIVE] The Safari of the Gemini ecosystem
git clone git://git.figbert.com/gemenon.git
Log | Files | Refs

HistoryView.swift (1495B)


      1 //
      2 //  HistoryView.swift
      3 //  Gemenon
      4 //
      5 //  Created by figbert on 9/6/22.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct HistoryView: View {
     11     @EnvironmentObject var data: BrowserData
     12     @State var tabs: [Tab] = []
     13 
     14     var body: some View {
     15         List {
     16             ForEach(tabs) { tab in
     17                 HStack {
     18                     Link(destination: tab.url!) { Text(tab.url?.absoluteString ?? "error") }
     19                     Spacer()
     20                     if tab.timestamp.timeIntervalSince(.now) <= -1*60*60*24 {
     21                         Text(tab.timestamp.formatted(date: .long, time: .complete))
     22                     } else {
     23                         Text(tab.timestamp.formatted(date: .omitted, time: .complete))
     24                     }
     25                 }
     26             }
     27         }
     28         .onAppear {
     29             genHistory(data.tab, list: &tabs)
     30         }
     31     }
     32 
     33     func genHistory(_ tab: Tab, list: inout [Tab]) {
     34         let start = seekEndOfHistory(tab)
     35         appendToURLs(start, list: &list)
     36     }
     37     func seekEndOfHistory(_ tab: Tab) -> Tab {
     38         if let next = tab.next {
     39             return seekEndOfHistory(next)
     40         } else {
     41             return tab
     42         }
     43     }
     44     func appendToURLs(_ tab: Tab, list: inout [Tab]) {
     45         if !tab.home {
     46             list.append(tab)
     47         }
     48         if tab.prev != nil {
     49             appendToURLs(tab.prev!, list: &list)
     50         }
     51     }
     52 }
     53 
     54 struct HistoryView_Previews: PreviewProvider {
     55     static var previews: some View {
     56         HistoryView()
     57     }
     58 }