Skip to content

Commit

Permalink
refactor: add extends InputUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Zied-Dahmani committed Sep 24, 2024
1 parent 095ac10 commit d74922d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/core/network/repository/proposal_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import 'package:hypha_wallet/core/network/repository/profile_repository.dart';
import 'package:hypha_wallet/ui/architecture/result/result.dart';
import 'package:hypha_wallet/ui/profile/interactor/profile_data.dart';
import 'package:hypha_wallet/ui/proposals/filter/interactor/filter_status.dart';
import 'package:hypha_wallet/ui/proposals/list/interactor/get_proposals_use_case_input.dart';

class ProposalRepository {
final ProposalService _proposalService;
final ProfileService _profileService;

ProposalRepository(this._proposalService, this._profileService);

Future<Result<List<ProposalModel>, HyphaError>> getProposals(UserProfileData user, List<DaoData> daos, FilterStatus filterStatus) async {
final List<Future<Result<Map<String, dynamic>, HyphaError>>> futures = daos.map((DaoData dao) {
return filterStatus == FilterStatus.active ? _proposalService.getActiveProposals(user, dao.docId) : _proposalService.getPastProposals(user, dao.docId);
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);
}).toList();

final List<Result<Map<String, dynamic>, HyphaError>> futureResults = await Future.wait(futures);
Expand All @@ -38,7 +39,7 @@ class ProposalRepository {
}

try {
final List<ProposalModel> proposals = await _parseProposalsFromResponse(response, daos[i], filterStatus);
final List<ProposalModel> proposals = await _parseProposalsFromResponse(response, input.daos[i], input.filterStatus);
allProposals.addAll(proposals);
} catch (e, stackTrace) {
LogHelper.e('Error parsing data into proposal model', error: e, stacktrace: stackTrace);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/ui/proposals/filter/interactor/filter_status.dart';

class GetProposalsUseCaseInput {
final List<DaoData> daos;
final FilterStatus filterStatus;

GetProposalsUseCaseInput(this.daos, this.filterStatus);
}
3 changes: 2 additions & 1 deletion lib/ui/proposals/list/interactor/proposals_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:hypha_wallet/ui/architecture/result/result.dart';
import 'package:hypha_wallet/ui/profile/interactor/profile_data.dart';
import 'package:hypha_wallet/ui/profile/usecases/fetch_profile_use_case.dart';
import 'package:hypha_wallet/ui/proposals/filter/interactor/filter_status.dart';
import 'package:hypha_wallet/ui/proposals/list/interactor/get_proposals_use_case_input.dart';
import 'package:hypha_wallet/ui/proposals/list/usecases/get_proposals_use_case.dart';

part 'proposals_bloc.freezed.dart';
Expand Down Expand Up @@ -49,7 +50,7 @@ class ProposalsBloc extends Bloc<ProposalsEvent, ProposalsState> {
}

Future<void> _fetchAndEmitProposals(Emitter<ProposalsState> emit, List<DaoData> daos, FilterStatus filterStatus) async {
final Result<List<ProposalModel>, HyphaError> proposalsResult = await _getProposalsUseCase.run(daos, filterStatus);
final Result<List<ProposalModel>, HyphaError> proposalsResult = await _getProposalsUseCase.run(GetProposalsUseCaseInput(daos, filterStatus));

if (proposalsResult.isValue) {
emit(state.copyWith(pageState: PageState.success, proposals: proposalsResult.asValue!.value));
Expand Down
10 changes: 5 additions & 5 deletions lib/ui/proposals/list/usecases/get_proposals_use_case.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:hypha_wallet/core/error_handler/model/hypha_error.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/core/network/models/proposal_model.dart';
import 'package:hypha_wallet/core/network/repository/auth_repository.dart';
import 'package:hypha_wallet/core/network/repository/proposal_repository.dart';
import 'package:hypha_wallet/ui/architecture/interactor/base_usecase.dart';
import 'package:hypha_wallet/ui/architecture/result/result.dart';
import 'package:hypha_wallet/ui/proposals/filter/interactor/filter_status.dart';
import 'package:hypha_wallet/ui/proposals/list/interactor/get_proposals_use_case_input.dart';

// TODO(Zied): add 'extends' (check)
class GetProposalsUseCase {
class GetProposalsUseCase extends InputUseCase<Result<List<ProposalModel>, HyphaError>, GetProposalsUseCaseInput> {
final AuthRepository _authRepository;
final ProposalRepository _proposalRepository;

GetProposalsUseCase(this._authRepository, this._proposalRepository);

Future<Result<List<ProposalModel>, HyphaError>> run(List<DaoData> daos, FilterStatus filterStatus) async => _proposalRepository.getProposals(_authRepository.authDataOrCrash.userProfileData, daos, filterStatus);
@override
Future<Result<List<ProposalModel>, HyphaError>> run(GetProposalsUseCaseInput input) async => _proposalRepository.getProposals(_authRepository.authDataOrCrash.userProfileData, input);
}

0 comments on commit d74922d

Please sign in to comment.