isaced / ISEmojiView

Emoji Keyboard for iOS
MIT License
522 stars 119 forks source link

Made delete button optional & make recent emojis public #72

Closed justinmann closed 2 years ago

justinmann commented 2 years ago

I am using ISEmojiView unbounded to the TextField, it is solely to select an emoji. In the parent UI, I am showing the recent emojis to offer a quick single click option. This is being done in SwiftUI, so I am wrap the recent emojis as a published array from an observableobject.

Great work on ISEmojiView, it saved me a bunch of time.

public class Emoji: ObservableObject, EmojiViewDelegate {
  public static let shared = Emoji()
  private var tapAwayView: UIView?
  private var emojiView: EmojiView?
  private var callback: ((String) -> Void)?
  private let defaultEmojis: [String] = ["❤", "🙌", "👀", "😍"]

  @Published public var popular: [String] = []

  private init() {
    updatePopular()
  }

  private func updatePopular() {
    var emojis = RecentEmojisManager.sharedInstance
      .recentEmojis()
      .prefix(4)
      .map(\.emoji)

    emojis.append(contentsOf: defaultEmojis)

    popular = Array(emojis.prefix(upTo: 4))
  }

  public func pick(callback: @escaping (String) -> Void) {
    let keyboardSettings = KeyboardSettings(bottomType: .categories)
    keyboardSettings.countOfRecentsEmojis = 0
    keyboardSettings.needToShowDeleteButton = false
    let emojiView = EmojiView(keyboardSettings: keyboardSettings)
    emojiView.translatesAutoresizingMaskIntoConstraints = false
    emojiView.delegate = self

    let tapAwayView = UIView()
    tapAwayView.addTapGesture { [weak self] _ in
      self?.dismiss()
    }

    if let viewController = UIApplication.topViewController() {
      self.callback = callback
      self.emojiView = emojiView
      self.tapAwayView = tapAwayView

      viewController.view?.addSubview(tapAwayView)
      viewController.view?.addSubview(emojiView)

      tapAwayView.snp.makeConstraints {
        $0.edges.equalToSuperview()
      }

      emojiView.snp.makeConstraints {
        $0.bottom.equalToSuperview()
        $0.horizontal.equalToSuperview()
        $0.height.equalTo(320)
      }
    }
  }

  public func emojiViewDidSelectEmoji(_ emoji: String, emojiView: EmojiView) {
    callback?(emoji)
    updatePopular()
    dismiss()
  }

  public func emojiViewDidPressDeleteBackwardButton(_ emojiView: EmojiView) {
    dismiss()
  }

  public func emojiViewDidPressDismissKeyboardButton(_ emojiView: EmojiView) {
    dismiss()
  }

  private func dismiss() {
    callback = nil
    tapAwayView?.removeFromSuperview()
    tapAwayView = nil
    emojiView?.removeFromSuperview()
    emojiView = nil
  }
isaced commented 2 years ago

Thanks @justinmann , v0.3.3 has been released with this change.