damus

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

ReactionsSettingsView.swift (2349B)


      1 //
      2 //  ReactionsSettingsView.swift
      3 //  damus
      4 //
      5 //  Created by Suhail Saqan on 7/3/23.
      6 //
      7 
      8 import SwiftUI
      9 import MCEmojiPicker
     10 
     11 struct ReactionsSettingsView: View {
     12     @ObservedObject var settings: UserSettingsStore
     13     @State private var isReactionsVisible: Bool = false
     14 
     15     var body: some View {
     16         Form {
     17             Section {
     18                 Text(settings.default_emoji_reaction)
     19                     .emojiPicker(
     20                         isPresented: $isReactionsVisible,
     21                         selectedEmoji: $settings.default_emoji_reaction,
     22                         arrowDirection: .up,
     23                         isDismissAfterChoosing: true
     24                     )
     25                     .onTapGesture {
     26                         isReactionsVisible = true
     27                     }
     28             } header: {
     29                 Text("Select default emoji", comment: "Prompt selection of user's default emoji reaction")
     30             }
     31         }
     32         .navigationTitle(NSLocalizedString("Reactions", comment: "Title of emoji reactions view"))
     33         .navigationBarTitleDisplayMode(.large)
     34     }
     35 }
     36 
     37 /// From: https://stackoverflow.com/a/39425959
     38 extension Character {
     39     /// A simple emoji is one scalar and presented to the user as an Emoji
     40     var isSimpleEmoji: Bool {
     41         guard let firstScalar = unicodeScalars.first else { return false }
     42         return firstScalar.properties.isEmoji && firstScalar.value > 0x238C
     43     }
     44 
     45     /// Checks if the scalars will be merged into an emoji
     46     var isCombinedIntoEmoji: Bool { unicodeScalars.count > 1 && unicodeScalars.first?.properties.isEmoji ?? false }
     47 
     48     var isEmoji: Bool { isSimpleEmoji || isCombinedIntoEmoji }
     49 }
     50 
     51 extension String {
     52     var isSingleEmoji: Bool { count == 1 && containsEmoji }
     53 
     54     var containsEmoji: Bool { contains { $0.isEmoji } }
     55 
     56     var containsOnlyEmoji: Bool { !isEmpty && !contains { !$0.isEmoji } }
     57 
     58     var emojiString: String { emojis.map { String($0) }.reduce("", +) }
     59 
     60     var emojis: [Character] { filter { $0.isEmoji } }
     61 
     62     var emojiScalars: [UnicodeScalar] { filter { $0.isEmoji }.flatMap { $0.unicodeScalars } }
     63 }
     64 
     65 func isValidEmoji(_ string: String) -> Bool {
     66     return string.isSingleEmoji
     67 }
     68 
     69 struct ReactionsSettingsView_Previews: PreviewProvider {
     70     static var previews: some View {
     71         ReactionsSettingsView(settings: UserSettingsStore())
     72     }
     73 }