@@ -21,12 +21,10 @@ final class ProposalCubit extends Cubit<ProposalState>
21
21
22
22
// Cache
23
23
DocumentRef ? _ref;
24
-
25
- // ignore: unused_field
26
24
ProposalData ? _proposal;
27
-
28
- // ignore: unused_field
25
+ CommentTemplate ? _commentTemplate;
29
26
List <CommentWithReplies >? _comments;
27
+ bool ? _isFavorite;
30
28
31
29
// Note for integration.
32
30
// 1. Fetch proposal document
@@ -46,45 +44,42 @@ final class ProposalCubit extends Cubit<ProposalState>
46
44
47
45
final proposal = await _proposalService.getProposal (ref: ref);
48
46
final comments = _buildComments ();
49
-
50
- if (isClosed) {
51
- return ;
52
- }
53
-
54
- final commentsSort = state.data.commentsSort;
55
-
56
- final proposalViewData = _buildProposalViewData (
57
- hasActiveAccount: _userService.user.activeAccount != null ,
58
- proposal: proposal,
59
- comments: comments,
60
- commentSchema: _buildCommentTemplate ().document.schema,
61
- commentsSort: commentsSort,
62
- isFavorite: false ,
63
- );
47
+ final isFavorite =
48
+ await _proposalService.watchIsFavoritesProposal (ref: ref).first;
64
49
65
50
_ref = ref;
66
51
_proposal = proposal;
52
+ _commentTemplate = _buildCommentTemplate ();
67
53
_comments = comments;
54
+ _isFavorite = isFavorite;
68
55
69
- emit (ProposalState (data: proposalViewData));
56
+ if (! isClosed) {
57
+ final proposalState = _rebuildProposalState ();
70
58
71
- if (proposalViewData.isCurrentVersionLatest == false ) {
72
- emitSignal (const ViewingOlderVersionSignal ());
59
+ emit (ProposalState (data: proposalState));
60
+
61
+ if (proposalState.isCurrentVersionLatest == false ) {
62
+ emitSignal (const ViewingOlderVersionSignal ());
63
+ }
73
64
}
74
65
} on LocalizedException catch (error, stack) {
75
66
_logger.severe ('Loading $ref failed' , error, stack);
76
67
77
68
_ref = null ;
78
69
_proposal = null ;
70
+ _commentTemplate = null ;
79
71
_comments = null ;
72
+ _isFavorite = null ;
80
73
81
74
emit (ProposalState (error: error));
82
75
} catch (error, stack) {
83
76
_logger.severe ('Loading $ref failed' , error, stack);
84
77
85
78
_ref = null ;
86
79
_proposal = null ;
80
+ _commentTemplate = null ;
87
81
_comments = null ;
82
+ _isFavorite = null ;
88
83
89
84
emit (const ProposalState (error: LocalizedUnknownException ()));
90
85
} finally {
@@ -149,39 +144,80 @@ final class ProposalCubit extends Cubit<ProposalState>
149
144
emit (state.copyWith (data: updatedData));
150
145
}
151
146
152
- // TODO(damian-molinski): not implemented
153
147
Future <void > updateIsFavorite ({required bool value}) async {
154
- final proposalId = _ref? .id ;
155
- assert (proposalId != null , 'Proposal ref not found. Load doc first' );
148
+ final ref = _ref;
149
+ assert (ref != null , 'Proposal ref not found. Load doc first' );
156
150
157
151
emit (state.copyWithFavorite (isFavorite: value));
152
+
153
+ if (value) {
154
+ await _proposalService.addFavoriteProposal (ref: ref! );
155
+ } else {
156
+ await _proposalService.removeFavoriteProposal (ref: ref! );
157
+ }
158
158
}
159
159
160
160
ProposalViewData _buildProposalViewData ({
161
161
required bool hasActiveAccount,
162
- required ProposalData proposal,
162
+ required ProposalData ? proposal,
163
163
required List <CommentWithReplies > comments,
164
- required DocumentSchema commentSchema,
164
+ required DocumentSchema ? commentSchema,
165
165
required ProposalCommentsSort commentsSort,
166
166
required bool isFavorite,
167
167
}) {
168
- final proposalDocument = proposal.document;
169
- final proposalDocumentRef = proposalDocument.metadata.selfRef;
170
- final documentSegments = mapDocumentToSegments (proposalDocument.document);
168
+ final proposalDocument = proposal? .document;
169
+ final proposalDocumentRef = proposalDocument? .metadata.selfRef;
171
170
172
171
/* cSpell:disable */
173
- final versions = proposal.versions.mapIndexed ((index, version) {
172
+ final proposalVersions = proposal? .versions ?? const [];
173
+ final versions = proposalVersions.mapIndexed ((index, version) {
174
+ final ver = version.document.metadata.selfRef.version;
175
+
174
176
return DocumentVersion (
175
- id: version.document.metadata.selfRef.version ?? '' ,
177
+ id: ver ?? '' ,
176
178
number: index + 1 ,
177
- isCurrent: version.document.metadata.selfRef.version ==
178
- proposalDocumentRef.version,
179
- isLatest: index == proposal.versions.length - 1 ,
179
+ isCurrent: ver == proposalDocumentRef? .version,
180
+ isLatest: index == proposalVersions.length - 1 ,
180
181
);
181
182
}).toList ();
182
-
183
183
final currentVersion = versions.singleWhereOrNull ((e) => e.isCurrent);
184
184
185
+ final segments = proposal != null
186
+ ? _buildSegments (
187
+ proposal: proposal,
188
+ version: currentVersion,
189
+ comments: comments,
190
+ commentSchema: commentSchema,
191
+ commentsSort: commentsSort,
192
+ hasActiveAccount: hasActiveAccount,
193
+ )
194
+ : const < Segment > [];
195
+
196
+ return ProposalViewData (
197
+ isCurrentVersionLatest: currentVersion? .isLatest,
198
+ header: ProposalViewHeader (
199
+ title: 'Project Mayhem: Freedom by Chaos' ,
200
+ authorDisplayName: 'Tyler Durden' ,
201
+ createdAt: DateTime .timestamp (),
202
+ commentsCount: comments.length,
203
+ versions: versions,
204
+ isFavorite: isFavorite,
205
+ ),
206
+ segments: segments,
207
+ commentsSort: commentsSort,
208
+ );
209
+ /* cSpell:enable */
210
+ }
211
+
212
+ /* cSpell:disable */
213
+ List <Segment > _buildSegments ({
214
+ required ProposalData proposal,
215
+ required DocumentVersion ? version,
216
+ required List <CommentWithReplies > comments,
217
+ required DocumentSchema ? commentSchema,
218
+ required ProposalCommentsSort commentsSort,
219
+ required bool hasActiveAccount,
220
+ }) {
185
221
final overviewSegment = ProposalOverviewSegment .build (
186
222
categoryName: 'Cardano Partners: Growth & Acceleration' ,
187
223
proposalTitle: 'Project Mayhem: Freedom by Chaos' ,
@@ -195,8 +231,8 @@ final class ProposalCubit extends Cubit<ProposalState>
195
231
'inspiring global action for liberation and a return to human '
196
232
'authenticity.' ,
197
233
status: ProposalStatus .draft,
198
- createdAt: currentVersion ? .id.tryDateTime ?? DateTime .now (),
199
- warningCreatedAt: currentVersion ? .isLatest == false ,
234
+ createdAt: version ? .id.tryDateTime ?? DateTime .now (),
235
+ warningCreatedAt: version ? .isLatest == false ,
200
236
tag: 'Community Outreach' ,
201
237
commentsCount: comments.length,
202
238
fundsRequested: 200000 ,
@@ -205,6 +241,8 @@ final class ProposalCubit extends Cubit<ProposalState>
205
241
),
206
242
);
207
243
244
+ final proposalSegments = mapDocumentToSegments (proposal.document.document);
245
+
208
246
final commentsSegment = ProposalCommentsSegment (
209
247
id: const NodeId ('comments' ),
210
248
sort: commentsSort,
@@ -213,31 +251,38 @@ final class ProposalCubit extends Cubit<ProposalState>
213
251
id: const NodeId ('comments.view' ),
214
252
comments: commentsSort.applyTo (comments),
215
253
),
216
- if (hasActiveAccount)
254
+ if (hasActiveAccount && commentSchema != null )
217
255
AddCommentSection (
218
256
id: const NodeId ('comments.add' ),
219
257
schema: commentSchema,
220
258
),
221
259
],
222
260
);
223
261
224
- return ProposalViewData (
225
- isCurrentVersionLatest: currentVersion? .isLatest,
226
- header: ProposalViewHeader (
227
- title: 'Project Mayhem: Freedom by Chaos' ,
228
- authorDisplayName: 'Tyler Durden' ,
229
- createdAt: DateTime .timestamp (),
230
- commentsCount: comments.length,
231
- versions: versions,
232
- isFavorite: isFavorite,
233
- ),
234
- segments: [
235
- overviewSegment,
236
- ...documentSegments,
237
- commentsSegment,
238
- ],
262
+ return [
263
+ overviewSegment,
264
+ ...proposalSegments,
265
+ commentsSegment,
266
+ ];
267
+ }
268
+
269
+ /* cSpell:enable */
270
+
271
+ ProposalViewData _rebuildProposalState () {
272
+ final proposal = _proposal;
273
+ final commentTemplate = _commentTemplate;
274
+ final comments = _comments ?? const [];
275
+ final commentsSort = state.data.commentsSort;
276
+ final isFavorite = _isFavorite ?? false ;
277
+ final activeAccount = _userService.user.activeAccount;
278
+
279
+ return _buildProposalViewData (
280
+ hasActiveAccount: activeAccount != null ,
281
+ proposal: proposal,
282
+ comments: comments,
283
+ commentSchema: commentTemplate? .document.schema,
239
284
commentsSort: commentsSort,
285
+ isFavorite: isFavorite,
240
286
);
241
- /* cSpell:enable */
242
287
}
243
288
}
0 commit comments