Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into polls
  • Loading branch information
martinmitrevski committed May 29, 2024
2 parents 30305b9 + ff9a9e6 commit c41f56b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct PollAttachmentView<Factory: ViewFactory>: View {

public var body: some View {
VStack(spacing: 16) {
VStack(alignment: .leading, spacing: 8) {
VStack(alignment: .leading, spacing: 2) {
HStack {
Text(poll.name)
.font(fonts.bodyBold)
Expand Down Expand Up @@ -64,7 +64,7 @@ public struct PollAttachmentView<Factory: ViewFactory>: View {
Button {
viewModel.allOptionsShown = true
} label: {
Text(L10n.Message.Polls.seeMoreOptions(options.count - 10))
Text(L10n.Message.Polls.Button.seeMoreOptions(options.count - 10))
}
.fullScreenCover(isPresented: $viewModel.allOptionsShown) {
PollAllOptionsView(viewModel: viewModel)
Expand All @@ -75,43 +75,38 @@ public struct PollAttachmentView<Factory: ViewFactory>: View {
Button {
viewModel.suggestOptionShown = true
} label: {
Text(L10n.Message.Polls.suggestAnOption)
Text(L10n.Message.Polls.Button.suggestAnOption)
}
.modifier(
SuggestOptionModifier(
title: L10n.Message.Polls.suggestAnOption,
showingAlert: $viewModel.suggestOptionShown,
text: $viewModel.suggestOptionText,
submit: {
viewModel.suggest(option: viewModel.suggestOptionText)
}
)
.uiAlert(
title: L10n.Alert.Title.suggestAnOption,
isPresented: $viewModel.suggestOptionShown,
text: $viewModel.suggestOptionText,
placeholder: L10n.Alert.TextField.pollsNewOption,
accept: L10n.Alert.Actions.send,
action: { viewModel.suggest(option: viewModel.suggestOptionText) }
)
}

if viewModel.showAddCommentButton {
Button {
viewModel.addCommentShown = true
} label: {
Text(L10n.Message.Polls.addComment)
Text(L10n.Message.Polls.Button.addComment)
}
.modifier(
SuggestOptionModifier(
title: L10n.Message.Polls.addComment,
showingAlert: $viewModel.addCommentShown,
text: $viewModel.commentText,
submit: {
viewModel.add(comment: viewModel.commentText)
}
)
.uiAlert(
title: L10n.Alert.Title.addComment,
isPresented: $viewModel.addCommentShown,
text: $viewModel.commentText,
accept: L10n.Alert.Actions.send,
action: { viewModel.add(comment: viewModel.commentText) }
)
}

if viewModel.poll.answersCount > 0 {
Button {
viewModel.allCommentsShown = true
} label: {
Text(L10n.Message.Polls.viewComments(viewModel.poll.answersCount))
Text(L10n.Message.Polls.Button.viewNumberOfComments(viewModel.poll.answersCount))
}
.fullScreenCover(isPresented: $viewModel.allCommentsShown) {
PollCommentsView(poll: viewModel.poll, pollController: viewModel.pollController)
Expand All @@ -121,7 +116,7 @@ public struct PollAttachmentView<Factory: ViewFactory>: View {
Button {
viewModel.pollResultsShown = true
} label: {
Text(L10n.Message.Polls.viewResults)
Text(L10n.Message.Polls.Button.viewResults)
}
.fullScreenCover(isPresented: $viewModel.pollResultsShown) {
PollResultsView(viewModel: viewModel)
Expand All @@ -131,7 +126,7 @@ public struct PollAttachmentView<Factory: ViewFactory>: View {
Button {
viewModel.endVote()
} label: {
Text(L10n.Message.Polls.endVote)
Text(L10n.Message.Polls.Button.endVote)
}
}
}
Expand Down Expand Up @@ -169,27 +164,6 @@ public struct PollAttachmentView<Factory: ViewFactory>: View {

extension PollOption: Identifiable {}

struct SuggestOptionModifier: ViewModifier {

var title: String
@Binding var showingAlert: Bool
@Binding var text: String
var submit: () -> Void

func body(content: Content) -> some View {
content
.uiAlert(
title: title,
isPresented: $showingAlert,
text: $text,
placeholder: L10n.Alert.TextField.pollsNewOption,
cancel: L10n.Alert.Actions.cancel,
accept: L10n.Alert.Actions.add,
action: submit
)
}
}

struct PollOptionView: View {

@ObservedObject var viewModel: PollAttachmentViewModel
Expand All @@ -201,7 +175,7 @@ struct PollOptionView: View {

var body: some View {
VStack(spacing: 4) {
HStack {
HStack(alignment: .top) {
if !viewModel.poll.isClosed {
Button {
if viewModel.optionVotedByCurrentUser(option) {
Expand Down Expand Up @@ -236,9 +210,9 @@ struct PollOptionView: View {
}

PollVotesIndicatorView(
mostVotes: viewModel.hasMostVotes(for: option),
alternativeStyle: viewModel.poll.isClosed && viewModel.hasMostVotes(for: option),
optionVotes: optionVotes ?? 0,
maxVotes: maxVotes
maxVotes: maxVotes ?? 0
)
.padding(.leading, 24)
}
Expand All @@ -249,9 +223,9 @@ struct PollVotesIndicatorView: View {

@Injected(\.colors) var colors

let mostVotes: Bool
let alternativeStyle: Bool
var optionVotes: Int
var maxVotes: Int?
var maxVotes: Int

private let height: CGFloat = 4

Expand All @@ -263,15 +237,14 @@ struct PollVotesIndicatorView: View {
.frame(width: reader.size.width, height: height)

RoundedRectangle(cornerRadius: 8)
.fill(mostVotes ? Color(colors.alternativeActiveTint) : colors.tintColor)
.fill(alternativeStyle ? Color(colors.alternativeActiveTint) : colors.tintColor)
.frame(width: reader.size.width * ratio, height: height)
}
}
.frame(height: height)
}

var ratio: CGFloat {
let maxVotes = max(maxVotes ?? 1, 1)
return CGFloat(optionVotes) / CGFloat(maxVotes)
CGFloat(optionVotes) / CGFloat(max(maxVotes, 1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class PollAttachmentViewModel: ObservableObject, PollControllerDelegate {
init(message: ChatMessage, poll: Poll, pollController: PollController) {
self.message = message
self.poll = poll
self.createdByCurrentUser = poll.createdBy?.id == InjectedValues[\.chatClient].currentUserId
createdByCurrentUser = poll.createdBy?.id == InjectedValues[\.chatClient].currentUserId
self.pollController = pollController
pollController.delegate = self
pollController.synchronize { [weak self] _ in
Expand Down Expand Up @@ -118,7 +118,7 @@ public class PollAttachmentViewModel: ObservableObject, PollControllerDelegate {
}

public func optionVotedByCurrentUser(_ option: PollOption) -> Bool {
return currentUserVote(for: option) != nil
currentUserVote(for: option) != nil
}

public func suggest(option: String) {
Expand All @@ -130,6 +130,8 @@ public class PollAttachmentViewModel: ObservableObject, PollControllerDelegate {
}

/// Returns true if the specified option has more votes than any other option.
///
/// - Note: When multiple options have the highest vote count, this function returns false.
public func hasMostVotes(for option: PollOption) -> Bool {
guard let allCounts = poll.voteCountsByOption else { return false }
guard let optionVoteCount = allCounts[option.id], optionVoteCount > 0 else { return false }
Expand All @@ -140,7 +142,7 @@ public class PollAttachmentViewModel: ObservableObject, PollControllerDelegate {
return optionsByVoteCounts[optionVoteCount]?.count == 1
}

//MARK: - PollControllerDelegate
// MARK: - PollControllerDelegate

public func pollController(_ pollController: PollController, didUpdatePoll poll: EntityChange<Poll>) {
self.poll = poll.item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ struct PollCommentsView: View {
Button(action: {
viewModel.addCommentShown = true
}, label: {
Text(L10n.Message.Polls.addComment)
Text(L10n.Message.Polls.Button.addComment)
.bold()
.foregroundColor(colors.tintColor)
})
.frame(maxWidth: .infinity)
.withPollsBackground()
.modifier(
SuggestOptionModifier(
title: L10n.Message.Polls.addComment,
showingAlert: $viewModel.addCommentShown,
text: $viewModel.newCommentText,
submit: {
viewModel.add(comment: viewModel.newCommentText)
}
)
.uiAlert(
title: L10n.Alert.Title.addComment,
isPresented: $viewModel.addCommentShown,
text: $viewModel.newCommentText,
accept: L10n.Alert.Actions.send,
action: { viewModel.add(comment: viewModel.newCommentText) }
)
}
.padding()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct PollOptionResultsView: View {
NavigationLink {
PollOptionAllVotesView(poll: poll, option: option)
} label: {
Text(L10n.Message.Polls.showAll)
Text(L10n.Message.Polls.Button.showAll)
}
}
}
Expand Down
48 changes: 28 additions & 20 deletions Sources/StreamChatSwiftUI/Generated/L10n.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ internal enum L10n {

internal enum Alert {
internal enum Actions {
/// Add
internal static var add: String { L10n.tr("Localizable", "alert.actions.add") }
/// Cancel
internal static var cancel: String { L10n.tr("Localizable", "alert.actions.cancel") }
/// Delete
Expand All @@ -37,6 +35,8 @@ internal enum L10n {
internal static var muteChannelTitle: String { L10n.tr("Localizable", "alert.actions.mute-channel-title") }
/// Ok
internal static var ok: String { L10n.tr("Localizable", "alert.actions.ok") }
/// Send
internal static var send: String { L10n.tr("Localizable", "alert.actions.send") }
/// Are you sure you want to unmute this
internal static var unmuteChannelTitle: String { L10n.tr("Localizable", "alert.actions.unmute-channel-title") }
/// View info
Expand All @@ -52,6 +52,12 @@ internal enum L10n {
/// Enter a new option
internal static var pollsNewOption: String { L10n.tr("Localizable", "alert.text-field.polls-new-option") }
}
internal enum Title {
/// Add a comment
internal static var addComment: String { L10n.tr("Localizable", "alert.title.add-comment") }
/// Suggest an option
internal static var suggestAnOption: String { L10n.tr("Localizable", "alert.title.suggest-an-option") }
}
}

internal enum Attachment {
Expand Down Expand Up @@ -387,30 +393,32 @@ internal enum L10n {
internal static var title: String { L10n.tr("Localizable", "message.giphy-attachment.title") }
}
internal enum Polls {
/// Add a comment
internal static var addComment: String { L10n.tr("Localizable", "message.polls.addComment") }
/// End Vote
internal static var endVote: String { L10n.tr("Localizable", "message.polls.endVote") }
/// See %d more options
internal static func seeMoreOptions(_ p1: Int) -> String {
return L10n.tr("Localizable", "message.polls.seeMoreOptions", p1)
}
/// Show All
internal static var showAll: String { L10n.tr("Localizable", "message.polls.show-all") }
/// Suggest an option
internal static var suggestAnOption: String { L10n.tr("Localizable", "message.polls.suggestAnOption") }
/// Anonymous
internal static var unknownVoteAuthor: String { L10n.tr("Localizable", "message.polls.unknown-vote-author") }
/// View %d comments
internal static func viewComments(_ p1: Int) -> String {
return L10n.tr("Localizable", "message.polls.viewComments", p1)
}
/// View Results
internal static var viewResults: String { L10n.tr("Localizable", "message.polls.viewResults") }
/// %d votes
internal static func votes(_ p1: Int) -> String {
return L10n.tr("Localizable", "message.polls.votes", p1)
}
internal enum Button {
/// Add a Comment
internal static var addComment: String { L10n.tr("Localizable", "message.polls.button.addComment") }
/// End Vote
internal static var endVote: String { L10n.tr("Localizable", "message.polls.button.endVote") }
/// See %d More Options
internal static func seeMoreOptions(_ p1: Int) -> String {
return L10n.tr("Localizable", "message.polls.button.seeMoreOptions", p1)
}
/// Show All
internal static var showAll: String { L10n.tr("Localizable", "message.polls.button.show-all") }
/// Suggest an Option
internal static var suggestAnOption: String { L10n.tr("Localizable", "message.polls.button.suggestAnOption") }
/// Plural format key: "%#@comments@"
internal static func viewNumberOfComments(_ p1: Int) -> String {
return L10n.tr("Localizable", "message.polls.button.view-number-of-comments", p1)
}
/// View Results
internal static var viewResults: String { L10n.tr("Localizable", "message.polls.button.viewResults") }
}
internal enum Subtitle {
/// Select one
internal static var selectOne: String { L10n.tr("Localizable", "message.polls.subtitle.selectOne") }
Expand Down
17 changes: 9 additions & 8 deletions Sources/StreamChatSwiftUI/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,30 @@
"message.cell.edited" = "Edited";
"message.reactions.currentUser" = "You";

"message.polls.addComment" = "Add a comment";
"message.polls.endVote" = "End Vote";
"message.polls.seeMoreOptions" = "See %d more options";
"message.polls.show-all" = "Show All";
"message.polls.button.addComment" = "Add a Comment";
"message.polls.button.endVote" = "End Vote";
"message.polls.button.seeMoreOptions" = "See %d More Options";
"message.polls.button.show-all" = "Show All";
"message.polls.button.suggestAnOption" = "Suggest an Option";
"message.polls.button.viewResults" = "View Results";
"message.polls.subtitle.selectOne" = "Select one";
"message.polls.subtitle.selectOneOrMore" = "Select one or more";
"message.polls.subtitle.selectUpTo" = "Select up to %d";
"message.polls.subtitle.voteEnded" = "Vote ended";
"message.polls.suggestAnOption" = "Suggest an option";
"message.polls.toolbar.comments-title" = "Poll Comments";
"message.polls.toolbar.options-title" = "Poll Options";
"message.polls.toolbar.results-title" = "Poll Results";
"message.polls.unknown-vote-author" = "Anonymous";
"message.polls.viewComments" = "View %d comments";
"message.polls.viewResults" = "View Results";
"message.polls.votes" = "%d votes";

"alert.actions.add" = "Add";
"alert.actions.cancel" = "Cancel";
"alert.actions.delete" = "Delete";
"alert.actions.ok" = "Ok";
"alert.actions.discard-changes" = "Discard Changes";
"alert.actions.delete-channel-title" = "Delete conversation";
"alert.actions.delete-channel-message" = "Are you sure you want to delete this conversation?";
"alert.actions.keep-editing" = "Keep Editing";
"alert.actions.send" = "Send";
"alert.error.title" = "Something went wrong.";
"alert.error.message" = "The operation couldn't be completed.";
"alert.actions.mute-channel-title" = "Are you sure you want to mute this";
Expand All @@ -88,6 +87,8 @@
"alert.actions.leave-group-button" = "Leave";
"alert.actions.view-info-title" = "View info";
"alert.text-field.polls-new-option" = "Enter a new option";
"alert.title.add-comment" = "Add a comment";
"alert.title.suggest-an-option" = "Suggest an option";

"message.only-visible-to-you" = "Only visible to you";
"message.deleted-message-placeholder" = "Message deleted";
Expand Down
Loading

0 comments on commit c41f56b

Please sign in to comment.