mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-29 08:16:28 +00:00
53 lines
1.2 KiB
Dart
53 lines
1.2 KiB
Dart
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++;
|
|
}
|
|
}
|