Skip to content

Commit

Permalink
Public polls, multiple answers, quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Jan 23, 2020
1 parent 2455545 commit a7e4d58
Show file tree
Hide file tree
Showing 9 changed files with 6,411 additions and 3,379 deletions.
66 changes: 51 additions & 15 deletions Unigram/Unigram/Controls/Messages/Content/PollContent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,57 @@
d:DesignHeight="300"
d:DesignWidth="400">

<TextBlock x:Name="Question"
TextWrapping="Wrap"
Style="{StaticResource BaseTextBlockStyle}"
FontFamily="{ThemeResource EmojiThemeFontFamily}"
IsTextSelectionEnabled="True"/>
<TextBlock x:Name="Type"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Margin="0,0,0,4"/>
<TextBlock
x:Name="Question"
TextWrapping="Wrap"
Style="{StaticResource BaseTextBlockStyle}"
FontFamily="{ThemeResource EmojiThemeFontFamily}"
IsTextSelectionEnabled="True"/>
<StackPanel
Orientation="Horizontal">
<TextBlock
x:Name="Type"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
VerticalAlignment="Top"
Margin="0,0,4,4"/>

<StackPanel x:Name="Options"
Margin="-10,0"/>
<StackPanel
Orientation="Horizontal"
x:Name="RecentVoters"/>
</StackPanel>

<TextBlock x:Name="Votes"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Margin="0,8,0,0"/>
<StackPanel
x:Name="Options"
Margin="-10,0"/>

<TextBlock
x:Name="Votes"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Margin="0,12,0,0"/>

<Button
x:Name="Submit"
Style="{StaticResource EmptyButtonStyle}"
Click="Submit_Click"
Margin="0,0,0,-8">
<TextBlock
Text="{CustomResource PollSubmitVotes}"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Margin="0,12,0,8"/>
</Button>

<Button
x:Name="View"
Style="{StaticResource EmptyButtonStyle}"
Click="View_Click"
Margin="-10,0,-10,-8">
<TextBlock
Text="{CustomResource PollViewResults}"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Margin="10,12,10,8"/>
</Button>
</StackPanel>
101 changes: 96 additions & 5 deletions Unigram/Unigram/Controls/Messages/Content/PollContent.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,29 @@ public void UpdateMessage(MessageViewModel message)
return;
}

var results = poll.Poll.IsClosed || poll.Poll.Options.Any(x => x.IsChosen);

Question.Text = poll.Poll.Question;
Type.Text = poll.Poll.IsClosed ? Strings.Resources.FinalResults : Strings.Resources.AnonymousPoll; // No public polls for now
Votes.Text = poll.Poll.TotalVoterCount > 0 ? Locale.Declension("Vote", poll.Poll.TotalVoterCount) : Strings.Resources.NoVotes;
Votes.Text = poll.Poll.TotalVoterCount > 0
? Locale.Declension(poll.Poll.Type is PollTypeQuiz ? "Answer" : "Vote", poll.Poll.TotalVoterCount)
: poll.Poll.Type is PollTypeQuiz
? Strings.Resources.NoVotesQuiz
: Strings.Resources.NoVotes;

if (poll.Poll.Type is PollTypeRegular reg)
{
Type.Text = poll.Poll.IsClosed ? Strings.Resources.FinalResults : poll.Poll.IsAnonymous ? Strings.Resources.AnonymousPoll : Strings.Resources.PublicPoll;
View.Visibility = results && !poll.Poll.IsAnonymous ? Visibility.Visible : Visibility.Collapsed;
Submit.Visibility = !results && reg.AllowMultipleAnswers ? Visibility.Visible : Visibility.Collapsed;
}
else if (poll.Poll.Type is PollTypeQuiz)
{
Type.Text = poll.Poll.IsClosed ? Strings.Resources.FinalResults : poll.Poll.IsAnonymous ? Strings.Resources.AnonymousQuizPoll : Strings.Resources.QuizPoll;
View.Visibility = results && !poll.Poll.IsAnonymous ? Visibility.Visible : Visibility.Collapsed;
Submit.Visibility = Visibility.Collapsed;
}

