Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Add "Try Again" button for EmptyView (#2214) #2226

Merged
merged 1 commit into from
Oct 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Classes/Issues/IssuesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class IssuesViewController:
FlatCacheListener,
IssueCommentSectionControllerDelegate,
IssueTextActionsViewSendDelegate,
EmptyViewDelegate,
MessageTextViewListener {

private let client: GithubClient
Expand Down Expand Up @@ -501,6 +502,8 @@ final class IssuesViewController:
case .idle:
let emptyView = EmptyView()
emptyView.label.text = NSLocalizedString("Issue cannot be found", comment: "")
emptyView.delegate = self
emptyView.button.isHidden = false
return emptyView
case .loading, .loadingNext:
return nil
Expand Down Expand Up @@ -615,6 +618,12 @@ final class IssuesViewController:
}
}

// MARK: EmptyViewDelegate

func didTapRetry() {
self.feed.refreshHead()
}

// MARK: MessageTextViewListener

func didChange(textView: MessageTextView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class PullRequestReviewCommentsViewController: MessageViewController,
ListAdapterDataSource,
FeedDelegate,
PullRequestReviewReplySectionControllerDelegate,
EmptyViewDelegate,
IssueTextActionsViewSendDelegate {

private let model: IssueDetailsModel
Expand Down Expand Up @@ -169,6 +170,8 @@ final class PullRequestReviewCommentsViewController: MessageViewController,
case .idle:
let emptyView = EmptyView()
emptyView.label.text = NSLocalizedString("Error loading review comments.", comment: "")
emptyView.delegate = self
emptyView.button.isHidden = false
return emptyView
case .loadingNext:
return nil
Expand All @@ -187,6 +190,12 @@ final class PullRequestReviewCommentsViewController: MessageViewController,
focusedReplyModel = reply
}

// MARK: EmptyViewDelegate

func didTapRetry() {
self.feed.refreshHead()
}

// MARK: IssueTextActionsViewSendDelegate

func didSend(for actionsView: IssueTextActionsView) {
Expand Down
10 changes: 9 additions & 1 deletion Classes/Repository/RepositoryCodeBlobViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import Squawk

final class RepositoryCodeBlobViewController: UIViewController {
final class RepositoryCodeBlobViewController: UIViewController, EmptyViewDelegate {

private let client: GithubClient
private let branch: String
Expand Down Expand Up @@ -55,6 +55,8 @@ final class RepositoryCodeBlobViewController: UIViewController {
view.backgroundColor = .white

emptyView.isHidden = true
emptyView.delegate = self
emptyView.button.isHidden = false
view.addSubview(emptyView)
view.addSubview(codeView)

Expand Down Expand Up @@ -136,4 +138,10 @@ final class RepositoryCodeBlobViewController: UIViewController {
codeView.set(code: text)
}

// MARK: EmptyViewDelegate

func didTapRetry() {
self.onRefresh()
}

}
12 changes: 12 additions & 0 deletions Classes/Repository/RepositoryWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ extension RepositoryWebViewController: WKNavigationDelegate {

}

// MARK: - RepositoryWebViewController (EmptyViewDelegate) -

extension RepositoryWebViewController: EmptyViewDelegate {

func didTapRetry() {
self.fetch()
}

}

// MARK: - RepositoryWebViewController (Fetch Data) -

extension RepositoryWebViewController {
Expand Down Expand Up @@ -179,6 +189,8 @@ extension RepositoryWebViewController {

state = .idle
emptyView.isHidden = true
emptyView.delegate = self
emptyView.button.isHidden = false

view.backgroundColor = .white
view.addSubview(emptyView)
Expand Down
22 changes: 17 additions & 5 deletions Classes/Search/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class SearchViewController: UIViewController,
ListAdapterDataSource,
PrimaryViewController,
UISearchBarDelegate,
InitialEmptyViewDelegate,
SearchRecentSectionControllerDelegate,
SearchRecentHeaderSectionControllerDelegate,
TabNavRootViewControllerType,
SearchResultSectionControllerDelegate {
EmptyViewDelegate,
InitialEmptyViewDelegate,
SearchRecentSectionControllerDelegate,
SearchRecentHeaderSectionControllerDelegate,
TabNavRootViewControllerType,
SearchResultSectionControllerDelegate {

private let client: GithubClient
private let noResultsKey = "com.freetime.SearchViewController.no-results-key" as ListDiffable
Expand Down Expand Up @@ -188,6 +189,8 @@ SearchResultSectionControllerDelegate {
case .error:
let view = EmptyView()
view.label.text = NSLocalizedString("Error finding results", comment: "")
view.delegate = self
view.button.isHidden = false
return view
case .results:
return nil
Expand Down Expand Up @@ -226,6 +229,15 @@ SearchResultSectionControllerDelegate {
update(animated: false)
}

// MARK: EmptyViewDelegate

func didTapRetry() {
searchBar.resignFirstResponder()

guard let term = searchTerm(for: searchBar.text) else { return }
search(term: term)
}

// MARK: InitialEmptyViewDelegate

func didTap(emptyView: InitialEmptyView) {
Expand Down
9 changes: 9 additions & 0 deletions Classes/View Controllers/BaseListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protocol BaseListViewControllerDataSource: class {
class BaseListViewController<PagingType: ListDiffable>: UIViewController,
ListAdapterDataSource,
FeedDelegate,
EmptyViewDelegate,
LoadMoreSectionControllerDelegate {

// required on init
Expand Down Expand Up @@ -160,6 +161,8 @@ LoadMoreSectionControllerDelegate {
guard hasError else { return nil }
let empty = EmptyView()
empty.label.text = emptyErrorMessage
empty.delegate = self
empty.button.isHidden = false
return empty
}

Expand All @@ -174,6 +177,12 @@ LoadMoreSectionControllerDelegate {
return false
}

// MARK: EmptyViewDelegate

func didTapRetry() {
self.feed.refreshHead()
}

// MARK: LoadMoreSectionControllerDelegate

final func didSelect(sectionController: LoadMoreSectionController) {
Expand Down
1 change: 1 addition & 0 deletions Classes/Views/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ enum Constants {
static let assignees = NSLocalizedString("Assignees", comment: "")
static let reviewers = NSLocalizedString("Reviewers", comment: "")
static let reviewGitHubAccess = NSLocalizedString("Review GitHub Access", comment: "")
static let tryAgain = NSLocalizedString("Try Again", comment: "")
}
}
22 changes: 22 additions & 0 deletions Classes/Views/EmptyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
import UIKit
import SnapKit

protocol EmptyViewDelegate: class {
func didTapRetry()
}

final class EmptyView: UIView {

let label = UILabel()
let button = UIButton(type: .system)
var delegate: EmptyViewDelegate?

override init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -22,14 +28,30 @@ final class EmptyView: UIView {
label.textColor = Styles.Colors.Gray.medium.color
label.numberOfLines = 0
addSubview(label)

button.isHidden = true
button.titleLabel?.font = Styles.Text.button.preferredFont
button.setTitle(Constants.Strings.tryAgain, for: .normal)
button.setTitleColor(Styles.Colors.Blue.medium.color, for: .normal)
button.addTarget(self, action: #selector(tapRetry), for: .touchUpInside)
addSubview(button)

label.snp.makeConstraints { make in
make.center.equalTo(self)
make.width.lessThanOrEqualToSuperview().offset(-Styles.Sizes.gutter)
}

button.snp.makeConstraints { make in
make.centerX.equalTo(self)
make.top.equalTo(label).offset(Styles.Sizes.gutter)
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc private func tapRetry(sender: UIButton) {
delegate?.didTapRetry()
}
}