Skip to content

Commit

Permalink
379 reduce force unwraps, use if/guard let and isEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis authored and SaurabhJamadagni committed Apr 18, 2024
1 parent 6c83590 commit c008c63
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 132 deletions.
4 changes: 2 additions & 2 deletions Keyboards/KeyboardsBase/Colors/ColorVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ var keyPressedColor = UIColor(.keyPressed)

var commandKeyColor = UIColor(.commandKey)
var commandBarColor = UIColor(.commandBar)
var commandBarBorderColor = UIColor(.commandBarBorder).cgColor
var commandBarBorderColor = UIColor(resource: .commandBarBorder).cgColor

var keyboardBgColor = UIColor(.keyboardBackground)
var keyShadowColor = UIColor(.keyShadow).cgColor
var keyShadowColor = UIColor(resource: .keyShadow).cgColor

// annotate colors.
var annotateRed = UIColor(.annotateRed)
Expand Down
3 changes: 1 addition & 2 deletions Keyboards/KeyboardsBase/InterfaceVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ func setKeyboard() {
func setKeyboardLayout() {
if commandState == .translate {
setENKeyboardLayout()
} else {
let setLayoutFxn: () -> Void = keyboardLayoutDict[controllerLanguage]!
} else if let setLayoutFxn = keyboardLayoutDict[controllerLanguage] {
setLayoutFxn()
}

Expand Down
85 changes: 43 additions & 42 deletions Keyboards/KeyboardsBase/KeyAltChars.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,50 +329,51 @@ func alternateKeysPathRight(
/// - sender: the long press of the given key.
func genAlternatesView(key: UIButton) {
// Get the frame in respect to the superview.
let frame = (key.superview?.convert(key.frame, to: nil))!
let width = key.frame.width

// Derive which button was pressed and get its alternates.
let char = key.layer.value(forKey: "original") as? String ?? ""
alternateKeys = keyAlternatesDict[char] ?? [""]

// Add the original key given its location on the keyboard.
if keysWithAlternatesLeft.contains(char) {
alternateKeys.insert(char, at: 0)
} else if keysWithAlternatesRight.contains(char) {
alternateKeys.append(char)
}
let numAlternates = CGFloat(alternateKeys.count)

if keysWithAlternatesLeft.contains(char) {
alternatesViewX = frame.origin.x - 4.0
alternatesShapeLayer.path = alternateKeysPathLeft(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: width, keyHeight: key.frame.height, numAlternates: numAlternates
).cgPath
} else if keysWithAlternatesRight.contains(char) {
alternatesViewX = frame.origin.x + width - CGFloat(width * numAlternates + (3.0 * numAlternates) + 2.0)
alternatesShapeLayer.path = alternateKeysPathRight(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: width, keyHeight: key.frame.height, numAlternates: numAlternates
).cgPath
}
if let frame = key.superview?.convert(key.frame, to: nil) {
let width = key.frame.width

// Derive which button was pressed and get its alternates.
let char = key.layer.value(forKey: "original") as? String ?? ""
alternateKeys = keyAlternatesDict[char] ?? [""]

// Add the original key given its location on the keyboard.
if keysWithAlternatesLeft.contains(char) {
alternateKeys.insert(char, at: 0)
} else if keysWithAlternatesRight.contains(char) {
alternateKeys.append(char)
}
let numAlternates = CGFloat(alternateKeys.count)

if keysWithAlternatesLeft.contains(char) {
alternatesViewX = frame.origin.x - 4.0
alternatesShapeLayer.path = alternateKeysPathLeft(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: width, keyHeight: key.frame.height, numAlternates: numAlternates
).cgPath
} else if keysWithAlternatesRight.contains(char) {
alternatesViewX = frame.origin.x + width - CGFloat(width * numAlternates + (3.0 * numAlternates) + 2.0)
alternatesShapeLayer.path = alternateKeysPathRight(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: width, keyHeight: key.frame.height, numAlternates: numAlternates
).cgPath
}

if numAlternates > 0 {
alternatesViewWidth = CGFloat(width * numAlternates + (3.0 * numAlternates) + 8.0)
}
if numAlternates > 0 {
alternatesViewWidth = CGFloat(width * numAlternates + (3.0 * numAlternates) + 8.0)
}

alternatesViewY = frame.origin.y - key.frame.height * 1.135
alternatesBtnHeight = key.frame.height * 0.9
alternatesKeyView = UIView(
frame: CGRect(
x: alternatesViewX,
y: alternatesViewY,
width: alternatesViewWidth,
height: key.frame.height * 1.2
alternatesViewY = frame.origin.y - key.frame.height * 1.135
alternatesBtnHeight = key.frame.height * 0.9
alternatesKeyView = UIView(
frame: CGRect(
x: alternatesViewX,
y: alternatesViewY,
width: alternatesViewWidth,
height: key.frame.height * 1.2
)
)
)

alternatesKeyView.tag = 1001
key.backgroundColor = keyColor
alternatesKeyView.tag = 1001
key.backgroundColor = keyColor
}
}
89 changes: 45 additions & 44 deletions Keyboards/KeyboardsBase/KeyAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,53 +270,54 @@ func centerKeyPopPath(
/// - displayChar: the character to display on the pop up.
func getKeyPopPath(key: UIButton, layer: CAShapeLayer, char: String, displayChar: String) {
// Get the frame in respect to the superview.
let frame = (key.superview?.convert(key.frame, to: nil))!
var labelVertPosition = frame.origin.y - key.frame.height / 1.75
// non-capital characters should be higher for portrait phone views.
if displayChar == char, DeviceType.isPhone, !isLandscapeView,
!["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].contains(char)
{
labelVertPosition = frame.origin.y - key.frame.height / 1.6
} else if DeviceType.isPad,
isLandscapeView
{
labelVertPosition = frame.origin.y - key.frame.height / 2
}

if centralKeyChars.contains(char) {
layer.path = centerKeyPopPath(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: key.frame.width, keyHeight: key.frame.height, char: char
).cgPath
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.5, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.5, y: labelVertPosition)
} else if leftKeyChars.contains(char) {
layer.path = leftKeyPopPath(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: key.frame.width, keyHeight: key.frame.height, char: char
).cgPath
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.85, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.85, y: labelVertPosition)
if DeviceType.isPad || (DeviceType.isPhone && isLandscapeView) {
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.65, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.65, y: labelVertPosition)
if let frame = key.superview?.convert(key.frame, to: nil) {
var labelVertPosition = frame.origin.y - key.frame.height / 1.75
// non-capital characters should be higher for portrait phone views.
if displayChar == char, DeviceType.isPhone, !isLandscapeView,
!["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].contains(char)
{
labelVertPosition = frame.origin.y - key.frame.height / 1.6
} else if DeviceType.isPad,
isLandscapeView
{
labelVertPosition = frame.origin.y - key.frame.height / 2
}
} else if rightKeyChars.contains(char) {
layer.path = rightKeyPopPath(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: key.frame.width, keyHeight: key.frame.height, char: char
).cgPath
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.15, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.15, y: labelVertPosition)
if DeviceType.isPad || (DeviceType.isPhone && isLandscapeView) {
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.35, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.35, y: labelVertPosition)

if centralKeyChars.contains(char) {
layer.path = centerKeyPopPath(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: key.frame.width, keyHeight: key.frame.height, char: char
).cgPath
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.5, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.5, y: labelVertPosition)
} else if leftKeyChars.contains(char) {
layer.path = leftKeyPopPath(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: key.frame.width, keyHeight: key.frame.height, char: char
).cgPath
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.85, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.85, y: labelVertPosition)
if DeviceType.isPad || (DeviceType.isPhone && isLandscapeView) {
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.65, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.65, y: labelVertPosition)
}
} else if rightKeyChars.contains(char) {
layer.path = rightKeyPopPath(
startX: frame.origin.x, startY: frame.origin.y,
keyWidth: key.frame.width, keyHeight: key.frame.height, char: char
).cgPath
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.15, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.15, y: labelVertPosition)
if DeviceType.isPad || (DeviceType.isPhone && isLandscapeView) {
keyPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.35, y: labelVertPosition)
keyHoldPopChar.center = CGPoint(x: frame.origin.x + key.frame.width * 0.35, y: labelVertPosition)
}
}
}

layer.strokeColor = keyShadowColor
layer.fillColor = keyColor.cgColor
layer.lineWidth = 1.0
layer.strokeColor = keyShadowColor
layer.fillColor = keyColor.cgColor
layer.lineWidth = 1.0
}
}

