FollowButtonView.swift (3314B)
1 // 2 // FollowButtonView.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-05-16. 6 // 7 8 import SwiftUI 9 10 struct FollowButtonView: View { 11 12 @Environment(\.colorScheme) var colorScheme 13 14 let target: FollowTarget 15 let follows_you: Bool 16 @State var follow_state: FollowState 17 18 var body: some View { 19 Button { 20 follow_state = perform_follow_btn_action(follow_state, target: target) 21 } label: { 22 Text(verbatim: "\(follow_btn_txt(follow_state, follows_you: follows_you))") 23 .frame(width: 105, height: 30) 24 //.padding(.vertical, 10) 25 .font(.caption.weight(.bold)) 26 .foregroundColor(follow_state == .unfollows ? filledTextColor() : borderColor()) 27 .background(follow_state == .unfollows ? fillColor() : emptyColor()) 28 .cornerRadius(20) 29 .overlay { 30 RoundedRectangle(cornerRadius: 16) 31 .stroke(follow_state == .unfollows ? .clear : borderColor(), lineWidth: 1) 32 } 33 } 34 .onReceive(handle_notify(.followed)) { follow in 35 guard case .pubkey(let pk) = follow, 36 pk == target.pubkey else { return } 37 38 self.follow_state = .follows 39 } 40 .onReceive(handle_notify(.unfollowed)) { unfollow in 41 guard case .pubkey(let pk) = unfollow, 42 pk == target.pubkey else { return } 43 44 self.follow_state = .unfollows 45 } 46 } 47 48 func filledTextColor() -> Color { 49 colorScheme == .light ? DamusColors.white : DamusColors.black 50 } 51 52 func fillColor() -> Color { 53 colorScheme == .light ? DamusColors.black : DamusColors.white 54 } 55 56 func emptyColor() -> Color { 57 Color.black.opacity(0) 58 } 59 60 func borderColor() -> Color { 61 colorScheme == .light ? DamusColors.darkGrey : DamusColors.lightGrey 62 } 63 } 64 65 struct FollowButtonPreviews: View { 66 let target: FollowTarget = .pubkey(test_pubkey) 67 var body: some View { 68 VStack { 69 Text(verbatim: "Unfollows") 70 FollowButtonView(target: target, follows_you: false, follow_state: .unfollows) 71 72 Text(verbatim: "Following") 73 FollowButtonView(target: target, follows_you: false, follow_state: .following) 74 75 Text(verbatim: "Follows") 76 FollowButtonView(target: target, follows_you: false, follow_state: .follows) 77 78 Text(verbatim: "Follows") 79 FollowButtonView(target: target, follows_you: true, follow_state: .follows) 80 81 Text(verbatim: "Unfollowing") 82 FollowButtonView(target: target, follows_you: false, follow_state: .unfollowing) 83 } 84 } 85 } 86 87 struct FollowButtonView_Previews: PreviewProvider { 88 static var previews: some View { 89 FollowButtonPreviews() 90 } 91 } 92 93 func perform_follow_btn_action(_ fs: FollowState, target: FollowTarget) -> FollowState { 94 switch fs { 95 case .follows: 96 notify(.unfollow(target)) 97 return .following 98 case .following: 99 return .following 100 case .unfollowing: 101 return .following 102 case .unfollows: 103 notify(.follow(target)) 104 return .unfollowing 105 } 106 } 107