Skip to content

Commit 2bbcf9e

Browse files
added docs, method renaming
1 parent dd63075 commit 2bbcf9e

File tree

6 files changed

+32
-17
lines changed

6 files changed

+32
-17
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public class MessageComposerViewModel: ObservableObject {
220220

221221
public var sendButtonEnabled: Bool {
222222
if let composerCommand = composerCommand,
223-
let handler = commandsHandler.canShowSuggestions(for: composerCommand) {
223+
let handler = commandsHandler.commandHandler(for: composerCommand) {
224224
return handler
225225
.canBeExecuted(composerCommand: composerCommand)
226226
}

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/CommandsHandler.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public protocol CommandHandler {
1212
/// Identifier of the command.
1313
var id: String { get }
1414

15+
/// Display info for the command.
1516
var displayInfo: CommandDisplayInfo? { get }
1617

1718
/// Checks whether the command can be handled.
@@ -24,7 +25,10 @@ public protocol CommandHandler {
2425
caretLocation: Int
2526
) -> ComposerCommand?
2627

27-
func canShowSuggestions(for command: ComposerCommand) -> CommandHandler?
28+
/// Returns a command handler for a command (if available).
29+
/// - Parameter command: the command whose handler will be returned.
30+
/// - Returns: Optional `CommandHandler`.
31+
func commandHandler(for command: ComposerCommand) -> CommandHandler?
2832

2933
/// Shows suggestions for the provided command.
3034
/// - Parameter command: the command whose suggestions will be shown.
@@ -46,14 +50,22 @@ public protocol CommandHandler {
4650
extraData: [String: Any]
4751
)
4852

53+
/// Checks whether the command can be executed on message sent.
54+
/// - Parameter command: the command to be checked.
55+
/// - Returns: `Bool` whether the command can be executed.
4956
func canBeExecuted(composerCommand: ComposerCommand) -> Bool
5057

58+
/// Needs to be implemented if you need some code executed before the message is sent.
59+
/// - Parameters:
60+
/// - composerCommand: the command to be executed.
61+
/// - completion: called when the command is executed.
5162
func executeOnMessageSent(
5263
composerCommand: ComposerCommand,
5364
completion: @escaping (Error?) -> Void
5465
)
5566
}
5667

68+
/// Default implementations.
5769
extension CommandHandler {
5870

5971
public func executeOnMessageSent(
@@ -74,7 +86,9 @@ public struct ComposerCommand {
7486
let id: String
7587
/// Typing suggestion that invokes the command.
7688
var typingSuggestion: TypingSuggestion
89+
/// Display info for the command.
7790
let displayInfo: CommandDisplayInfo?
91+
/// Whether execution of the command replaces sending of a message.
7892
var replacesMessageSent: Bool = false
7993
}
8094

@@ -86,6 +100,7 @@ public struct SuggestionInfo {
86100
let value: Any
87101
}
88102

103+
/// Display information about a command.
89104
public struct CommandDisplayInfo {
90105
let displayName: String
91106
let icon: UIImage
@@ -118,9 +133,9 @@ public class CommandsHandler: CommandHandler {
118133
return nil
119134
}
120135

121-
public func canShowSuggestions(for command: ComposerCommand) -> CommandHandler? {
136+
public func commandHandler(for command: ComposerCommand) -> CommandHandler? {
122137
for handler in commands {
123-
if handler.canShowSuggestions(for: command) != nil {
138+
if handler.commandHandler(for: command) != nil {
124139
return handler
125140
}
126141
}
@@ -131,7 +146,7 @@ public class CommandsHandler: CommandHandler {
131146
public func showSuggestions(
132147
for command: ComposerCommand
133148
) -> Future<SuggestionInfo, Error> {
134-
if let handler = canShowSuggestions(for: command) {
149+
if let handler = commandHandler(for: command) {
135150
return handler.showSuggestions(for: command)
136151
}
137152

@@ -148,7 +163,7 @@ public class CommandsHandler: CommandHandler {
148163
return
149164
}
150165

151-
if let handler = canShowSuggestions(for: commandValue), handler.id != id {
166+
if let handler = commandHandler(for: commandValue), handler.id != id {
152167
handler.handleCommand(
153168
for: text,
154169
selectedRangeLocation: selectedRangeLocation,
@@ -162,7 +177,7 @@ public class CommandsHandler: CommandHandler {
162177
composerCommand: ComposerCommand,
163178
completion: @escaping (Error?) -> Void
164179
) {
165-
if let handler = canShowSuggestions(for: composerCommand) {
180+
if let handler = commandHandler(for: composerCommand) {
166181
handler.executeOnMessageSent(
167182
composerCommand: composerCommand,
168183
completion: completion
@@ -171,7 +186,7 @@ public class CommandsHandler: CommandHandler {
171186
}
172187

173188
public func canBeExecuted(composerCommand: ComposerCommand) -> Bool {
174-
if let handler = canShowSuggestions(for: composerCommand), handler.id != id {
189+
if let handler = commandHandler(for: composerCommand), handler.id != id {
175190
return handler.canBeExecuted(composerCommand: composerCommand)
176191
}
177192

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/InstantCommands/GiphyCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public struct GiphyCommandHandler: CommandHandler {
6262
extraData: [String: Any]
6363
) {}
6464

65-
public func canShowSuggestions(for command: ComposerCommand) -> CommandHandler? {
65+
public func commandHandler(for command: ComposerCommand) -> CommandHandler? {
6666
nil
6767
}
6868

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/InstantCommands/InstantCommandsHandler.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public class InstantCommandsHandler: CommandHandler {
5454
}
5555
}
5656

57-
public func canShowSuggestions(for command: ComposerCommand) -> CommandHandler? {
57+
public func commandHandler(for command: ComposerCommand) -> CommandHandler? {
5858
for instant in commands {
59-
if instant.canShowSuggestions(for: command) != nil {
59+
if instant.commandHandler(for: command) != nil {
6060
return instant
6161
}
6262
}
@@ -65,7 +65,7 @@ public class InstantCommandsHandler: CommandHandler {
6565
}
6666

6767
public func showSuggestions(for command: ComposerCommand) -> Future<SuggestionInfo, Error> {
68-
if let handler = canShowSuggestions(for: command), handler.id != id {
68+
if let handler = commandHandler(for: command), handler.id != id {
6969
return handler.showSuggestions(for: command)
7070
}
7171
let suggestionInfo = SuggestionInfo(key: id, value: commands)
@@ -79,7 +79,7 @@ public class InstantCommandsHandler: CommandHandler {
7979
extraData: [String: Any]
8080
) {
8181
if let commandValue = command.wrappedValue,
82-
let handler = canShowSuggestions(for: commandValue), handler.id != id {
82+
let handler = commandHandler(for: commandValue), handler.id != id {
8383
handler.handleCommand(
8484
for: text,
8585
selectedRangeLocation: selectedRangeLocation,
@@ -99,7 +99,7 @@ public class InstantCommandsHandler: CommandHandler {
9999
composerCommand: ComposerCommand,
100100
completion: @escaping (Error?) -> Void
101101
) {
102-
if let handler = canShowSuggestions(for: composerCommand) {
102+
if let handler = commandHandler(for: composerCommand) {
103103
handler.executeOnMessageSent(
104104
composerCommand: composerCommand,
105105
completion: completion
@@ -108,7 +108,7 @@ public class InstantCommandsHandler: CommandHandler {
108108
}
109109

110110
public func canBeExecuted(composerCommand: ComposerCommand) -> Bool {
111-
if let handler = canShowSuggestions(for: composerCommand), handler.id != id {
111+
if let handler = commandHandler(for: composerCommand), handler.id != id {
112112
return handler.canBeExecuted(composerCommand: composerCommand)
113113
}
114114

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/InstantCommands/MuteCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class MuteCommandHandler: CommandHandler {
9898
}
9999
}
100100

101-
public func canShowSuggestions(for command: ComposerCommand) -> CommandHandler? {
101+
public func commandHandler(for command: ComposerCommand) -> CommandHandler? {
102102
if let mutedUser = mutedUser,
103103
command.typingSuggestion.text != mentionText(for: mutedUser) {
104104
self.mutedUser = nil

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/Mentions/MentionsCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public struct MentionsCommandHandler: CommandHandler {
7070
command.wrappedValue = nil
7171
}
7272

73-
public func canShowSuggestions(for command: ComposerCommand) -> CommandHandler? {
73+
public func commandHandler(for command: ComposerCommand) -> CommandHandler? {
7474
command.id == id ? self : nil
7575
}
7676

0 commit comments

Comments
 (0)