Skip to content

Commit c7c932f

Browse files
code cleanup, localizing texts
1 parent e2a7df0 commit c7c932f

File tree

8 files changed

+108
-54
lines changed

8 files changed

+108
-54
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public struct GiphyCommandHandler: CommandHandler {
3232
displayInfo = CommandDisplayInfo(
3333
displayName: "Giphy",
3434
icon: images.commandGiphy,
35-
format: "\(id) [text]",
35+
format: "\(id) [\(L10n.Composer.Commands.Format.text)]",
3636
isInstant: true
3737
)
3838
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ import Combine
66
import StreamChat
77
import SwiftUI
88

9+
/// Handler for istant commands.
910
public class InstantCommandsHandler: CommandHandler {
1011

1112
public let id: String
1213
public var displayInfo: CommandDisplayInfo?
1314

14-
private let typingSuggester = TypingSuggester(
15-
options:
16-
TypingSuggestionOptions(
17-
symbol: "/",
18-
shouldTriggerOnlyAtStart: true
19-
)
20-
)
15+
private let typingSuggester: TypingSuggester
2116
private let commands: [CommandHandler]
2217

2318
public init(
2419
commands: [CommandHandler],
20+
symbol: String = "/",
2521
id: String = "instantCommands"
2622
) {
2723
self.commands = commands
2824
self.id = id
25+
typingSuggester = TypingSuggester(
26+
options:
27+
TypingSuggestionOptions(
28+
symbol: symbol,
29+
shouldTriggerOnlyAtStart: true
30+
)
31+
)
2932
}
3033

3134
public func canHandleCommand(in text: String, caretLocation: Int) -> ComposerCommand? {

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

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import StreamChat
66
import SwiftUI
77

8+
/// View for the instant commands suggestions.
89
struct InstantCommandsView: View {
910

1011
@Injected(\.images) private var images
@@ -16,46 +17,26 @@ struct InstantCommandsView: View {
1617

1718
var body: some View {
1819
VStack {
19-
HStack {
20-
Image(uiImage: images.smallBolt)
21-
.renderingMode(.template)
22-
.resizable()
23-
.frame(width: 24, height: 24)
24-
.foregroundColor(colors.tintColor)
25-
Text(L10n.Composer.Suggestions.Commands.header)
26-
.font(fonts.body)
27-
.foregroundColor(Color(colors.textLowEmphasis))
28-
Spacer()
29-
}
30-
.standardPadding()
20+
InstantCommandsHeader()
21+
.standardPadding()
3122

3223
ForEach(0..<instantCommands.count) { i in
3324
let command = instantCommands[i]
3425
if let displayInfo = command.displayInfo {
35-
HStack {
36-
Image(uiImage: displayInfo.icon)
37-
Text(displayInfo.displayName)
38-
.font(fonts.title3)
39-
.bold()
40-
.foregroundColor(Color(colors.text))
41-
Text(displayInfo.format)
42-
.font(fonts.body)
43-
.foregroundColor(Color(colors.textLowEmphasis))
44-
Spacer()
45-
}
46-
.standardPadding()
47-
.highPriorityGesture(
48-
TapGesture()
49-
.onEnded { _ in
50-
let instantCommand = ComposerCommand(
51-
id: command.id,
52-
typingSuggestion: TypingSuggestion.empty,
53-
displayInfo: command.displayInfo,
54-
replacesMessageSent: command.replacesMessageSent
55-
)
56-
commandSelected(instantCommand)
57-
}
58-
)
26+
InstantCommandView(displayInfo: displayInfo)
27+
.standardPadding()
28+
.highPriorityGesture(
29+
TapGesture()
30+
.onEnded { _ in
31+
let instantCommand = ComposerCommand(
32+
id: command.id,
33+
typingSuggestion: TypingSuggestion.empty,
34+
displayInfo: command.displayInfo,
35+
replacesMessageSent: command.replacesMessageSent
36+
)
37+
commandSelected(instantCommand)
38+
}
39+
)
5940
}
6041
}
6142
}
@@ -65,3 +46,49 @@ struct InstantCommandsView: View {
6546
.animation(.spring())
6647
}
6748
}
49+
50+
/// View for the instant commands header.
51+
struct InstantCommandsHeader: View {
52+
53+
@Injected(\.images) private var images
54+
@Injected(\.fonts) private var fonts
55+
@Injected(\.colors) private var colors
56+
57+
var body: some View {
58+
HStack {
59+
Image(uiImage: images.smallBolt)
60+
.renderingMode(.template)
61+
.resizable()
62+
.frame(width: 24, height: 24)
63+
.foregroundColor(colors.tintColor)
64+
Text(L10n.Composer.Suggestions.Commands.header)
65+
.font(fonts.body)
66+
.foregroundColor(Color(colors.textLowEmphasis))
67+
Spacer()
68+
}
69+
}
70+
}
71+
72+
/// View for an instant command entry.
73+
struct InstantCommandView: View {
74+
75+
@Injected(\.images) private var images
76+
@Injected(\.fonts) private var fonts
77+
@Injected(\.colors) private var colors
78+
79+
var displayInfo: CommandDisplayInfo
80+
81+
var body: some View {
82+
HStack {
83+
Image(uiImage: displayInfo.icon)
84+
Text(displayInfo.displayName)
85+
.font(fonts.title3)
86+
.bold()
87+
.foregroundColor(Color(colors.text))
88+
Text(displayInfo.format)
89+
.font(fonts.body)
90+
.foregroundColor(Color(colors.textLowEmphasis))
91+
Spacer()
92+
}
93+
}
94+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public class MuteCommandHandler: TwoStepMentionCommand {
2323
id: id
2424
)
2525
let displayInfo = CommandDisplayInfo(
26-
displayName: "Mute",
26+
displayName: L10n.Composer.Commands.mute,
2727
icon: images.commandMute,
28-
format: "\(id) [@username]",
28+
format: "\(id) [\(L10n.Composer.Commands.Format.username)]",
2929
isInstant: true
3030
)
3131
self.displayInfo = displayInfo

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import StreamChat
77
import SwiftUI
88

99
/// Base class that supports two step commands, where the second one is mentioning users.
10-
public class TwoStepMentionCommand: CommandHandler {
10+
open class TwoStepMentionCommand: CommandHandler {
1111

1212
@Injected(\.images) private var images
1313
@Injected(\.colors) private var colors
1414

1515
private let channelController: ChatChannelController
1616
private let mentionsCommandHandler: MentionsCommandHandler
17+
private let mentionSymbol: String
1718

1819
internal var selectedUser: ChatUser?
1920

@@ -25,13 +26,15 @@ public class TwoStepMentionCommand: CommandHandler {
2526
channelController: ChatChannelController,
2627
commandSymbol: String,
2728
id: String,
28-
displayInfo: CommandDisplayInfo? = nil
29+
displayInfo: CommandDisplayInfo? = nil,
30+
mentionSymbol: String = "@"
2931
) {
3032
self.channelController = channelController
3133
self.id = id
34+
self.mentionSymbol = mentionSymbol
3235
mentionsCommandHandler = MentionsCommandHandler(
3336
channelController: channelController,
34-
commandSymbol: "@",
37+
commandSymbol: mentionSymbol,
3538
mentionAllAppUsers: false
3639
)
3740
self.displayInfo = displayInfo
@@ -87,9 +90,9 @@ public class TwoStepMentionCommand: CommandHandler {
8790

8891
private func mentionText(for user: ChatUser) -> String {
8992
if let name = user.name, !name.isEmpty {
90-
return "@\(name)"
93+
return "\(mentionSymbol)\(name)"
9194
} else {
92-
return "@\(user.id)"
95+
return "\(mentionSymbol)\(user.id)"
9396
}
9497
}
9598

@@ -114,7 +117,7 @@ public class TwoStepMentionCommand: CommandHandler {
114117
}
115118
let oldText = command.typingSuggestion.text
116119
let text = oldText.replacingOccurrences(
117-
of: "@", with: ""
120+
of: mentionSymbol, with: ""
118121
).trimmingCharacters(in: .whitespaces)
119122
let oldRange = command.typingSuggestion.locationRange
120123
let offset = oldText.count - text.count

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public class UnmuteCommandHandler: TwoStepMentionCommand {
2323
id: id
2424
)
2525
let displayInfo = CommandDisplayInfo(
26-
displayName: "Unmute",
26+
displayName: L10n.Composer.Commands.unmute,
2727
icon: images.commandUnmute,
28-
format: "\(id) [@username]",
28+
format: "\(id) [\(L10n.Composer.Commands.Format.username)]",
2929
isInstant: true
3030
)
3131
self.displayInfo = displayInfo

Sources/StreamChatSwiftUI/Generated/L10n.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ internal enum L10n {
9393
/// Also send as direct message
9494
internal static var directMessageReply: String { L10n.tr("Localizable", "composer.checkmark.direct-message-reply") }
9595
}
96+
internal enum Commands {
97+
/// Giphy
98+
internal static var giphy: String { L10n.tr("Localizable", "composer.commands.giphy") }
99+
/// Mute
100+
internal static var mute: String { L10n.tr("Localizable", "composer.commands.mute") }
101+
/// Unmute
102+
internal static var unmute: String { L10n.tr("Localizable", "composer.commands.unmute") }
103+
internal enum Format {
104+
/// text
105+
internal static var text: String { L10n.tr("Localizable", "composer.commands.format.text") }
106+
/// @username
107+
internal static var username: String { L10n.tr("Localizable", "composer.commands.format.username") }
108+
}
109+
}
96110
internal enum Files {
97111
/// Add more files
98112
internal static var addMore: String { L10n.tr("Localizable", "composer.files.add-more") }

Sources/StreamChatSwiftUI/Resources/en.lproj/Localizable.strings

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676
"composer.quoted.video" = "Video";
7777
"composer.quoted.giphy" = "Giphy";
7878

79+
"composer.commands.giphy" = "Giphy";
80+
"composer.commands.mute" = "Mute";
81+
"composer.commands.unmute" = "Unmute";
82+
"composer.commands.format.text" = "text";
83+
"composer.commands.format.username" = "@username";
84+
85+
7986
"composer.suggestions.commands.header" = "Instant Commands";
8087
"message.sending.attachment-uploading-failed" = "UPLOADING FAILED";
8188

0 commit comments

Comments
 (0)