Skip to content

Commit

Permalink
Notification, Append the new User, and move to the User Page. Works g…
Browse files Browse the repository at this point in the history
…reat! But with a 0.1 second delay?
  • Loading branch information
JPKribs committed Oct 31, 2024
1 parent b360fea commit 237adbf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Shared/Services/SwiftfinNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,6 @@ extension Notifications.Key {
static let didChangeUserProfileImage = NotificationKey("didChangeUserProfileImage")

static let didStartPlayback = NotificationKey("didStartPlayback")

static let didAddServerUser = NotificationKey("didStartPlayback")
}
33 changes: 33 additions & 0 deletions Shared/ViewModels/ServerUsersViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ final class ServerUsersViewModel: ViewModel, Eventful, Stateful, Identifiable {
enum Action: Equatable {
case getUsers(isHidden: Bool = false, isDisabled: Bool = false)
case deleteUsers([String])
case appendUser(UserDto)
}

// MARK: - BackgroundState

enum BackgroundState: Hashable {
case gettingUsers
case deletingUsers
case appendingUsers
}

// MARK: - State
Expand Down Expand Up @@ -116,6 +118,28 @@ final class ServerUsersViewModel: ViewModel, Eventful, Stateful, Identifiable {
}
.asAnyCancellable()

return state

case let .appendUser(user):
userTask?.cancel()
backgroundStates.append(.appendingUsers)

userTask = Task {
do {
await self.appendUser(user: user)

await MainActor.run {
self.state = .content
self.eventSubject.send(.deleted)
}
}

await MainActor.run {
_ = self.backgroundStates.remove(.appendingUsers)
}
}
.asAnyCancellable()

return state
}
}
Expand Down Expand Up @@ -167,4 +191,13 @@ final class ServerUsersViewModel: ViewModel, Eventful, Stateful, Identifiable {
let request = Paths.deleteUser(userID: id)
try await userSession.client.send(request)
}

// MARK: - Append User

private func appendUser(user: UserDto) async {
await MainActor.run {
users.append(user)
users = users.sorted(using: \.name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ struct AddServerUserView: View {
isPresentingError = true
case let .createdNewUser(newUser):
UIDevice.feedback(.success)

// TODO: somehow route to new user details

router.dismissCoordinator()
Notifications[.didAddServerUser].post(object: newUser)
}
}
.topBarTrailing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ struct ServerUsersView: View {
} message: {
Text(L10n.deleteUserSelfDeletion(viewModel.userSession.user.username))
}
.onNotification(.didAddServerUser) { notification in
Task {
let newUser = notification.object as! UserDto
try await Task.sleep(nanoseconds: 100_000_000)
viewModel.send(.appendUser(newUser))
router.route(to: \.userDetails, newUser)
}
}
}

// MARK: - User List View
Expand Down

0 comments on commit 237adbf

Please sign in to comment.