Skip to content

Commit cbfe1c3

Browse files
committed
Added update and delete functions to notification manager.
1 parent 7e4c603 commit cbfe1c3

File tree

6 files changed

+67
-11
lines changed

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
@@ -172,6 +172,64 @@ final class NotificationManager: NSObject, ObservableObject {
172172
}
173173
}
174174

175+
/// Updates an existing notification
176+
/// - Parameters:
177+
/// - id: UUID of notification to update
178+
/// - title: New title (optional)
179+
/// - description: New description (optional)
180+
/// - actionButtonTitle: New action button title (optional)
181+
/// - action: New action closure (optional)
182+
/// - isSticky: New sticky state (optional)
183+
func update(
184+
id: UUID,
185+
title: String? = nil,
186+
description: String? = nil,
187+
actionButtonTitle: String? = nil,
188+
action: (() -> Void)? = nil,
189+
isSticky: Bool? = nil
190+
) {
191+
if let index = notifications.firstIndex(where: { $0.id == id }) {
192+
var notification = notifications[index]
193+
194+
if let title = title {
195+
notification.title = title
196+
}
197+
if let description = description {
198+
notification.description = description
199+
}
200+
if let actionButtonTitle = actionButtonTitle {
201+
notification.actionButtonTitle = actionButtonTitle
202+
}
203+
if let action = action {
204+
notification.action = action
205+
}
206+
if let isSticky = isSticky {
207+
notification.isSticky = isSticky
208+
}
209+
210+
notifications[index] = notification
211+
}
212+
}
213+
214+
/// Deletes a notification
215+
/// - Parameter id: UUID of notification to delete
216+
func delete(id: UUID) {
217+
if let notification = notifications.first(where: { $0.id == id }) {
218+
dismissNotification(notification)
219+
}
220+
}
221+
222+
/// Deletes a notification after a delay
223+
/// - Parameters:
224+
/// - id: UUID of notification to delete
225+
/// - delay: Time to wait before deleting
226+
func delete(id: UUID, delay: TimeInterval) {
227+
Task { @MainActor in
228+
try? await Task.sleep(for: .seconds(delay))
229+
delete(id: id)
230+
}
231+
}
232+
175233
override init() {
176234
super.init()
177235
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
@Environment(\.colorScheme)
1817
private var colorScheme

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)