From 09d384b49a168bd4aa44f2ac60bb8f19b463227c Mon Sep 17 00:00:00 2001 From: Gerson Noboa Date: Fri, 20 Oct 2023 13:17:00 +0300 Subject: [PATCH] Fix crash related to force unwrapping and unowned self 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 --- GliaWidgets/Sources/View/Chat/ChatView.swift | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/GliaWidgets/Sources/View/Chat/ChatView.swift b/GliaWidgets/Sources/View/Chat/ChatView.swift index 07492095e..1874607b4 100644 --- a/GliaWidgets/Sources/View/Chat/ChatView.swift +++ b/GliaWidgets/Sources/View/Chat/ChatView.swift @@ -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 @@ -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) @@ -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 @@ -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() } )