Skip to content

Commit

Permalink
Remove switch and ComposeContentToolbarView.ViewModel.Action cases th…
Browse files Browse the repository at this point in the history
…at do not correspond to buttons
  • Loading branch information
j-f1 committed Mar 13, 2023
1 parent c3ed24d commit 0098743
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,6 @@ extension ComposeContentViewController: ComposeContentToolbarViewDelegate {
toolbarItemDidPressed action: ComposeContentToolbarView.ViewModel.Action
) {
switch action {
case .attachment:
assertionFailure()
case .poll:
self.viewModel.isPollActive.toggle()
case .emoji:
Expand All @@ -549,8 +547,6 @@ extension ComposeContentViewController: ComposeContentToolbarViewDelegate {
self.viewModel.setContentTextViewFirstResponderIfNeeds()
}
}
case .visibility, .language:
assertionFailure()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ extension ComposeContentToolbarView {

extension ComposeContentToolbarView.ViewModel {
enum Action: CaseIterable {
case attachment
case poll
case emoji
case contentWarning
case visibility
case language
}

enum AttachmentAction: CaseIterable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,142 +32,151 @@ struct ComposeContentToolbarView: View {

var body: some View {
HStack(spacing: .zero) {
ForEach(ComposeContentToolbarView.ViewModel.Action.allCases, id: \.self) { action in
let basicHandler = {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(String(describing: action))")
viewModel.delegate?.composeContentToolbarView(viewModel, toolbarItemDidPressed: action)
}
let makeBasicHandler: (ViewModel.Action) -> () -> Void = { action in {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(String(describing: action))")
viewModel.delegate?.composeContentToolbarView(viewModel, toolbarItemDidPressed: action)
} }

switch action {
case .attachment:
Menu {
ForEach(ComposeContentToolbarView.ViewModel.AttachmentAction.allCases, id: \.self) { attachmentAction in
Button {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public), \(attachmentAction.title)")
viewModel.delegate?.composeContentToolbarView(viewModel, attachmentMenuDidPressed: attachmentAction)
} label: {
Label {
Text(attachmentAction.title)
} icon: {
Image(uiImage: attachmentAction.image)
}
}
}
// MARK: Attachment
Menu {
ForEach(ComposeContentToolbarView.ViewModel.AttachmentAction.allCases, id: \.self) { attachmentAction in
Button {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public), \(attachmentAction.title)")
viewModel.delegate?.composeContentToolbarView(viewModel, attachmentMenuDidPressed: attachmentAction)
} label: {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Accessibility.appendAttachment,
image: Asset.Scene.Compose.media
)
}
.disabled(!viewModel.isAttachmentButtonEnabled)
case .visibility:
Menu {
Picker(selection: $viewModel.visibility) {
ForEach(viewModel.allVisibilities, id: \.self) { visibility in
Label {
Text(visibility.title)
} icon: {
visibility.image.swiftUIImage
}
}
} label: {
Text(viewModel.visibility.title)
Label {
Text(attachmentAction.title)
} icon: {
Image(uiImage: attachmentAction.image)
}
} label: {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Keyboard.selectVisibilityEntry(viewModel.visibility.title),
image: viewModel.visibility.image
)
}
.disabled(!viewModel.isVisibilityButtonEnabled)
case .poll:
Button(action: basicHandler) {
ComposeContentToolbarAction(
label: viewModel.isPollActive
? L10n.Scene.Compose.Accessibility.removePoll
: L10n.Scene.Compose.Accessibility.appendPoll,
image: viewModel.isPollActive
? Asset.Scene.Compose.pollFill
: Asset.Scene.Compose.poll
)
}
.disabled(!viewModel.isPollButtonEnabled)
case .language:
Menu {
Section {} // workaround a bug where the “Suggested” section doesn’t appear
if !viewModel.suggestedLanguages.isEmpty {
Section(L10n.Scene.Compose.Language.suggested) {
ForEach(viewModel.suggestedLanguages.compactMap(Language.init(id:))) { lang in
Toggle(isOn: languageBinding(for: lang.id)) {
Text(lang.label)
}
}
}
}
let recent = viewModel.recentLanguages.filter { !viewModel.suggestedLanguages.contains($0) }
if !recent.isEmpty {
Section(L10n.Scene.Compose.Language.recent) {
ForEach(recent.compactMap(Language.init(id:))) { lang in
Toggle(isOn: languageBinding(for: lang.id)) {
Text(lang.label)
}
}
}
}
} label: {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Accessibility.appendAttachment,
image: Asset.Scene.Compose.media
)
}
.disabled(!viewModel.isAttachmentButtonEnabled)
.frame(width: Self.toolbarHeight, height: Self.toolbarHeight)

// MARK: Poll
Button(action: makeBasicHandler(.poll)) {
ComposeContentToolbarAction(
label: viewModel.isPollActive
? L10n.Scene.Compose.Accessibility.removePoll
: L10n.Scene.Compose.Accessibility.appendPoll,
image: viewModel.isPollActive
? Asset.Scene.Compose.pollFill
: Asset.Scene.Compose.poll
)
}
.disabled(!viewModel.isPollButtonEnabled)
.frame(width: Self.toolbarHeight, height: Self.toolbarHeight)

// MARK: Emoji
Button(action: makeBasicHandler(.emoji)) {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Accessibility.customEmojiPicker,
image: viewModel.isEmojiActive ? Asset.Scene.Compose.emojiFill : Asset.Scene.Compose.emoji
)
}
.frame(width: Self.toolbarHeight, height: Self.toolbarHeight)

// MARK: Content Warning
Button(action: makeBasicHandler(.contentWarning)) {
ComposeContentToolbarAction(
label: viewModel.isContentWarningActive
? L10n.Scene.Compose.Accessibility.disableContentWarning
: L10n.Scene.Compose.Accessibility.enableContentWarning,
image: viewModel.isContentWarningActive
? Asset.Scene.Compose.chatWarningFill
: Asset.Scene.Compose.chatWarning
)
}
.frame(width: Self.toolbarHeight, height: Self.toolbarHeight)

// MARK: Visibility
Menu {
Picker(selection: $viewModel.visibility) {
ForEach(viewModel.allVisibilities, id: \.self) { visibility in
Label {
Text(visibility.title)
} icon: {
visibility.image.swiftUIImage
}
if !(recent + viewModel.suggestedLanguages).contains(viewModel.language) {
Toggle(isOn: languageBinding(for: viewModel.language)) {
Text(Language(id: viewModel.language)?.label ?? AttributedString("\(viewModel.language)"))
}
} label: {
Text(viewModel.visibility.title)
}
} label: {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Keyboard.selectVisibilityEntry(viewModel.visibility.title),
image: viewModel.visibility.image
)
}
.disabled(!viewModel.isVisibilityButtonEnabled)
.frame(width: Self.toolbarHeight, height: Self.toolbarHeight)

// MARK: Language
Menu {
Section {} // workaround a bug where the “Suggested” section doesn’t appear
if !viewModel.suggestedLanguages.isEmpty {
Section(L10n.Scene.Compose.Language.suggested) {
ForEach(viewModel.suggestedLanguages.compactMap(Language.init(id:))) { lang in
Toggle(isOn: languageBinding(for: lang.id)) {
Text(lang.label)
}
}
Button(L10n.Scene.Compose.Language.other) {
showingLanguagePicker = true
}
} label: {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Language.title,
icon: LanguagePickerIcon(language: viewModel.language, showBadge: {
if let suggested = viewModel.highConfidenceSuggestedLanguage {
return !didChangeLanguage && suggested != viewModel.language
}
return false
}())
).accessibilityValue(Text(Language(id: viewModel.language)?.label ?? AttributedString("\(viewModel.language)")))
}
.popover(isPresented: $showingLanguagePicker) {
let picker = LanguagePicker { newLanguage in
viewModel.language = newLanguage
didChangeLanguage = true
showingLanguagePicker = false
}
if verticalSizeClass == .regular && horizontalSizeClass == .regular {
// explicitly size picker when it’s a popover
picker.frame(width: 400, height: 500)
} else {
picker
}
let recent = viewModel.recentLanguages.filter { !viewModel.suggestedLanguages.contains($0) }
if !recent.isEmpty {
Section(L10n.Scene.Compose.Language.recent) {
ForEach(recent.compactMap(Language.init(id:))) { lang in
Toggle(isOn: languageBinding(for: lang.id)) {
Text(lang.label)
}
}
}

case .emoji:
Button(action: basicHandler) {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Accessibility.customEmojiPicker,
image: viewModel.isEmojiActive ? Asset.Scene.Compose.emojiFill : Asset.Scene.Compose.emoji
)
}
case .contentWarning:
Button(action: basicHandler) {
ComposeContentToolbarAction(
label: viewModel.isContentWarningActive
? L10n.Scene.Compose.Accessibility.disableContentWarning
: L10n.Scene.Compose.Accessibility.enableContentWarning,
image: viewModel.isContentWarningActive
? Asset.Scene.Compose.chatWarningFill
: Asset.Scene.Compose.chatWarning
)
}
if !(recent + viewModel.suggestedLanguages).contains(viewModel.language) {
Toggle(isOn: languageBinding(for: viewModel.language)) {
Text(Language(id: viewModel.language)?.label ?? AttributedString("\(viewModel.language)"))
}
}
}.frame(width: 48, height: 48)
Button(L10n.Scene.Compose.Language.other) {
showingLanguagePicker = true
}
} label: {
ComposeContentToolbarAction(
label: L10n.Scene.Compose.Language.title,
icon: LanguagePickerIcon(language: viewModel.language, showBadge: {
if let suggested = viewModel.highConfidenceSuggestedLanguage {
return !didChangeLanguage && suggested != viewModel.language
}
return false
}())
).accessibilityValue(Text(Language(id: viewModel.language)?.label ?? AttributedString("\(viewModel.language)")))
}
.frame(width: Self.toolbarHeight, height: Self.toolbarHeight)
.popover(isPresented: $showingLanguagePicker) {
let picker = LanguagePicker { newLanguage in
viewModel.language = newLanguage
didChangeLanguage = true
showingLanguagePicker = false
}
if verticalSizeClass == .regular, horizontalSizeClass == .regular {
// explicitly size picker when it’s a popover
picker.frame(width: 400, height: 500)
} else {
picker
}
}

Spacer()

// MARK: Character count
let count: Int = {
if viewModel.isContentWarningActive {
return viewModel.contentWeightedLength + viewModel.contentWarningWeightedLength
Expand Down

0 comments on commit 0098743

Please sign in to comment.