Skip to content

Commit a07bf49

Browse files
committed
Add url opener and view extension
1 parent 55bf01f commit a07bf49

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

Readmes/Navigation.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Navigation
2+
3+
`SwiftUIKit` contains in-app `Navigation` utilities.
4+
5+
6+
## UrlOpener
7+
8+
The `UrlOpener` can be implemeneted by anything that should be able to open a url.
9+
10+
The protocol provides a `canOpen` and a `tryOpen` function, as well as many convenience variants.
11+
12+
The default implementation opens a url with the shared application. You can override this in any way you want.

Release Notes.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
## 0.9.4
55

6-
This version adds new extensions, e.g. for taking a screenshot of any `SwiftUI` view.
6+
This version adds a `UrlOpener`, as well as new extensions:
7+
8+
* `EdgeInsets+Edge` simplifies getting the inset for a certain edge.
9+
* `View+Screenshot` can be used to snapshot any SwiftUI view.
10+
* `View+Visible` has conditional extensions to hide or show a view.
711

812

913
## 0.9.3

Sources/SwiftUIKit/Extensions/EdgeInsets+Edge.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import CoreGraphics
1010
import SwiftUI
1111

1212
public extension EdgeInsets {
13-
13+
1414
/**
1515
Get the inset for a certain edge.
1616
*/
1717
func inset(for edge: Edge) -> CGFloat {
1818
switch edge {
19-
case.top: return top
20-
case.bottom: return bottom
21-
case.leading: return leading
22-
case.trailing: return trailing
19+
case.top: return top
20+
case.bottom: return bottom
21+
case.leading: return leading
22+
case.trailing: return trailing
2323
}
2424
}
2525
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// View+Visible.swift
3+
// SwiftUIKit
4+
//
5+
// Created by Daniel Saidi on 2020-10-05.
6+
// Copyright © 2020 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
public extension View {
12+
13+
/**
14+
Hides the view if the provided `condition` is `true`.
15+
*/
16+
@ViewBuilder
17+
func hidden(if condition: Bool) -> some View {
18+
if condition {
19+
self.hidden()
20+
} else {
21+
self
22+
}
23+
}
24+
25+
/**
26+
Shows the view if the provided `condition` is `true`.
27+
*/
28+
func visible(if condition: Bool) -> some View {
29+
hidden(if: !condition)
30+
}
31+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// UrlOpener.swift
3+
// SwiftUIKit
4+
//
5+
// Created by Daniel Saidi on 2020-10-05.
6+
// Copyright © 2020 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
/**
12+
This protocol can be implemented by anything that should be
13+
able to open a URL.
14+
15+
The default implementation uses the shared application when
16+
you call it in `iOS`. It currently has no implementation on
17+
the other platforms.
18+
*/
19+
public protocol UrlOpener {
20+
21+
typealias OpenUrlCompletion = (Bool) -> Void
22+
23+
func canOpen(_ url: URL) -> Bool
24+
func tryOpen(_ url: URL, completion: OpenUrlCompletion)
25+
}
26+
27+
28+
// MARK: - Default Implementations
29+
30+
public extension UrlOpener {
31+
32+
func canOpen(_ url: URL) -> Bool {
33+
#if os(iOS)
34+
return app.canOpenURL(url)
35+
#else
36+
assertionFailure("Not yet implemented")
37+
#endif
38+
}
39+
40+
func tryOpen(_ url: URL, completion: OpenUrlCompletion) {
41+
#if os(iOS)
42+
app.open(url, options: [:], completionHandler: nil)
43+
#else
44+
assertionFailure("Not yet implemented")
45+
#endif
46+
}
47+
}
48+
49+
50+
// MARK: - Convenience Functions
51+
52+
public extension UrlOpener {
53+
54+
#if os(iOS)
55+
var app: UIApplication { .shared }
56+
#endif
57+
58+
func canOpen(_ url: URL?) -> Bool {
59+
guard let url = url else { return false }
60+
return canOpen(url)
61+
}
62+
63+
func canOpen(urlString: String) -> Bool {
64+
canOpen(URL(string: urlString))
65+
}
66+
67+
func tryOpen(_ url: URL) {
68+
tryOpen(url) { _ in }
69+
}
70+
71+
func tryOpen(_ url: URL?, completion: OpenUrlCompletion = { _ in }) {
72+
guard let url = url else { return completion(false) }
73+
tryOpen(url, completion: completion)
74+
}
75+
76+
func tryOpen(urlString: String, completion: OpenUrlCompletion = { _ in }) {
77+
tryOpen(URL(string: urlString), completion: completion)
78+
}
79+
}

0 commit comments

Comments
 (0)