Skip to content

Commit

Permalink
Merge pull request #312 from hypha-dao/chore/implement-dao-and-creato…
Browse files Browse the repository at this point in the history
…r-image

refactor: display dao and creator images
  • Loading branch information
Zied-Dahmani authored Sep 17, 2024
2 parents 1269fd7 + eb3bcf4 commit 5879997
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 81 deletions.
2 changes: 1 addition & 1 deletion lib/core/network/api/services/proposal_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ProposalService {
const ProposalService(this._graphQLService);

Future<Result<Map<String, dynamic>, HyphaError>> getProposals(UserProfileData user, int daoId) async {
final String query = '{"query":"query Proposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { details_daoName_n proposal { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Budget { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Policy { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Circle { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Payout { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Role { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Badge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Suspend { details_title_s details_ballotAlignment_i 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_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } } } } }","variables":{"docId":"$daoId"}}';
final String query = '{"query":"query Proposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { proposal { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Budget { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Policy { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Circle { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Payout { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Role { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Badge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Suspend { details_title_s details_ballotAlignment_i 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_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } } } } }","variables":{"docId":"$daoId"}}';

return _graphQLService.graphQLQuery(network: user.network, query: query);
}
Expand Down
10 changes: 6 additions & 4 deletions lib/core/network/models/base_proposal_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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';
import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
Expand All @@ -7,7 +9,7 @@ abstract class BaseProposalModel {
final String id;

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

@JsonKey(name: 'details_timeShareX100_i')
final int? commitment;
Expand All @@ -25,20 +27,20 @@ abstract class BaseProposalModel {
final DateTime? expiration;

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

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

BaseProposalModel({
required this.id,
this.daoName,
this.dao,
this.commitment,
this.title,
this.unity,
this.quorum,
this.expiration,
required this.creator,
this.creator,
this.votes,
});
}
2 changes: 2 additions & 0 deletions lib/core/network/models/dao_data_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ class DaoData {
settingsDaoUrl: settings['settings_daoUrl_s'] ?? '',
);
}

Map<String, dynamic> toJson() => {};
}
14 changes: 8 additions & 6 deletions lib/core/network/models/proposal_details_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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';
import 'package:hypha_wallet/ui/profile/interactor/profile_data.dart';
import 'package:json_annotation/json_annotation.dart';

