Skip to content

Commit

Permalink
Resolved some weird keyboard glitches described in error Themoji#6
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom de ruiter committed Nov 26, 2016
1 parent c6e480e commit 2d51ad4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Themoji.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
CA659AE11C4DCB2700A4C7EF /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA659AD81C4DCB2700A4C7EF /* ViewController.swift */; };
CA8E134A1C4C760900FB865B /* ThemojiUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA8E13491C4C760900FB865B /* ThemojiUITests.swift */; };
CA8E13521C4C761500FB865B /* SnapshotHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA8E13511C4C761500FB865B /* SnapshotHelper.swift */; };
CADF5C601C68267D0035D126 /* Apple Color Emoji.ttf in Resources */ = {isa = PBXBuildFile; fileRef = CADF5C5F1C68267D0035D126 /* Apple Color Emoji.ttf */; };
CADF5C641C6829D30035D126 /* FontRendering.m in Sources */ = {isa = PBXBuildFile; fileRef = CADF5C631C6829D30035D126 /* FontRendering.m */; };
E22EFF121DE9A5680077950A /* Apple Color Emoji.ttf in Resources */ = {isa = PBXBuildFile; fileRef = CADF5C5F1C68267D0035D126 /* Apple Color Emoji.ttf */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -261,7 +261,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CADF5C601C68267D0035D126 /* Apple Color Emoji.ttf in Resources */,
E22EFF121DE9A5680077950A /* Apple Color Emoji.ttf in Resources */,
CA659ADC1C4DCB2700A4C7EF /* Main.storyboard in Resources */,
CA659ADF1C4DCB2700A4C7EF /* CategoryButton.xib in Resources */,
CA659ADA1C4DCB2700A4C7EF /* Assets.xcassets in Resources */,
Expand Down
74 changes: 48 additions & 26 deletions Themoji/ViewController.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
var isCategoryOpen = false
var isFullScreen = false

let originialInset: CGFloat = 5.0
let defaultAutoCompletionHeight: CGFloat = 94
let increasedAutoCompletionHeight: CGFloat = 250
var emojiLabelHeight: CGFloat = 0.0 // this is NOT the font size
Expand All @@ -40,6 +41,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.changeInputMode(_:)), name: UITextInputCurrentInputModeDidChangeNotification, object: nil)

let nibName = UINib(nibName: "CategoryButton", bundle:nil)
self.categoriesCollectionView.registerNib(nibName, forCellWithReuseIdentifier: "Yo")
Expand All @@ -52,6 +54,16 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.didTriggerEmoji(_:)), name: "ShowEmoji", object: nil)
}

func changeInputMode(notification: NSNotification) {
if isEmojiKeyboard() {
self.autocompletionTableView.hidden = true
} else {
// So that everything is back to normal

self.valueDidChange(self.textInput)
}
}

func didTriggerEmoji(noti: NSNotification) {
let searchString = (noti.object as! String).lowercaseString

Expand All @@ -73,19 +85,25 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
self.emojiHeightConstraint.constant = emojiLabelHeight
}

func isEmojiKeyboard() -> Bool {
return textInput.textInputMode?.primaryLanguage == "emoji" || textInput.textInputMode?.primaryLanguage == nil
}

func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
self.bottomConstraint.constant = contentInsets.bottom + 5.0
self.categoriesCollectionView.hidden = true
if self.bottomConstraint.constant == originialInset {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
self.bottomConstraint.constant = contentInsets.bottom + 5.0
self.categoriesCollectionView.hidden = true
}
UIView.animateWithDuration(0.25) {
self.view.layoutIfNeeded()
}
}
}

func keyboardWillHide(notification: NSNotification) {
self.bottomConstraint.constant = 5.0
self.bottomConstraint.constant = self.originialInset
UIView.animateWithDuration(0.25) {
self.view.layoutIfNeeded()
}
Expand Down Expand Up @@ -171,30 +189,34 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
}

@IBAction func valueDidChange(sender: AnyObject) {
if (self.textInput.text! != "") {
let input = self.textInput.text!.characters.last!
if emojiToText(input) != nil
{
setCurrentEmoji("\(input)")
resetTextField(false)
}
else
{
let searchString = self.textInput.text!.lowercaseString // all chars since the last match

emojiFetcher.cancelFetches()
emojiFetcher.query(searchString) { emojiResults in
self.autocompletionItemsName = []
self.autocompletionItemsEmoji = []
for (emoji) in emojiResults {
self.autocompletionItemsEmoji.append(emoji.character)
self.autocompletionItemsName.append(emoji.name)
if self.textInput.text!.characters.count > 0 {
if (self.textInput.text! != "") {
let input = self.textInput.text!.characters.last!
if emojiToText(input) != nil
{
setCurrentEmoji("\(input)")
resetTextField(false)
}
else
{
let searchString = self.textInput.text!.lowercaseString // all chars since the last match

emojiFetcher.cancelFetches()
emojiFetcher.query(searchString) { emojiResults in
self.autocompletionItemsName = []
self.autocompletionItemsEmoji = []
for (emoji) in emojiResults {
self.autocompletionItemsEmoji.append(emoji.character)
self.autocompletionItemsName.append(emoji.name)
}
self.autocompletionTableView.reloadData()
self.autocompletionTableView.hidden = (emojiResults.count == 0)
self.autocompletionTableView.flashScrollIndicators()
}
self.autocompletionTableView.reloadData()
self.autocompletionTableView.hidden = (emojiResults.count == 0)
self.autocompletionTableView.flashScrollIndicators()
}
}
} else {
self.autocompletionTableView.hidden = true
}
}

Expand Down Expand Up @@ -235,7 +257,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
}

func setCurrentEmoji(e: String) {
self.textInput.hidden = true
self.textInput.hidden = false
self.emojiLabel.text = e

if var recent = NSUserDefaults.standardUserDefaults().objectForKey(emojiHistoryKey) {
Expand Down

0 comments on commit 2d51ad4

Please sign in to comment.