Skip to content

Commit

Permalink
handling interpretation based percent accumulation
Browse files Browse the repository at this point in the history
  • Loading branch information
noumantahir committed Sep 4, 2024
1 parent b380f06 commit b64aecf
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 92 deletions.
135 changes: 92 additions & 43 deletions lib/core/services/poll_service/poll_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PollModel {
final String parentAuthor;
final double protocolVersion;
final String question;
final String preferredInterpretation;
final PollPreferredInterpretation? preferredInterpretation;
final String? token;
final DateTime endTime;
final String status;
Expand Down Expand Up @@ -54,7 +54,8 @@ class PollModel {
parentAuthor: json['parent_author'],
protocolVersion: json['protocol_version'].toDouble(),
question: json['question'],
preferredInterpretation: json['preferred_interpretation'],
preferredInterpretation: PollPreferredInterpretation.fromString(
json['preferred_interpretation']),
token: json['token'],
endTime: DateTime.parse(json['end_time']),
status: json['status'],
Expand Down Expand Up @@ -96,10 +97,13 @@ class PollModel {
}

double get totalInterpretedVotes {
return pollChoices.fold(0.0, (prev, entry) {
num val = preferredInterpretation == PollPreferredInterpretation.tokens
? entry.votes?.hiveHp ?? 0
: entry.votes?.totalVotes ?? 0;

//TODO: return value based on selected interpretation;
return pollChoices.fold(0, (val, entry) => val + (entry.votes?.totalVotes ?? 0));

return prev + val;
});
}

List<int> userVotedIds(String? username) {
Expand All @@ -112,7 +116,6 @@ class PollModel {
}

void injectPollVoteCache(String username, List<int> selection) {

double userHp = 1; //TOOD: use real user hp

//extract previously votes choices and new choices
Expand Down Expand Up @@ -149,35 +152,35 @@ class PollModel {
pollChoices = [
...notTouchedChoices,
...removedChoices.map((pc) => pc.copyWith(
votes: Votes(
totalVotes: (pc.votes?.totalVotes ?? 0) - 1,
hiveHp: (pc.votes?.hiveHp ?? 0) - userHp,
hiveProxiedHp: 0,
hiveHpInclProxied: (pc.votes?.hiveHpInclProxied ?? 0) - userHp,
splSpsp: 0,
),
)),
votes: Votes(
totalVotes: (pc.votes?.totalVotes ?? 0) - 1,
hiveHp: (pc.votes?.hiveHp ?? 0) - userHp,
hiveProxiedHp: 0,
hiveHpInclProxied: (pc.votes?.hiveHpInclProxied ?? 0) - userHp,
splSpsp: 0,
),
)),
...newChoices.map((pc) => pc.copyWith(
votes: Votes(
totalVotes: (pc.votes?.totalVotes ?? 0) + 1,
hiveHp: (pc.votes?.hiveHp ?? 0) + userHp,
hiveProxiedHp: 0,
hiveHpInclProxied: (pc.votes?.hiveHpInclProxied ?? 0) + userHp,
splSpsp: 0,
),
)),
votes: Votes(
totalVotes: (pc.votes?.totalVotes ?? 0) + 1,
hiveHp: (pc.votes?.hiveHp ?? 0) + userHp,
hiveProxiedHp: 0,
hiveHpInclProxied: (pc.votes?.hiveHpInclProxied ?? 0) + userHp,
splSpsp: 0,
),
)),
]..sort((a, b) => a.choiceNum.compareTo(b.choiceNum));

//update poll voters with updated selection
pollVoters = [
...otherVoters,
PollVoter(
name: username,
choices: selection,
hiveHp: userHp,
splSpsp: 0,
hiveProxiedHp: 0,
hiveHpInclProxied: userHp)
name: username,
choices: selection,
hiveHp: userHp,
splSpsp: 0,
hiveProxiedHp: 0,
hiveHpInclProxied: userHp)
];
}
}
Expand Down Expand Up @@ -223,13 +226,11 @@ class PollChoice {
};
}


PollChoice copyWith({Votes? votes}) {
PollChoice copyWith({Votes? votes}) {
return PollChoice(
choiceNum: choiceNum,
choiceText: choiceText,
votes: votes ?? this.votes
);
choiceNum: choiceNum,
choiceText: choiceText,
votes: votes ?? this.votes);
}
}

Expand Down Expand Up @@ -272,16 +273,25 @@ class Votes {
};
}

Votes copyWith({int? totalVotes, double? hiveHp, double? hiveHpInclProxied}) {
Votes copyWith({int? totalVotes, double? hiveHp, double? hiveHpInclProxied}) {
return Votes(
totalVotes: totalVotes ?? this.totalVotes,
hiveHp: hiveHp ?? this.hiveHp,
hiveProxiedHp: hiveProxiedHp,
hiveHpInclProxied: hiveHpInclProxied ?? this.hiveHpInclProxied,
splSpsp: splSpsp
);


totalVotes: totalVotes ?? this.totalVotes,
hiveHp: hiveHp ?? this.hiveHp,
hiveProxiedHp: hiveProxiedHp,
hiveHpInclProxied: hiveHpInclProxied ?? this.hiveHpInclProxied,
splSpsp: splSpsp);
}

num getInterprettedVotes(PollPreferredInterpretation? interpretation) {
return interpretation == PollPreferredInterpretation.tokens
? hiveHp
: totalVotes;
}