part 'proposal_details_model.g.dart';
Expand Down Expand Up @@ -44,15 +46,15 @@ class ProposalDetailsModel extends BaseProposalModel {

ProposalDetailsModel({
required super.id,
required super.creator,
required this.type,
required this.creationDate,
super.daoName,
super.dao,
super.commitment,
super.title,
super.unity,
super.quorum,
super.expiration,
super.creator,
super.votes,
this.tokenMixPercentage,
this.cycleCount,
Expand All @@ -67,8 +69,6 @@ class ProposalDetailsModel extends BaseProposalModel {
});

factory ProposalDetailsModel.fromJson(Map<String, dynamic> json) {
json['pass'] = json['pass']['count'];
json['fail'] = json['fail']['count'];
if(json['start'] is List){
if((json['start'] as List).isNotEmpty){
json['start'] = json['start'][0]['details_startTime_t'];
Expand All @@ -78,9 +78,11 @@ class ProposalDetailsModel extends BaseProposalModel {
}
}
// TODO(Saif): check this
if(json['dao'] != null) {
json['dao'] = null;
json['creator'] = null;
/*if(json['dao'] != null) {
json['dao'] = json['dao'][0]['settings'][0]['settings_daoTitle_s'];
}
}*/
return _$ProposalDetailsModelFromJson(json);
}

Expand Down
10 changes: 7 additions & 3 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.

9 changes: 6 additions & 3 deletions lib/core/network/models/proposal_model.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import 'package:freezed_annotation/freezed_annotation.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';
import 'package:json_annotation/json_annotation.dart';
import 'package:hypha_wallet/ui/profile/interactor/profile_data.dart';

part 'proposal_model.g.dart';

@JsonSerializable()
class ProposalModel extends BaseProposalModel{
ProposalModel({
required super.id,
required super.creator,
super.daoName,
super.dao,
super.commitment,
super.title,
super.unity,
super.quorum,
super.expiration,
super.creator,
super.votes,
});

factory ProposalModel.fromJson(Map<String, dynamic> json) {
if (json.containsKey('original')) {
json['details_title_s'] = json['original'][0]['details_title_s'];
Expand Down
10 changes: 7 additions & 3 deletions lib/core/network/models/proposal_model.g.dart

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

3 changes: 1 addition & 2 deletions lib/core/network/repository/profile_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class ProfileService {
.get(url);
if (response.statusCode == 200) {
final map = Map<String, dynamic>.from(response.data);
return Result.value(ProfileData.fromJson(
map, user is UserProfileData ? user.network : Network.telos, []));
return Result.value(ProfileData.fromJson(map,network: user is UserProfileData ? user.network : Network.telos));
} else {
LogHelper.i('get profile error status code: ${response.statusMessage}');
return Result.error(HyphaError(
Expand Down
28 changes: 20 additions & 8 deletions lib/core/network/repository/proposal_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ProposalRepository {

final List<ProposalModel> allProposals = [];

for (final Result<Map<String, dynamic>, HyphaError> result in futureResults) {
for (int i = 0; i < futureResults.length; i++) {
final Result<Map<String, dynamic>, HyphaError> result = futureResults[i];

if (result.isValue) {
final Map<String, dynamic> response = result.asValue!.value;
Expand All @@ -36,7 +37,7 @@ class ProposalRepository {
}

try {
final List<ProposalModel> proposals = _parseProposalsFromResponse(response);
final List<ProposalModel> proposals = await _parseProposalsFromResponse(response, daos[i]);
allProposals.addAll(proposals);
} catch (e, stackTrace) {
LogHelper.e('Error parsing data into proposal model', error: e, stacktrace: stackTrace);
Expand All @@ -52,20 +53,31 @@ class ProposalRepository {
return Result.value(allProposals);
}

List<ProposalModel> _parseProposalsFromResponse(Map<String, dynamic> response) {
Future<List<ProposalModel>> _parseProposalsFromResponse(Map<String, dynamic> response, DaoData daoData) async {
final List<dynamic> proposalsData = response['data']['queryDao'];
return proposalsData.expand((dao) {
final String daoName = dao['details_daoName_n'];

final List<Future<ProposalModel>> proposalFutures = proposalsData.expand((dao) {
final List<dynamic> proposals = dao['proposal'] as List<dynamic>;
return proposals.map((dynamic proposal) {
return ProposalModel.fromJson({...{'dao': daoName}, ...proposal});
return proposals.map((dynamic proposal) async {
final Result<ProfileData, HyphaError> creator = await _profileService.getProfile(proposal['creator']);
proposal['creator'] = null;

final ProposalModel proposalModel = ProposalModel.fromJson(proposal);
if (creator.isValue) {
proposalModel.creator = creator.asValue!.value;
}
proposalModel.dao = daoData;

return proposalModel;
});
}).toList();

return Future.wait(proposalFutures);
}

void sortProposals(List<ProposalModel> proposals) {
proposals.sort((a, b) {
final int daoNameComparison = (a.daoName ?? '').compareTo(b.daoName ?? '');
final int daoNameComparison = (a.dao?.settingsDaoTitle ?? '').compareTo(b.dao?.settingsDaoTitle ?? '');
if (daoNameComparison != 0) {
return daoNameComparison;
}
Expand Down
1 change: 1 addition & 0 deletions lib/design/avatar_image/hypha_avatar_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class HyphaAvatarImage extends StatelessWidget {
} else if (name != null) {
image = _buildFallbackAvatar(context);
} else {
// TODO(Zied): change the icon
image = Icon(HyphaIcons.image, size: imageRadius, color: context.textTheme.titleSmall?.color);
}

Expand Down
9 changes: 7 additions & 2 deletions lib/ui/profile/interactor/profile_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ class ProfileData {
required this.daos,
});

factory ProfileData.fromJson(Map<String, dynamic> json, Network network, List<DaoData> daos) {
final account = json['eosAccount'];
factory ProfileData.fromJson(
Map<String, dynamic> json, {
Network network = Network.telos,
List<DaoData> daos = const [],
}) { final account = json['eosAccount'];
final avatarUrl = json['avatarUrl'];
final publicData = json['publicData'];
final name = publicData['name'];
Expand All @@ -49,6 +52,8 @@ class ProfileData {
);
}

Map<String, dynamic> toJson() => {};

ProfileData updateBio(String? bio) => ProfileData(
name: name,
account: account,
Expand Down
10 changes: 5 additions & 5 deletions lib/ui/proposals/components/proposal_creator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import 'package:flutter/material.dart';
import 'package:hypha_wallet/design/avatar_image/hypha_avatar_image.dart';
import 'package:hypha_wallet/design/hypha_colors.dart';
import 'package:hypha_wallet/design/themes/extensions/theme_extension_provider.dart';
import 'package:hypha_wallet/ui/profile/interactor/profile_data.dart';

class ProposalCreator extends StatelessWidget {
final String creatorName;
final String creatorImageUrl;
const ProposalCreator(this.creatorName, this.creatorImageUrl, {super.key});
final ProfileData? creator;
const ProposalCreator(this.creator, {super.key});

@override
Widget build(BuildContext context) {
return Row(
children: [
HyphaAvatarImage(
imageRadius: 24,
imageFromUrl: creatorImageUrl,
imageFromUrl: creator?.avatarUrl,
),
const SizedBox(width: 10),
Expanded(
child: Text(
creatorName,
creator?.name ?? '',
style: context.hyphaTextTheme.ralMediumSmallNote.copyWith(color: HyphaColors.midGrey),
),
),
Expand Down
31 changes: 20 additions & 11 deletions lib/ui/proposals/components/proposal_header.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import 'package:flutter/material.dart';
import 'package:hypha_wallet/design/avatar_image/hypha_avatar_image.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/design/ipfs_image.dart';
import 'package:hypha_wallet/design/themes/extensions/theme_extension_provider.dart';

class ProposalHeader extends StatelessWidget {
final String daoName;
final String daoImageUrl;

const ProposalHeader(this.daoName, this.daoImageUrl, {super.key});
final DaoData? _daoData;
const ProposalHeader(this._daoData, {super.key});

@override
Widget build(BuildContext context) {
return Row(
children: [
HyphaAvatarImage(
imageRadius: 20,
name: '',
imageFromUrl: daoImageUrl,
Container(
width: 48,
height: 48,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color:
Colors.white,
),
child: ClipOval(
child: IpfsImage(
ipfsHash: _daoData?.logoIPFSHash ?? '',
type: _daoData?.logoType ?? '',
),
),
),
const SizedBox(width: 4),
const SizedBox(width: 10),
Flexible(
child: Text(
daoName,
_daoData?.settingsDaoTitle ?? '',
style: context.hyphaTextTheme.ralMediumSmallNote.copyWith(fontWeight: FontWeight.bold),
overflow: TextOverflow.ellipsis,
),
Expand Down
Loading

0 comments on commit 5879997

Please sign in to comment.