damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

MainTabView.swift (2971B)


      1 //
      2 //  MainTabView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2022-05-19.
      6 //
      7 
      8 import SwiftUI
      9 
     10 enum Timeline: String, CustomStringConvertible, Hashable {
     11     case home
     12     case notifications
     13     case search
     14     case dms
     15     
     16     var description: String {
     17         return self.rawValue
     18     }
     19 }
     20 
     21 func show_indicator(timeline: Timeline, current: NewEventsBits, indicator_setting: Int) -> Bool {
     22     if timeline == .notifications {
     23         return (current.rawValue & indicator_setting & NewEventsBits.notifications.rawValue) > 0
     24     }
     25     return (current.rawValue & indicator_setting) == timeline_to_notification_bits(timeline, ev: nil).rawValue
     26 }
     27     
     28 struct TabButton: View {
     29     let timeline: Timeline
     30     let img: String
     31     @Binding var selected: Timeline
     32     @ObservedObject var nstatus: NotificationStatusModel
     33     
     34     let settings: UserSettingsStore
     35     let action: (Timeline) -> ()
     36     
     37     var body: some View {
     38         ZStack(alignment: .center) {
     39             Tab
     40             
     41             if show_indicator(timeline: timeline, current: nstatus.new_events, indicator_setting: settings.notification_indicators) {
     42                 Circle()
     43                     .size(CGSize(width: 8, height: 8))
     44                     .frame(width: 10, height: 10, alignment: .topTrailing)
     45                     .alignmentGuide(VerticalAlignment.center) { a in a.height + 2.0 }
     46                     .alignmentGuide(HorizontalAlignment.center) { a in a.width - 12.0 }
     47                     .foregroundColor(.accentColor)
     48             }
     49         }
     50     }
     51     
     52     var Tab: some View {
     53         Button(action: {
     54             action(timeline)
     55             let bits = timeline_to_notification_bits(timeline, ev: nil)
     56             nstatus.new_events = NewEventsBits(rawValue: nstatus.new_events.rawValue & ~bits.rawValue)
     57         }) {
     58             Image(selected != timeline ? img : "\(img).fill")
     59                 .contentShape(Rectangle())
     60                 .frame(maxWidth: .infinity, minHeight: 30.0)
     61         }
     62         .foregroundColor(.primary)
     63     }
     64 }
     65     
     66 
     67 struct TabBar: View {
     68     var nstatus: NotificationStatusModel
     69     @Binding var selected: Timeline
     70     
     71     let settings: UserSettingsStore
     72     let action: (Timeline) -> ()
     73     
     74     var body: some View {
     75         VStack {
     76             Divider()
     77             HStack {
     78                 TabButton(timeline: .home, img: "home", selected: $selected, nstatus: nstatus, settings: settings, action: action).keyboardShortcut("1")
     79                 TabButton(timeline: .dms, img: "messages", selected: $selected, nstatus: nstatus, settings: settings, action: action).keyboardShortcut("2")
     80                 TabButton(timeline: .search, img: "search", selected: $selected, nstatus: nstatus, settings: settings, action: action).keyboardShortcut("3")
     81                 TabButton(timeline: .notifications, img: "notification-bell", selected: $selected, nstatus: nstatus, settings: settings, action: action).keyboardShortcut("4")
     82             }
     83         }
     84     }
     85 }