Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement pagination #379

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/core/extension/map_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:hypha_wallet/core/extension/proposal_state_extension.dart';
import 'package:hypha_wallet/core/extension/proposal_type_extension.dart';
import 'package:hypha_wallet/core/extension/string_extension.dart';

extension MapExtension on Map<String, dynamic> {
Map<String, dynamic> calculateUnityAndQuorum() {
if (this['votetally'] != null && this['votetally'].isNotEmpty) {
final double abstain = (this['votetally'][0]['abstain_votePower_a'] as String).quantityAsDouble;
final double pass = (this['votetally'][0]['pass_votePower_a'] as String).quantityAsDouble;
final double fail = (this['votetally'][0]['fail_votePower_a'] as String).quantityAsDouble;

final double unity = (pass + fail > 0) ? pass / (pass + fail) : 0;

final double supply = (this['details_ballotSupply_a'] as String?)?.quantityAsDouble ?? 0;
final double quorum = supply > 0 ? (abstain + pass + fail) / supply : 0;

this['unity'] = unity * 100;
this['quorum'] = quorum * 100;
}

return this;
}

bool isValidProposal() {
final DateTime? ballotExpiration = DateTime.tryParse(this['ballot_expiration_t'] ?? '');
final bool isBallotExpired = ballotExpiration != null && ballotExpiration.isBefore(DateTime.now());

final Set<String> validStates = {ProposalState.Approved.string, ProposalState.Rejected.string, ProposalState.Archived.string, ProposalState.Proposed.string};
final bool isValidState = validStates.contains(this['details_state_s']);

/// This code checks the proposal type in addition to fixing it in the query, as the proposal can sometimes return unexpected types like 'Assignment'.
return (this['type'] == ProposalType.Policy.string || this['type'] == ProposalType.Payout.string) && isBallotExpired && isValidState;
}
}
12 changes: 0 additions & 12 deletions lib/core/extension/proposal_creation_model_extension.dart

This file was deleted.

20 changes: 20 additions & 0 deletions lib/core/extension/proposal_state_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
enum ProposalState { Approved, Rejected, Archived, Proposed }

extension ProposalStateExtension on ProposalState {
/// Don't modify these values to uppercase.
/// If there's a need to capitalize the first letter, create a String extension and use `replaceFirst`.
String get string {
switch (this) {
case ProposalState.Approved:
return 'approved';
case ProposalState.Rejected:
return 'rejected';
case ProposalState.Archived:
return 'archived';
case ProposalState.Proposed:
return 'proposed';
default:
return 'unknown';
}
}
}
16 changes: 16 additions & 0 deletions lib/core/extension/proposal_type_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
enum ProposalType { Policy, Payout }

extension ProposalTypeExtension on ProposalType {
/// Don't modify these values to uppercase.
/// If there's a need to lowercase the first letter, use `toLowerCase`.
String get string {
switch (this) {
case ProposalType.Policy:
return 'Policy';
case ProposalType.Payout:
return 'Payout';
default:
return 'Unknown';
}
}
}
21 changes: 0 additions & 21 deletions lib/core/extension/vote_tally_extension.dart

This file was deleted.