Votes.Visibility = View.Visibility == Visibility.Collapsed && Submit.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;

//Options.Children.Clear();

Expand All @@ -60,10 +80,20 @@ public void UpdateMessage(MessageViewModel message)
if (i < Options.Children.Count)
{
var button = Options.Children[i] as PollOptionControl;
button.Click -= Option_Click;

if (i < poll.Poll.Options.Count)
{
button.UpdatePollOption(poll.Poll, poll.Poll.Options[i]);

if (poll.Poll.Type is PollTypeRegular regular && regular.AllowMultipleAnswers)
{

}
else
{
button.Click += Option_Click;
}
}
else
{
Expand All @@ -73,12 +103,47 @@ public void UpdateMessage(MessageViewModel message)
else
{
var button = new PollOptionControl();
button.Click += Option_Click;
button.UpdatePollOption(poll.Poll, poll.Poll.Options[i]);

if (poll.Poll.Type is PollTypeRegular regular && regular.AllowMultipleAnswers)
{

}
else
{
button.Click += Option_Click;
}

Options.Children.Add(button);
}
}

RecentVoters.Children.Clear();

foreach (var id in poll.Poll.RecentVoterUserIds)
{
var user = message.ProtoService.GetUser(id);
if (user == null)
{
continue;
}

var picture = new ProfilePicture();
picture.Source = PlaceholderHelper.GetUser(message.ProtoService, user, 16);
picture.Width = 16;
picture.Height = 16;

if (RecentVoters.Children.Count > 0)
{
picture.Margin = new Thickness(-6, -1, 0, 0);
}
else
{
picture.Margin = new Thickness(0, -1, 0, 0);
}

RecentVoters.Children.Add(picture);
}
}

public bool IsValid(MessageContent content, bool primary)
Expand All @@ -95,7 +160,7 @@ private async void Option_Click(object sender, RoutedEventArgs e)
}

var button = sender as PollOptionControl;
if (button.IsChecked == true)
if (button.IsChecked == null)
{
return;
}
Expand All @@ -112,7 +177,33 @@ private async void Option_Click(object sender, RoutedEventArgs e)
return;
}

_message.Delegate.VotePoll(_message, option);
_message.Delegate.VotePoll(_message, new[] { option });
}

private void Submit_Click(object sender, RoutedEventArgs e)
{
var options = new List<PollOption>();

foreach (PollOptionControl button in Options.Children)
{
if (button.IsChecked == true && button.Tag is PollOption option)
{
options.Add(option);
}
}

var poll = _message?.Content as MessagePoll;
if (poll == null)
{
return;
}

_message.Delegate.VotePoll(_message, options);
}

private void View_Click(object sender, RoutedEventArgs e)
{
_message.Delegate.OpenMedia(_message, null);
}
}
}
146 changes: 90 additions & 56 deletions Unigram/Unigram/Controls/PollOptionControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,101 @@
d:DesignHeight="300"
d:DesignWidth="400">

<Grid Padding="10,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="28"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
<Ellipse x:Name="Ellipse"
Width="20"
Height="20"
UseLayoutRounding="False"
Stroke="{ThemeResource MessageSubtleForegroundBrush}"
StrokeThickness="{ThemeResource RadioButtonBorderThemeThickness}"
VerticalAlignment="Top"
Margin="0,8,8,8"/>
<UserControl x:Name="LayoutRoot">
<Grid Padding="10,0">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ResultState">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Wrong">
<VisualState.Setters>
<Setter Target="TickGlyph.Glyph" Value="&#xF13D;"/>
<Setter Target="TickFill.Foreground" Value="Red"/>
<Setter Target="Zero.Fill" Value="Red"/>
<Setter Target="Votes.Foreground" Value="Red"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="28"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
<Ellipse x:Name="Ellipse"
Width="20"
Height="20"
UseLayoutRounding="False"
Stroke="{ThemeResource MessageSubtleForegroundBrush}"
StrokeThickness="{ThemeResource RadioButtonBorderThemeThickness}"
VerticalAlignment="Top"
Margin="0,8,8,8"/>
<FontIcon
Visibility="{x:Bind ConvertCheckMark(IsChecked), Mode=OneWay}"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
Glyph="&#xEC61;"
FontSize="20"
VerticalAlignment="Top"
Margin="0,8,8,8"/>


