-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathActivityManager.swift
More file actions
132 lines (115 loc) Β· 3.87 KB
/
ActivityManager.swift
File metadata and controls
132 lines (115 loc) Β· 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// ActivityManager.swift
// CodeEdit
//
// Created by Tommy Ludwig on 21.06.24.
//
import Foundation
import Combine
import SwiftUI
/// Manages activities for a workspace
@MainActor
final class ActivityManager: ObservableObject {
/// Currently displayed activities
@Published private(set) var activities: [CEActivity] = []
/// Debounce work item for batching updates
private var updateWorkItems: [String: DispatchWorkItem] = [:]
/// Posts a new activity
/// - Parameters:
/// - priority: Whether to insert at start of list
/// - title: Activity title
/// - message: Optional detail message
/// - percentage: Optional progress percentage (0-1)
/// - isLoading: Whether activity shows loading indicator
/// - Returns: The created activity
@discardableResult
func post(
priority: Bool = false,
title: String,
message: String? = nil,
percentage: Double? = nil,
isLoading: Bool = false
) -> CEActivity {
let activity = CEActivity(
id: UUID().uuidString,
title: title,
message: message,
percentage: percentage,
isLoading: isLoading
)
withAnimation(.easeInOut(duration: 0.3)) {
if priority {
activities.insert(activity, at: 0)
} else {
activities.append(activity)
}
}
return activity
}
/// Updates an existing activity with debouncing
/// - Parameters:
/// - id: ID of activity to update
/// - title: New title (optional)
/// - message: New message (optional)
/// - percentage: New progress percentage (optional)
/// - isLoading: New loading state (optional)
func update(
id: String,
title: String? = nil,
message: String? = nil,
percentage: Double? = nil,
isLoading: Bool? = nil
) {
// Cancel any pending update for this specific activity
updateWorkItems[id]?.cancel()
// Create new work item
let workItem = DispatchWorkItem { [weak self] in
guard let self else { return }
if let index = self.activities.firstIndex(where: { $0.id == id }) {
var activity = self.activities[index]
if let title = title {
activity.title = title
}
if let message = message {
activity.message = message
}
if let percentage = percentage {
activity.percentage = percentage
}
if let isLoading = isLoading {
activity.isLoading = isLoading
}
withAnimation(.easeInOut(duration: 0.15)) {
self.activities[index] = activity
}
}
self.updateWorkItems.removeValue(forKey: id)
}
// Store work item and schedule after delay
updateWorkItems[id] = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05, execute: workItem)
}
/// Deletes an activity
/// - Parameter id: ID of activity to delete
func delete(id: String) {
// Clear any pending updates for this activity
updateWorkItems[id]?.cancel()
updateWorkItems.removeValue(forKey: id)
withAnimation(.easeInOut(duration: 0.3)) {
activities.removeAll { $0.id == id }
}
}
/// Deletes an activity after a delay
/// - Parameters:
/// - id: ID of activity to delete
/// - delay: Time to wait before deleting
func delete(id: String, delay: TimeInterval) {
Task { @MainActor in
try? await Task.sleep(for: .seconds(delay))
delete(id: id)
}
}
}
extension Notification.Name {
static let activity = Notification.Name("activity")
}