damus

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

GradientFollowButton.swift (2839B)


      1 //
      2 //  GradientFollowButton.swift
      3 //  damus
      4 //
      5 //  Created by klabo on 7/18/23.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct GradientFollowButton: View {
     11 
     12     let target: FollowTarget
     13     let follows_you: Bool
     14 
     15     @State var follow_state: FollowState
     16 
     17     private let grayTextColor = Color(#colorLiteral(red: 0.1450980392, green: 0.1607843137, blue: 0.1764705882, alpha: 1))
     18     private let grayBorder = Color(#colorLiteral(red: 0.8666666667, green: 0.8823529412, blue: 0.8901960784, alpha: 1))
     19 
     20     var body: some View {
     21 
     22         Button(action: {
     23             follow_state = perform_follow_btn_action(follow_state, target: target)
     24         }) {
     25             let followButtonText = follow_btn_txt(follow_state, follows_you: follows_you)
     26             Text(followButtonText)
     27                     .foregroundColor(follow_state == .unfollows ? .white : grayTextColor)
     28                     .font(.callout)
     29                     .fontWeight(.medium)
     30                     .padding([.top, .bottom], 10)
     31                     .padding([.leading, .trailing], 12)
     32                     .overlay(
     33                         RoundedRectangle(cornerRadius: 12)
     34                             .stroke(grayBorder, lineWidth: follow_state == .unfollows ? 0 : 1)
     35                             .frame(width: 100)
     36                     )
     37                     .frame(width: 100)
     38                     .minimumScaleFactor(0.5)
     39                     .lineLimit(1)
     40         }
     41         .background(follow_state == .unfollows ? PinkGradient : GrayGradient)
     42         .cornerRadius(12)
     43         .frame(width: 100)
     44         .onReceive(handle_notify(.followed)) { ref in
     45             guard target.follow_ref == ref else { return }
     46             self.follow_state = .follows
     47         }
     48         .onReceive(handle_notify(.unfollowed)) { ref in
     49             guard target.follow_ref == ref else { return }
     50             self.follow_state = .unfollows
     51         }
     52     }
     53 }
     54 
     55 struct GradientFollowButtonPreviews: View {
     56     let target: FollowTarget = .pubkey(.empty)
     57     var body: some View {
     58         VStack {
     59             Text(verbatim: "Unfollows")
     60             GradientFollowButton(target: target, follows_you: false, follow_state: .unfollows)
     61 
     62             Text(verbatim: "Following")
     63             GradientFollowButton(target: target, follows_you: false, follow_state: .following)
     64 
     65             Text(verbatim: "Follows")
     66             GradientFollowButton(target: target, follows_you: false, follow_state: .follows)
     67 
     68             Text(verbatim: "Follows")
     69             GradientFollowButton(target: target, follows_you: true, follow_state: .follows)
     70 
     71             Text(verbatim: "Unfollowing")
     72             GradientFollowButton(target: target, follows_you: false, follow_state: .unfollowing)
     73         }
     74     }
     75 }
     76 
     77 struct GradientButton_Previews: PreviewProvider {
     78     static var previews: some View {
     79         GradientFollowButtonPreviews()
     80     }
     81 }