Skip to content

Commit ebc2d5c

Browse files
committed
Allow customisation. Added support SwiftUI.
1 parent 465ffbf commit ebc2d5c

9 files changed

+98
-71
lines changed

README.md

+37-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ I tried to recreate Apple's alerts as much as possible. You can find these alert
55

66
![Alert Kit v5](https://cdn.sparrowcode.io/github/alertkit/v5/preview-v1_2.png)
77

8-
For run alert just call this:
8+
For UIKit & SwiftUI call this:
99

1010
```swift
1111
AlertKitAPI.present(
@@ -27,25 +27,31 @@ public enum AlertViewStyle {
2727
```
2828

2929
### Community
30-
30+
3131
<p float="left">
3232
<a href="https://twitter.com/sparrowcode_en">
3333
<img src="https://cdn.sparrowcode.io/github%2Fbadges%2Ftwitter.png?version=4" height="52">
3434
</a>
3535
<a href="https://t.me/sparrowcode_en">
3636
<img src="https://cdn.sparrowcode.io/github/badges/telegram.png?version=1" height="52">
3737
</a>
38-
<a href="https://mastodon.social/@sparrowcode_en">
39-
<img src="https://cdn.sparrowcode.io/github/badges/mastodon.png?version=2" height="52">
40-
</a>
4138
<a href="#apps-using">
4239
<img src="https://cdn.sparrowcode.io/github/badges/download-on-the-appstore.png?version=4" height="52">
4340
</a>
4441
</p>
4542

43+
## Navigate
44+
45+
- [Installation](#installation)
46+
- [Swift Package Manager](#swift-package-manager)
47+
- [CocoaPods](#cocoapods)
48+
- [SwiftUI](#swiftui)
49+
- [Customisation](#customisation)
50+
- [Apps Using](#apps-using)
51+
4652
## Installation
4753

48-
Ready to use on iOS 13+.
54+
Ready to use on iOS 13+. Supports iOS and visionOS. Working with `UIKit` and `SwiftUI`.
4955

5056
### Swift Package Manager
5157

@@ -59,7 +65,7 @@ or adding it to the `dependencies` of your `Package.swift`:
5965

6066
```swift
6167
dependencies: [
62-
.package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.0.0"))
68+
.package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.1.0"))
6369
]
6470
```
6571

@@ -80,11 +86,35 @@ pod 'SPAlert'
8086

8187
If you prefer not to use any of dependency managers, you can integrate manually. Put `Sources/AlertKit` folder in your Xcode project. Make sure to enable `Copy items if needed` and `Create groups`.
8288

89+
## SwiftUI
90+
91+
You can use basic way via AlertKitAPI or call via modifier:
92+
93+
```swift
94+
let alertView = AlertAppleMusic17View(title: "Hello", subtitle: nil, icon: .done)
95+
96+
VStack {}
97+
.alert(isPresent: $alertPresented, view: alertView)
98+
```
99+
100+
## Customisation
101+
102+
If you need customisation fonts, icon, colors or any other, make view:
103+
104+
```swift
105+
let alertView = AlertAppleMusic17View(title: "Added to Library", subtitle: nil, icon: .done)
106+
// Change content color
107+
alertView.contentColor = .systemBlue
108+
// Change font
109+
alertView.titleLabel.font = UIFont.systemFont(ofSize: 21)
110+
```
111+
83112
## Apps Using
84113

85114
<p float="left">
86115
<a href="https://apps.apple.com/app/id1624477055"><img src="https://cdn.sparrowcode.io/github/apps-using/id1624477055.png?version=2" height="65"></a>
87116
<a href="https://apps.apple.com/app/id1625641322"><img src="https://cdn.sparrowcode.io/github/apps-using/id1625641322.png?version=2" height="65"></a>
117+
<a href="https://apps.apple.com/app/id1625641322"><img src="https://cdn.sparrowcode.io/github/apps-using/id6449774982.png?version=2" height="65"></a>
88118
<a href="https://apps.apple.com/app/id875280793"><img src="https://cdn.sparrowcode.io/github/apps-using/id875280793.png?version=2" height="65"></a>
89119
<a href="https://apps.apple.com/app/id743843090"><img src="https://cdn.sparrowcode.io/github/apps-using/id743843090.png?version=2" height="65"></a>
90120
<a href="https://apps.apple.com/app/id537070378"><img src="https://cdn.sparrowcode.io/github/apps-using/id537070378.png?version=2" height="65"></a>

SPAlert.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = 'SPAlert'
4-
s.version = '5.0.1'
4+
s.version = '5.1.0'
55
s.summary = 'Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets. Support SwiftUI.'
66
s.homepage = 'https://github.com/sparrowcode/AlertKit'
77
s.source = { :git => 'https://github.com/sparrowcode/AlertKit.git', :tag => s.version }

Sources/AlertKit/AlertKitAPI.swift

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import UIKit
22

33
public enum AlertKitAPI {
44

5+
public static func present(view: AlertViewProtocol, completion: @escaping ()->Void = {}) {
6+
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return }
7+
view.present(on: window, completion: completion)
8+
}
9+
510
public static func present(title: String? = nil, subtitle: String? = nil, icon: AlertIcon? = nil, style: AlertViewStyle, haptic: AlertHaptic? = nil) {
611
switch style {
712
case .iOS16AppleMusic:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import SwiftUI
2+
3+
@available(iOS 13.0, *)
4+
extension View {
5+
6+
public func alert(isPresent: Binding<Bool>, view: AlertViewProtocol) -> some View {
7+
if isPresent.wrappedValue {
8+
let alertCompletion = view.completion
9+
let completion = {
10+
isPresent.wrappedValue = false
11+
alertCompletion?()
12+
}
13+
if let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first {
14+
view.present(on: window, completion: completion)
15+
}
16+
}
17+
return self
18+
}
19+
}

Sources/AlertKit/Extensions/UIFontExtension.swift

-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
// The MIT License (MIT)
2-
// Copyright © 2020 Ivan Vorobei ([email protected])
3-
//
4-
// Permission is hereby granted, free of charge, to any person obtaining a copy
5-
// of this software and associated documentation files (the "Software"), to deal
6-
// in the Software without restriction, including without limitation the rights
7-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
// copies of the Software, and to permit persons to whom the Software is
9-
// furnished to do so, subject to the following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be included in all
12-
// copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20-
// SOFTWARE.
21-
221
import UIKit
232

243
extension UIFont {

Sources/AlertKit/Icons/AlertIconDoneView.swift

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class AlertIconDoneView: UIView, AlertIconAnimatable {
2424
animatableLayer.path = animatablePath.cgPath
2525
animatableLayer.fillColor = UIColor.clear.cgColor
2626
animatableLayer.strokeColor = tintColor?.cgColor
27-
//animatableLayer.lineWidth = 9
2827
animatableLayer.lineWidth = lineThick
2928
animatableLayer.lineCap = .round
3029
animatableLayer.lineJoin = .round

Sources/AlertKit/Views/AlertAppleMusic16View.swift

+12-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import UIKit
22

3-
public class AlertAppleMusic16View: UIView {
3+
public class AlertAppleMusic16View: UIView, AlertViewProtocol {
44

55
open var dismissByTap: Bool = true
66
open var dismissInTime: Bool = true
77
open var duration: TimeInterval = 1.5
88
open var haptic: AlertHaptic? = nil
99

10-
fileprivate let titleLabel: UILabel?
11-
fileprivate let subtitleLabel: UILabel?
12-
fileprivate let iconView: UIView?
10+
public let titleLabel: UILabel?
11+
public let subtitleLabel: UILabel?
12+
public let iconView: UIView?
13+
14+
public var contentColor = UIColor { trait in
15+
switch trait.userInterfaceStyle {
16+
case .dark: UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
17+
default: UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
18+
}
19+
}
1320

1421
fileprivate weak var viewForPresent: UIView?
1522
fileprivate var presentDismissDuration: TimeInterval = 0.2
1623
fileprivate var presentDismissScale: CGFloat = 0.8
1724

18-
var completion: (() -> Void)? = nil
25+
open var completion: (() -> Void)? = nil
1926

2027
private lazy var backgroundView: UIVisualEffectView = {
2128
let view: UIVisualEffectView = {
@@ -114,22 +121,6 @@ public class AlertAppleMusic16View: UIView {
114121

115122
open func present(on view: UIView, completion: @escaping ()->Void = {}) {
116123

117-
let contentColor = {
118-
let darkColor = UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
119-
let lightColor = UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
120-
if #available(iOS 12.0, *) {
121-
let interfaceStyle = view.traitCollection.userInterfaceStyle
122-
switch interfaceStyle {
123-
case .light: return lightColor
124-
case .dark: return darkColor
125-
case .unspecified: return lightColor
126-
@unknown default: return lightColor
127-
}
128-
} else {
129-
return lightColor
130-
}
131-
}()
132-
133124
self.titleLabel?.textColor = contentColor
134125
self.subtitleLabel?.textColor = contentColor
135126
self.iconView?.tintColor = contentColor

Sources/AlertKit/Views/AlertAppleMusic17View.swift

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import UIKit
22

3-
public class AlertAppleMusic17View: UIView {
3+
public class AlertAppleMusic17View: UIView, AlertViewProtocol {
44

55
open var dismissByTap: Bool = true
66
open var dismissInTime: Bool = true
77
open var duration: TimeInterval = 1.5
88
open var haptic: AlertHaptic? = nil
99

10-
fileprivate let titleLabel: UILabel?
11-
fileprivate let subtitleLabel: UILabel?
12-
fileprivate let iconView: UIView?
10+
public let titleLabel: UILabel?
11+
public let subtitleLabel: UILabel?
12+
public let iconView: UIView?
13+
14+
public var contentColor = UIColor { trait in
15+
switch trait.userInterfaceStyle {
16+
case .dark: UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
17+
default: UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
18+
}
19+
}
1320

1421
fileprivate weak var viewForPresent: UIView?
1522
fileprivate var presentDismissDuration: TimeInterval = 0.2
@@ -112,22 +119,6 @@ public class AlertAppleMusic17View: UIView {
112119

113120
open func present(on view: UIView, completion: @escaping ()->Void = {}) {
114121

115-
let contentColor = {
116-
let darkColor = UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
117-
let lightColor = UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
118-
if #available(iOS 12.0, *) {
119-
let interfaceStyle = view.traitCollection.userInterfaceStyle
120-
switch interfaceStyle {
121-
case .light: return lightColor
122-
case .dark: return darkColor
123-
case .unspecified: return lightColor
124-
@unknown default: return lightColor
125-
}
126-
} else {
127-
return lightColor
128-
}
129-
}()
130-
131122
self.titleLabel?.textColor = contentColor
132123
self.subtitleLabel?.textColor = contentColor
133124
self.iconView?.tintColor = contentColor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import UIKit
2+
3+
public protocol AlertViewProtocol {
4+
5+
func present(on view: UIView, completion: @escaping ()->Void)
6+
7+
var completion: (() -> Void)? { get set }
8+
}
9+
10+
extension AlertViewProtocol where Self: UIView {
11+
12+
func present(on view: UIView, completion: @escaping ()->Void = {}) {}
13+
}

0 commit comments

Comments
 (0)