Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made delete button optional & make recent emojis public #72

Merged
merged 6 commits into from Sep 11, 2022

Conversation

justinmann
Copy link
Contributor

@justinmann justinmann commented Sep 10, 2022

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 isaced merged commit aa0c754 into isaced:master Sep 11, 2022
@isaced
Copy link
Owner

isaced commented Sep 11, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants