Skip to content

Commit 42f802e

Browse files
committed
Refactored Activities (previously TaskNotifications) to have a clear distinction in name and have a consistent API when compared with Notifications.
1 parent 6bdde36 commit 42f802e

25 files changed

Lines changed: 3429 additions & 431 deletions

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 3153 additions & 7 deletions
Large diffs are not rendered by default.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// ActivityManager.swift
3+
// CodeEdit
4+
//
5+
// Created by Tommy Ludwig on 21.06.24.
6+
//
7+
8+
import Foundation
9+
import Combine
10+
import SwiftUI
11+
12+
/// Manages activities for a workspace
13+
final class ActivityManager: ObservableObject {
14+
/// Currently displayed activities
15+
@Published private(set) var activities: [CEActivity] = []
16+
17+
/// Posts a new activity
18+
/// - Parameters:
19+
/// - priority: Whether to insert at start of list
20+
/// - title: Activity title
21+
/// - message: Optional detail message
22+
/// - percentage: Optional progress percentage (0-1)
23+
/// - isLoading: Whether activity shows loading indicator
24+
/// - Returns: The created activity
25+
@discardableResult
26+
func post(
27+
priority: Bool = false,
28+
title: String,
29+
message: String? = nil,
30+
percentage: Double? = nil,
31+
isLoading: Bool = false
32+
) -> CEActivity {
33+
let activity = CEActivity(
34+
id: UUID().uuidString,
35+
title: title,
36+
message: message,
37+
percentage: percentage,
38+
isLoading: isLoading
39+
)
40+
41+
withAnimation(.easeInOut(duration: 0.3)) {
42+
if priority {
43+
activities.insert(activity, at: 0)
44+
} else {
45+
activities.append(activity)
46+
}
47+
}
48+
49+
return activity
50+
}
51+
52+
/// Updates an existing activity
53+
/// - Parameters:
54+
/// - id: ID of activity to update
55+
/// - title: New title (optional)
56+
/// - message: New message (optional)
57+
/// - percentage: New progress percentage (optional)
58+
/// - isLoading: New loading state (optional)
59+
func update(
60+
id: String,
61+
title: String? = nil,
62+
message: String? = nil,
63+
percentage: Double? = nil,
64+
isLoading: Bool? = nil
65+
) {
66+
if let index = activities.firstIndex(where: { $0.id == id }) {
67+
var activity = activities[index]
68+
69+
if let title = title {
70+
activity.title = title
71+
}
72+
if let message = message {
73+
activity.message = message
74+
}
75+
if let percentage = percentage {
76+
activity.percentage = percentage
77+
}
78+
if let isLoading = isLoading {
79+
activity.isLoading = isLoading
80+
}
81+
82+
activities[index] = activity
83+
}
84+
}
85+
86+
/// Deletes an activity
87+
/// - Parameter id: ID of activity to delete
88+
func delete(id: String) {
89+
withAnimation(.easeInOut(duration: 0.3)) {
90+
activities.removeAll { $0.id == id }
91+
}
92+
}
93+
94+
/// Deletes an activity after a delay
95+
/// - Parameters:
96+
/// - id: ID of activity to delete
97+
/// - delay: Time to wait before deleting
98+
func delete(id: String, delay: TimeInterval) {
99+
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
100+
self?.delete(id: id)
101+
}
102+
}
103+
}
104+
105+
extension Notification.Name {
106+
static let activity = Notification.Name("activity")
107+
}

CodeEdit/Features/ActivityViewer/Models/TaskNotificationModel.swift renamed to CodeEdit/Features/Activities/Models/CEActivity.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//
2-
// TaskNotificationModel.swift
2+
// CEActivity.swift
33
// CodeEdit
44
//
55
// Created by Tommy Ludwig on 21.06.24.
66
//
77

88
import Foundation
99

10-
/// Represents a notifications or tasks, that are displayed in the activity viewer
11-
struct TaskNotificationModel: Equatable {
10+
/// Represents an activity, that is displayed in the activity viewer
11+
struct CEActivity: Equatable {
1212
var id: String
1313
var title: String
1414
var message: String?

CodeEdit/Features/ActivityViewer/Tasks/DropdownMenuItemStyleModifier.swift renamed to CodeEdit/Features/Activities/ViewModifiers/DropdownMenuItemStyleModifier.swift

File renamed without changes.

CodeEdit/Features/ActivityViewer/Tasks/ActiveTaskView.swift renamed to CodeEdit/Features/Activities/Views/ActiveTaskView.swift

File renamed without changes.

CodeEdit/Features/ActivityViewer/Notifications/TaskNotificationView.swift renamed to CodeEdit/Features/Activities/Views/ActivitiesView.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
//
2-
// TaskNotificationView.swift
2+
// ActivityView.swift
33
// CodeEdit
44
//
55
// Created by Tommy Ludwig on 21.06.24.
66
//
77

88
import SwiftUI
99

10-
struct TaskNotificationView: View {
10+
struct ActivityView: View {
1111
@Environment(\.controlActiveState)
1212
private var activeState
1313

14-
@ObservedObject var taskNotificationHandler: TaskNotificationHandler
14+
@ObservedObject var activityManager: ActivityManager
1515
@State private var isPresented: Bool = false
16-
@State var notification: TaskNotificationModel?
16+
@State var activity: CEActivity?
1717

1818
var body: some View {
1919
ZStack {
20-
if let notification {
20+
if let activity {
2121
HStack {
22-
Text(notification.title)
22+
Text(activity.title)
2323
.font(.subheadline)
2424
.transition(
2525
.asymmetric(insertion: .move(edge: .top), removal: .move(edge: .bottom))
2626
.combined(with: .opacity)
2727
)
28-
.id("NotificationTitle" + notification.title)
28+
.id("ActivityTitle" + activity.title)
2929

30-
if notification.isLoading {
30+
if activity.isLoading {
3131
CECircularProgressView(
32-
progress: notification.percentage,
33-
currentTaskCount: taskNotificationHandler.notifications.count
32+
progress: activity.percentage,
33+
currentTaskCount: activityManager.activities.count
3434
)
3535
.padding(.horizontal, -1)
3636
.frame(height: 16)
3737
} else {
38-
if taskNotificationHandler.notifications.count > 1 {
39-
Text("\(taskNotificationHandler.notifications.count)")
38+
if activityManager.activities.count > 1 {
39+
Text("\(activityManager.activities.count)")
4040
.font(.caption)
4141
.padding(5)
4242
.background(
@@ -54,23 +54,23 @@ struct TaskNotificationView: View {
5454
.padding(-3)
5555
.padding(.trailing, 3)
5656
.popover(isPresented: $isPresented, arrowEdge: .bottom) {
57-
TaskNotificationsDetailView(taskNotificationHandler: taskNotificationHandler)
57+
ActivitysDetailView(activityManager: activityManager)
5858
}
5959
.onTapGesture {
6060
self.isPresented.toggle()
6161
}
6262
}
6363
}
64-
.animation(.easeInOut, value: notification)
65-
.onChange(of: taskNotificationHandler.notifications) { newValue in
64+
.animation(.easeInOut, value: activity)
65+
.onChange(of: activityManager.activities) { newValue in
6666
withAnimation {
67-
notification = newValue.first
67+
activity = newValue.first
6868
}
6969
}
7070
}
7171

7272
}
7373

7474
#Preview {
75-
TaskNotificationView(taskNotificationHandler: TaskNotificationHandler())
75+
ActivityView(activityManager: ActivityManager())
7676
}

CodeEdit/Features/ActivityViewer/ActivityViewer.swift renamed to CodeEdit/Features/Activities/Views/ActivityViewer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct ActivityViewer: View {
1414

1515
var workspaceFileManager: CEWorkspaceFileManager?
1616

17-
@ObservedObject var taskNotificationHandler: TaskNotificationHandler
17+
@ObservedObject var activityManager: ActivityManager
1818
@ObservedObject var workspaceSettingsManager: CEWorkspaceSettings
1919

2020
// TODO: try to get this from the envrionment
@@ -23,12 +23,12 @@ struct ActivityViewer: View {
2323
init(
2424
workspaceFileManager: CEWorkspaceFileManager?,
2525
workspaceSettingsManager: CEWorkspaceSettings,
26-
taskNotificationHandler: TaskNotificationHandler,
26+
activityManager: ActivityManager,
2727
taskManager: TaskManager
2828
) {
2929
self.workspaceFileManager = workspaceFileManager
3030
self.workspaceSettingsManager = workspaceSettingsManager
31-
self.taskNotificationHandler = taskNotificationHandler
31+
self.activityManager = activityManager
3232
self.taskManager = taskManager
3333
}
3434
var body: some View {
@@ -42,7 +42,7 @@ struct ActivityViewer: View {
4242

4343
Spacer(minLength: 0)
4444

45-
TaskNotificationView(taskNotificationHandler: taskNotificationHandler)
45+
ActivityView(activityManager: activityManager)
4646
.fixedSize()
4747
}
4848
.fixedSize(horizontal: false, vertical: false)

CodeEdit/Features/ActivityViewer/Notifications/CECircularProgressView.swift renamed to CodeEdit/Features/Activities/Views/CECircularProgressView.swift

File renamed without changes.

CodeEdit/Features/ActivityViewer/Tasks/OptionMenuItemView.swift renamed to CodeEdit/Features/Activities/Views/OptionMenuItemView.swift

File renamed without changes.

CodeEdit/Features/ActivityViewer/Tasks/SchemeDropDownView.swift renamed to CodeEdit/Features/Activities/Views/SchemeDropDownView.swift

File renamed without changes.

0 commit comments

Comments
 (0)