damus

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

SupporterBadge.swift (3498B)


      1 //
      2 //  SupporterBadge.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-05-15.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct SupporterBadge: View {
     11     let percent: Int?
     12     let purple_account: DamusPurple.Account?
     13     let style: Style
     14     
     15     init(percent: Int?, purple_account: DamusPurple.Account? = nil, style: Style) {
     16         self.percent = percent
     17         self.purple_account = purple_account
     18         self.style = style
     19     }
     20     
     21     let size: CGFloat = 17
     22     
     23     var body: some View {
     24         HStack {
     25             if let purple_account, purple_account.active == true {
     26                 HStack(spacing: 1) {
     27                     Image("star.fill")
     28                         .resizable()
     29                         .frame(width:size, height:size)
     30                         .foregroundStyle(GoldGradient)
     31                     if self.style == .full {
     32                         Text(verbatim: format_date(date: purple_account.created_at, time_style: .none))
     33                             .foregroundStyle(.secondary)
     34                             .font(.caption)
     35                     }
     36                 }
     37             }
     38             else if let percent, percent < 100 {
     39                 Image("star.fill")
     40                     .resizable()
     41                     .frame(width:size, height:size)
     42                     .foregroundColor(support_level_color(percent))
     43             } else if let percent, percent == 100 {
     44                 Image("star.fill")
     45                     .resizable()
     46                     .frame(width:size, height:size)
     47                     .foregroundStyle(GoldGradient)
     48             }
     49         }
     50     }
     51     
     52     enum Style {
     53         case full       // Shows the entire badge with a purple subscriber number if present
     54         case compact    // Does not show purple subscriber number. Only shows the star (if applicable)
     55     }
     56 }
     57 
     58 func support_level_color(_ percent: Int) -> Color {
     59     if percent == 0 {
     60         return .gray
     61     }
     62     
     63     let percent_f = Double(percent) / 100.0
     64     let cutoff = 0.5
     65     let h = cutoff + (percent_f * cutoff); // Hue (note 0.2 = Green, see huge chart below)
     66     let s = 0.9; // Saturation
     67     let b = 0.9; // Brightness
     68     
     69     return Color(hue: h, saturation: s, brightness: b)
     70 }
     71 
     72 struct SupporterBadge_Previews: PreviewProvider {
     73     static func Level(_ p: Int) -> some View {
     74         HStack(alignment: .center) {
     75             SupporterBadge(percent: p, style: .full)
     76                 .frame(width: 50)
     77             Text(verbatim: p.formatted())
     78                 .frame(width: 50)
     79         }
     80     }
     81     
     82     static func Purple(_ subscriber_number: Int) -> some View {
     83         HStack(alignment: .center) {
     84             SupporterBadge(
     85                 percent: nil,
     86                 purple_account: DamusPurple.Account(pubkey: test_pubkey, created_at: .now, expiry: .now.addingTimeInterval(10000), subscriber_number: subscriber_number, active: true),
     87                 style: .full
     88             )
     89                 .frame(width: 100)
     90         }
     91     }
     92     
     93     static var previews: some View {
     94         VStack(spacing: 0) {
     95             VStack(spacing: 0) {
     96                 Level(1)
     97                 Level(10)
     98                 Level(20)
     99                 Level(30)
    100                 Level(40)
    101                 Level(50)
    102             }
    103             Level(60)
    104             Level(70)
    105             Level(80)
    106             Level(90)
    107             Level(100)
    108             Purple(1)
    109             Purple(2)
    110             Purple(3)
    111             Purple(99)
    112             Purple(100)
    113             Purple(1971)
    114         }
    115     }
    116 }
    117 
    118