EventActionBar.swift (6170B)
1 // 2 // EventActionBar.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-04-16. 6 // 7 8 import SwiftUI 9 import UIKit 10 11 enum ActionBarSheet: Identifiable { 12 case reply 13 14 var id: String { 15 switch self { 16 case .reply: return "reply" 17 } 18 } 19 } 20 21 struct EventActionBar: View { 22 let damus_state: DamusState 23 let event: NostrEvent 24 let generator = UIImpactFeedbackGenerator(style: .medium) 25 @State var sheet: ActionBarSheet? = nil 26 @State var confirm_boost: Bool = false 27 @State var show_share_sheet: Bool = false 28 @StateObject var bar: ActionBarModel 29 30 var body: some View { 31 HStack { 32 /* 33 EventActionButton(img: "square.and.arrow.up") { 34 print("share") 35 } 36 37 Spacer() 38 39 */ 40 if damus_state.keypair.privkey != nil { 41 EventActionButton(img: "bubble.left", col: nil) { 42 notify(.reply, event) 43 } 44 .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) 45 } 46 47 HStack(alignment: .bottom) { 48 Text("\(bar.boosts > 0 ? "\(bar.boosts)" : "")") 49 .font(.footnote.weight(.medium)) 50 .foregroundColor(bar.boosted ? Color.green : Color.gray) 51 52 EventActionButton(img: "arrow.2.squarepath", col: bar.boosted ? Color.green : nil) { 53 if bar.boosted { 54 notify(.delete, bar.our_boost) 55 } else { 56 self.confirm_boost = true 57 } 58 } 59 } 60 .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) 61 62 HStack(alignment: .bottom) { 63 Text("\(bar.likes > 0 ? "\(bar.likes)" : "")") 64 .font(.footnote.weight(.medium)) 65 .foregroundColor(bar.liked ? Color.orange : Color.gray) 66 67 LikeButton(liked: bar.liked) { 68 if bar.liked { 69 notify(.delete, bar.our_like) 70 } else { 71 send_like() 72 } 73 } 74 } 75 .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) 76 77 EventActionButton(img: "square.and.arrow.up", col: Color.gray) { 78 show_share_sheet = true 79 } 80 .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) 81 82 /* 83 HStack(alignment: .bottom) { 84 Text("\(bar.tips > 0 ? "\(bar.tips)" : "")") 85 .font(.footnote) 86 .foregroundColor(bar.tipped ? Color.orange : Color.gray) 87 88 EventActionButton(img: bar.tipped ? "bitcoinsign.circle.fill" : "bitcoinsign.circle", col: bar.tipped ? Color.orange : nil) { 89 if bar.tipped { 90 //notify(.delete, bar.our_tip) 91 } else { 92 //notify(.boost, event) 93 } 94 } 95 } 96 */ 97 } 98 .sheet(isPresented: $show_share_sheet) { 99 if let note_id = bech32_note_id(event.id) { 100 if let url = URL(string: "https://damus.io/" + note_id) { 101 ShareSheet(activityItems: [url]) 102 } 103 } 104 } 105 .alert(NSLocalizedString("Boost", comment: "Title of alert for confirming to boost a post."), isPresented: $confirm_boost) { 106 Button("Cancel") { 107 confirm_boost = false 108 } 109 Button(NSLocalizedString("Boost", comment: "Button to confirm boosting a post.")) { 110 send_boost() 111 } 112 } message: { 113 Text("Are you sure you want to boost this post?", comment: "Alert message to ask if user wants to boost a post.") 114 } 115 .onReceive(handle_notify(.liked)) { n in 116 let liked = n.object as! Counted 117 if liked.id != event.id { 118 return 119 } 120 self.bar.likes = liked.total 121 if liked.event.pubkey == damus_state.keypair.pubkey { 122 self.bar.our_like = liked.event 123 } 124 } 125 } 126 127 func send_boost() { 128 guard let privkey = self.damus_state.keypair.privkey else { 129 return 130 } 131 132 let boost = make_boost_event(pubkey: damus_state.keypair.pubkey, privkey: privkey, boosted: self.event) 133 134 self.bar.our_boost = boost 135 136 damus_state.pool.send(.event(boost)) 137 } 138 139 func send_like() { 140 guard let privkey = damus_state.keypair.privkey else { 141 return 142 } 143 144 let like_ev = make_like_event(pubkey: damus_state.pubkey, privkey: privkey, liked: event) 145 146 self.bar.our_like = like_ev 147 148 generator.impactOccurred() 149 150 damus_state.pool.send(.event(like_ev)) 151 } 152 } 153 154 155 func EventActionButton(img: String, col: Color?, action: @escaping () -> ()) -> some View { 156 Button(action: action) { 157 Label(" ", systemImage: img) 158 .font(.footnote.weight(.medium)) 159 .foregroundColor(col == nil ? Color.gray : col!) 160 } 161 } 162 163 struct LikeButton: View { 164 let liked: Bool 165 let action: () -> () 166 167 @Environment(\.colorScheme) var colorScheme 168 169 var body: some View { 170 Button(action: action) { 171 if liked { 172 Text("🤙", comment: "Button with emoji to like an event.") 173 } else { 174 Image("shaka") 175 .renderingMode(.template) 176 .foregroundColor(.gray) 177 } 178 } 179 } 180 } 181 182 183 struct EventActionBar_Previews: PreviewProvider { 184 static var previews: some View { 185 let pk = "pubkey" 186 let ds = test_damus_state() 187 let bar = ActionBarModel(likes: 0, boosts: 0, tips: 0, our_like: nil, our_boost: nil, our_tip: nil) 188 let ev = NostrEvent(content: "hi", pubkey: pk) 189 EventActionBar(damus_state: ds, event: ev, bar: bar) 190 } 191 }