Skip to content

Commit 5b12a40

Browse files
committed
Added update and delete functions to notification manager.
1 parent 03b9b0e commit 5b12a40

6 files changed

+67
-11
lines changed

CodeEdit/Features/Notifications/Models/CENotification.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import SwiftUI
1111
struct CENotification: Identifiable, Equatable {
1212
let id: UUID
1313
let icon: IconType
14-
let title: String
15-
let description: String
16-
let actionButtonTitle: String
17-
let action: () -> Void
18-
let isSticky: Bool
14+
var title: String
15+
var description: String
16+
var actionButtonTitle: String
17+
var action: () -> Void
18+
var isSticky: Bool
1919
var isRead: Bool
2020
let timestamp: Date
2121
var isBeingDismissed: Bool = false

CodeEdit/Features/Notifications/NotificationManager+Delegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension NotificationManager: UNUserNotificationCenterDelegate {
4343
completionHandler([.banner, .sound])
4444
}
4545

46-
func setupNotificationDelegate() {
46+
public func setupNotificationDelegate() {
4747
UNUserNotificationCenter.current().delegate = self
4848

4949
// Create action button

CodeEdit/Features/Notifications/NotificationManager+System.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import UserNotifications
1010

1111
extension NotificationManager {
1212
/// Shows a system notification when app is in background
13-
func showSystemNotification(_ notification: CENotification) {
13+
public func showSystemNotification(_ notification: CENotification) {
1414
let content = UNMutableNotificationContent()
1515
content.title = notification.title
1616
content.body = notification.description
@@ -29,14 +29,14 @@ extension NotificationManager {
2929
}
3030

3131
/// Removes a system notification
32-
func removeSystemNotification(_ notification: CENotification) {
32+
public func removeSystemNotification(_ notification: CENotification) {
3333
UNUserNotificationCenter.current().removeDeliveredNotifications(
3434
withIdentifiers: [notification.id.uuidString]
3535
)
3636
}
3737

3838
/// Handles response from system notification
39-
func handleSystemNotificationResponse(id: String) {
39+
public func handleSystemNotificationResponse(id: String) {
4040
if let uuid = UUID(uuidString: id),
4141
let notification = notifications.first(where: { $0.id == uuid }) {
4242
notification.action()

CodeEdit/Features/Notifications/NotificationManager.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,64 @@ final class NotificationManager: NSObject, ObservableObject {
144144
}
145145
}
146146

147+
/// Updates an existing notification
148+
/// - Parameters:
149+
/// - id: UUID of notification to update
150+
/// - title: New title (optional)
151+
/// - description: New description (optional)
152+
/// - actionButtonTitle: New action button title (optional)
153+
/// - action: New action closure (optional)
154+
/// - isSticky: New sticky state (optional)
155+
func update(
156+
id: UUID,
157+
title: String? = nil,
158+
description: String? = nil,
159+
actionButtonTitle: String? = nil,
160+
action: (() -> Void)? = nil,
161+
isSticky: Bool? = nil
162+
) {
163+
if let index = notifications.firstIndex(where: { $0.id == id }) {
164+
var notification = notifications[index]
165+
166+
if let title = title {
167+
notification.title = title
168+
}
169+
if let description = description {
170+
notification.description = description
171+
}
172+
if let actionButtonTitle = actionButtonTitle {
173+
notification.actionButtonTitle = actionButtonTitle
174+
}
175+
if let action = action {
176+
notification.action = action
177+
}
178+
if let isSticky = isSticky {
179+
notification.isSticky = isSticky
180+
}
181+
182+
notifications[index] = notification
183+
}
184+
}
185+
186+
/// Deletes a notification
187+
/// - Parameter id: UUID of notification to delete
188+
func delete(id: UUID) {
189+
if let notification = notifications.first(where: { $0.id == id }) {
190+
dismissNotification(notification)
191+
}
192+
}
193+
194+
/// Deletes a notification after a delay
195+
/// - Parameters:
196+
/// - id: UUID of notification to delete
197+
/// - delay: Time to wait before deleting
198+
func delete(id: UUID, delay: TimeInterval) {
199+
Task { @MainActor in
200+
try? await Task.sleep(for: .seconds(delay))
201+
delete(id: id)
202+
}
203+
}
204+
147205
override init() {
148206
super.init()
149207
setupNotificationDelegate()

CodeEdit/Features/Notifications/Views/NotificationBannerView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ struct NotificationBannerView: View {
1212
private var colorScheme
1313

1414
@EnvironmentObject private var workspace: WorkspaceDocument
15-
@ObservedObject private var notificationManager = NotificationManager.shared
1615

1716
let notification: CENotification
1817
let onDismiss: () -> Void

CodeEdit/Features/Notifications/Views/NotificationPanelView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ struct NotificationPanelView: View {
1212
@Environment(\.controlActiveState)
1313
private var controlActiveState
1414

15-
@ObservedObject private var notificationManager = NotificationManager.shared
1615
@FocusState private var isFocused: Bool
1716

1817
// ID for the top anchor

0 commit comments

Comments
 (0)