youtube_explode/lib/src/common/engagement.dart

36 lines
891 B
Dart
Raw Normal View History

2020-02-24 14:28:52 +01:00
import 'package:equatable/equatable.dart';
2020-02-20 19:50:10 +01:00
/// User activity statistics.
2020-06-03 23:02:21 +02:00
class Engagement extends Equatable {
2020-02-20 19:50:10 +01:00
/// View count.
final int viewCount;
/// Like count.
2021-03-04 12:20:00 +01:00
final int? likeCount;
2020-02-20 19:50:10 +01:00
/// Dislike count.
2021-03-04 12:20:00 +01:00
final int? dislikeCount;
2020-02-20 19:50:10 +01:00
2021-03-04 12:20:00 +01:00
/// Initializes an instance of [Engagement]
2020-06-03 23:02:21 +02:00
const Engagement(this.viewCount, this.likeCount, this.dislikeCount);
2020-02-20 19:50:10 +01:00
/// Average user rating in stars (1 star to 5 stars).
2021-03-04 12:20:00 +01:00
/// Returns -1 if likeCount or dislikeCount is null.
2020-02-20 19:50:10 +01:00
num get avgRating {
2021-03-04 12:20:00 +01:00
if (likeCount == null || dislikeCount == null) {
return -1;
}
if (likeCount! + dislikeCount! == 0) {
2020-02-20 19:50:10 +01:00
return 0;
}
2021-03-04 12:20:00 +01:00
return 1 + 4.0 * likeCount! / (likeCount! + dislikeCount!);
2020-02-20 19:50:10 +01:00
}
2020-02-24 14:28:52 +01:00
@override
String toString() =>
'$viewCount views, $likeCount likes, $dislikeCount dislikes';
2021-03-04 12:20:00 +01:00
@override
List<Object?> get props => [viewCount, likeCount, dislikeCount];
}