Skip to content

Commit

Permalink
Vote referendum logic (#1203)
Browse files Browse the repository at this point in the history
* Vote referendum logic

* voice model handling referendum voice

* Igonre added

* Igonre removed

* Ignore2

* add referendum condition to show status label

* Fix vote model serialization for referendums votes
  • Loading branch information
RaulUrtecho authored Sep 22, 2021
1 parent 102c351 commit dbb2d87
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SearchUserBloc extends Bloc<SearchUserEvent, SearchUserState> {
@override
Stream<Transition<SearchUserEvent, SearchUserState>> transformEvents(
Stream<SearchUserEvent> events,
// ignore: deprecated_member_use
TransitionFunction<SearchUserEvent, SearchUserState> transitionFn,
) {
final nonDebounceStream = events.where((event) => event is ClearIconTapped);
Expand Down
19 changes: 19 additions & 0 deletions lib/datasource/local/cache_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:seeds/datasource/remote/model/vote_model.dart';
// Hive box names

const String _proposalVotesBox = 'proposalVotesBox';
const String _referendumsVotesBox = 'referendumsVotesBox';
const String _membersBox = 'membersBox';

// Cache Repo
Expand Down Expand Up @@ -46,4 +47,22 @@ class CacheRepository {
}
await box.put(proposalId, voteModel);
}

Future<VoteModel?> getReferendumVote(String account, int proposalId) async {
final box = await Hive.openBox<VoteModel>(_referendumsVotesBox);
if (_boxIsClosed(box)) {
return null;
}
// Vote cache needs to support multiple accounts.
// The vote cache needs to store what account the vote was for.
return box.get('${account}_$proposalId');
}

Future<void> saveReferendumVote(int proposalId, VoteModel voteModel) async {
final box = await Hive.openBox<VoteModel>(_referendumsVotesBox);
if (_boxIsClosed(box)) {
return;
}
await box.put(proposalId, voteModel);
}
}
2 changes: 1 addition & 1 deletion lib/datasource/remote/api/proposals_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class ProposalsRepository extends NetworkRepository with EosRepository {
return http
.post(proposalsURL, headers: headers, body: request)
.then((http.Response response) => mapHttpResponse(response, (dynamic body) {
return VoteModel.fromJson(body);
return VoteModel.fromJsonReferendum(body);
}))
.catchError((error) => mapHttpError(error));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/remote/api/voice_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class VoiceRepository extends NetworkRepository {
return http
.post(voiceURL, headers: headers, body: request)
.then((http.Response response) => mapHttpResponse(response, (dynamic body) {
return VoiceModel.fromJson(body);
return VoiceModel.fromBalanceJson(body);
}))
.catchError((error) => mapHttpError(error));
}
Expand Down
8 changes: 8 additions & 0 deletions lib/datasource/remote/model/voice_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ class VoiceModel {
return const VoiceModel(0);
}
}

factory VoiceModel.fromBalanceJson(Map<String, dynamic> balanceJson) {
if (balanceJson["rows"].isNotEmpty) {
return VoiceModel(balanceJson["rows"][0]["voice"] as int);
} else {
return const VoiceModel(0);
}
}
}
9 changes: 9 additions & 0 deletions lib/datasource/remote/model/vote_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ class VoteModel {
return const VoteModel(amount: 0, isVoted: false);
}
}

