Hashtags.swift (2114B)
1 // 2 // Hashtags.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-04-06. 6 // 7 8 import Foundation 9 import SwiftUI 10 11 struct CustomHashtag { 12 let name: String 13 let offset: CGFloat? 14 let color: Color? 15 16 init(name: String, color: Color? = nil, offset: CGFloat? = nil) { 17 self.name = name 18 self.color = color 19 self.offset = offset 20 } 21 22 static let coffee = CustomHashtag(name: "coffee", color: DamusColors.brown, offset: -1.0) 23 static let bitcoin = CustomHashtag(name: "bitcoin", color: Color.orange, offset: -3.0) 24 static let nostr = CustomHashtag(name: "nostr", color: DamusColors.purple, offset: -2.0) 25 static let plebchain = CustomHashtag(name: "plebchain", color: DamusColors.deepPurple, offset: -3.0) 26 static let zap = CustomHashtag(name: "zap", color: DamusColors.yellow, offset: -4.0) 27 } 28 29 30 let custom_hashtags: [String: CustomHashtag] = [ 31 "bitcoin": CustomHashtag.bitcoin, 32 "btc": CustomHashtag.bitcoin, 33 "nostr": CustomHashtag.nostr, 34 "coffee": CustomHashtag.coffee, 35 "coffeechain": CustomHashtag.coffee, 36 "plebchain": CustomHashtag.plebchain, 37 "zap": CustomHashtag.zap, 38 "zaps": CustomHashtag.zap, 39 "zapathon": CustomHashtag.zap, 40 "onlyzaps": CustomHashtag.zap, 41 ] 42 43 func hashtag_str(_ htag: String) -> CompatibleText { 44 var attributedString = AttributedString(stringLiteral: "#\(htag)") 45 attributedString.link = URL(string: "damus:t:\(htag.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? htag)") 46 47 let lowertag = htag.lowercased() 48 49 if let custom_hashtag = custom_hashtags[lowertag] { 50 if let col = custom_hashtag.color { 51 attributedString.foregroundColor = col 52 } 53 54 let name = custom_hashtag.name 55 56 attributedString = attributedString + " " 57 return CompatibleText(items: [.attributed_string(attributedString), .icon(named: "\(name)-hashtag", offset: custom_hashtag.offset ?? 0.0)]) 58 } else { 59 attributedString.foregroundColor = DamusColors.purple 60 return CompatibleText(items: [.attributed_string(attributedString)]) 61 } 62 }