Skip to content

Commit ff85747

Browse files
committed
Replace VotingVisibility with enumerated struct
1 parent 880dc68 commit ff85747

5 files changed

Lines changed: 67 additions & 9 deletions

File tree

Assets/Plugins/StreamChat/Core/LowLevelClient/Models/Poll.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public partial class Poll : ModelBase, ILoadableFrom<PollInternalDTO, Poll>, ILo
5151

5252
public Dictionary<string, int> VoteCountsByOption { get; set; }
5353

54-
public string VotingVisibility { get; set; }
54+
public VotingVisibility VotingVisibility { get; set; }
5555

5656
Poll ILoadableFrom<PollInternalDTO, Poll>.LoadFromDto(PollInternalDTO dto)
5757
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using StreamChat.Core.InternalDTO.Requests;
2+
3+
namespace StreamChat.Core.LowLevelClient.Models
4+
{
5+
/// <summary>
6+
/// Voting visibility for polls
7+
/// </summary>
8+
public readonly struct VotingVisibility : System.IEquatable<VotingVisibility>
9+
{
10+
/// <summary>
11+
/// Anonymous voting - votes are not associated with users
12+
/// </summary>
13+
public static readonly VotingVisibility Anonymous = new VotingVisibility("anonymous");
14+
15+
/// <summary>
16+
/// Public voting - votes are visible to all users
17+
/// </summary>
18+
public static readonly VotingVisibility Public = new VotingVisibility("public");
19+
20+
public VotingVisibility(string value)
21+
{
22+
_value = value;
23+
}
24+
25+
public override string ToString() => _value;
26+
27+
public bool Equals(VotingVisibility other) => _value == other._value;
28+
29+
public override bool Equals(object obj) => obj is VotingVisibility other && Equals(other);
30+
31+
public override int GetHashCode() => _value.GetHashCode();
32+
33+
public static bool operator ==(VotingVisibility left, VotingVisibility right) => left.Equals(right);
34+
35+
public static bool operator !=(VotingVisibility left, VotingVisibility right) => !left.Equals(right);
36+
37+
public static implicit operator VotingVisibility(string value) => new VotingVisibility(value);
38+
39+
public static implicit operator string(VotingVisibility type) => type._value;
40+
41+
internal CreatePollRequestVotingVisibilityInternalDTO ToCreatePollRequestDto()
42+
=> new CreatePollRequestVotingVisibilityInternalDTO(_value);
43+
44+
internal UpdatePollRequestVotingVisibilityInternalDTO ToUpdatePollRequestDto()
45+
=> new UpdatePollRequestVotingVisibilityInternalDTO(_value);
46+
47+
private readonly string _value;
48+
}
49+
}
50+

Assets/Plugins/StreamChat/Core/LowLevelClient/Models/VotingVisibility.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/StreamChat/Core/LowLevelClient/Requests/CreatePollRequest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public partial class CreatePollRequest : RequestObjectBase, ISavableTo<CreatePol
3131

3232
public List<PollOptionInput> Options { get; set; }
3333

34-
public string VotingVisibility { get; set; }
34+
public VotingVisibility? VotingVisibility { get; set; }
3535

3636
CreatePollRequestInternalDTO ISavableTo<CreatePollRequestInternalDTO>.SaveToDto()
3737
=> new CreatePollRequestInternalDTO
@@ -46,9 +46,7 @@ CreatePollRequestInternalDTO ISavableTo<CreatePollRequestInternalDTO>.SaveToDto(
4646
MaxVotesAllowed = MaxVotesAllowed,
4747
Name = Name,
4848
Options = Options.TrySaveToDtoCollection<PollOptionInput, PollOptionInputInternalDTO>(),
49-
VotingVisibility = VotingVisibility != null
50-
? new CreatePollRequestVotingVisibilityInternalDTO { Value = VotingVisibility }
51-
: (CreatePollRequestVotingVisibilityInternalDTO?)null,
49+
VotingVisibility = VotingVisibility?.ToCreatePollRequestDto(),
5250
};
5351
}
5452
}

Assets/Plugins/StreamChat/Core/LowLevelClient/Requests/UpdatePollRequest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using StreamChat.Core.Helpers;
33
using StreamChat.Core.InternalDTO.Requests;
4+
using StreamChat.Core.LowLevelClient.Models;
45

56
namespace StreamChat.Core.LowLevelClient.Requests
67
{
@@ -27,7 +28,7 @@ public partial class UpdatePollRequest : RequestObjectBase, ISavableTo<UpdatePol
2728

2829
public string Name { get; set; }
2930

30-
public string VotingVisibility { get; set; }
31+
public VotingVisibility? VotingVisibility { get; set; }
3132

3233
UpdatePollRequestInternalDTO ISavableTo<UpdatePollRequestInternalDTO>.SaveToDto()
3334
=> new UpdatePollRequestInternalDTO
@@ -41,9 +42,7 @@ UpdatePollRequestInternalDTO ISavableTo<UpdatePollRequestInternalDTO>.SaveToDto(
4142
IsClosed = IsClosed,
4243
MaxVotesAllowed = MaxVotesAllowed,
4344
Name = Name,
44-
VotingVisibility = VotingVisibility != null
45-
? new UpdatePollRequestVotingVisibilityInternalDTO { Value = VotingVisibility }
46-
: (UpdatePollRequestVotingVisibilityInternalDTO?)null,
45+
VotingVisibility = VotingVisibility?.ToUpdatePollRequestDto(),
4746
};
4847
}
4948
}

0 commit comments

Comments
 (0)