Skip to content

Commit

Permalink
Create MessagePathView
Browse files Browse the repository at this point in the history
  • Loading branch information
nityanandaz committed Mar 14, 2024
1 parent 3affb83 commit 6a2c695
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public struct NavigationDestinationThreadViewModifier: ViewModifier {
public init() {}

public func body(content: Content) -> some View {
content.navigationDestination(for: MessagePath.self, destination: MessageDetailView.init)
content.navigationDestination(for: MessagePath.self, destination: MessagePathView.init)
}
}
29 changes: 29 additions & 0 deletions ArtemisKit/Sources/Messages/Navigation/PathViewModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ final class ConversationPathViewModel {
}
}
}

@Observable
final class MessagePathViewModel {
static let size = 50

let path: MessagePath
var message: DataState<BaseMessage>

private let messagesService: MessagesService

init(path: MessagePath, messagesService: MessagesService = MessagesServiceFactory.shared) {
self.path = path
self.message = path.message.wrappedValue.value.map(DataState.done) ?? .loading

self.messagesService = messagesService
}

func loadMessage() async {
let result = await messagesService.getMessages(
for: path.conversationPath.coursePath.id, and: path.conversationPath.id, size: MessagePathViewModel.size
)
self.message = result.flatMap { messages in
guard let message = messages.first(where: { $0.id == path.id }) else {
return .failure(UserFacingError(title: R.string.localizable.messageCouldNotBeLoadedError()))
}
return .success(message)
}
}
}
30 changes: 30 additions & 0 deletions ArtemisKit/Sources/Messages/Navigation/PathViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,33 @@ public extension ConversationPathView where Content == ConversationView {
self.init(viewModel: ConversationPathViewModel(path: path), content: Content.init)
}
}

extension ConversationPathView {
init(path: ConversationPath, @ViewBuilder content: @escaping (Course, Conversation) -> Content) {
self.init(viewModel: ConversationPathViewModel(path: path), content: content)
}
}

// MARK: - MessagePathView

struct MessagePathView<Content: View>: View {
@State var viewModel: MessagePathViewModel
let content: (Course, Conversation, BaseMessage) -> Content

var body: some View {
DataStateView(data: $viewModel.message) {
await viewModel.loadMessage()
} content: { message in
ConversationPathView(path: viewModel.path.conversationPath) { (course, conversation) in
content(course, conversation, message)
}
}

}
}

extension MessagePathView where Content == MessageDetailView {
init(path: MessagePath) {
self.init(viewModel: MessagePathViewModel(path: path), content: Content.init)
}
}

0 comments on commit 6a2c695

Please sign in to comment.