Skip to content

Commit

Permalink
Fix crash related to force unwrapping and unowned self
Browse files Browse the repository at this point in the history
There were a couple of instances of unowned self and force unwrapping of a
variable that could (and have) crashed the widgets under certain obscure
circumstances.

MOB-2773
  • Loading branch information
gersonnoboa authored and EgorovEI committed Oct 20, 2023
1 parent 3308922 commit 09d384b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions GliaWidgets/Sources/View/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ChatView: EngagementView {
private lazy var quickReplyView = QuickReplyView(
style: style.gliaVirtualAssistant.quickReplyButton
)
private var messageEntryViewBottomConstraint: NSLayoutConstraint!
private var messageEntryViewBottomConstraint: NSLayoutConstraint?
private var callBubble: BubbleView?
private let keyboardObserver = KeyboardObserver()
private let kUnreadMessageIndicatorInset: CGFloat = -3
Expand Down Expand Up @@ -172,7 +172,9 @@ class ChatView: EngagementView {
edges: .bottom,
insets: messageEntryInsets
).first
constraints += messageEntryViewBottomConstraint
if let messageEntryViewBottomConstraint {
constraints += messageEntryViewBottomConstraint
}

constraints += messageEntryView.layoutIn(safeAreaLayoutGuide, edges: .horizontal)
constraints += messageEntryView.topAnchor.constraint(equalTo: quickReplyView.bottomAnchor)
Expand Down Expand Up @@ -584,15 +586,18 @@ extension ChatView {

extension ChatView {
private func observeKeyboard() {
keyboardObserver.keyboardWillShow = { [unowned self] properties in
let bottomInset = safeAreaInsets.bottom
keyboardObserver.keyboardWillShow = { [weak self] properties in
guard let self else { return }

let bottomInset = self.safeAreaInsets.bottom
let newEntryConstraint = -properties.finalFrame.height + bottomInset

UIView.animate(
withDuration: properties.duration,
delay: 0.0,
options: properties.animationOptions,
animations: { [weak self] in
self?.messageEntryViewBottomConstraint.constant = newEntryConstraint
self?.messageEntryViewBottomConstraint?.constant = newEntryConstraint
self?.layoutIfNeeded()
},
completion: { [weak self] _ in
Expand All @@ -601,13 +606,13 @@ extension ChatView {
)
}

keyboardObserver.keyboardWillHide = { [unowned self] properties in
keyboardObserver.keyboardWillHide = { [weak self] properties in
UIView.animate(
withDuration: properties.duration,
delay: 0.0,
options: properties.animationOptions,
animations: { [weak self] in
self?.messageEntryViewBottomConstraint.constant = 0
self?.messageEntryViewBottomConstraint?.constant = 0
self?.layoutIfNeeded()
}
)
Expand Down

0 comments on commit 09d384b

Please sign in to comment.