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

refactor: merge proposal details UI with business logic #307

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions lib/core/di/di_setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import 'package:hypha_wallet/ui/profile/usecases/remove_avatar_use_case.dart';
import 'package:hypha_wallet/ui/profile/usecases/set_bio_use_case.dart';
import 'package:hypha_wallet/ui/profile/usecases/set_image_use_case.dart';
import 'package:hypha_wallet/ui/profile/usecases/set_name_use_case.dart';
import 'package:hypha_wallet/ui/proposals/details/usecases/get_proposal_details_use_case.dart';
import 'package:hypha_wallet/ui/proposals/list/interactor/proposals_bloc.dart';
import 'package:hypha_wallet/ui/proposals/list/usecases/get_proposals_use_case.dart';
import 'package:hypha_wallet/ui/search_user/interactor/search_user_bloc.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/core/di/usecases_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ void _registerUseCasesModule() {
_registerFactory(() => GetDaoNameUseCase(_getIt<DaoService>()));

_registerFactory(() => GetProposalsUseCase(_getIt<AuthRepository>(), _getIt<ProposalRepository>()));

_registerFactory(() => GetProposalDetailsUseCase(_getIt<AuthRepository>(), _getIt<ProposalRepository>()));
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:hypha_wallet/core/network/models/proposal_model.dart';
import 'package:hypha_wallet/core/network/models/base_proposal_model.dart';