8 changes: 4 additions & 4 deletions lib/core/network/api/services/proposal_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class ProposalService {

const ProposalService(this._graphQLService);

Future<Result<Map<String, dynamic>, HyphaError>> getActiveProposals(UserProfileData user, int daoId) async {
final String query = '{"query":"query ActiveProposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { proposal { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Budget { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Policy { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Circle { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Payout { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Role { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Badge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Suspend { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Edit { details_timeShareX100_i original { ... on Assignbadge { details_title_s } ... on Assignment { details_title_s } } details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } } } }","variables":{"docId":"$daoId"}}';
Future<Result<Map<String, dynamic>, HyphaError>> getActiveProposals(UserProfileData user, int daoId,int first, int offset) async {
final String query = '{"query":"query ActiveProposals(\$docId: String!, \$first: Int, \$offset: Int) { queryDao(filter: { docId: { eq: \$docId } }) { proposal(first: \$first, offset: \$offset) { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Budget { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Policy { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Circle { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Payout { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Role { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Badge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Suspend { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Edit { details_timeShareX100_i original { ... on Assignbadge { details_title_s } ... on Assignment { details_title_s } } details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } } } }","variables":{"docId":"$daoId", "first":$first, "offset":$offset}}';

return _graphQLService.graphQLQuery(network: user.network, query: query);
}

Future<Result<Map<String, dynamic>, HyphaError>> getPastProposals(UserProfileData user, int daoId) async {
final String query = '{"query":"query PastProposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { votable { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Budget { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Policy { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Circle { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Payout { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Role { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Badge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Edit { details_timeShareX100_i votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } original { ... on Assignbadge { details_title_s } ... on Assignment { details_title_s } } details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } } } } }","variables":{"docId":"$daoId"}}';
Future<Result<Map<String, dynamic>, HyphaError>> getPastProposals(UserProfileData user, int daoId, int first, int offset) async {
final String query = '{"query":"query PastProposals(\$docId: String!, \$first: Int, \$offset: Int) { queryDao(filter: { docId: { eq: \$docId } }) { votable(first: \$first, offset: \$offset, order: { desc: createdDate } ) { docId type ... on Payout { details_state_s details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Policy { details_state_s details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } } } }","variables":{"docId":"$daoId", "first":$first, "offset":$offset}}';

return _graphQLService.graphQLQuery(network: user.network, query: query);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/core/network/models/base_proposal_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:hypha_wallet/core/extension/proposal_state_extension.dart';
import 'package:hypha_wallet/core/extension/proposal_type_extension.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/core/network/models/vote_model.dart';
import 'package:hypha_wallet/ui/profile/interactor/profile_data.dart';
Expand All @@ -8,6 +10,11 @@ abstract class BaseProposalModel {
@JsonKey(name: 'docId')
final String? id;

final ProposalType? type;

@JsonKey(name: 'details_state_s')
final ProposalState? state;

DaoData? dao;

@JsonKey(name: 'details_timeShareX100_i')
Expand Down Expand Up @@ -39,6 +46,8 @@ abstract class BaseProposalModel {

BaseProposalModel({
required this.id,
this.type,
this.state,
this.dao,
this.commitment,
this.title,
Expand Down
7 changes: 2 additions & 5 deletions lib/core/network/models/proposal_creation_model.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import 'package:hypha_wallet/core/extension/proposal_type_extension.dart';
import 'package:hypha_wallet/core/network/models/base_proposal_model.dart';

enum ProposalType { policy }

class ProposalCreationModel extends BaseProposalModel {
final String? details;
final ProposalType type;

ProposalCreationModel({super.id, super.title, this.details, super.dao, this.type = ProposalType.policy});
ProposalCreationModel({super.id, super.title, this.details, super.dao, super.type = ProposalType.Policy});

ProposalCreationModel copyWith(Map<String, dynamic> updates) {
return ProposalCreationModel(
Expand Down
2 changes: 1 addition & 1 deletion lib/core/network/models/proposal_details_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:hypha_wallet/core/extension/vote_tally_extension.dart';
import 'package:hypha_wallet/core/extension/map_extension.dart';
import 'package:hypha_wallet/core/network/models/base_proposal_model.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/core/network/models/vote_model.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/core/network/models/proposal_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hypha_wallet/core/extension/vote_tally_extension.dart';
import 'package:hypha_wallet/core/extension/map_extension.dart';
import 'package:hypha_wallet/core/network/models/base_proposal_model.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/core/network/models/vote_model.dart';
Expand Down
44 changes: 14 additions & 30 deletions lib/core/network/repository/proposal_repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:hypha_wallet/core/crypto/seeds_esr/eos_action.dart';
import 'package:hypha_wallet/core/error_handler/model/hypha_error.dart';
import 'package:hypha_wallet/core/extension/base_proposal_model_extension.dart';
import 'package:hypha_wallet/core/extension/map_extension.dart';
import 'package:hypha_wallet/core/logging/log_helper.dart';
import 'package:hypha_wallet/core/network/api/actions/vote_action_factory.dart';
import 'package:hypha_wallet/core/network/api/eos_service.dart';
Expand Down Expand Up @@ -35,8 +36,8 @@ class ProposalRepository {
Future<Result<List<ProposalModel>, HyphaError>> getProposals(UserProfileData user, GetProposalsUseCaseInput input) async {
final List<Future<Result<Map<String, dynamic>, HyphaError>>> futures = input.daos.map((DaoData dao) {
return input.filterStatus == FilterStatus.active
? _proposalService.getActiveProposals(user, dao.docId)
: _proposalService.getPastProposals(user, dao.docId);
? _proposalService.getActiveProposals(user, dao.docId, input.first, input.offset)
: _proposalService.getPastProposals(user, dao.docId, input.first, input.offset);
}).toList();

final List<Result<Map<String, dynamic>, HyphaError>> futureResults = await Future.wait(futures);
Expand Down Expand Up @@ -67,19 +68,23 @@ class ProposalRepository {
}
}

sortProposals(allProposals);
return Result.value(allProposals);
}

Future<List<ProposalModel>> _parseProposalsFromResponse(
Map<String, dynamic> response,
DaoData daoData,
FilterStatus filterStatus) async {
Future<List<ProposalModel>> _parseProposalsFromResponse(Map<String, dynamic> response, DaoData daoData, FilterStatus filterStatus) async {
final List<dynamic> proposalsData = response['data']['queryDao'];

final List<Future<ProposalModel>> proposalFutures = proposalsData.expand((dao) {
final List<dynamic> proposals = dao[filterStatus == FilterStatus.active ? 'proposal' : 'votable'] as List<dynamic>;
return proposals.map((dynamic proposal) async {

return proposals.where((dynamic proposal) {
if (filterStatus == FilterStatus.past) {
final Map<String, dynamic> proposalMap = Map<String, dynamic>.from(proposal);
return proposalMap.isValidProposal();
}

return true;
}).map((dynamic proposal) async {
final Result<ProfileData, HyphaError> creator = await _profileService.getProfile(proposal['creator']);
proposal['creator'] = null;

Expand All @@ -96,27 +101,6 @@ class ProposalRepository {
return Future.wait(proposalFutures);
}

void sortProposals(List<ProposalModel> proposals,) {
proposals.sort((a, b) {
final int daoNameComparison = (a.dao?.settingsDaoTitle ?? '')
.compareTo(b.dao?.settingsDaoTitle ?? '');
if (daoNameComparison != 0) {
return daoNameComparison;
}

final bool isAExpired = a.expiration != null && a.isExpired();
final bool isBExpired = b.expiration != null && b.isExpired();

if (isAExpired && !isBExpired) {
return 1;
} else if (!isAExpired && isBExpired) {
return -1;
}

return a.expiration?.compareTo(b.expiration ?? DateTime.now()) ?? 0;
});
}

Future<Result<ProposalDetailsModel, HyphaError>> getProposalDetails(String proposalId, UserProfileData user) async {
final Result<Map<String, dynamic>, HyphaError> result = await _proposalService.getProposalDetails(proposalId, user);

Expand Down
Loading