<ProgressRing x:Name="Loading"
MinWidth="20"
MinHeight="20"
Width="20"
Height="20"
UseLayoutRounding="False"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
VerticalAlignment="Top"
Margin="0,8,8,8"/>
<ProgressRing x:Name="Loading"
MinWidth="20"
MinHeight="20"
Width="20"
Height="20"
UseLayoutRounding="False"
Foreground="{ThemeResource MessageSubtleForegroundBrush}"
VerticalAlignment="Top"
Margin="0,8,8,8"/>

<TextBlock x:Name="Percentage"
Style="{StaticResource CaptionTextBlockStyle}"
FontWeight="SemiBold"
TextWrapping="NoWrap"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Margin="-10,10,8,8"/>
<TextBlock x:Name="Percentage"
Style="{StaticResource CaptionTextBlockStyle}"
FontWeight="SemiBold"
TextWrapping="NoWrap"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Margin="-10,10,6,8"/>

<TextBlock x:Name="Text"
Style="{StaticResource BodyTextBlockStyle}"
FontFamily="{ThemeResource EmojiThemeFontFamily}"
VerticalAlignment="Top"
Margin="0,8,0,8"
Grid.Column="1"/>
<TextBlock x:Name="Text"
Style="{StaticResource BodyTextBlockStyle}"
FontFamily="{ThemeResource EmojiThemeFontFamily}"
VerticalAlignment="Top"
Margin="0,8,0,8"
Grid.Column="1"/>

<Ellipse x:Name="Zero"
Fill="{ThemeResource MessageHeaderBorderBrush}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="4"
Height="4"
Margin="0,-4,0,0"
Grid.Column="1"
Grid.Row="1"/>
<Grid x:Name="Tick"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Width="12"
Height="12"
Margin="0,-8,6,-4"
Grid.Column="0"
Grid.Row="1">
<FontIcon x:Name="TickFill" Glyph="&#xF136;" FontSize="12" Foreground="{ThemeResource MessageHeaderBorderBrush}"/>
<FontIcon x:Name="TickGlyph" Glyph="&#xF13E;" FontSize="12" Foreground="White"/>
</Grid>

<ProgressBar x:Name="Votes"
Foreground="{ThemeResource MessageHeaderBorderBrush}"
Style="{StaticResource PollOptionProgressBarStyle}"
Background="Transparent"
Margin="0,-4,0,0"
<Ellipse x:Name="Zero"
Fill="{ThemeResource MessageHeaderBorderBrush}"
VerticalAlignment="Top"
Grid.Row="1"
Grid.Column="1"/>
</Grid>
HorizontalAlignment="Left"
Width="4"
Height="4"
Margin="0,-4,0,0"
Grid.Column="1"
Grid.Row="1"/>

<ProgressBar x:Name="Votes"
Foreground="{ThemeResource MessageHeaderBorderBrush}"
Style="{StaticResource PollOptionProgressBarStyle}"
Background="Transparent"
Margin="0,-4,0,0"
VerticalAlignment="Top"
Grid.Row="1"
Grid.Column="1"/>
</Grid>
</UserControl>
</ToggleButton>
Loading

0 comments on commit a7e4d58

Please sign in to comment.