IceCubesApp/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift

82 lines
2.5 KiB
Swift
Raw Normal View History

import DesignSystem
import Env
2023-01-17 10:36:01 +00:00
import Models
import Network
import Shimmer
import SwiftUI
public struct ConversationsListView: View {
@EnvironmentObject private var routeurPath: RouterPath
@EnvironmentObject private var watcher: StreamWatcher
@EnvironmentObject private var client: Client
@EnvironmentObject private var theme: Theme
2023-01-17 10:36:01 +00:00
@StateObject private var viewModel = ConversationsListViewModel()
2023-01-17 10:36:01 +00:00
public init() {}
private var conversations: [Conversation] {
if viewModel.isLoadingFirstPage {
return Conversation.placeholders()
}
return viewModel.conversations
}
2023-01-17 10:36:01 +00:00
public var body: some View {
ScrollView {
LazyVStack {
if !conversations.isEmpty || viewModel.isLoadingFirstPage {
ForEach(conversations) { conversation in
if viewModel.isLoadingFirstPage {
ConversationsListRow(conversation: conversation, viewModel: viewModel)
.padding(.horizontal, .layoutPadding)
.redacted(reason: .placeholder)
.shimmering()
} else {
ConversationsListRow(conversation: conversation, viewModel: viewModel)
.padding(.horizontal, .layoutPadding)
}
Divider()
}
2023-01-07 17:01:06 +00:00
} else if conversations.isEmpty && !viewModel.isLoadingFirstPage && !viewModel.isError {
EmptyView(iconName: "tray",
title: "Inbox Zero",
message: "Looking for some social media love? You'll find all your direct messages and private mentions right here. Happy messaging! 📱❤️")
2023-01-07 17:01:06 +00:00
} else if viewModel.isError {
ErrorView(title: "An error occurred",
message: "Error while loading your messages",
buttonTitle: "Retry") {
Task {
await viewModel.fetchConversations()
}
}
}
}
.padding(.top, .layoutPadding)
}
.scrollContentBackground(.hidden)
.background(theme.primaryBackgroundColor)
.navigationTitle("Direct Messages")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
2023-01-06 20:34:24 +00:00
StatusEditorToolbarItem(visibility: .direct)
}
2023-01-17 10:36:01 +00:00
.onChange(of: watcher.latestEvent?.id) { _ in
if let latestEvent = watcher.latestEvent {
viewModel.handleEvent(event: latestEvent)
}
}
.refreshable {
await viewModel.fetchConversations()
}
.onAppear {
viewModel.client = client
if client.isAuth {
Task {
await viewModel.fetchConversations()
}
}
}
}
}