Skip to content

Commit

Permalink
Allow customisation. Added support SwiftUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Oct 14, 2023
1 parent 465ffbf commit ebc2d5c
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 71 deletions.
44 changes: 37 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ I tried to recreate Apple's alerts as much as possible. You can find these alert

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

For run alert just call this:
For UIKit & SwiftUI call this:

```swift
AlertKitAPI.present(
Expand All @@ -27,25 +27,31 @@ public enum AlertViewStyle {
```

### Community

<p float="left">
<a href="https://twitter.com/sparrowcode_en">
<img src="https://cdn.sparrowcode.io/github%2Fbadges%2Ftwitter.png?version=4" height="52">
</a>
<a href="https://t.me/sparrowcode_en">
<img src="https://cdn.sparrowcode.io/github/badges/telegram.png?version=1" height="52">
</a>
<a href="https://mastodon.social/@sparrowcode_en">
<img src="https://cdn.sparrowcode.io/github/badges/mastodon.png?version=2" height="52">
</a>
<a href="#apps-using">
<img src="https://cdn.sparrowcode.io/github/badges/download-on-the-appstore.png?version=4" height="52">
</a>
</p>

## Navigate

- [Installation](#installation)
- [Swift Package Manager](#swift-package-manager)
- [CocoaPods](#cocoapods)
- [SwiftUI](#swiftui)
- [Customisation](#customisation)
- [Apps Using](#apps-using)

## Installation

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

### Swift Package Manager

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

```swift
dependencies: [
.package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.0.0"))
.package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.1.0"))
]
```

Expand All @@ -80,11 +86,35 @@ pod 'SPAlert'

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`.

## SwiftUI

You can use basic way via AlertKitAPI or call via modifier:

```swift
let alertView = AlertAppleMusic17View(title: "Hello", subtitle: nil, icon: .done)

VStack {}
.alert(isPresent: $alertPresented, view: alertView)
```

## Customisation

If you need customisation fonts, icon, colors or any other, make view:

```swift
let alertView = AlertAppleMusic17View(title: "Added to Library", subtitle: nil, icon: .done)
// Change content color
alertView.contentColor = .systemBlue
// Change font
alertView.titleLabel.font = UIFont.systemFont(ofSize: 21)
```

## Apps Using

<p float="left">
<a href="https://apps.apple.com/app/id1624477055"><img src="https://cdn.sparrowcode.io/github/apps-using/id1624477055.png?version=2" height="65"></a>
<a href="https://apps.apple.com/app/id1625641322"><img src="https://cdn.sparrowcode.io/github/apps-using/id1625641322.png?version=2" height="65"></a>
<a href="https://apps.apple.com/app/id1625641322"><img src="https://cdn.sparrowcode.io/github/apps-using/id6449774982.png?version=2" height="65"></a>
<a href="https://apps.apple.com/app/id875280793"><img src="https://cdn.sparrowcode.io/github/apps-using/id875280793.png?version=2" height="65"></a>
<a href="https://apps.apple.com/app/id743843090"><img src="https://cdn.sparrowcode.io/github/apps-using/id743843090.png?version=2" height="65"></a>
<a href="https://apps.apple.com/app/id537070378"><img src="https://cdn.sparrowcode.io/github/apps-using/id537070378.png?version=2" height="65"></a>
Expand Down
2 changes: 1 addition & 1 deletion SPAlert.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'SPAlert'
s.version = '5.0.1'
s.version = '5.1.0'
s.summary = 'Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets. Support SwiftUI.'
s.homepage = 'https://github.com/sparrowcode/AlertKit'
s.source = { :git => 'https://github.com/sparrowcode/AlertKit.git', :tag => s.version }
Expand Down
5 changes: 5 additions & 0 deletions Sources/AlertKit/AlertKitAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import UIKit

public enum AlertKitAPI {

public static func present(view: AlertViewProtocol, completion: @escaping ()->Void = {}) {
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return }
view.present(on: window, completion: completion)
}

public static func present(title: String? = nil, subtitle: String? = nil, icon: AlertIcon? = nil, style: AlertViewStyle, haptic: AlertHaptic? = nil) {
switch style {
case .iOS16AppleMusic:
Expand Down
19 changes: 19 additions & 0 deletions Sources/AlertKit/Extensions/SwiftUIExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import SwiftUI

@available(iOS 13.0, *)
extension View {

public func alert(isPresent: Binding<Bool>, view: AlertViewProtocol) -> some View {
if isPresent.wrappedValue {
let alertCompletion = view.completion
let completion = {
isPresent.wrappedValue = false
alertCompletion?()
}
if let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first {
view.present(on: window, completion: completion)
}
}
return self
}
}
21 changes: 0 additions & 21 deletions Sources/AlertKit/Extensions/UIFontExtension.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
// The MIT License (MIT)
// Copyright © 2020 Ivan Vorobei ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import UIKit

extension UIFont {
Expand Down
1 change: 0 additions & 1 deletion Sources/AlertKit/Icons/AlertIconDoneView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class AlertIconDoneView: UIView, AlertIconAnimatable {
animatableLayer.path = animatablePath.cgPath
animatableLayer.fillColor = UIColor.clear.cgColor
animatableLayer.strokeColor = tintColor?.cgColor
//animatableLayer.lineWidth = 9
animatableLayer.lineWidth = lineThick
animatableLayer.lineCap = .round
animatableLayer.lineJoin = .round
Expand Down
33 changes: 12 additions & 21 deletions Sources/AlertKit/Views/AlertAppleMusic16View.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import UIKit

public class AlertAppleMusic16View: UIView {
public class AlertAppleMusic16View: UIView, AlertViewProtocol {

open var dismissByTap: Bool = true
open var dismissInTime: Bool = true
open var duration: TimeInterval = 1.5
open var haptic: AlertHaptic? = nil

fileprivate let titleLabel: UILabel?
fileprivate let subtitleLabel: UILabel?
fileprivate let iconView: UIView?
public let titleLabel: UILabel?
public let subtitleLabel: UILabel?
public let iconView: UIView?

public var contentColor = UIColor { trait in
switch trait.userInterfaceStyle {
case .dark: UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
default: UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
}
}

fileprivate weak var viewForPresent: UIView?
fileprivate var presentDismissDuration: TimeInterval = 0.2
fileprivate var presentDismissScale: CGFloat = 0.8

var completion: (() -> Void)? = nil
open var completion: (() -> Void)? = nil

private lazy var backgroundView: UIVisualEffectView = {
let view: UIVisualEffectView = {
Expand Down Expand Up @@ -114,22 +121,6 @@ public class AlertAppleMusic16View: UIView {

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

let contentColor = {
let darkColor = UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
let lightColor = UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
if #available(iOS 12.0, *) {
let interfaceStyle = view.traitCollection.userInterfaceStyle
switch interfaceStyle {
case .light: return lightColor
case .dark: return darkColor
case .unspecified: return lightColor
@unknown default: return lightColor
}
} else {
return lightColor
}
}()

self.titleLabel?.textColor = contentColor
self.subtitleLabel?.textColor = contentColor
self.iconView?.tintColor = contentColor
Expand Down
31 changes: 11 additions & 20 deletions Sources/AlertKit/Views/AlertAppleMusic17View.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import UIKit

public class AlertAppleMusic17View: UIView {
public class AlertAppleMusic17View: UIView, AlertViewProtocol {

open var dismissByTap: Bool = true
open var dismissInTime: Bool = true
open var duration: TimeInterval = 1.5
open var haptic: AlertHaptic? = nil

fileprivate let titleLabel: UILabel?
fileprivate let subtitleLabel: UILabel?
fileprivate let iconView: UIView?
public let titleLabel: UILabel?
public let subtitleLabel: UILabel?
public let iconView: UIView?

public var contentColor = UIColor { trait in
switch trait.userInterfaceStyle {
case .dark: UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
default: UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
}
}

fileprivate weak var viewForPresent: UIView?
fileprivate var presentDismissDuration: TimeInterval = 0.2
Expand Down Expand Up @@ -112,22 +119,6 @@ public class AlertAppleMusic17View: UIView {

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

let contentColor = {
let darkColor = UIColor(red: 127 / 255, green: 127 / 255, blue: 129 / 255, alpha: 1)
let lightColor = UIColor(red: 88 / 255, green: 87 / 255, blue: 88 / 255, alpha: 1)
if #available(iOS 12.0, *) {
let interfaceStyle = view.traitCollection.userInterfaceStyle
switch interfaceStyle {
case .light: return lightColor
case .dark: return darkColor
case .unspecified: return lightColor
@unknown default: return lightColor
}
} else {
return lightColor
}
}()

self.titleLabel?.textColor = contentColor
self.subtitleLabel?.textColor = contentColor
self.iconView?.tintColor = contentColor
Expand Down
13 changes: 13 additions & 0 deletions Sources/AlertKit/Views/AlertViewProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import UIKit

public protocol AlertViewProtocol {

func present(on view: UIView, completion: @escaping ()->Void)

var completion: (() -> Void)? { get set }
}

extension AlertViewProtocol where Self: UIView {

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

0 comments on commit ebc2d5c

Please sign in to comment.