extension ProposalModelTimeFormatting on ProposalModel {
extension BaseProposalModelExtension on BaseProposalModel {
String formatExpiration() {
if (expiration == null) return 'Expired';

Expand Down Expand Up @@ -32,6 +32,5 @@ extension ProposalModelTimeFormatting on ProposalModel {
bool isExpired() => expiration!.toLocal().isBefore(DateTime.now());
// TODO(saif): Replace hardcoded values (.2 and .8) with dynamic values fetched from the server.
// These thresholds are relative to each DAO and should be retrieved from the DAO settings.
bool isPassing()=>quorumToPercent()>=.2 && unityToPercent()>=.8;
bool isPassing() => quorumToPercent() >= .2 && unityToPercent() >= .8;
}

65 changes: 65 additions & 0 deletions lib/core/extension/proposal_details_model_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:hypha_wallet/core/network/models/proposal_details_model.dart';
import 'package:intl/intl.dart';

extension ProposalDetailsModelExtension on ProposalDetailsModel {
double tokenMixToPercent() => tokenMixPercentage == null ? 0 : tokenMixPercentage! * .01;

String formatCycleStartDate() => cycleStartDate != null ? DateFormat('EEEE, MMMM yyyy').format(cycleStartDate!) : '';

String cycleEndDate() => DateFormat('EEEE, MMMM yyyy').format(cycleStartDate!.add(Duration(days: cycleCount! * 7)));

String? tokenTitle(int index) {
String input;

switch (index) {
case 0:
input = utilityAmount ?? utilityAmountPerPeriod!;
break;
case 1:
input = voiceAmount ?? voiceAmountPerPeriod!;
break;
case 2:
input = cashAmount ?? cashAmountPerPeriod!;
break;
default:
return null;
}

final RegExp regExp = RegExp(r'\d+\s+(.*)');
final match = regExp.firstMatch(input);

if (match != null) {
String result = match.group(1)!;

if (result.isNotEmpty && index != 0) {
result = result[0].toLowerCase() + result.substring(1);
}
return result;
}
return null;
}

// TODO(Saif): adjust this function
String? tokenValue(int index, bool isOneCycleRewardsShown) {
String input;

switch (index) {
case 0:
input = utilityAmount ?? utilityAmountPerPeriod!;
break;
case 1:
input = voiceAmount ?? voiceAmountPerPeriod!;
break;
case 2:
input = cashAmount ?? cashAmountPerPeriod!;
break;
default:
return null;
}

final RegExp regExp = RegExp(r'(\S+)\s');
final match = regExp.firstMatch(input);

return match?.group(1);
}
}
44 changes: 44 additions & 0 deletions lib/core/network/models/base_proposal_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:hypha_wallet/core/network/models/vote_model.dart';
import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
abstract class BaseProposalModel {
@JsonKey(name: 'docId')
final String id;

@JsonKey(name: 'dao')
final String? daoName;

@JsonKey(name: 'details_timeShareX100_i')
final int? commitment;

@JsonKey(name: 'details_title_s')
final String? title;

@JsonKey(name: 'details_ballotAlignment_i')
final int? unity;

@JsonKey(name: 'details_ballotQuorum_i')
final int? quorum;

@JsonKey(name: 'ballot_expiration_t')
final DateTime? expiration;

@JsonKey(name: 'creator')
final String creator;

@JsonKey(name: 'vote')
final List<VoteModel>? votes;

BaseProposalModel({
required this.id,
this.daoName,
this.commitment,
this.title,
this.unity,
this.quorum,
this.expiration,
required this.creator,
this.votes,
});
}
87 changes: 32 additions & 55 deletions lib/core/network/models/proposal_details_model.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import 'package:hypha_wallet/core/network/models/base_proposal_model.dart';
import 'package:hypha_wallet/core/network/models/vote_model.dart';
import 'package:json_annotation/json_annotation.dart';

part 'proposal_details_model.g.dart';

@JsonSerializable()
class ProposalDetailsModel {
@JsonKey(name: 'docId')
final String id;

@JsonKey(name: 'creator')
final String creator;

class ProposalDetailsModel extends BaseProposalModel {
@JsonKey(name: '__typename')
final String type;

Expand All @@ -26,73 +21,49 @@ class ProposalDetailsModel {
@JsonKey(name: 'start')
final DateTime? cycleStartDate;

@JsonKey(name: 'details_pegAmount_a')
@JsonKey(name: 'details_rewardAmount_a')
final String? utilityAmount;

@JsonKey(name: 'details_voiceAmount_a')
final String? voiceAmount;

@JsonKey(name: 'details_rewardAmount_a')
@JsonKey(name: 'details_pegAmount_a')
final String? cashAmount;

@JsonKey(name: 'details_pegSalaryPerPeriod_a')
@JsonKey(name: 'details_rewardSalaryPerPeriod_a')
final String? utilityAmountPerPeriod;

@JsonKey(name: 'details_voiceSalaryPerPeriod_a')
final String? voiceAmountPerPeriod;

@JsonKey(name: 'details_rewardSalaryPerPeriod_a')
@JsonKey(name: 'details_pegSalaryPerPeriod_a')
final String? cashAmountPerPeriod;

@JsonKey(name: 'ballot_expiration_t')
final DateTime? expiration;

@JsonKey(name: 'details_timeShareX100_i')
final int? commitment;

final int? unity;
@JsonKey(name: 'details_ballotQuorum_i')
final int? quorum;
@JsonKey(name: 'details_title_s')
final String? title;

@JsonKey(name: 'dao')
final String? daoName;
@JsonKey(name: 'details_description_s')
final String? description;

@JsonKey(name: 'pass')
final int? votedYesCount;
@JsonKey(name: 'fail')
final int? votedNoCount;

@JsonKey(name: 'vote')
final List<VoteModel>? voters;

ProposalDetailsModel(
this.creator,
this.type,
this.creationDate,
this.utilityAmountPerPeriod,
this.voiceAmountPerPeriod,
this.cashAmountPerPeriod,
this.unity,
this.quorum,
this.voters,
this.daoName,
this.votedYesCount,
this.votedNoCount, {
required this.id,
this.title,
this.commitment,
ProposalDetailsModel({
required super.id,
required super.creator,
required this.type,
required this.creationDate,
super.daoName,
super.commitment,
super.title,
super.unity,
super.quorum,
super.expiration,
super.votes,
this.tokenMixPercentage,
this.cycleCount,
this.cashAmount,
this.cycleStartDate,
this.voiceAmount,
this.utilityAmount,
this.description,
this.expiration,
this.voiceAmount,
this.cashAmount,
this.utilityAmountPerPeriod,
this.voiceAmountPerPeriod,
this.cashAmountPerPeriod,
this.description
});

factory ProposalDetailsModel.fromJson(Map<String, dynamic> json) {
Expand All @@ -106,9 +77,15 @@ class ProposalDetailsModel {
json['start']=null;
}
}
json['dao'] = json['dao'][0]['settings'][0]['settings_daoTitle_s'];
// TODO(Saif): check this
if(json['dao'] != null) {
json['dao'] = json['dao'][0]['settings'][0]['settings_daoTitle_s'];
}
return _$ProposalDetailsModelFromJson(json);
}

Map<String, dynamic> toJson() => _$ProposalDetailsModelToJson(this);
List<VoteModel> fetchVotersByStatus(VoteStatus voteStatus) => votes
?.where((vote) => vote.voteStatus == voteStatus)
.map((vote) => vote)
.toList() ?? [];
}
63 changes: 30 additions & 33 deletions lib/core/network/models/proposal_details_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading