-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Communication: Add Profile Info View (#177)
- Loading branch information
Showing
7 changed files
with
199 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
ArtemisKit/Sources/Messages/ViewModels/MessageDetailViewModels/ProfileViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// ProfileViewModel.swift | ||
// ArtemisKit | ||
// | ||
// Created by Anian Schleyer on 23.09.24. | ||
// | ||
|
||
import Common | ||
import Foundation | ||
import Navigation | ||
import SharedModels | ||
import SharedServices | ||
import UserStore | ||
|
||
@Observable | ||
class ProfileViewModel { | ||
var error: UserFacingError? | ||
var isLoading = false | ||
var showProfileSheet = false | ||
|
||
let user: ConversationUser | ||
let role: UserRole? | ||
private var login: String? | ||
let course: Course | ||
|
||
init(course: Course, user: ConversationUser, role: UserRole?) { | ||
self.course = course | ||
self.user = user | ||
self.role = role | ||
} | ||
|
||
// We can only create a conversation if we have the user's login | ||
// Don't allow sending messages to oneself | ||
var canSendMessage: Bool { | ||
(user.login != nil || login != nil) | ||
&& UserSessionFactory.shared.user?.id != user.id | ||
} | ||
|
||
func loadUserLogin() async { | ||
guard let name = user.name else { return } | ||
isLoading = true | ||
let result = await CourseServiceFactory.shared.getCourseMembers(courseId: course.id, searchLoginOrName: name) | ||
switch result { | ||
case .done(let matches): | ||
login = matches.first(where: { $0.name == user.name })?.login | ||
isLoading = false | ||
default: | ||
// No user found – cannot send a message | ||
break | ||
} | ||
} | ||
|
||
@MainActor | ||
func openConversation(navigationController: NavigationController, completion: @escaping () -> Void) { | ||
guard let login = user.login ?? login else { return } | ||
isLoading = true | ||
Task { | ||
let messageCellModel = MessageCellModel(course: course, conversationPath: nil, isHeaderVisible: false, roundBottomCorners: false, retryButtonAction: {}) | ||
if let conversation = await messageCellModel.getOneToOneChatOrCreate(login: login) { | ||
navigationController.goToCourseConversation(courseId: course.id, conversation: conversation) | ||
completion() | ||
} | ||
isLoading = false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters