Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
08ec50d1
Commit
08ec50d1
authored
Oct 17, 2018
by
Orkhan Alikhanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added DialogDelegate
parent
7f72f648
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
163 additions
and
15 deletions
+163
-15
Sources/iOS/Dialog.swift
+163
-15
No files found.
Sources/iOS/Dialog.swift
View file @
08ec50d1
...
...
@@ -32,19 +32,92 @@
*/
import
UIKit
import
Motion
@objc
public
protocol
DialogDelegate
{
/**
A delegation method that is executed when the Dialog is cancelled through tapping background.
- Parameter _ dialog: A Dialog.
*/
@objc
optional
func
dialogDidCancel
(
_
dialog
:
Dialog
)
/**
A delegation method that is executed when the Dialog will appear.
- Parameter _ dialog: A Dialog.
*/
@objc
optional
func
dialogWillAppear
(
_
dialog
:
Dialog
)
/**
A delegation method that is executed when the Dialog did disappear.
- Parameter _ dialog: A Dialog.
*/
@objc
optional
func
dialogDidDisappear
(
_
dialog
:
Dialog
)
/**
A delegation method that is executed to determine if the Dialog should be dismissed.
- Parameter _ dialog: A Dialog.
- Parameter shouldDismiss button: The tapped button. nil if dialog is being
cancelled through tapping background.
- Returns: A Boolean.
*/
@objc
optional
func
dialog
(
_
dialog
:
Dialog
,
shouldDismiss
button
:
Button
?)
->
Bool
/**
A delegation method that is executed when the positive button of Dialog is tapped.
- Parameter _ dialog: A Dialog.
- Parameter didTapPositive button: A Button.
*/
@objc
optional
func
dialog
(
_
dialog
:
Dialog
,
didTapPositive
button
:
Button
)
/**
A delegation method that is executed when the negative button of Dialog is tapped.
- Parameter _ dialog: A Dialog.
- Parameter didTapNegative button: A Button.
*/
@objc
optional
func
dialog
(
_
dialog
:
Dialog
,
didTapNegative
button
:
Button
)
/**
A delegation method that is executed when the neutral button of Dialog is tapped.
- Parameter _ dialog: A Dialog.
- Parameter didTapNeutral button: A Button.
*/
@objc
optional
func
dialog
(
_
dialog
:
Dialog
,
didTapNeutral
button
:
Button
)
}
/// A builder for DialogController.
open
class
Dialog
{
open
class
Dialog
:
NSObject
{
/// A reference to dialog controller.
public
let
controller
=
DialogController
<
DialogView
>
()
/// A weak reference to DialogDelegate.
open
weak
var
delegate
:
DialogDelegate
?
/// An empty initializer.
public
init
()
{
}
public
override
init
()
{
super
.
init
()
/// Set callbacks for delegate.
_
=
shouldDismiss
(
handler
:
nil
)
.
positive
(
nil
,
handler
:
nil
)
.
negative
(
nil
,
handler
:
nil
)
.
neutral
(
nil
,
handler
:
nil
)
.
isCancelable
(
controller
.
isCancelable
,
handler
:
nil
)
.
willAppear
(
handler
:
nil
)
.
didDisappear
(
handler
:
nil
)
}
/**
Sets title of the dialog.
- Parameter _ text: A string.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
open
func
title
(
_
text
:
String
?)
->
Dialog
{
dialogView
.
titleLabel
.
text
=
text
...
...
@@ -54,7 +127,7 @@ open class Dialog {
/**
Sets details of the dialog.
- Parameter _ text: A string.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
open
func
details
(
_
text
:
String
?)
->
Dialog
{
dialogView
.
detailsLabel
.
text
=
text
...
...
@@ -65,11 +138,14 @@ open class Dialog {
Sets title and handler for positive button of dialog.
- Parameter _ title: A string.
- Parameter handler: A closure handling tap.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
open
func
positive
(
_
title
:
String
?,
handler
:
(()
->
Void
)?)
->
Dialog
{
dialogView
.
positiveButton
.
title
=
title
controller
.
didTapPositiveButtonHandler
=
handler
controller
.
didTapPositiveButtonHandler
=
{
[
unowned
self
]
in
self
.
delegate
?
.
dialog
?(
self
,
didTapPositive
:
self
.
controller
.
dialogView
.
positiveButton
)
handler
?()
}
return
self
}
...
...
@@ -77,11 +153,14 @@ open class Dialog {
Sets title and handler for negative button of dialog.
- Parameter _ title: A string.
- Parameter handler: A closure handling tap.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
open
func
negative
(
_
title
:
String
?,
handler
:
(()
->
Void
)?)
->
Dialog
{
dialogView
.
negativeButton
.
title
=
title
controller
.
didTapNegativeButtonHandler
=
handler
controller
.
didTapNegativeButtonHandler
=
{
[
unowned
self
]
in
self
.
delegate
?
.
dialog
?(
self
,
didTapNegative
:
self
.
controller
.
dialogView
.
negativeButton
)
handler
?()
}
return
self
}
...
...
@@ -89,11 +168,14 @@ open class Dialog {
Sets title and handler for neutral button of dialog.
- Parameter _ title: A string.
- Parameter handler: A closure handling tap.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
open
func
neutral
(
_
title
:
String
?,
handler
:
(()
->
Void
)?)
->
Dialog
{
dialogView
.
neutralButton
.
title
=
title
controller
.
didTapNeutralButtonHandler
=
handler
controller
.
didTapNeutralButtonHandler
=
{
[
unowned
self
]
in
self
.
delegate
?
.
dialog
?(
self
,
didTapNeutral
:
self
.
controller
.
dialogView
.
neutralButton
)
handler
?()
}
return
self
}
...
...
@@ -101,11 +183,14 @@ open class Dialog {
Sets cancelability of dialog and handler for when it's cancelled.
- Parameter _ value: A Bool.
- Parameter handler: A closure handling cancellation.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
open
func
isCancelable
(
_
value
:
Bool
,
handler
:
(()
->
Void
)?
=
nil
)
->
Dialog
{
controller
.
isCancelable
=
value
controller
.
didCancelHandler
=
handler
controller
.
didCancelHandler
=
{
[
unowned
self
]
in
self
.
delegate
?
.
dialogDidCancel
?(
self
)
handler
?()
}
return
self
}
...
...
@@ -113,20 +198,62 @@ open class Dialog {
Sets should-dismiss handler of dialog which takes dialogView and tapped
button and returns a boolean indicating if dialog should be dismissed.
- Parameter handler: A closure handling if dialog can be dismissed.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
open
func
shouldDismiss
(
handler
:
((
DialogView
,
Button
?)
->
Bool
)?)
->
Dialog
{
controller
.
shouldDismissHandler
=
handler
controller
.
shouldDismissHandler
=
{
[
unowned
self
]
dialogView
,
button
in
let
d
=
self
.
delegate
?
.
dialog
?(
self
,
shouldDismiss
:
button
)
??
true
let
h
=
handler
?(
dialogView
,
button
)
??
true
return
d
&&
h
}
return
self
}
/**
Sets handler for when view controller will appear.
- Parameter handler: A closure handling the event.
- Returns: Dialog itself to allow chaining.
*/
open
func
willAppear
(
handler
:
(()
->
Void
)?)
->
Dialog
{
controller
.
willAppear
=
{
[
unowned
self
]
in
self
.
delegate
?
.
dialogWillAppear
?(
self
)
handler
?()
}
return
self
}
/**
Sets handler for when view controller did disappear.
- Parameter handler: A closure handling the event.
- Returns: Dialog itself to allow chaining.
*/
open
func
didDisappear
(
handler
:
(()
->
Void
)?)
->
Dialog
{
controller
.
didDisappear
=
{
[
unowned
self
]
in
self
.
delegate
?
.
dialogDidDisappear
?(
self
)
handler
?()
self
.
controller
.
dialog
=
nil
}
return
self
}
/**
Sets dialog delegate.
- Parameter delegate: A DialogDelegate.
- Returns: Dialog itself to allow chaining.
*/
open
func
delegate
(
_
delegate
:
DialogDelegate
)
->
Dialog
{
self
.
delegate
=
delegate
return
self
}
/**
Presents dialog modally from given viewController.
- Parameter _ viewController: A UIViewController.
- Returns: Dialog
Builder
itself to allow chaining.
- Returns: Dialog itself to allow chaining.
*/
@discardableResult
open
func
show
(
_
viewController
:
UIViewController
)
->
Dialog
{
controller
.
dialog
=
self
viewController
.
present
(
controller
,
animated
:
true
,
completion
:
nil
)
return
self
}
...
...
@@ -138,3 +265,24 @@ private extension Dialog {
return
controller
.
dialogView
}
}
/// A memory reference to companion Dialog instance.
private
var
DialogKey
:
UInt8
=
0
private
extension
DialogController
{
/**
A Dialog instance attached to the dialog controller.
This is used to keep Dialog alive throughout the lifespan
of the controller.
*/
var
dialog
:
Dialog
?
{
get
{
return
AssociatedObject
.
get
(
base
:
self
,
key
:
&
DialogKey
)
{
return
nil
}
}
set
(
value
)
{
AssociatedObject
.
set
(
base
:
self
,
key
:
&
DialogKey
,
value
:
value
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment