Skip to content

Commit 4b914be

Browse files
committed
squash! comments wip
Signed-off-by: Zixuan James Li <[email protected]>
1 parent b0a0230 commit 4b914be

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

lib/api/model/polls.dart

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@ import 'package:json_annotation/json_annotation.dart';
22

33
part 'polls.g.dart';
44

5+
// const maxLatestOptionIndex = 1000; // TODO validate
6+
57
/// Data structure seen from [Submessage.content] to support Zulip's widget
68
/// system.
79
sealed class WidgetData {}
810

11+
/// [WidgetData] that defines the starting state of the poll.
12+
///
13+
/// If a message contains a poll, the first [Submessage] from
14+
/// [Message.submessages] should always be a [PollStartData].
915
@JsonSerializable(fieldRename: FieldRename.snake)
10-
class WidgetStartData extends WidgetData {
11-
WidgetStartData({required this.widgetType, required this.extraData});
16+
class PollStartData extends WidgetData {
17+
PollStartData({required this.widgetType, required this.extraData});
1218

1319
final WidgetType widgetType;
1420
final PollExtraData extraData;
1521

16-
factory WidgetStartData.fromJson(Map<String, dynamic> json) =>
17-
_$WidgetStartDataFromJson(json);
22+
factory PollStartData.fromJson(Map<String, dynamic> json) =>
23+
_$PollStartDataFromJson(json);
1824

19-
Map<String, dynamic> toJson() => _$WidgetStartDataToJson(this);
25+
Map<String, dynamic> toJson() => _$PollStartDataToJson(this);
2026
}
2127

2228
@JsonSerializable(fieldRename: FieldRename.snake)
@@ -55,18 +61,21 @@ sealed class PollEvent extends WidgetData {
5561
Map<String, dynamic> toJson();
5662
}
5763

64+
/// Event when an option is added to the poll.
5865
@JsonSerializable(fieldRename: FieldRename.snake)
5966
class PollOptionEvent extends PollEvent {
6067
@override
6168
@JsonKey(includeToJson: true)
6269
PollEventType get type => PollEventType.newOption;
6370

6471
final String option;
65-
final int idx;
72+
/// The index of the latest option added by a specific user.
73+
@JsonKey(name: 'idx')
74+
final int latestOptionIndex;
6675

6776
PollOptionEvent({
6877
required this.option,
69-
required this.idx,
78+
required this.latestOptionIndex,
7079
});
7180

7281
@override
@@ -77,6 +86,7 @@ class PollOptionEvent extends PollEvent {
7786
Map<String, dynamic> toJson() => _$PollOptionEventToJson(this);
7887
}
7988

89+
/// Event when the question of the poll has been edited.
8090
@JsonSerializable(fieldRename: FieldRename.snake)
8191
class PollQuestionEvent extends PollEvent {
8292
@override
@@ -97,18 +107,22 @@ class PollQuestionEvent extends PollEvent {
97107
Map<String, dynamic> toJson() => _$PollQuestionEventToJson(this);
98108
}
99109

110+
/// Event when a vote has been casted or removed.
100111
@JsonSerializable(fieldRename: FieldRename.snake)
101112
class PollVoteEvent extends PollEvent {
102113
@override
103114
@JsonKey(includeToJson: true)
104115
PollEventType get type => PollEventType.vote;
105116

117+
/// Key corresponding to an option in the `'${sender_id},{latestOptionIndex}'`
118+
/// format.
106119
final String key;
107-
final int vote;
120+
@JsonKey(name: 'vote', unknownEnumValue: VoteOp.unknown)
121+
final VoteOp op;
108122

109123
PollVoteEvent({
110124
required this.key,
111-
required this.vote,
125+
required this.op,
112126
});
113127

114128
@override
@@ -119,6 +133,19 @@ class PollVoteEvent extends PollEvent {
119133
Map<String, dynamic> toJson() => _$PollVoteEventToJson(this);
120134
}
121135

136+
@JsonEnum(valueField: 'apiValue')
137+
enum VoteOp {
138+
add(apiValue: 1),
139+
remove(apiValue: -1),
140+
unknown(apiValue: null);
141+
142+
const VoteOp({required this.apiValue});
143+
144+
final int? apiValue;
145+
146+
int? toJson() => apiValue;
147+
}
148+
122149
class UnknownPollEvent extends PollEvent {
123150
@override
124151
@JsonKey(includeToJson: true)

lib/api/model/polls.g.dart

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

0 commit comments

Comments
 (0)