String getInterprettedSymbol(PollPreferredInterpretation? interpretation) {
return interpretation == PollPreferredInterpretation.tokens
? "HP"
: "voted";
}
}

Expand Down Expand Up @@ -368,3 +378,42 @@ class PollStats {
};
}
}

enum PollPreferredInterpretation
implements Comparable<PollPreferredInterpretation> {
tokens(value: 'tokens'),
numberOfVotes(value: 'number_of_votes');

const PollPreferredInterpretation({
required this.value,
});

final String value;

// Lookup map to find enum from string
static final Map<String, PollPreferredInterpretation> _valueMap = {
'tokens': PollPreferredInterpretation.tokens,
'number_of_votes': PollPreferredInterpretation.numberOfVotes,
};

// Convert string to enum
static PollPreferredInterpretation? fromString(String? value) {
if (value == null) {
return null;
}

final result = _valueMap[value];
if (result == null) {
throw ArgumentError('Unknown value: $value');
}
return result;
}

// Convert enum to string
String toShortString() {
return value;
}

@override
int compareTo(PollPreferredInterpretation other) => 0;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:waves/core/services/poll_service/poll_model.dart';
import 'package:waves/core/utilities/save_convert.dart';
import 'package:waves/features/threads/models/thread_feeds/thread_json_meta_data/thread_json_meta_data_video.dart';

Expand All @@ -15,45 +16,7 @@ enum ContentType implements Comparable<ContentType> {
int compareTo(ContentType other) => 0;
}

enum PollPreferredInterpretation
implements Comparable<PollPreferredInterpretation> {
tokens(value: 'tokens'),
numberOfVotes(value: 'number_of_votes');

const PollPreferredInterpretation({
required this.value,
});

final String value;

// Lookup map to find enum from string
static final Map<String, PollPreferredInterpretation> _valueMap = {
'tokens': PollPreferredInterpretation.tokens,
'number_of_votes': PollPreferredInterpretation.numberOfVotes,
};

// Convert string to enum
static PollPreferredInterpretation? fromString(String? value) {

if(value == null){
return null;
}

final result = _valueMap[value];
if (result == null) {
throw ArgumentError('Unknown value: $value');
}
return result;
}

// Convert enum to string
String toShortString() {
return value;
}

@override
int compareTo(PollPreferredInterpretation other) => 0;
}

class PollFilters {
final int accountAge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,10 @@ class PollChoices extends HookWidget {
const SizedBox(width: 10),
const Spacer(),
Text(
pollOption.votes.toString(),
style: votedPercentageTextStyle,
'${pollOption.votes.toString()} ${pollOption.votesPostfix ?? ''}',
style: const TextStyle(
fontSize: 12
),
),
],
),
Expand Down Expand Up @@ -348,7 +350,7 @@ class PollChoices extends HookWidget {
width: pollOptionsWidth,
padding: EdgeInsets.zero,
decoration: BoxDecoration(
color: pollOptionsFillColor,
color: pollOptionsFillColor,
border: pollOptionsBorder ??
Border.all(
color: selectedIds.contains(pollOption.id) ? Colors.blue : pollOptionsFillColor!,
Expand Down Expand Up @@ -384,9 +386,11 @@ class PollOption {
required this.id,
required this.title,
required this.votes,
this.votesPostfix,
});

final int id;
final Widget title;
int votes;
num votes;
String? votesPostfix;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:waves/core/locales/locale_text.dart';
import 'package:waves/core/services/poll_service/poll_model.dart';
import 'package:waves/features/threads/models/thread_feeds/thread_json_meta_data/thread_json_meta_data.dart';

class PollHeader extends StatelessWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class _PostPollState extends State<PostPoll> {

@override
void initState() {
// TODO: implement initState
super.initState();

if (widget.item.jsonMetadata!.contentType == ContentType.poll) {
Expand Down Expand Up @@ -61,6 +60,7 @@ class _PostPollState extends State<PostPoll> {
(!hasVoted || enableRevote) &&
selection.isNotEmpty;

//prepare for vote action boadcast
onCastVote() async {
PollController pollController = context.read<PollController>();

Expand All @@ -80,6 +80,7 @@ class _PostPollState extends State<PostPoll> {
}
}

//change selection is user interact with choices
onSelection(int choiceNum, bool value) {
int? maxSelectable = meta?.maxChoicesVoted ?? 1;

Expand All @@ -99,13 +100,10 @@ class _PostPollState extends State<PostPoll> {
} else {
// if only one choice allowed, overwrite selection
setState(() {
selection = [choiceNum]; // [choiceNum];
selection = [choiceNum];
});
}

// setState(() {
// selection = {...selection, id: value};
// });
}

onRevote() {
Expand All @@ -121,12 +119,14 @@ class _PostPollState extends State<PostPoll> {
List<PollOption> pollOptions() {
List<PollChoice> choices =
poll?.pollChoices ?? PollChoice.fromValues(meta.choices!);


return choices
.map((e) => PollOption(
id: e.choiceNum,
title: Text(e.choiceText, maxLines: 2),
votes: e.votes?.totalVotes ?? 0))
title: Text(e.choiceText, maxLines: 2, style: const TextStyle(fontSize: 12),),
votes: e.votes?.getInterprettedVotes(meta.preferredInterpretation) ?? 0,
votesPostfix: e.votes?.getInterprettedSymbol(meta.preferredInterpretation)))
.toList();
}

Expand Down

0 comments on commit b64aecf

Please sign in to comment.