Skip to content

Commit

Permalink
support for showing vote results and showing voting indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
noumantahir committed Aug 15, 2024
1 parent bffd522 commit 8345235
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,7 @@ class PollChoices extends HookWidget {
width: pollOptionsWidth,
padding: EdgeInsets.zero,
decoration: BoxDecoration(
color: (userVotedOptionIds?.contains(pollOption.id) ?? false)
? voteInProgressColor
: pollOptionsFillColor,
color: pollOptionsFillColor,
border: pollOptionsBorder ??
Border.all(
color: Colors.black,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:provider/provider.dart';
import 'package:waves/core/services/poll_service/poll_model.dart';
import 'package:waves/features/auth/presentation/controller/auth_controller.dart';
import 'package:waves/features/threads/models/thread_feeds/thread_feed_model.dart';
import 'package:waves/features/threads/models/thread_feeds/thread_json_meta_data/thread_json_meta_data.dart';
import 'package:waves/features/threads/presentation/thread_feed/controller/poll_controller.dart';
import 'package:waves/features/threads/presentation/thread_feed/widgets/post_poll/poll_choices.dart';
import 'package:waves/features/threads/presentation/thread_feed/widgets/post_poll/poll_header.dart';
import 'package:waves/features/user/view/user_controller.dart';

class PostPoll extends StatefulWidget {
const PostPoll({super.key, required this.item});
Expand All @@ -19,7 +21,8 @@ class PostPoll extends StatefulWidget {

class _PostPollState extends State<PostPoll> {
Map<int, bool> selection = {};
bool hasVoted = false;
bool enableRevote = false;
bool isVoting = false;

@override
void initState() {
Expand All @@ -38,27 +41,45 @@ class _PostPollState extends State<PostPoll> {
String author = widget.item.author;
String permlink = widget.item.permlink;

String? username = context.select<UserController, String?>(
(userController) => userController.userName);
PollModel? poll = context.select<PollController, PollModel?>(
(pollController) => pollController.getPollData(author, permlink));

bool hasEnded = poll?.endTime.isBefore(DateTime.now()) ?? false;

List<int> userVotedIds = poll?.userVotedIds(username) ?? [];
bool hasVoted = userVotedIds.isNotEmpty;

bool voteEnabled = poll != null &&
!hasEnded &&
!hasVoted &&
(!hasVoted || enableRevote) &&
selection.entries
.fold(false, (prevVal, entry) => entry.value || prevVal);

onCastVote() async {
bool status = await Future.delayed(Duration(seconds: 2), () => true);
PollController pollController = context.read<PollController>();

if (status) {
if (poll?.pollTrxId != null && selection.isNotEmpty) {
setState(() {
hasVoted = true;
isVoting = true;
});
}

return status;
List<int> selectedIds = selection.entries
.where((entry) => entry.value)
.map((entry) => entry.key)
.toList();

bool status = await pollController.castVote(
poll!.author, poll.permlink, selectedIds);

if (status) {
setState(() {
enableRevote = false;
isVoting = false;
});
}
}
}

onSelection(int id, bool value) {
Expand All @@ -67,6 +88,12 @@ class _PostPollState extends State<PostPoll> {
});
}

onRevote() {
setState(() {
enableRevote = true;
});
}

if (meta == null || meta.contentType != ContentType.poll) {
return Container();
}
Expand Down Expand Up @@ -96,10 +123,10 @@ class _PostPollState extends State<PostPoll> {
pollOptions: pollOptions(),
selectedIds: selection,
pollEnded: hasEnded,
hasVoted: hasVoted,
hasVoted: !enableRevote && hasVoted,
heightBetweenOptions: 16,
pollOptionsHeight: 40,
userVotedOptionIds: poll?.userVotedIds("tahir"),
userVotedOptionIds: userVotedIds,
totalVotes: poll?.pollStats.totalVotingAccountsNum.toDouble() ?? 0,
votedBackgroundColor: const Color(0xff2e3d51),
pollOptionsFillColor: const Color(0xff2e3d51),
Expand All @@ -109,14 +136,35 @@ class _PostPollState extends State<PostPoll> {
const Icon(Icons.check, color: Colors.white, size: 24),
),
Align(
alignment: Alignment.centerRight,
child: ElevatedButton.icon(
onPressed: voteEnabled ? () => onCastVote() : null,
icon: const Icon(Icons.bar_chart),
label: const Text("Vote"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
padding: const EdgeInsets.symmetric(horizontal: 32)),
alignment: Alignment.centerLeft,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
if (hasVoted && !enableRevote)
TextButton(
onPressed: () => onRevote(),
child: const Text("Revote"),
),
ElevatedButton.icon(
onPressed: voteEnabled ? () => onCastVote() : null,
icon: isVoting
? Container(
width: 24.0,
height: 24.0,
padding: const EdgeInsets.all(4.0),
child: const CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.white),
strokeWidth: 2,
),
)
: const Icon(Icons.bar_chart),
label: const Text("Vote"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
padding: const EdgeInsets.symmetric(horizontal: 32)),
),
],
),
)
],
Expand Down

0 comments on commit 8345235

Please sign in to comment.