factory VoteModel.fromJsonReferendum(Map<String, dynamic>? json) {
if (json != null && json['rows'].isNotEmpty) {
final vote = json['rows'].first;
return VoteModel(amount: vote['favoured'] == 1 ? vote['amount'] : -vote['amount'], isVoted: true);
} else {
return const VoteModel(amount: 0, isVoted: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class VoteStatusLabel extends StatelessWidget {
Widget build(BuildContext context) {
return BlocBuilder<ProposalDetailsBloc, ProposalDetailsState>(
builder: (context, state) {
if (state.proposals[state.currentIndex].stage == 'active') {
if (state.proposals[state.currentIndex].stage == 'active' ||
state.proposals[state.currentIndex].stage.isEmpty) {
switch (state.voteStatus) {
case VoteStatus.notCitizen:
return Padding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ class GetProposalDataUseCase {
}

Future<Result> _fetchVote(ProposalViewModel proposal, String account) async {
final cacheRepository = const CacheRepository();
late Result result;
VoteModel? voteModel;

if (proposal.proposalCategory == ProposalCategory.referendum) {
result = await _proposalsRepository.getReferendumVote(proposal.id, account);
if (result.isValue) {
voteModel = result.asValue!.value as VoteModel;
voteModel = await cacheRepository.getReferendumVote(account, proposal.id);
if (voteModel == null) {
result = await _proposalsRepository.getReferendumVote(proposal.id, account);
if (result.isValue) {
voteModel = result.asValue!.value as VoteModel;
if (voteModel.isVoted) {
await cacheRepository.saveReferendumVote(proposal.id, voteModel);
}
}
}
} else {
final cacheRepository = const CacheRepository();
voteModel = await cacheRepository.getProposalVote(account, proposal.id);
if (voteModel == null) {
result = await _proposalsRepository.getProposalVote(proposal.id, account);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import 'package:async/async.dart';
import 'package:seeds/datasource/local/settings_storage.dart';
import 'package:seeds/datasource/remote/api/proposals_repository.dart';
import 'package:seeds/screens/explore_screens/vote_screens/proposals/viewmodels/proposal_view_model.dart';

class VoteProposalUseCase {
Future<Result> run({required int id, required int amount}) {
return ProposalsRepository().voteProposal(id: id, amount: amount, accountName: settingsStorage.accountName);
Future<Result> run({required ProposalViewModel proposal, required int amount}) {
final ProposalsRepository _proposalsRepository = ProposalsRepository();
final String accountName = settingsStorage.accountName;
if (proposal.proposalCategory == ProposalCategory.referendum) {
return _proposalsRepository.voteReferendum(id: proposal.id, amount: amount, accountName: accountName);
} else {
return _proposalsRepository.voteProposal(id: proposal.id, amount: amount, accountName: accountName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ProposalDetailsBloc extends Bloc<ProposalDetailsEvent, ProposalDetailsStat
if (event is OnConfirmVoteButtonPressed) {
yield state.copyWith(pageState: PageState.loading);
final Result result = await VoteProposalUseCase().run(
id: state.proposals[state.currentIndex].id,
proposal: state.proposals[state.currentIndex],
amount: state.voteAmount,
);
yield VoteProposalStateMapper().mapResultsToState(state, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ class ProposalDetailsState extends Equatable {
bool get shouldShowNexProposalButton {
final isVoted = vote?.isVoted ?? false;
final hasMoreItems = currentIndex < proposals.length - 1;
return (showNextButton || isVoted || !isCitizen || proposals[currentIndex].stage != 'active') && hasMoreItems;
return showNextButton ||
isVoted ||
!isCitizen ||
(proposals[currentIndex].stage != 'active' || proposals[currentIndex].status != 'active') && hasMoreItems;
}

bool get shouldShowVoteModule {
final isVoted = vote?.isVoted ?? false;
return !showNextButton && !isVoted && isCitizen && proposals[currentIndex].stage == 'active';
return !showNextButton &&
!isVoted &&
isCitizen &&
(proposals[currentIndex].stage == 'active' || proposals[currentIndex].status == 'active');
}

ProposalDetailsState copyWith({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ class GetVoteUseCase {
final ProposalsRepository _proposalsRepository = ProposalsRepository();

Future<Result> run(ProposalViewModel proposal, String account) async {
final cacheRepository = const CacheRepository();
late Result result;
VoteModel? voteModel;

if (proposal.proposalCategory == ProposalCategory.referendum) {
result = await _proposalsRepository.getReferendumVote(proposal.id, account);
if (result.isValue) {
voteModel = result.asValue!.value as VoteModel;
voteModel = await cacheRepository.getReferendumVote(account, proposal.id);
if (voteModel == null) {
result = await _proposalsRepository.getReferendumVote(proposal.id, account);
if (result.isValue) {
voteModel = result.asValue!.value as VoteModel;
if (voteModel.isVoted) {
await cacheRepository.saveReferendumVote(proposal.id, voteModel);
}
}
}
} else {
final cacheRepository = const CacheRepository();
voteModel = await cacheRepository.getProposalVote(account, proposal.id);
if (voteModel == null) {
result = await _proposalsRepository.getProposalVote(proposal.id, account);
Expand Down

0 comments on commit dbb2d87

Please sign in to comment.