/// Sizes the character displayed on a key pop for iPhones.
Expand Down
6 changes: 0 additions & 6 deletions Keyboards/KeyboardsBase/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1855,12 +1855,6 @@ class KeyboardViewController: UIInputViewController {
}
}

if userDefaults.bool(forKey: "svAccentCharacters") {
disableAccentCharacters = true
} else {
disableAccentCharacters = false
}

// Actions to be done only on initial loads.
if isFirstKeyboardLoad {
shiftButtonState = .shift
Expand Down
7 changes: 6 additions & 1 deletion Keyboards/KeyboardsBase/ScribeFunctionality/CommandBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ class CommandBar: UILabel {

/// Allows the class to be accessed from Keyboard.xib.
class func instanceFromNib() -> UIView {
return UINib(nibName: "Keyboard", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
let nibContents = UINib(nibName: "Keyboard", bundle: nil).instantiate(withOwner: nil, options: nil)
if let view = nibContents.first as? UIView {
return view
} else {
fatalError("Failed to instantiate view from nib.")
}
}

var shadow: UIButton!
Expand Down
2 changes: 1 addition & 1 deletion Keyboards/KeyboardsBase/ScribeFunctionality/Plural.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func queryPlural(commandBar: UILabel) {
let args = [noun]
let outputCols = ["plural"]
wordToReturn = queryDBRow(query: query, outputCols: outputCols, args: args)[0]

guard !wordToReturn.isEmpty else {
commandState = .invalid
return
Expand Down
5 changes: 4 additions & 1 deletion Keyboards/KeyboardsBase/ScribeFunctionality/ScribeKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class ScribeKey: UIButton {

/// Allows the class to be accessed from Keyboard.xib.
class func instanceFromNib() -> UIView {
return UINib(nibName: "Keyboard", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
guard let nibContents = UINib(nibName: "Keyboard", bundle: nil).instantiate(withOwner: nil, options: nil).first as? UIView else {
fatalError("Failed to instantiate view from nib.")
}
return nibContents
}

/// Converts the Scribe key to an escape key to return to the base keyboard view.
Expand Down
16 changes: 6 additions & 10 deletions Scribe/AboutTab/AboutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import UIKit
import MessageUI
import StoreKit
import UIKit

final class AboutViewController: BaseTableViewController {

override var dataSet: [ParentTableCellModel] {
AboutTableData.aboutTableData
}
Expand All @@ -39,10 +38,10 @@ final class AboutViewController: BaseTableViewController {
// MARK: UITableViewDataSource

extension AboutViewController {

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: AboutTableViewCell.reuseIdentifier, for: indexPath) as! AboutTableViewCell

guard let cell = tableView.dequeueReusableCell(withIdentifier: AboutTableViewCell.reuseIdentifier, for: indexPath) as? AboutTableViewCell else {
fatalError("Failed to dequeue AboutTableViewCell.")
}
cell.configureCell(for: dataSet[indexPath.section].section[indexPath.row])

return cell
Expand All @@ -52,7 +51,6 @@ extension AboutViewController {
// MARK: UITableViewDelegate

extension AboutViewController {

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tableSection = dataSet[indexPath.section]
let section = tableSection.section[indexPath.row]
Expand Down Expand Up @@ -100,11 +98,10 @@ extension AboutViewController {

case .appLang: break
case .none: break
case .specificLang(_): break
case .specificLang: break
case .externalLink: break
}


if let selectedIndexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: selectedIndexPath, animated: false)
}
Expand Down Expand Up @@ -159,15 +156,14 @@ extension AboutViewController {
guard let encodedURLString = encodedString, let url = URL(string: encodedURLString) else { return }

let shareSheetVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)

present(shareSheetVC, animated: true, completion: nil)
}
}

// MARK: - MFMailComposeViewControllerDelegate

extension AboutViewController: MFMailComposeViewControllerDelegate {

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith _: MFMailComposeResult, error _: Error?) {
controller.dismiss(animated: true, completion: nil)
}
Expand Down
21 changes: 15 additions & 6 deletions Scribe/AppExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,29 @@ extension UIImage {
if let image = UIImage(named: imageString) {
return image
} else {
return UIImage(systemName: imageString) ?? UIImage(systemName: "info.circle")!
if let image = UIImage(systemName: imageString) {
return image
} else {
guard let infoCircleSymbol = UIImage(systemName: "info.circle") else {
fatalError("Failed to create info circle symbol image.")
}
return infoCircleSymbol
}
}
}
}

extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as? UIViewController
var currentResponder: UIResponder? = self

while let responder = currentResponder {
if let viewController = responder as? UIViewController {
return viewController
}
currentResponder = responder.next
}

return nil
}
}
Expand Down
8 changes: 6 additions & 2 deletions Scribe/AppUISymbols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func getSettingsSymbol(fontSize: CGFloat) -> UIImage {
settingsSymbolConfig = UIImage.SymbolConfiguration(pointSize: fontSize * 0.15, weight: .medium, scale: .medium)
}
}
let settingsSymbol = UIImage(systemName: "gearshape", withConfiguration: settingsSymbolConfig)!
guard let settingsSymbol = UIImage(systemName: "gearshape", withConfiguration: settingsSymbolConfig) else {
fatalError("Failed to create settings symbol image.")
}

return settingsSymbol
}
Expand All @@ -50,7 +52,9 @@ func getPrivacySymbol(fontSize: CGFloat) -> UIImage {
privacySymbolConfig = UIImage.SymbolConfiguration(pointSize: fontSize * 0.2, weight: .medium, scale: .medium)
}
}
let privacySymbol = UIImage(systemName: "lock.shield", withConfiguration: privacySymbolConfig)!
guard let privacySymbol = UIImage(systemName: "lock.shield", withConfiguration: privacySymbolConfig) else {
fatalError("Failed to create privacy symbol image.")
}

return privacySymbol
}
Expand Down
Loading

0 comments on commit c008c63

Please sign in to comment.