mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 00:05:16 +00:00
Add posts with surveys support
This commit is contained in:
@ -3,6 +3,7 @@ import 'package:comunic/enums/post_visibility_level.dart';
|
||||
import 'package:comunic/enums/user_access_levels.dart';
|
||||
import 'package:comunic/lists/comments_list.dart';
|
||||
import 'package:comunic/models/like_element.dart';
|
||||
import 'package:comunic/models/survey.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Single post information
|
||||
@ -31,6 +32,7 @@ class Post implements LikeElement {
|
||||
bool userLike;
|
||||
final UserAccessLevels access;
|
||||
final CommentsList comments;
|
||||
final Survey survey;
|
||||
|
||||
Post(
|
||||
{@required this.id,
|
||||
@ -53,7 +55,8 @@ class Post implements LikeElement {
|
||||
@required this.likes,
|
||||
@required this.userLike,
|
||||
@required this.access,
|
||||
@required this.comments})
|
||||
@required this.comments,
|
||||
@required this.survey})
|
||||
: assert(id != null),
|
||||
assert(userID != null),
|
||||
assert(userPageID != 0 || groupID != 0),
|
||||
@ -62,6 +65,7 @@ class Post implements LikeElement {
|
||||
assert(visibilityLevel != null),
|
||||
assert(kind != null),
|
||||
assert(kind != PostKind.COUNTDOWN || timeEnd != null),
|
||||
assert(kind != PostKind.SURVEY || survey != null),
|
||||
assert(likes != null),
|
||||
assert(userLike != null),
|
||||
assert(access != null);
|
||||
|
52
lib/models/survey.dart
Normal file
52
lib/models/survey.dart
Normal file
@ -0,0 +1,52 @@
|
||||
import 'package:comunic/models/survey_choice.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Survey information
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class Survey {
|
||||
final int id;
|
||||
final int userID;
|
||||
final int postID;
|
||||
final int creationTime;
|
||||
final String question;
|
||||
int userChoice;
|
||||
final Set<SurveyChoice> choices;
|
||||
|
||||
Survey({
|
||||
@required this.id,
|
||||
@required this.userID,
|
||||
@required this.postID,
|
||||
@required this.creationTime,
|
||||
@required this.question,
|
||||
@required this.userChoice,
|
||||
@required this.choices,
|
||||
}) : assert(id != null),
|
||||
assert(userID != null),
|
||||
assert(postID != null),
|
||||
assert(creationTime != null),
|
||||
assert(question != null),
|
||||
assert(userChoice != null),
|
||||
assert(choices != null),
|
||||
assert(choices.length > 0);
|
||||
|
||||
bool get hasResponded => this.userChoice != null && this.userChoice > 0;
|
||||
|
||||
SurveyChoice get userResponse {
|
||||
if (!hasResponded) return null;
|
||||
|
||||
return choices.firstWhere((e) => e.id == userChoice);
|
||||
}
|
||||
|
||||
void cancelUserResponse() {
|
||||
if (hasResponded) userResponse.responses--;
|
||||
userChoice = 0;
|
||||
}
|
||||
|
||||
void setUserResponse(SurveyChoice choice) {
|
||||
cancelUserResponse();
|
||||
userChoice = choice.id;
|
||||
choice.responses++;
|
||||
}
|
||||
}
|
19
lib/models/survey_choice.dart
Normal file
19
lib/models/survey_choice.dart
Normal file
@ -0,0 +1,19 @@
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Single survey choice
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class SurveyChoice {
|
||||
final int id;
|
||||
final String name;
|
||||
int responses;
|
||||
|
||||
SurveyChoice({
|
||||
@required this.id,
|
||||
@required this.name,
|
||||
@required this.responses,
|
||||
}) : assert(id != null),
|
||||
assert(name != null),
|
||||
assert(responses != null);
|
||||
}
|
Reference in New Issue
Block a user