Skip to content

Commit 964c4ef

Browse files
committed
ErrorView action added
1 parent 28b0811 commit 964c4ef

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

Example/LoadingViewController/ViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class ViewController: LoadingViewController {
2020
self?.setVisibleScreen(.Loading)
2121
self?.delay(3, closure: { [weak self] in
2222
self?.noDataMessage = "You don't have any search results. Please, refine your search criteria and try again."
23-
self?.setVisibleScreen(.NoData)
23+
self?.setVisibleScreen(.Failure, actionHandler: {
24+
25+
})
2426
})
2527
}
2628
}

LoadingViewController/Classes/LoadingViewController.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ public class LoadingViewController: UIViewController {
6363
}
6464
}
6565
public var errorIcon: UIImage?
66-
public var errorAction: String?
6766

68-
var noDataAction: String?
69-
public var noDataMessage: String = NSLocalizedString("No data availabel", comment: "")
67+
public var noDataMessage: String = NSLocalizedString("No data available. Please, try another request.", comment: "")
7068

7169
var contentViewAllwaysAvailabel: Bool = false
7270

@@ -78,8 +76,8 @@ public class LoadingViewController: UIViewController {
7876
return view
7977
}
8078

81-
func defaultErrorView() -> UIView {
82-
let view = ErrorView.viewWithStyle(errorViewStyle(), actionHandler: nil)
79+
func defaultErrorView(action: ActionHandler? = nil) -> UIView {
80+
let view = ErrorView.viewWithStyle(errorViewStyle(), actionHandler: action)
8381
view.title = errorTitle
8482
view.message = errorMessage
8583
view.image = errorIcon
@@ -127,12 +125,12 @@ public class LoadingViewController: UIViewController {
127125
view.layoutIfNeeded()
128126
}
129127

130-
func viewForScreen(contentType: ContentType) -> UIView {
128+
func viewForScreen(contentType: ContentType, action: ActionHandler? = nil) -> UIView {
131129
switch contentType {
132130
case .Content:
133131
return contentView
134132
case .Failure:
135-
return defaultErrorView()
133+
return defaultErrorView(action)
136134
case .Loading:
137135
return defaultLoadingView()
138136
case .NoData:
@@ -145,10 +143,12 @@ public class LoadingViewController: UIViewController {
145143
}
146144

147145
// TODO: add ActionHandler support to handle 'Retry' tap on ErrorViews
146+
148147
public func setVisibleScreen(contentType: ContentType, animated: Bool = true, actionHandler:ActionHandler? = nil) {
149148
if visibleContentType != contentType {
150149
visibleContentType = contentType
151-
setActiveView(viewForScreen(visibleContentType), animated: animated)
150+
let view = viewForScreen(visibleContentType, action: actionHandler)
151+
setActiveView(view, animated: animated)
152152
}
153153
}
154154

LoadingViewController/Classes/Views/ErrorViews/ErrorView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ErrorView: UIView {
3232
}
3333
}
3434

35-
static func viewWithStyle(style: ErrorViewStyle, actionHandler:ActionHandler?) -> ErrorView {
35+
static func viewWithStyle(style: ErrorViewStyle, actionHandler:ActionHandler? = nil) -> ErrorView {
3636

3737
switch style {
3838
case .Simple:

LoadingViewController/Classes/Views/ErrorViews/SimpleErrorView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ class SimpleErrorView: ErrorView {
3030
guard let newImage = image else { return }
3131
imageView?.image = newImage
3232
}
33+
34+
@IBAction func action(sender: AnyObject) {
35+
action?()
36+
}
3337
}

LoadingViewController/Classes/Views/ErrorViews/SimpleErrorView.xib

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<state key="highlighted">
3838
<color key="titleColor" red="0.24313725489999999" green="0.42352941179999998" blue="0.5450980392" alpha="1" colorSpace="calibratedRGB"/>
3939
</state>
40+
<connections>
41+
<action selector="action:" destination="iN0-l3-epB" eventType="touchUpInside" id="V8e-tp-Lzm"/>
42+
</connections>
4043
</button>
4144
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" verticalCompressionResistancePriority="250" image="sad.png" translatesAutoresizingMaskIntoConstraints="NO" id="pyy-8x-tJd">
4245
<rect key="frame" x="46" y="64" width="128" height="128"/>

0 commit comments

Comments
 (0)