965 lines
32 KiB
Swift
965 lines
32 KiB
Swift
import SwiftUI
|
|
|
|
public struct RootView: View {
|
|
@StateObject private var store: SessionStore
|
|
@StateObject private var authViewModel: AuthViewModel
|
|
@StateObject private var chatListViewModel: ChatListViewModel
|
|
|
|
public init(store: SessionStore) {
|
|
let authViewModel = AuthViewModel(store: store)
|
|
let chatListViewModel = ChatListViewModel(bridge: store.bridge)
|
|
_store = StateObject(wrappedValue: store)
|
|
_authViewModel = StateObject(wrappedValue: authViewModel)
|
|
_chatListViewModel = StateObject(wrappedValue: chatListViewModel)
|
|
}
|
|
|
|
public var body: some View {
|
|
Group {
|
|
switch store.authState {
|
|
case .ready:
|
|
ChatListView(
|
|
viewModel: chatListViewModel,
|
|
bridge: store.bridge,
|
|
networkState: store.networkState,
|
|
typingState: store.typingState
|
|
)
|
|
default:
|
|
AuthView(state: store.authState, viewModel: authViewModel)
|
|
}
|
|
}
|
|
.task {
|
|
await store.refreshAuthState()
|
|
await store.refreshNetworkState()
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct AuthView: View {
|
|
public var state: AuthState
|
|
@ObservedObject public var viewModel: AuthViewModel
|
|
|
|
public init(state: AuthState, viewModel: AuthViewModel) {
|
|
self.state = state
|
|
self.viewModel = viewModel
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(spacing: 16) {
|
|
Text("Telegram")
|
|
.font(.largeTitle)
|
|
.fontWeight(.semibold)
|
|
|
|
authField
|
|
|
|
Button(action: {
|
|
Task { await viewModel.submitCurrentStep() }
|
|
}) {
|
|
Text(buttonTitle)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.disabled(viewModel.isLoading || !canSubmit)
|
|
|
|
if let errorMessage = viewModel.errorMessage {
|
|
Text(errorMessage)
|
|
.font(.footnote)
|
|
.foregroundStyle(.red)
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var authField: some View {
|
|
switch state {
|
|
case .waitPhoneNumber, .waitTdlibParameters:
|
|
TextField("Phone number", text: $viewModel.phone)
|
|
.textContentType(.telephoneNumber)
|
|
.textFieldStyle(.roundedBorder)
|
|
case .waitCode:
|
|
TextField("Code", text: $viewModel.code)
|
|
.textContentType(.oneTimeCode)
|
|
.textFieldStyle(.roundedBorder)
|
|
case .waitPassword:
|
|
SecureField("Password", text: $viewModel.password)
|
|
.textContentType(.password)
|
|
.textFieldStyle(.roundedBorder)
|
|
case .ready:
|
|
Text("Ready")
|
|
case .closed:
|
|
Text("Session closed")
|
|
case let .error(message):
|
|
Text(message)
|
|
.foregroundStyle(.red)
|
|
}
|
|
}
|
|
|
|
private var buttonTitle: String {
|
|
switch state {
|
|
case .waitPhoneNumber, .waitTdlibParameters:
|
|
"Continue"
|
|
case .waitCode:
|
|
"Verify"
|
|
case .waitPassword:
|
|
"Unlock"
|
|
default:
|
|
"Continue"
|
|
}
|
|
}
|
|
|
|
private var canSubmit: Bool {
|
|
switch state {
|
|
case .waitPhoneNumber, .waitTdlibParameters:
|
|
!viewModel.phone.isEmpty
|
|
case .waitCode:
|
|
!viewModel.code.isEmpty
|
|
case .waitPassword:
|
|
!viewModel.password.isEmpty
|
|
default:
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct ChatListView: View {
|
|
@ObservedObject public var viewModel: ChatListViewModel
|
|
public let bridge: SessionBridge
|
|
public var networkState: NetworkState
|
|
public var typingState: TypingState
|
|
@State private var selectedChat: ChatSummary?
|
|
@State private var showsAccountSwitcher = false
|
|
|
|
public init(
|
|
viewModel: ChatListViewModel,
|
|
bridge: SessionBridge,
|
|
networkState: NetworkState = .ready,
|
|
typingState: TypingState = .idle
|
|
) {
|
|
self.viewModel = viewModel
|
|
self.bridge = bridge
|
|
self.networkState = networkState
|
|
self.typingState = typingState
|
|
}
|
|
|
|
public var body: some View {
|
|
NavigationSplitView {
|
|
VStack(spacing: 0) {
|
|
List(selection: $selectedChat) {
|
|
ForEach(viewModel.filteredChats) { chat in
|
|
NavigationLink(value: chat) {
|
|
ChatRow(chat: chat)
|
|
}
|
|
}
|
|
}
|
|
ChatListStatusBar(networkState: networkState, typingState: typingState)
|
|
}
|
|
.navigationTitle("Chats")
|
|
.searchable(text: $viewModel.searchText)
|
|
.toolbar {
|
|
ToolbarItem {
|
|
Button("Accounts") {
|
|
showsAccountSwitcher = true
|
|
}
|
|
}
|
|
ToolbarItem {
|
|
folderMenu
|
|
}
|
|
}
|
|
.sheet(isPresented: $showsAccountSwitcher) {
|
|
AccountSwitcherView()
|
|
}
|
|
.task {
|
|
await viewModel.load()
|
|
}
|
|
} detail: {
|
|
if let selectedChat {
|
|
ChatDetailView(viewModel: ChatViewModel(chat: selectedChat, bridge: bridge), bridge: bridge) {
|
|
self.selectedChat = nil
|
|
Task { await viewModel.load() }
|
|
}
|
|
} else {
|
|
Text("Select a chat")
|
|
}
|
|
}
|
|
}
|
|
|
|
private var folderMenu: some View {
|
|
Menu("Folders") {
|
|
Button("All") {
|
|
viewModel.selectedFolderId = nil
|
|
Task { await viewModel.load() }
|
|
}
|
|
ForEach(viewModel.folders) { folder in
|
|
Button(folder.name) {
|
|
viewModel.selectedFolderId = folder.id
|
|
Task { await viewModel.load() }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct ChatListStatusBar: View {
|
|
public var networkState: NetworkState
|
|
public var typingState: TypingState
|
|
|
|
public init(networkState: NetworkState, typingState: TypingState) {
|
|
self.networkState = networkState
|
|
self.typingState = typingState
|
|
}
|
|
|
|
public var body: some View {
|
|
HStack(spacing: 8) {
|
|
Image(systemName: networkIconName)
|
|
.foregroundStyle(networkState == .ready ? .green : .orange)
|
|
Text(statusText)
|
|
.font(.footnote)
|
|
.lineLimit(1)
|
|
Spacer()
|
|
}
|
|
.padding(.horizontal, 12)
|
|
.padding(.vertical, 8)
|
|
.background(.bar)
|
|
}
|
|
|
|
private var networkIconName: String {
|
|
switch networkState {
|
|
case .ready:
|
|
"checkmark.circle.fill"
|
|
case .waitingForNetwork:
|
|
"wifi.slash"
|
|
case .connectingToProxy:
|
|
"shield.lefthalf.filled"
|
|
case .connecting:
|
|
"antenna.radiowaves.left.and.right"
|
|
case .updating:
|
|
"arrow.triangle.2.circlepath"
|
|
}
|
|
}
|
|
|
|
private var statusText: String {
|
|
switch typingState {
|
|
case let .typing(_, _, text) where !text.isEmpty:
|
|
text
|
|
case .typing:
|
|
"Typing"
|
|
case .idle:
|
|
networkText
|
|
}
|
|
}
|
|
|
|
private var networkText: String {
|
|
switch networkState {
|
|
case .ready:
|
|
"Online"
|
|
case .waitingForNetwork:
|
|
"Waiting for network"
|
|
case .connectingToProxy:
|
|
"Connecting to proxy"
|
|
case .connecting:
|
|
"Connecting"
|
|
case .updating:
|
|
"Updating"
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct ChatRow: View {
|
|
public var chat: ChatSummary
|
|
|
|
public init(chat: ChatSummary) {
|
|
self.chat = chat
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
HStack {
|
|
Text(chat.title)
|
|
.font(.headline)
|
|
.lineLimit(1)
|
|
if chat.isPinned {
|
|
Image(systemName: "pin.fill")
|
|
.font(.caption)
|
|
}
|
|
if chat.isMuted {
|
|
Image(systemName: "bell.slash")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
Spacer()
|
|
if chat.unreadMentionCount > 0 {
|
|
Text("@\(chat.unreadMentionCount)")
|
|
.font(.caption)
|
|
.padding(.horizontal, 7)
|
|
.padding(.vertical, 3)
|
|
.background(.orange, in: Capsule())
|
|
.foregroundStyle(.white)
|
|
}
|
|
if chat.unreadCount > 0 {
|
|
Text("\(chat.unreadCount)")
|
|
.font(.caption)
|
|
.padding(.horizontal, 7)
|
|
.padding(.vertical, 3)
|
|
.background(.blue, in: Capsule())
|
|
.foregroundStyle(.white)
|
|
}
|
|
}
|
|
HStack(spacing: 4) {
|
|
if chat.draft != nil {
|
|
Text("Draft")
|
|
.fontWeight(.semibold)
|
|
}
|
|
Text(chat.draft?.text ?? chat.lastMessage)
|
|
}
|
|
.font(.subheadline)
|
|
.foregroundStyle(chat.draft == nil ? Color.secondary : Color.red)
|
|
.lineLimit(2)
|
|
}
|
|
.padding(.vertical, 4)
|
|
}
|
|
}
|
|
|
|
public struct ChatDetailView: View {
|
|
@StateObject public var viewModel: ChatViewModel
|
|
public let bridge: SessionBridge
|
|
public let clipboard: ClipboardWriting
|
|
@StateObject private var profileViewModel: ProfileViewModel
|
|
@State private var showsProfile = false
|
|
@State private var editingMessage: Message?
|
|
@State private var editedText = ""
|
|
@State private var deleteCandidate: Message?
|
|
@State private var forwardCandidate: Message?
|
|
@State private var reactionCandidate: Message?
|
|
@State private var forwardChatIdText = ""
|
|
private let onChatLeft: () -> Void
|
|
|
|
public init(
|
|
viewModel: ChatViewModel,
|
|
bridge: SessionBridge,
|
|
clipboard: ClipboardWriting = SystemClipboardWriter(),
|
|
onChatLeft: @escaping () -> Void = {}
|
|
) {
|
|
_viewModel = StateObject(wrappedValue: viewModel)
|
|
self.bridge = bridge
|
|
self.clipboard = clipboard
|
|
self.onChatLeft = onChatLeft
|
|
_profileViewModel = StateObject(wrappedValue: ProfileViewModel(bridge: bridge))
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(spacing: 0) {
|
|
ScrollViewReader { scrollProxy in
|
|
VStack(spacing: 0) {
|
|
if !viewModel.pinnedMessages.isEmpty {
|
|
PinnedMessagesBar(messages: viewModel.pinnedMessages) { message in
|
|
withAnimation {
|
|
scrollProxy.scrollTo(message.id, anchor: .center)
|
|
}
|
|
}
|
|
}
|
|
List {
|
|
if !viewModel.searchText.isEmpty {
|
|
Section("Search") {
|
|
if viewModel.searchResults.isEmpty {
|
|
Text("No results")
|
|
.foregroundStyle(.secondary)
|
|
} else {
|
|
ForEach(viewModel.searchResults) { message in
|
|
MessageRow(message: message)
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Section {
|
|
ForEach(Array(viewModel.messages.enumerated()), id: \.element.id) { index, message in
|
|
if shouldShowDateSeparator(at: index) {
|
|
DateSeparatorView(timestamp: message.date)
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
MessageRow(message: message, showsSender: shouldShowSender(at: index))
|
|
.id(message.id)
|
|
.contextMenu {
|
|
Button {
|
|
viewModel.beginReply(to: message)
|
|
} label: {
|
|
Label("Reply", systemImage: "arrowshape.turn.up.left")
|
|
}
|
|
Button {
|
|
editingMessage = message
|
|
editedText = message.text
|
|
} label: {
|
|
Label("Edit", systemImage: "pencil")
|
|
}
|
|
Button {
|
|
forwardCandidate = message
|
|
forwardChatIdText = ""
|
|
} label: {
|
|
Label("Forward", systemImage: "arrowshape.turn.up.forward")
|
|
}
|
|
Button {
|
|
reactionCandidate = message
|
|
} label: {
|
|
Label("React", systemImage: "face.smiling")
|
|
}
|
|
Button {
|
|
Task {
|
|
await viewModel.copyPayload(for: message)
|
|
if let payload = viewModel.copiedPayload {
|
|
await clipboard.write(text: payload)
|
|
}
|
|
}
|
|
} label: {
|
|
Label("Copy", systemImage: "doc.on.doc")
|
|
}
|
|
Button(role: .destructive) {
|
|
deleteCandidate = message
|
|
} label: {
|
|
Label("Delete", systemImage: "trash")
|
|
}
|
|
}
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ComposeBar(
|
|
text: $viewModel.composeText,
|
|
replyTo: viewModel.replyTo,
|
|
cancelReply: { viewModel.cancelReply() }
|
|
) {
|
|
Task { await viewModel.send() }
|
|
}
|
|
}
|
|
.navigationTitle(viewModel.chat.title)
|
|
.searchable(text: $viewModel.searchText)
|
|
.onSubmit(of: .search) {
|
|
Task { await viewModel.search() }
|
|
}
|
|
.toolbar {
|
|
Button("Profile") {
|
|
showsProfile = true
|
|
Task { await profileViewModel.load(chatId: viewModel.chat.id) }
|
|
}
|
|
}
|
|
.sheet(isPresented: $showsProfile) {
|
|
ProfileView(viewModel: profileViewModel, chatId: viewModel.chat.id) {
|
|
showsProfile = false
|
|
onChatLeft()
|
|
}
|
|
}
|
|
.alert("Edit Message", isPresented: editAlertBinding) {
|
|
TextField("Message", text: $editedText)
|
|
Button("Save") {
|
|
if let editingMessage {
|
|
Task { await viewModel.edit(message: editingMessage, text: editedText) }
|
|
}
|
|
editingMessage = nil
|
|
}
|
|
Button("Cancel", role: .cancel) {
|
|
editingMessage = nil
|
|
}
|
|
}
|
|
.alert("Delete Message", isPresented: deleteAlertBinding) {
|
|
Button("Delete", role: .destructive) {
|
|
if let deleteCandidate {
|
|
Task { await viewModel.delete(message: deleteCandidate) }
|
|
}
|
|
deleteCandidate = nil
|
|
}
|
|
Button("Cancel", role: .cancel) {
|
|
deleteCandidate = nil
|
|
}
|
|
}
|
|
.alert("Forward Message", isPresented: forwardAlertBinding) {
|
|
TextField("Chat ID", text: $forwardChatIdText)
|
|
Button("Forward") {
|
|
if let forwardCandidate, let chatId = Int64(forwardChatIdText) {
|
|
Task { await viewModel.forward(message: forwardCandidate, to: chatId) }
|
|
}
|
|
forwardCandidate = nil
|
|
}
|
|
Button("Cancel", role: .cancel) {
|
|
forwardCandidate = nil
|
|
}
|
|
}
|
|
.confirmationDialog("React", isPresented: reactionDialogBinding, titleVisibility: .visible) {
|
|
ForEach(["👍", "❤️", "😂", "😮", "😢", "🙏"], id: \.self) { reaction in
|
|
Button(reaction) {
|
|
if let reactionCandidate {
|
|
Task { await viewModel.react(message: reactionCandidate, reaction: reaction) }
|
|
}
|
|
reactionCandidate = nil
|
|
}
|
|
}
|
|
Button("Cancel", role: .cancel) {
|
|
reactionCandidate = nil
|
|
}
|
|
}
|
|
.task {
|
|
await viewModel.load()
|
|
}
|
|
}
|
|
|
|
private var editAlertBinding: Binding<Bool> {
|
|
Binding(
|
|
get: { editingMessage != nil },
|
|
set: { isPresented in
|
|
if !isPresented {
|
|
editingMessage = nil
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
private var deleteAlertBinding: Binding<Bool> {
|
|
Binding(
|
|
get: { deleteCandidate != nil },
|
|
set: { isPresented in
|
|
if !isPresented {
|
|
deleteCandidate = nil
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
private var forwardAlertBinding: Binding<Bool> {
|
|
Binding(
|
|
get: { forwardCandidate != nil },
|
|
set: { isPresented in
|
|
if !isPresented {
|
|
forwardCandidate = nil
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
private var reactionDialogBinding: Binding<Bool> {
|
|
Binding(
|
|
get: { reactionCandidate != nil },
|
|
set: { isPresented in
|
|
if !isPresented {
|
|
reactionCandidate = nil
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
private func shouldShowDateSeparator(at index: Int) -> Bool {
|
|
guard viewModel.messages.indices.contains(index), viewModel.messages[index].date > 0 else {
|
|
return false
|
|
}
|
|
guard index > 0, viewModel.messages.indices.contains(index - 1) else {
|
|
return true
|
|
}
|
|
let current = Date(timeIntervalSince1970: TimeInterval(viewModel.messages[index].date))
|
|
let previous = Date(timeIntervalSince1970: TimeInterval(viewModel.messages[index - 1].date))
|
|
return !Calendar.current.isDate(current, inSameDayAs: previous)
|
|
}
|
|
|
|
private func shouldShowSender(at index: Int) -> Bool {
|
|
guard viewModel.messages.indices.contains(index) else {
|
|
return true
|
|
}
|
|
let message = viewModel.messages[index]
|
|
guard !message.isOutgoing else {
|
|
return false
|
|
}
|
|
guard index > 0, viewModel.messages.indices.contains(index - 1) else {
|
|
return true
|
|
}
|
|
let previous = viewModel.messages[index - 1]
|
|
return previous.isOutgoing
|
|
|| previous.senderName != message.senderName
|
|
|| shouldShowDateSeparator(at: index)
|
|
}
|
|
}
|
|
|
|
public struct DateSeparatorView: View {
|
|
public var timestamp: Int32
|
|
|
|
public init(timestamp: Int32) {
|
|
self.timestamp = timestamp
|
|
}
|
|
|
|
public var body: some View {
|
|
HStack {
|
|
Spacer()
|
|
Text(label)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.padding(.horizontal, 10)
|
|
.padding(.vertical, 4)
|
|
.background(Color.gray.opacity(0.12), in: Capsule())
|
|
Spacer()
|
|
}
|
|
.padding(.vertical, 6)
|
|
}
|
|
|
|
private var label: String {
|
|
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
|
|
let calendar = Calendar.current
|
|
if calendar.isDateInToday(date) {
|
|
return "Today"
|
|
}
|
|
if calendar.isDateInYesterday(date) {
|
|
return "Yesterday"
|
|
}
|
|
return Self.formatter.string(from: date)
|
|
}
|
|
|
|
private static let formatter: DateFormatter = {
|
|
let formatter = DateFormatter()
|
|
formatter.dateStyle = .medium
|
|
formatter.timeStyle = .none
|
|
return formatter
|
|
}()
|
|
}
|
|
|
|
public struct PinnedMessagesBar: View {
|
|
public var messages: [Message]
|
|
public var select: (Message) -> Void
|
|
|
|
public init(messages: [Message], select: @escaping (Message) -> Void) {
|
|
self.messages = messages
|
|
self.select = select
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(spacing: 0) {
|
|
ForEach(messages.prefix(3)) { message in
|
|
Button {
|
|
select(message)
|
|
} label: {
|
|
HStack(spacing: 8) {
|
|
Image(systemName: "pin.fill")
|
|
.font(.caption)
|
|
.foregroundStyle(.blue)
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(message.senderName)
|
|
.font(.caption2)
|
|
.foregroundStyle(.secondary)
|
|
Text(message.text)
|
|
.font(.subheadline)
|
|
.lineLimit(1)
|
|
}
|
|
Spacer()
|
|
}
|
|
.padding(.horizontal, 12)
|
|
.padding(.vertical, 8)
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
}
|
|
.background(Color.blue.opacity(0.08))
|
|
.overlay(alignment: .bottom) {
|
|
Divider()
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct MessageRow: View {
|
|
public var message: Message
|
|
public var showsSender: Bool
|
|
|
|
public init(message: Message, showsSender: Bool = true) {
|
|
self.message = message
|
|
self.showsSender = showsSender
|
|
}
|
|
|
|
public var body: some View {
|
|
HStack {
|
|
if message.isOutgoing {
|
|
Spacer(minLength: 48)
|
|
}
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
if showsSender && !message.isOutgoing {
|
|
Text(message.senderName)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
if let replyText = message.replyText {
|
|
Text(replyText)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.padding(.leading, 6)
|
|
}
|
|
if let forwardSenderName = message.forwardSenderName {
|
|
Label(forwardSenderName, systemImage: "arrowshape.turn.up.forward")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
if let media = message.media {
|
|
MediaPlaceholderView(media: media, mediaAlbumId: message.mediaAlbumId)
|
|
}
|
|
Text(renderedText)
|
|
.textSelection(.enabled)
|
|
if !message.reactions.isEmpty {
|
|
Text(message.reactions.map(\.emoji).joined(separator: " "))
|
|
.font(.caption)
|
|
}
|
|
if message.editDate != nil || message.date > 0 || message.isOutgoing {
|
|
HStack(spacing: 6) {
|
|
if message.date > 0 {
|
|
Text(Self.timeFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(message.date))))
|
|
}
|
|
if message.editDate != nil {
|
|
Text("edited")
|
|
}
|
|
if message.isOutgoing {
|
|
Image(systemName: message.isRead ? "checkmark.circle.fill" : "checkmark.circle")
|
|
}
|
|
}
|
|
.font(.caption2)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
.padding(10)
|
|
.background(message.isOutgoing ? Color.blue.opacity(0.16) : Color.gray.opacity(0.12), in: RoundedRectangle(cornerRadius: 8))
|
|
if !message.isOutgoing {
|
|
Spacer(minLength: 48)
|
|
}
|
|
}
|
|
}
|
|
|
|
private var renderedText: AttributedString {
|
|
(
|
|
try? AttributedString(
|
|
markdown: message.text,
|
|
options: AttributedString.MarkdownParsingOptions(
|
|
interpretedSyntax: .inlineOnlyPreservingWhitespace
|
|
)
|
|
)
|
|
) ?? AttributedString(message.text)
|
|
}
|
|
|
|
private static let timeFormatter: DateFormatter = {
|
|
let formatter = DateFormatter()
|
|
formatter.dateStyle = .none
|
|
formatter.timeStyle = .short
|
|
return formatter
|
|
}()
|
|
}
|
|
|
|
public struct MediaPlaceholderView: View {
|
|
public var media: MessageMedia
|
|
public var mediaAlbumId: Int64?
|
|
|
|
public init(media: MessageMedia, mediaAlbumId: Int64? = nil) {
|
|
self.media = media
|
|
self.mediaAlbumId = mediaAlbumId
|
|
}
|
|
|
|
public var body: some View {
|
|
HStack(spacing: 8) {
|
|
Image(systemName: iconName)
|
|
.frame(width: 22, height: 22)
|
|
.foregroundStyle(.blue)
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
HStack(spacing: 6) {
|
|
Text(title)
|
|
.font(.subheadline)
|
|
if mediaAlbumId != nil {
|
|
Image(systemName: "square.stack.3d.up.fill")
|
|
.font(.caption2)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
Text(detail)
|
|
.font(.caption2)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
Spacer(minLength: 0)
|
|
}
|
|
.padding(8)
|
|
.background(Color.gray.opacity(0.10), in: RoundedRectangle(cornerRadius: 8))
|
|
}
|
|
|
|
private var iconName: String {
|
|
switch media {
|
|
case .photo:
|
|
"photo"
|
|
case .voice:
|
|
"waveform"
|
|
}
|
|
}
|
|
|
|
private var title: String {
|
|
switch media {
|
|
case .photo:
|
|
"Photo"
|
|
case .voice:
|
|
"Voice"
|
|
}
|
|
}
|
|
|
|
private var detail: String {
|
|
switch media {
|
|
case let .photo(photo):
|
|
"\(photo.width)x\(photo.height) · \(downloadLabel(photo.downloadState))"
|
|
case let .voice(voice):
|
|
"\(voice.duration)s · \(downloadLabel(voice.downloadState))"
|
|
}
|
|
}
|
|
|
|
private func downloadLabel(_ state: MediaDownloadState) -> String {
|
|
switch state {
|
|
case .notDownloaded:
|
|
"not downloaded"
|
|
case .downloading:
|
|
"downloading"
|
|
case .downloaded:
|
|
"downloaded"
|
|
case .error:
|
|
"error"
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct ComposeBar: View {
|
|
@Binding public var text: String
|
|
public var replyTo: Message?
|
|
public var cancelReply: () -> Void
|
|
public var send: () -> Void
|
|
|
|
public init(
|
|
text: Binding<String>,
|
|
replyTo: Message? = nil,
|
|
cancelReply: @escaping () -> Void = {},
|
|
send: @escaping () -> Void
|
|
) {
|
|
_text = text
|
|
self.replyTo = replyTo
|
|
self.cancelReply = cancelReply
|
|
self.send = send
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(spacing: 8) {
|
|
if let replyTo {
|
|
HStack {
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(replyTo.senderName)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
Text(replyTo.text)
|
|
.font(.footnote)
|
|
.lineLimit(1)
|
|
}
|
|
Spacer()
|
|
Button(action: cancelReply) {
|
|
Image(systemName: "xmark.circle.fill")
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
.padding(.horizontal, 10)
|
|
.padding(.vertical, 6)
|
|
.background(Color.gray.opacity(0.12), in: RoundedRectangle(cornerRadius: 8))
|
|
}
|
|
HStack(spacing: 10) {
|
|
if !text.isEmpty {
|
|
Button {
|
|
text = ""
|
|
} label: {
|
|
Image(systemName: "xmark.circle.fill")
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
TextField("Message", text: $text, axis: .vertical)
|
|
.textFieldStyle(.roundedBorder)
|
|
.lineLimit(1...5)
|
|
Button {
|
|
send()
|
|
} label: {
|
|
Image(systemName: "paperplane.fill")
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.disabled(text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
|
}
|
|
}
|
|
.padding()
|
|
.background(.bar)
|
|
}
|
|
}
|
|
|
|
public struct ProfileView: View {
|
|
@ObservedObject public var viewModel: ProfileViewModel
|
|
public var chatId: Int64?
|
|
public var onLeave: () -> Void
|
|
@State private var confirmsLeave = false
|
|
|
|
public init(viewModel: ProfileViewModel, chatId: Int64? = nil, onLeave: @escaping () -> Void = {}) {
|
|
self.viewModel = viewModel
|
|
self.chatId = chatId
|
|
self.onLeave = onLeave
|
|
}
|
|
|
|
public var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
if let profile = viewModel.profile {
|
|
Section {
|
|
Text(profile.title)
|
|
.font(.title2)
|
|
if let username = profile.username {
|
|
Text("@\(username)")
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
if let bio = profile.bio {
|
|
Text(bio)
|
|
}
|
|
}
|
|
if let memberCount = profile.memberCount {
|
|
Section {
|
|
Text("\(memberCount) members")
|
|
}
|
|
}
|
|
if profile.isGroup, chatId != nil {
|
|
Section {
|
|
Button(role: .destructive) {
|
|
confirmsLeave = true
|
|
} label: {
|
|
Label("Leave Chat", systemImage: "rectangle.portrait.and.arrow.right")
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
ProgressView()
|
|
}
|
|
}
|
|
.navigationTitle("Profile")
|
|
.alert("Leave Chat", isPresented: $confirmsLeave) {
|
|
Button("Leave", role: .destructive) {
|
|
if let chatId {
|
|
Task {
|
|
await viewModel.leave(chatId: chatId)
|
|
if viewModel.errorMessage == nil {
|
|
onLeave()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Button("Cancel", role: .cancel) {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct AccountSwitcherView: View {
|
|
public init() {}
|
|
|
|
public var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
Section {
|
|
Label("Default", systemImage: "person.crop.circle")
|
|
Label("Add account", systemImage: "plus.circle")
|
|
}
|
|
}
|
|
.navigationTitle("Accounts")
|
|
}
|
|
}
|
|
}
|