Commit ea59f7c4 by Daniel Jonathan Committed by GitHub

Merge pull request #1276 from jbaez/dialog-fix

Fix crash when Dialog builder already deallocated when callback executed
parents 059ed9c3 24e9d6d6
...@@ -137,8 +137,9 @@ open class Dialog: NSObject { ...@@ -137,8 +137,9 @@ open class Dialog: NSObject {
@discardableResult @discardableResult
open func positive(_ title: String?, handler: (() -> Void)?) -> Dialog { open func positive(_ title: String?, handler: (() -> Void)?) -> Dialog {
dialogView.positiveButton.title = title dialogView.positiveButton.title = title
controller.didTapPositiveButtonHandler = { [unowned self] in controller.didTapPositiveButtonHandler = { [weak self] in
self.delegate?.dialog?(self, didTapPositive: self.controller.dialogView.positiveButton) guard let strongSelf = self else { return }
strongSelf.delegate?.dialog?(strongSelf, didTapPositive: strongSelf.controller.dialogView.positiveButton)
handler?() handler?()
} }
return self return self
...@@ -153,8 +154,9 @@ open class Dialog: NSObject { ...@@ -153,8 +154,9 @@ open class Dialog: NSObject {
@discardableResult @discardableResult
open func negative(_ title: String?, handler: (() -> Void)?) -> Dialog { open func negative(_ title: String?, handler: (() -> Void)?) -> Dialog {
dialogView.negativeButton.title = title dialogView.negativeButton.title = title
controller.didTapNegativeButtonHandler = { [unowned self] in controller.didTapNegativeButtonHandler = { [weak self] in
self.delegate?.dialog?(self, didTapNegative: self.controller.dialogView.negativeButton) guard let strongSelf = self else { return }
strongSelf.delegate?.dialog?(strongSelf, didTapNegative: strongSelf.controller.dialogView.negativeButton)
handler?() handler?()
} }
return self return self
...@@ -169,8 +171,9 @@ open class Dialog: NSObject { ...@@ -169,8 +171,9 @@ open class Dialog: NSObject {
@discardableResult @discardableResult
open func neutral(_ title: String?, handler: (() -> Void)?) -> Dialog { open func neutral(_ title: String?, handler: (() -> Void)?) -> Dialog {
dialogView.neutralButton.title = title dialogView.neutralButton.title = title
controller.didTapNeutralButtonHandler = { [unowned self] in controller.didTapNeutralButtonHandler = { [weak self] in
self.delegate?.dialog?(self, didTapNeutral: self.controller.dialogView.neutralButton) guard let strongSelf = self else { return }
strongSelf.delegate?.dialog?(strongSelf, didTapNeutral: strongSelf.controller.dialogView.neutralButton)
handler?() handler?()
} }
return self return self
...@@ -185,8 +188,9 @@ open class Dialog: NSObject { ...@@ -185,8 +188,9 @@ open class Dialog: NSObject {
@discardableResult @discardableResult
open func isCancelable(_ value: Bool, handler: (() -> Void)? = nil) -> Dialog { open func isCancelable(_ value: Bool, handler: (() -> Void)? = nil) -> Dialog {
controller.isCancelable = value controller.isCancelable = value
controller.didCancelHandler = { [unowned self] in controller.didCancelHandler = { [weak self] in
self.delegate?.dialogDidCancel?(self) guard let strongSelf = self else { return }
strongSelf.delegate?.dialogDidCancel?(strongSelf)
handler?() handler?()
} }
return self return self
...@@ -200,8 +204,9 @@ open class Dialog: NSObject { ...@@ -200,8 +204,9 @@ open class Dialog: NSObject {
*/ */
@discardableResult @discardableResult
open func shouldDismiss(handler: ((DialogView, Button?) -> Bool)?) -> Dialog { open func shouldDismiss(handler: ((DialogView, Button?) -> Bool)?) -> Dialog {
controller.shouldDismissHandler = { [unowned self] dialogView, button in controller.shouldDismissHandler = { [weak self] dialogView, button in
let d = self.delegate?.dialog?(self, shouldDismiss: button) ?? true guard let strongSelf = self else { return true }
let d = strongSelf.delegate?.dialog?(strongSelf, shouldDismiss: button) ?? true
let h = handler?(dialogView, button) ?? true let h = handler?(dialogView, button) ?? true
return d && h return d && h
} }
...@@ -215,8 +220,9 @@ open class Dialog: NSObject { ...@@ -215,8 +220,9 @@ open class Dialog: NSObject {
*/ */
@discardableResult @discardableResult
open func willAppear(handler: (() -> Void)?) -> Dialog { open func willAppear(handler: (() -> Void)?) -> Dialog {
controller.willAppear = { [unowned self] in controller.willAppear = { [weak self] in
self.delegate?.dialogWillAppear?(self) guard let strongSelf = self else { return }
strongSelf.delegate?.dialogWillAppear?(strongSelf)
handler?() handler?()
} }
return self return self
...@@ -229,10 +235,11 @@ open class Dialog: NSObject { ...@@ -229,10 +235,11 @@ open class Dialog: NSObject {
*/ */
@discardableResult @discardableResult
open func didDisappear(handler: (() -> Void)?) -> Dialog { open func didDisappear(handler: (() -> Void)?) -> Dialog {
controller.didDisappear = { [unowned self] in controller.didDisappear = { [weak self] in
self.delegate?.dialogDidDisappear?(self) guard let strongSelf = self else { return }
strongSelf.delegate?.dialogDidDisappear?(strongSelf)
handler?() handler?()
self.controller.dialog = nil strongSelf.controller.dialog = nil
} }
return self return self
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment