Skip to content

Commit

Permalink
Added support vision os.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Oct 15, 2023
1 parent 0bc23a5 commit 89a90fd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// swift-tools-version:5.3
// swift-tools-version:5.9

import PackageDescription

let package = Package(
name: "AlertKit",
platforms: [
.iOS(.v13)
.iOS(.v13),
.visionOS(.v1)
],
products: [
.library(
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ 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
// Change Font
alertView.titleLabel.font = UIFont.systemFont(ofSize: 21)
// Change Color
alertView.titleLabel.textColor = .white
```

## Apps Using
Expand Down
4 changes: 4 additions & 0 deletions Sources/AlertKit/AlertKitAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ public enum AlertKitAPI {

public static func present(title: String? = nil, subtitle: String? = nil, icon: AlertIcon? = nil, style: AlertViewStyle, haptic: AlertHaptic? = nil) {
switch style {
#if os(iOS)
case .iOS16AppleMusic:
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return }
let view = AlertAppleMusic16View(title: title, subtitle: subtitle, icon: icon)
view.haptic = haptic
view.present(on: window)
#endif
#if os(iOS) || os(visionOS)
case .iOS17AppleMusic:
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return }
let view = AlertAppleMusic17View(title: title, subtitle: subtitle, icon: icon)
view.haptic = haptic
view.present(on: window)
#endif
}
}
}
5 changes: 5 additions & 0 deletions Sources/AlertKit/AlertViewStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import Foundation

public enum AlertViewStyle {

#if os(iOS)
case iOS16AppleMusic
#endif

#if os(iOS) || os(visionOS)
case iOS17AppleMusic
#endif
}
12 changes: 6 additions & 6 deletions Sources/AlertKit/Views/AlertAppleMusic16View.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UIKit

@available(iOS 13, *)
public class AlertAppleMusic16View: UIView, AlertViewProtocol {

open var dismissByTap: Bool = true
Expand All @@ -11,7 +12,7 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
public let subtitleLabel: UILabel?
public let iconView: UIView?

public var contentColor = UIColor { trait in
public static var defaultContentColor = 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)
Expand Down Expand Up @@ -81,6 +82,10 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
layout = AlertLayout(for: icon ?? .heart)
}

self.titleLabel?.textColor = Self.defaultContentColor
self.subtitleLabel?.textColor = Self.defaultContentColor
self.iconView?.tintColor = Self.defaultContentColor

super.init(frame: .zero)

preservesSuperviewLayoutMargins = false
Expand Down Expand Up @@ -120,11 +125,6 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
}

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

self.titleLabel?.textColor = contentColor
self.subtitleLabel?.textColor = contentColor
self.iconView?.tintColor = contentColor

self.completion = completion
self.viewForPresent = view
viewForPresent?.addSubview(self)
Expand Down
60 changes: 42 additions & 18 deletions Sources/AlertKit/Views/AlertAppleMusic17View.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import UIKit
import SwiftUI

@available(iOS 13, visionOS 1, *)
public class AlertAppleMusic17View: UIView, AlertViewProtocol {

open var dismissByTap: Bool = true
Expand All @@ -11,11 +13,15 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
public let subtitleLabel: UILabel?
public let iconView: UIView?

public var contentColor = UIColor { trait in
public static var defaultContentColor = UIColor { trait in
#if os(visionOS)
return .label
#else
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)
}
#endif
}

fileprivate weak var viewForPresent: UIView?
Expand All @@ -24,20 +30,18 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {

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

private lazy var backgroundView: UIVisualEffectView = {
let view: UIVisualEffectView = {
#if !os(tvOS)
if #available(iOS 13.0, *) {
return UIVisualEffectView(effect: UIBlurEffect(style: .systemThickMaterial))
} else {
return UIVisualEffectView(effect: UIBlurEffect(style: .light))
}
#else
return UIVisualEffectView(effect: UIBlurEffect(style: .light))
#endif
}()
private lazy var backgroundView: UIView = {
#if os(visionOS)
let swiftUIView = VisionGlassBackgroundView(cornerRadius: 12)
let host = UIHostingController(rootView: swiftUIView)
let hostView = host.view ?? UIView()
hostView.isUserInteractionEnabled = false
return hostView
#else
let view = UIVisualEffectView(effect: UIBlurEffect())
view.isUserInteractionEnabled = false
return view
#endif
}()

public init(title: String?, subtitle: String?, icon: AlertIcon?) {
Expand Down Expand Up @@ -75,6 +79,10 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
self.iconView = nil
}

self.titleLabel?.textColor = Self.defaultContentColor
self.subtitleLabel?.textColor = Self.defaultContentColor
self.iconView?.tintColor = Self.defaultContentColor

super.init(frame: .zero)

preservesSuperviewLayoutMargins = false
Expand All @@ -89,6 +97,7 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
if let subtitleLabel = self.subtitleLabel {
addSubview(subtitleLabel)
}

if let iconView = self.iconView {
addSubview(iconView)
}
Expand Down Expand Up @@ -118,11 +127,6 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
}

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

self.titleLabel?.textColor = contentColor
self.subtitleLabel?.textColor = contentColor
self.iconView?.tintColor = contentColor

self.completion = completion
self.viewForPresent = view
viewForPresent?.addSubview(self)
Expand All @@ -131,7 +135,12 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
alpha = 0
sizeToFit()
center.x = viewForPresent.frame.midX
#if os(visionOS)
frame.origin.y = viewForPresent.safeAreaInsets.top + 24
#elseif os(iOS)
frame.origin.y = viewForPresent.frame.height - viewForPresent.safeAreaInsets.bottom - frame.height - 64
#endif

transform = transform.scaledBy(x: self.presentDismissScale, y: self.presentDismissScale)

if dismissByTap {
Expand Down Expand Up @@ -258,4 +267,19 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {

iconView?.center.y = frame.height / 2
}

#if os(visionOS)
struct VisionGlassBackgroundView: View {

let cornerRadius: CGFloat

var body: some View {
ZStack {
Color.clear
}
.glassBackgroundEffect(in: .rect(cornerRadius: cornerRadius))
.opacity(0.4)
}
}
#endif
}

0 comments on commit 89a90fd

Please sign in to comment.