This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bump demo app to iOS 14 * Convert error view to SwiftUI * Move ErrorPresenter in and make delegate optional * reload -> retry * Make ErrorPresenter public * Document error handling * [skip ci] Update CHANGELOG
- Loading branch information
1 parent
847bc87
commit e069cfb
Showing
8 changed files
with
125 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import SwiftUI | ||
import UIKit | ||
|
||
public protocol ErrorPresenter: UIViewController { | ||
typealias Handler = () -> Void | ||
|
||
func presentError(_ error: Error, handler: @escaping Handler) | ||
} | ||
|
||
public extension ErrorPresenter { | ||
func presentError(_ error: Error, handler: @escaping () -> Void) { | ||
let errorView = ErrorView(error: error) { [unowned self] in | ||
handler() | ||
self.removeErrorViewController() | ||
} | ||
|
||
let controller = UIHostingController(rootView: errorView) | ||
addChild(controller) | ||
addFullScreenSubview(controller.view) | ||
controller.didMove(toParent: self) | ||
} | ||
|
||
private func removeErrorViewController() { | ||
if let child = children.first(where: { $0 is UIHostingController<ErrorView> }) { | ||
child.willMove(toParent: nil) | ||
child.view.removeFromSuperview() | ||
child.removeFromParent() | ||
} | ||
} | ||
} | ||
|
||
extension UIViewController: ErrorPresenter {} | ||
|
||
// MARK: Private | ||
|
||
private struct ErrorView: View { | ||
let error: Error | ||
let handler: ErrorPresenter.Handler? | ||
|
||
var body: some View { | ||
VStack(spacing: 16) { | ||
Image(systemName: "exclamationmark.triangle") | ||
.font(.system(size: 38, weight: .semibold)) | ||
.foregroundColor(.accentColor) | ||
|
||
Text("Error loading page") | ||
.font(.largeTitle) | ||
|
||
Text(error.localizedDescription) | ||
.font(.body) | ||
.multilineTextAlignment(.center) | ||
|
||
Button("Retry") { | ||
handler?() | ||
} | ||
.font(.system(size: 17, weight: .bold)) | ||
} | ||
.padding(32) | ||
} | ||
} | ||
|
||
private struct ErrorView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
return ErrorView(error: NSError( | ||
domain: "com.example.error", | ||
code: 1001, | ||
userInfo: [NSLocalizedDescriptionKey: "Could not connect to the server."] | ||
)) {} | ||
} | ||
} | ||
|
||
private extension UIViewController { | ||
func addFullScreenSubview(_ subview: UIView) { | ||
view.addSubview(subview) | ||
subview.translatesAutoresizingMaskIntoConstraints = false | ||
NSLayoutConstraint.activate([ | ||
subview.leadingAnchor.constraint(equalTo: view.leadingAnchor), | ||
subview.trailingAnchor.constraint(equalTo: view.trailingAnchor), | ||
subview.topAnchor.constraint(equalTo: view.topAnchor), | ||
subview.bottomAnchor.constraint(equalTo: view.bottomAnchor) | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters