@@ -14,14 +14,28 @@ struct PollView: View {
1414 @Environment ( \. tintColor) private var tintColor
1515
1616 let poll : Topic . Poll
17- let onVoteButtonTapped : ( [ String : [ Int ] ] ) -> Void
17+ let onVoteButtonTapped : ( [ Int : Set < Int > ] ) -> Void
1818
19+ @State private var isSending = false
1920 @State private var showVoteResultsButtonTapped = false
20- @State private var selections : [ String : Set < Int > ] = [ : ]
21+ @State private var selections : [ Int : Set < Int > ] = [ : ]
22+
23+ private var isVotable : Bool {
24+ for option in poll. options {
25+ if selections [ option. id] == nil {
26+ return false
27+ }
28+ if selections [ option. id] != nil ,
29+ selections [ option. id] !. isEmpty {
30+ return false
31+ }
32+ }
33+ return true
34+ }
2135
2236 init (
2337 poll: Topic . Poll ,
24- onVoteButtonTapped: @escaping ( [ String : [ Int ] ] ) -> Void
38+ onVoteButtonTapped: @escaping ( [ Int : Set < Int > ] ) -> Void
2539 ) {
2640 self . poll = poll
2741 self . onVoteButtonTapped = onVoteButtonTapped
@@ -44,7 +58,7 @@ struct PollView: View {
4458 . foregroundStyle ( Color ( . Labels. primary) )
4559 . frame ( maxWidth: . infinity, alignment: . leading)
4660
47- if showVoteResultsButtonTapped {
61+ if showVoteResultsButtonTapped || poll . voted {
4862 OptionChoices ( choices: option. choices)
4963 } else {
5064 OptionChoicesSelect ( option: option)
@@ -58,7 +72,9 @@ struct PollView: View {
5872 . foregroundStyle ( Color ( . Labels. teritary) )
5973 . frame ( maxWidth: . infinity, alignment: . leading)
6074
61- PollActionButtons ( )
75+ if !poll. voted {
76+ PollActionButtons ( )
77+ }
6278 }
6379 . padding ( 16 )
6480 }
@@ -72,16 +88,18 @@ struct PollView: View {
7288 if showVoteResultsButtonTapped {
7389 showVoteResultsButtonTapped = false
7490 } else {
75- // TODO: Implement selection data sending...
91+ isSending = true
92+ onVoteButtonTapped ( selections)
7693 }
7794 } label: {
7895 Text ( " Vote " , bundle: . module)
7996 . padding ( . horizontal, 18 )
8097 . padding ( . vertical, 9 )
8198 }
82- . foregroundStyle ( tintColor )
83- . background ( tintColor . opacity ( 0.12 ) )
99+ . foregroundStyle ( voteButtonForegroundColor ( ) )
100+ . background ( voteButtonBackgroundColor ( ) )
84101 . clipShape ( RoundedRectangle ( cornerRadius: 10 ) )
102+ . disabled ( !showVoteResultsButtonTapped && !isVotable)
85103
86104 Spacer ( )
87105
@@ -93,9 +111,10 @@ struct PollView: View {
93111 . padding ( . horizontal, 18 )
94112 . padding ( . vertical, 9 )
95113 }
96- . foregroundStyle ( tintColor)
97- . background ( tintColor. opacity ( 0.12 ) )
114+ . foregroundStyle ( isSending ? Color ( . Labels . quintuple ) : tintColor)
115+ . background ( isSending ? Color ( . Main . greyAlpha ) : tintColor. opacity ( 0.12 ) )
98116 . clipShape ( RoundedRectangle ( cornerRadius: 10 ) )
117+ . disabled ( isSending)
99118 }
100119 }
101120 }
@@ -109,21 +128,21 @@ struct PollView: View {
109128 HStack ( alignment: . top, spacing: 11 ) {
110129 if option. several {
111130 Toggle ( isOn: Binding (
112- get: { isSelected ( option. name , choice. id) } ,
131+ get: { isSelected ( option. id , choice. id) } ,
113132 set: { isSelected in
114133 withAnimation {
115- updateMultiSelections ( option. name , choice. id, isSelected)
134+ updateMultiSelections ( option. id , choice. id, isSelected)
116135 }
117136 }
118137 ) ) { }
119138 . toggleStyle ( CheckBoxToggleStyle ( ) )
120139 } else {
121140 Button {
122141 withAnimation {
123- selections [ option. name ] = Set ( [ choice. id] )
142+ selections [ option. id ] = Set ( [ choice. id] )
124143 }
125144 } label: {
126- if isSelected ( option. name , choice. id) {
145+ if isSelected ( option. id , choice. id) {
127146 ZStack {
128147 Circle ( )
129148 . strokeBorder ( Color ( . Labels. quintuple) )
@@ -195,21 +214,29 @@ struct PollView: View {
195214 return CGFloat ( choice. votes) / CGFloat( totalVotes)
196215 }
197216
198- private func isSelected( _ option: String , _ choiceId: Int ) -> Bool {
199- return if selections [ option] != nil {
200- selections [ option] !. contains ( choiceId)
217+ private func voteButtonForegroundColor( ) -> Color {
218+ return ( !isVotable && !showVoteResultsButtonTapped || isSending) ? Color ( . Labels. quintuple) : tintColor
219+ }
220+
221+ private func voteButtonBackgroundColor( ) -> Color {
222+ return ( !isVotable && !showVoteResultsButtonTapped || isSending) ? Color ( . Main. greyAlpha) : tintColor. opacity ( 0.12 )
223+ }
224+
225+ private func isSelected( _ optionId: Int , _ choiceId: Int ) -> Bool {
226+ return if selections [ optionId] != nil {
227+ selections [ optionId] !. contains ( choiceId)
201228 } else { false }
202229 }
203230
204- private func updateMultiSelections( _ option : String , _ choiceId: Int , _ isSelected: Bool ) {
205- if selections [ option ] != nil {
231+ private func updateMultiSelections( _ optionId : Int , _ choiceId: Int , _ isSelected: Bool ) {
232+ if selections [ optionId ] != nil {
206233 if isSelected {
207- selections [ option ] !. insert ( choiceId)
234+ selections [ optionId ] !. insert ( choiceId)
208235 } else {
209- selections [ option ] !. remove ( choiceId)
236+ selections [ optionId ] !. remove ( choiceId)
210237 }
211238 } else {
212- selections [ option ] = Set ( [ choiceId] )
239+ selections [ optionId ] = Set ( [ choiceId] )
213240 }
214241 }
215242}
0 commit comments