forked from GetStream/stream-chat-swiftui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiMessagePocView.swift
145 lines (127 loc) · 4.84 KB
/
iMessagePocView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Copyright © 2024 Stream.io Inc. All rights reserved.
//
import StreamChat
import StreamChatSwiftUI
import SwiftUI
struct iMessagePocView: View {
@Injected(\.colors) var colors
@StateObject var viewModel: iMessageChatChannelListViewModel
@StateObject private var channelHeaderLoader = ChannelHeaderLoader()
private var factory = iMessageViewFactory.shared
init() {
_viewModel = StateObject(
wrappedValue:
iMessageChatChannelListViewModel(
channelListController: nil,
selectedChannelId: nil
)
)
}
var body: some View {
NavigationView {
VStack {
if !viewModel.pinnedChannels.isEmpty {
ScrollView(.horizontal) {
HStack {
ForEach(viewModel.pinnedChannels) { channel in
ChannelAvatarView(
avatar: channelHeaderLoader.image(for: channel),
showOnlineIndicator: false
)
.padding()
}
}
}
.frame(height: 60)
}
ChannelList(
factory: factory,
channels: viewModel.channels,
selectedChannel: $viewModel.selectedChannel,
swipedChannelId: $viewModel.swipedChannelId,
onlineIndicatorShown: viewModel.onlineIndicatorShown(for:),
imageLoader: channelHeaderLoader.image(for:),
onItemTap: { channel in
viewModel.selectedChannel = ChannelSelectionInfo(channel: channel, message: nil)
},
onItemAppear: viewModel.checkForChannels(index:),
channelNaming: viewModel.name(forChannel:),
channelDestination: factory.makeChannelDestination(),
trailingSwipeRightButtonTapped: viewModel.onDeleteTapped(channel:),
trailingSwipeLeftButtonTapped: viewModel.onMoreTapped(channel:),
leadingSwipeButtonTapped: viewModel.pinChannelTapped(_:)
)
.alert(isPresented: $viewModel.alertShown) {
switch viewModel.channelAlertType {
case let .deleteChannel(channel):
return Alert(
title: Text("Delete"),
message: Text("Are you sure you want to delete this channel?"),
primaryButton: .destructive(Text("Delete")) {
viewModel.delete(channel: channel)
},
secondaryButton: .cancel()
)
default:
return Alert.defaultErrorAlert
}
}
Spacer()
}
.blur(radius: (viewModel.customAlertShown || viewModel.alertShown) ? 6 : 0)
.overlay(viewModel.customAlertShown ? customViewOverlay() : nil)
.accentColor(colors.tintColor)
.navigationTitle("Messages")
}
}
@ViewBuilder
private func customViewOverlay() -> some View {
switch viewModel.customChannelPopupType {
case let .moreActions(channel):
factory.makeMoreChannelActionsView(
for: channel,
swipedChannelId: $viewModel.swipedChannelId
) {
withAnimation {
viewModel.customChannelPopupType = nil
}
} onError: { error in
viewModel.showErrorPopup(error)
}
.edgesIgnoringSafeArea(.all)
default:
EmptyView()
}
}
}
class iMessageChatChannelListViewModel: ChatChannelListViewModel {
@Published var pinnedChannels = [ChatChannel]()
func pinChannelTapped(_ channel: ChatChannel) {
if !pinnedChannels.contains(channel) {
pinnedChannels.append(channel)
}
}
}
class iMessageViewFactory: ViewFactory {
@Injected(\.chatClient) var chatClient
@Injected(\.colors) var colors
static let shared = iMessageViewFactory()
private init() {}
func makeLeadingSwipeActionsView(
channel: ChatChannel,
offsetX: CGFloat,
buttonWidth: CGFloat,
buttonTapped: @escaping (ChatChannel) -> Void
) -> some View {
HStack {
ActionItemButton(imageName: "pin.fill") {
buttonTapped(channel)
}
.frame(width: buttonWidth)
.foregroundColor(Color.white)
.background(Color.yellow)
Spacer()
}
}
}