Skip to content

Commit

Permalink
fix: filter proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
Zied-Dahmani committed Sep 26, 2024
1 parent 095ac10 commit 5bf5e32
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 122 deletions.
1 change: 0 additions & 1 deletion lib/core/di/bloc_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ void _registerBlocsModule() {
_getIt<ProposalsBloc>(),
_getIt<FetchProfileUseCase>(),
_getIt<AggregateDaoProposalCountsUseCase>(),
_getIt<GetDaosFromProposalCountsUseCase>(),
_getIt<ErrorHandlerManager>(),
));
}
7 changes: 7 additions & 0 deletions lib/core/extension/proposals_filter_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:hypha_wallet/core/network/models/proposal_model.dart';

extension ProposalFilterExtension on List<ProposalModel> {
List<ProposalModel> filterByDao(List<int> daoIds) {
return where((proposal) => daoIds.contains(proposal.dao?.docId)).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class FilterProposalsView extends StatelessWidget {
HyphaAppButton(
title: 'SAVE FILTERS',
onPressed: () {
filterProposalsBloc.add(FilterProposalsEvent.saveFilters(filterProposalsBloc.selectedDaoIndexNotifier.value == null ? state.daoProposalCounts: [state.daoProposalCounts[filterProposalsBloc.selectedDaoIndexNotifier.value!]], filterProposalsBloc.selectedStatusIndexNotifier.value == 0 ? FilterStatus.active : FilterStatus.past));
final int? index = filterProposalsBloc.selectedDaoIndexNotifier.value;
filterProposalsBloc.add(FilterProposalsEvent.saveFilters(index == null ? state.daoProposalCounts : [state.daoProposalCounts[index]], filterProposalsBloc.selectedStatusIndexNotifier.value == 0 ? FilterStatus.active : FilterStatus.past));
},
),
const SizedBox(
Expand Down
6 changes: 2 additions & 4 deletions lib/ui/proposals/filter/components/hypha_filter_card.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'package:flutter/material.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/design/avatar_image/hypha_avatar_image.dart';
import 'package:hypha_wallet/design/dao_image.dart';
import 'package:hypha_wallet/design/hypha_card.dart';
import 'package:hypha_wallet/design/hypha_colors.dart';
import 'package:hypha_wallet/design/ipfs_image.dart';
import 'package:hypha_wallet/design/themes/extensions/theme_extension_provider.dart';
import 'package:hypha_wallet/design/dao_image.dart';

class HyphaFilterCard extends StatelessWidget {
final DaoData? dao;
Expand All @@ -21,7 +19,7 @@ class HyphaFilterCard extends StatelessWidget {
// TODO(Saif): fix the card height (filter by status)
return GestureDetector(
onTap: () {
valueNotifier.value = index;
valueNotifier.value = valueNotifier.value == index ? null : index;
},
child: HyphaCard(
child: Padding(
Expand Down
10 changes: 5 additions & 5 deletions lib/ui/proposals/filter/interactor/filter_proposals_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:hypha_wallet/ui/profile/usecases/fetch_profile_use_case.dart';
import 'package:hypha_wallet/ui/proposals/filter/interactor/dao_proposal_count_entity.dart';
import 'package:hypha_wallet/ui/proposals/filter/interactor/filter_status.dart';
import 'package:hypha_wallet/ui/proposals/filter/usecases/aggregate_dao_proposal_counts_use_case.dart';
import 'package:hypha_wallet/ui/proposals/filter/usecases/get_daos_from_proposal_counts_use_case.dart';
import 'package:hypha_wallet/ui/proposals/list/interactor/proposals_bloc.dart';

part 'page_command.dart';
Expand All @@ -23,14 +22,12 @@ class FilterProposalsBloc extends Bloc<FilterProposalsEvent, FilterProposalsStat
final ProposalsBloc _proposalsBloc;
final FetchProfileUseCase _fetchProfileUseCase;
final AggregateDaoProposalCountsUseCase _aggregateDaoProposalCountsUseCase;
final GetDaosFromProposalCountsUseCase _getDaosFromProposalCountsUseCase;
final ErrorHandlerManager _errorHandlerManager;

FilterProposalsBloc(
this._proposalsBloc,
this._fetchProfileUseCase,
this._aggregateDaoProposalCountsUseCase,
this._getDaosFromProposalCountsUseCase,
this._errorHandlerManager,
) : super(const FilterProposalsState()) {
on<_Initial>(_initial);
Expand All @@ -40,11 +37,13 @@ class FilterProposalsBloc extends Bloc<FilterProposalsEvent, FilterProposalsStat
add(const FilterProposalsEvent.initial());
}

final ValueNotifier<int?> _selectedDaoIndexNotifier = ValueNotifier<int?>(null);
final ValueNotifier<int?> _selectedDaoIndexNotifier = ValueNotifier<int?>(0);
final ValueNotifier<int> _selectedStatusIndexNotifier = ValueNotifier<int>(0);
List<int>? _daoIds;

ValueNotifier<int?> get selectedDaoIndexNotifier => _selectedDaoIndexNotifier;
ValueNotifier<int> get selectedStatusIndexNotifier => _selectedStatusIndexNotifier;
List<int>? get daoIds => _daoIds;

Future<void> _initial(_Initial event, Emitter<FilterProposalsState> emit) async {
emit(state.copyWith(pageState: PageState.loading));
Expand Down Expand Up @@ -76,7 +75,8 @@ class FilterProposalsBloc extends Bloc<FilterProposalsEvent, FilterProposalsStat
}

Future<void> _saveFilters(_SaveFilters event, Emitter<FilterProposalsState> emit) async {
_proposalsBloc.add(ProposalsEvent.initial(daos: _getDaosFromProposalCountsUseCase.run(event.daoProposalCounts), filterStatus: event.filterStatus));
_daoIds = event.daoProposalCounts.map((DaoProposalCountEntity daoProposalCount) => daoProposalCount.dao.docId).toList();
_proposalsBloc.add(ProposalsEvent.initial(filterStatus: event.filterStatus));
emit(state.copyWith(command: const PageCommand.navigateToProposals()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class AggregateDaoProposalCountsUseCase {
}
}

return daoProposalCounts.entries
.map((entry) => DaoProposalCountEntity(entry.key, entry.value))
.toList();
final List<MapEntry<DaoData, int>> sortedEntries = daoProposalCounts.entries.toList()
..sort((a, b) => a.key.settingsDaoTitle.compareTo(b.key.settingsDaoTitle));

return sortedEntries.map((entry) => DaoProposalCountEntity(entry.key, entry.value)).toList();
}
}

This file was deleted.

67 changes: 37 additions & 30 deletions lib/ui/proposals/list/components/proposals_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart' as GetX;
import 'package:get_it/get_it.dart';
import 'package:hypha_wallet/core/extension/proposals_filter_extension.dart';
import 'package:hypha_wallet/core/network/models/proposal_model.dart';
import 'package:hypha_wallet/design/avatar_image/hypha_avatar_image.dart';
import 'package:hypha_wallet/design/background/hypha_page_background.dart';
import 'package:hypha_wallet/design/hypha_colors.dart';
Expand Down Expand Up @@ -84,36 +87,40 @@ class ProposalsView extends StatelessWidget {
),
child: HyphaBodyWidget(
pageState: state.pageState,
success: (context) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 22),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 22,
),
Text(
'${state.proposals.length} ${context.read<ProposalsBloc>().filterStatus.string} Proposal${state.proposals.length == 1 ? '' : 's'}',
style: context.hyphaTextTheme.ralMediumBody
.copyWith(color: HyphaColors.midGrey),
),
const SizedBox(
height: 20,
),
Expanded(
child: ListView.separated(
padding: const EdgeInsets.only(bottom: 22),
itemBuilder: (BuildContext context,
int index) =>
HyphaProposalsActionCard(state.proposals[index]),
separatorBuilder:
(BuildContext context, int index) {
return const SizedBox(height: 16);
},
itemCount: state.proposals.length)),
],
),
),
success: (context) {
final List<int>? daoIds = GetIt.I.get<FilterProposalsBloc>().daoIds;
final List<ProposalModel> proposals = daoIds != null ? state.proposals.filterByDao(daoIds) : state.proposals;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 22),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 22,
),
Text(
'${proposals.length} ${context.read<ProposalsBloc>().filterStatus.string} Proposal${proposals.length == 1 ? '' : 's'}',
style: context.hyphaTextTheme.ralMediumBody
.copyWith(color: HyphaColors.midGrey),
),
const SizedBox(
height: 20,
),
Expanded(
child: ListView.separated(
padding: const EdgeInsets.only(bottom: 22),
itemBuilder: (BuildContext context,
int index) =>
HyphaProposalsActionCard(proposals[index]),
separatorBuilder:
(BuildContext context, int index) {
return const SizedBox(height: 16);
},
itemCount: proposals.length)),
],
),
);
},
),
)),
floatingActionButton: IconButton(
Expand Down
25 changes: 13 additions & 12 deletions lib/ui/proposals/list/interactor/proposals_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProposalsBloc extends Bloc<ProposalsEvent, ProposalsState> {
on<_Initial>(_initial);
}

List<DaoData>? _daos;
FilterStatus filterStatus = FilterStatus.active;

Future<void> _initial(_Initial event, Emitter<ProposalsState> emit) async {
Expand All @@ -32,19 +33,19 @@ class ProposalsBloc extends Bloc<ProposalsEvent, ProposalsState> {
filterStatus = event.filterStatus;
}

if (event.daos != null) {
await _fetchAndEmitProposals(emit, event.daos!, filterStatus);
return;
}

final Result<ProfileData, HyphaError> profileResult = await _fetchProfileUseCase.run();

if (profileResult.isValue && profileResult.asValue!.value.daos.isNotEmpty) {
await _fetchAndEmitProposals(emit, profileResult.asValue!.value.daos, filterStatus);
if (_daos == null) {
final Result<ProfileData, HyphaError> profileResult = await _fetchProfileUseCase.run();

if (profileResult.isValue && profileResult.asValue!.value.daos.isNotEmpty) {
_daos = profileResult.asValue!.value.daos;
await _fetchAndEmitProposals(emit, _daos!, filterStatus);
} else {
final HyphaError error = profileResult.isError ? profileResult.asError!.error : HyphaError.api('Failed to retrieve DAOs');
await _errorHandlerManager.handlerError(error);
emit(state.copyWith(pageState: PageState.failure));
}
} else {
final HyphaError error = profileResult.isError ? profileResult.asError!.error : HyphaError.api('Failed to retrieve DAOs');
await _errorHandlerManager.handlerError(error);
emit(state.copyWith(pageState: PageState.failure));
await _fetchAndEmitProposals(emit, _daos!, filterStatus);
}
}

Expand Down
Loading

0 comments on commit 5bf5e32

Please sign in to comment.