Skip to content

Commit

Permalink
fix(ios): clean up and reduce log statements
Browse files Browse the repository at this point in the history
Fixes: #5897
  • Loading branch information
sgschantz committed Dec 9, 2024
1 parent 026edfe commit c9d4c5c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ private class CustomInputView: UIInputView, UIInputViewAudioFeedback {
self.buildKeyboardHeightConstraints(bannerHeight: InputViewController.topBarHeight)
}

/**
* Due to new custom keyboard height as chosen by the user.
* The value for the new keyboard height originates from KeyboardHeightViewController.
*/
func keyboardHeightChanged() {
os_log("CustomInputView keyboardHeightChanged", log: KeymanEngineLogger.ui, type: .info)

Expand Down Expand Up @@ -547,6 +551,10 @@ open class InputViewController: UIInputViewController, KeymanWebDelegate {
fixLayout()
}

/**
* Due to new custom keyboard height as chosen by the user.
* The value for the new keyboard height originates from KeyboardHeightViewController.
*/
func keyboardHeightChanged() {
os_log("InputViewController keyboardHeightChanged", log: KeymanEngineLogger.ui, type: .debug)
if let customInputView = self.inputView as? CustomInputView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ extension KeymanWebViewController {
self.delegate?.menuKeyHeld(self)
}

func constraintTargetHeight(isPortrait: Bool) -> CGFloat {
os_log("constraintTargetHeight", log:KeymanEngineLogger.ui, type: .info)
func determineDefaultKeyboardHeight(isPortrait: Bool) -> CGFloat {
os_log("determineDefaultKeyboardHeight", log:KeymanEngineLogger.ui, type: .info)

let keyboardHeight = KeyboardScaleMap.getDeviceDefaultKeyboardScale(forPortrait: isPortrait)?.keyboardHeight ?? 216 // default for ancient devices

Expand All @@ -714,27 +714,27 @@ extension KeymanWebViewController {
width = UIScreen.main.bounds.width
var height: CGFloat

os_log("initKeyboardSize()", log:KeymanEngineLogger.ui, type: .info)

// get orientation for system or in-app
// get orientation differently if system or in-app keyboard
let portrait = Util.isSystemKeyboard ? InputViewController.isPortrait : UIDevice.current.orientation.isPortrait

// if keyboard height has been saved, then use it
/**
* If keyboard height is saved in UserDefaults, then use it
*/
if let savedHeight = self.readKeyboardHeight(isPortrait: portrait) {
height = savedHeight
} else { // find default keyboard height
height = self.constraintTargetHeight(isPortrait: portrait)

} else {
/**
* The keyboard height is not yet saved to UserDefaults, so save the default values for this orientation
* Otherwise, get default keyboard height for this orientation and write to UserDefaults
*/
height = self.determineDefaultKeyboardHeight(isPortrait: portrait)
self.writeKeyboardHeightIfDoesNotExist(isPortrait: portrait, height: height)

/**
* If we have not saved a keyboard height for one orientation yet, then we do not
* expect that the other has been saved yet either. Do so now if necessary.
* If we need to write out the keyboard height for one orientation, then we
* expect that the other must be written also.
* Write it out now, but only if a value for keyboard height does not already exist.
*/
height = self.constraintTargetHeight(isPortrait: !portrait)
height = self.determineDefaultKeyboardHeight(isPortrait: !portrait)
self.writeKeyboardHeightIfDoesNotExist(isPortrait: !portrait, height: height)
}

Expand Down Expand Up @@ -769,6 +769,11 @@ extension KeymanWebViewController {
return height;
}

/**
* Write out the keyboard height to the UserDefaults but only if it does not exist there yet.
* If it exists, then we assume it was configured by the user and do not want to
* overwrite that value with a default value derived for this device.
*/
func writeKeyboardHeightIfDoesNotExist(isPortrait: Bool, height: CGFloat) {
let writeMessage = "writeKeyboardHeight, isPortrait: \(isPortrait) height: \(height)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, writeMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ class KeyboardHeightViewController: UIViewController {
os_log("KeyboardHeightViewController viewDidLoad", log:KeymanEngineLogger.ui, type: .info)

self.determineOrientation()
self.loadDefaultKeyboardHeights()
self.determineDefaultKeyboardHeights()
self.applyKeyboardHeight()

//title = NSLocalizedString("menu-settings-spacebar-title", bundle: engineBundle, comment: "")
title = NSLocalizedString("button-label-reset-default-keyboard-height", bundle: engineBundle, comment: "")
title = "Adjust Height"
title = NSLocalizedString("adjust-keyboard-height-title", bundle: engineBundle, comment: "")
navigationItem.setHidesBackButton(false, animated: true)
navigationItem.leftBarButtonItem?.isEnabled = true

Expand All @@ -68,8 +66,7 @@ class KeyboardHeightViewController: UIViewController {
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .debug, message)
}

private func loadDefaultKeyboardHeights() {
os_log("loadDefaultKeyboardHeights", log:KeymanEngineLogger.ui, type: .info)
private func determineDefaultKeyboardHeights() {
// if no KeyboardScaleMap found for device, then default to 216.0
let portraitKeyboardScale = KeyboardScaleMap.getDeviceDefaultKeyboardScale(forPortrait: true)
self.defaultPortraitHeight = Double(portraitKeyboardScale?.keyboardHeight ?? 216.0)
Expand All @@ -78,41 +75,48 @@ class KeyboardHeightViewController: UIViewController {
self.defaultLandscapeHeight = Double(landscapeKeyboardScale?.keyboardHeight ?? 216.0)
}

/**
* Read the previously set keyboard heights from UserDefaults.
* Running the first time, these values would be defaults set in KeymanWebviewController.initKeyboardSize()
* These values should be set prior to now; if not, log an error and use default values
*/
private func applyKeyboardHeight() {
let introMessage = "applyKeyboardHeight, with self.isPortrait: \(self.isPortrait)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, introMessage)
if (self.isPortrait) {
if (Storage.active.userDefaults.object(forKey: Key.portraitKeyboardHeight) != nil) {
self.keyboardHeight = Storage.active.userDefaults.portraitKeyboardHeight
let message = "applyKeyboardHeight, from UserDefaults loaded portrait value \(self.keyboardHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, message)
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .debug, message)
} else {
self.keyboardHeight = self.defaultPortraitHeight
let message = "applyKeyboardHeight, portraitHeight not found in UserDefaults, using default value \(self.keyboardHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, message)
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .error, message)
}
} else {
if (Storage.active.userDefaults.object(forKey: Key.portraitKeyboardHeight) != nil) {
self.keyboardHeight = Storage.active.userDefaults.landscapeKeyboardHeight
let message = "applyKeyboardHeight, from UserDefaults loaded landscape value \(self.keyboardHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, message)
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .debug, message)
} else {
self.keyboardHeight = self.defaultLandscapeHeight
let message = "applyKeyboardHeight, landscapeHeight not found in UserDefaults, using default value \(self.keyboardHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, message)
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .error, message)
}
}
}


/**
* Set some reasonable limits on how small or large a keyboard can be set to during resizing.
*/
private func calculateKeyboardHeightLimits() {
// minimum height
self.minKeyboardHeight = 100.0

// set maxKeyboardHeight to 100 pts smaller than the contentView height
self.maxKeyboardHeight = contentView.frame.height - 100.0

let messageBegan = "minKeyboardHeight: \(minKeyboardHeight) maxKeyboardHeight: \(maxKeyboardHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, messageBegan)
let message = "minKeyboardHeight: \(minKeyboardHeight) maxKeyboardHeight: \(maxKeyboardHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .debug, message)
}

/**
Expand Down Expand Up @@ -167,6 +171,9 @@ class KeyboardHeightViewController: UIViewController {
return approvedTranslation
}

/**
* Called when the keyboardResizer view is being moved to adjust the keyboard height.
*/
@objc func handleDrag(_ dragRecognizer: UIPanGestureRecognizer) {
switch dragRecognizer.state {
case .began:
Expand All @@ -184,27 +191,25 @@ class KeyboardHeightViewController: UIViewController {
let approvedTranslation = self.applyKeyboardSizeLimits(attemptedTranslation: verticalTranslation)
if (approvedTranslation != 0) {
let newKeyboardHeight = keyboardHeight - approvedTranslation;
self.changeKeyboardHeight(newHeight: newKeyboardHeight)
self.writeNewKeyboardHeight(newHeight: newKeyboardHeight)
} else {
os_log("handleDrag .ended with no resizing", log:KeymanEngineLogger.ui, type: .info)
}
default: break
}
}

/**
* Stretch the keyboard image and move the keyboardResizier object as it is being resized.
*/
private func provideResizeFeedback(drag: UIPanGestureRecognizer) {
// how much did we drag vertically
let verticalTranslation = drag.translation(in: self.contentView).y

let messageBegan = "verticalTranslation: \(verticalTranslation)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .debug, messageBegan)

// enforce keyboard size limits on translation
let approvedTranslation = self.applyKeyboardSizeLimits(attemptedTranslation: verticalTranslation)

if (approvedTranslation == 0) {
os_log("reached keyboard size limit", log:KeymanEngineLogger.ui, type: .debug)
} else if (approvedTranslation != 0) {
if (approvedTranslation != 0) {
let keyboardTranslation = CGAffineTransform(translationX: 0, y: approvedTranslation)
self.keyboardResizer.transform = keyboardTranslation

Expand All @@ -220,22 +225,20 @@ class KeyboardHeightViewController: UIViewController {
}
}

private func changeKeyboardHeight (newHeight: Double) {
let messageOne = "changeKeyboardHeight, newHeight :\(newHeight) isPortrait: \(isPortrait)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, messageOne)
/**
* Write the new height of the keyboard to to UserDefaults.
*/
private func writeNewKeyboardHeight (newHeight: Double) {
let message = "writeNewKeyboardHeight, newHeight :\(newHeight) isPortrait: \(isPortrait)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, message)

keyboardHeight = newHeight
self.updateKeyboardConstraints()
//self.view.setNeedsLayout()

if (isPortrait) {
Storage.active.userDefaults.portraitKeyboardHeight = newHeight
let persistMessage = "changeKeyboardHeight, persist portraitKeyboardHeight :\(newHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, persistMessage)
} else {
Storage.active.userDefaults.landscapeKeyboardHeight = newHeight
let persistMessage = "changeKeyboardHeight, persist landscapeKeyboardHeight :\(newHeight)"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, persistMessage)
}
}

Expand All @@ -250,7 +253,9 @@ class KeyboardHeightViewController: UIViewController {
NSLayoutConstraint.activate(self.keyboardConstraintsArray)
}

// recalculate constraints for new portrait keyboard height
/**
* recalculate constraints for new portrait keyboard height
*/
private func updateKeyboardConstraints() {
if (!self.keyboardConstraintsArray.isEmpty) {
NSLayoutConstraint.deactivate(self.keyboardConstraintsArray)
Expand Down Expand Up @@ -301,7 +306,7 @@ class KeyboardHeightViewController: UIViewController {
let defaultHeight = self.isPortrait ? self.defaultPortraitHeight : self.defaultLandscapeHeight

UIView.animate(withDuration: 1.1, delay: 0.1, usingSpringWithDamping: 0.35, initialSpringVelocity: 4.5, options: .curveEaseOut, animations: {
self.changeKeyboardHeight(newHeight: defaultHeight)
self.writeNewKeyboardHeight(newHeight: defaultHeight)
self.view.layoutIfNeeded()
}, completion: nil)
}
Expand Down Expand Up @@ -397,7 +402,7 @@ class KeyboardHeightViewController: UIViewController {
keyboardImage.image = kbImage

let kbImageMessage = "updateKeyboardImage, kbImage: \(String(describing: kbImage))"
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .info, kbImageMessage)
os_log("%{public}s", log:KeymanEngineLogger.ui, type: .debug, kbImageMessage)
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
Expand Down
4 changes: 2 additions & 2 deletions ios/engine/KMEI/KeymanEngine/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@
/* Label for the "Adjust Keyboard Height" item on the main settings screen */
"menu-settings-adjust-keyboard-height" = "Adjust Keyboard Height";

/* Title for the "Adjust Keyboard Height" item on the main settings screen */
"menu-settings-adjust-keyboard-height" = "Adjust Keyboard Height";
/* Title for the "Adjust Keyboard Height" settings child screen */
"adjust-keyboard-height-title" = "Adjust Keyboard";

/* Label for "Reset to Default Keyboard Height" button on the adjust height screen */
"button-label-reset-default-keyboard-height" = "Reset to Default Keyboard Height";
Expand Down

0 comments on commit c9d4c5c

Please sign in to comment.