-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add predefined message styles and types
- Loading branch information
1 parent
61fed4e
commit a35c4d1
Showing
7 changed files
with
260 additions
and
25 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 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
165 changes: 165 additions & 0 deletions
165
Sources/SystemNotification/SystemNotificationMessage+Predefined.swift
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,165 @@ | ||
// | ||
// SystemNotificationMessage+Predefined.swift | ||
// SystemNotification | ||
// | ||
// Created by Daniel Saidi on 2024-04-24. | ||
// Copyright © 2024 Daniel Saidi. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension SystemNotificationMessageStyle { | ||
|
||
static var error: Self { | ||
prominent(backgroundColor: .red) | ||
} | ||
|
||
static var success: Self { | ||
prominent(backgroundColor: .green) | ||
} | ||
|
||
static var warning: Self { | ||
prominent(backgroundColor: .orange) | ||
} | ||
|
||
static func prominent( | ||
backgroundColor: Color | ||
) -> Self { | ||
.init( | ||
backgroundColor: backgroundColor, | ||
iconColor: .white, | ||
textColor: .white.opacity(0.8), | ||
titleColor: .white | ||
) | ||
} | ||
} | ||
|
||
extension SystemNotificationMessage where IconView == Image { | ||
|
||
static func error( | ||
icon: Image = .init(systemName: "exclamationmark.triangle"), | ||
title: LocalizedStringKey? = nil, | ||
text: LocalizedStringKey | ||
) -> Self { | ||
.init( | ||
icon: icon, | ||
title: title, | ||
text: text, | ||
style: .error | ||
) | ||
} | ||
|
||
static func success( | ||
icon: Image = .init(systemName: "checkmark"), | ||
title: LocalizedStringKey? = nil, | ||
text: LocalizedStringKey | ||
) -> Self { | ||
.init( | ||
icon: icon, | ||
title: title, | ||
text: text, | ||
style: .success | ||
) | ||
} | ||
|
||
static func warning( | ||
icon: Image = .init(systemName: "exclamationmark.triangle"), | ||
title: LocalizedStringKey? = nil, | ||
text: LocalizedStringKey | ||
) -> Self { | ||
.init( | ||
icon: icon, | ||
title: title, | ||
text: text, | ||
style: .warning | ||
) | ||
} | ||
} | ||
|
||
public extension SystemNotificationMessage where IconView == AnyView { | ||
|
||
/// This message mimics a native iOS silent mode message. | ||
static func silentMode( | ||
on: Bool, | ||
title: LocalizedStringKey? = nil | ||
) -> Self { | ||
.init( | ||
icon: AnyView(SilentModeBell(isSilentModeOn: on)), | ||
text: title ?? "Silent Mode \(on ? "On" : "Off")" | ||
) | ||
} | ||
} | ||
|
||
private struct SilentModeBell: View { | ||
|
||
var isSilentModeOn = false | ||
|
||
@State | ||
private var isRotated: Bool = false | ||
|
||
@State | ||
private var isAnimated: Bool = false | ||
|
||
var body: some View { | ||
Image(systemName: iconName) | ||
.rotationEffect( | ||
.degrees(isRotated ? -45 : 0), | ||
anchor: .top | ||
) | ||
.animation( | ||
.interpolatingSpring( | ||
mass: 0.5, | ||
stiffness: animationStiffness, | ||
damping: animationDamping, | ||
initialVelocity: 0 | ||
), | ||
value: isAnimated) | ||
.foregroundColor(iconColor) | ||
.onAppear(perform: animate) | ||
} | ||
} | ||
|
||
private extension SilentModeBell { | ||
|
||
func animate() { | ||
withAnimation { isRotated = true } | ||
perform(after: 0.1) { | ||
isRotated = false | ||
isAnimated = true | ||
} | ||
} | ||
|
||
func perform(after: Double, action: @escaping () -> Void) { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: action) | ||
} | ||
|
||
var animationDamping: Double { | ||
isSilentModeOn ? 4 : 1.5 | ||
} | ||
|
||
var animationStiffness: Double { | ||
isSilentModeOn ? 129 : 179 | ||
} | ||
|
||
var iconName: String { | ||
isSilentModeOn ? "bell.slash.fill" : "bell.fill" | ||
} | ||
|
||
var iconColor: Color { | ||
isSilentModeOn ? .red : .gray | ||
} | ||
} | ||
|
||
#Preview { | ||
|
||
VStack { | ||
SystemNotificationMessage.silentMode(on: true) | ||
SystemNotificationMessage.silentMode(on: false) | ||
SystemNotificationMessage.error(title: "Error!", text: "Something failed!") | ||
SystemNotificationMessage.success(title: "Success!", text: "You did it!") | ||
SystemNotificationMessage.warning(title: "Warning!", text: "Danger ahead!") | ||
} | ||
.padding() | ||
.background(Color.black.opacity(0.1)) | ||
.clipShape(.rect(cornerRadius: 10)) | ||
} |
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