Skip to content

Commit

Permalink
Merge pull request #302 from CollActionteam/fix/1.2.0-fixes
Browse files Browse the repository at this point in the history
Fix/1.2.0 fixes
  • Loading branch information
Xazin committed Oct 26, 2022
2 parents 410bfe5 + 5ca989c commit f13be80
Show file tree
Hide file tree
Showing 46 changed files with 1,274 additions and 1,024 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/// TODO: Remove all old code related to GraphQL!!!
import 'package:bloc/bloc.dart';
import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';

import '../../../domain/core/page_info.dart';
import '../../../domain/crowdaction/crowdaction.dart';
import '../../../domain/crowdaction/crowdaction_failures.dart';
import '../../../domain/crowdaction/i_crowdaction_repository.dart';
Expand All @@ -19,36 +18,45 @@ class CrowdActionGetterBloc

CrowdActionGetterBloc(this._crowdActionRepository)
: super(const CrowdActionGetterState.initial()) {
on<CrowdActionGetterEvent>(
(event, emit) async {
await event.map(
getMore: (event) async {
emit(const CrowdActionGetterState.fetchingCrowdActions());
try {
Either<CrowdActionFailure, List<CrowdAction>> response;
if (event.amount != null && event.amount! > 0) {
response = await _crowdActionRepository.getCrowdActions(
amount: event.amount!,
on<CrowdActionGetterEvent>((event, emit) async {
await event.map(
init: (_) async {
emit(const CrowdActionGetterState.loading());
emit(const CrowdActionGetterState.initial());
},
getCrowdActions: (event) async {
emit(const CrowdActionGetterState.loading());

final paginatedCrowdActionsOrFailure =
await _crowdActionRepository.getCrowdActions(
pageNumber: event.pageNumber,
);

paginatedCrowdActionsOrFailure.fold(
(failure) => emit(
CrowdActionGetterState.failure(failure),
),
(paginatedCrowdActions) {
if (paginatedCrowdActions.pageInfo.page ==
paginatedCrowdActions.pageInfo.totalPages) {
emit(
CrowdActionGetterState.finished(
crowdActions: paginatedCrowdActions.crowdActions,
),
);
} else {
response = await _crowdActionRepository.getCrowdActions();
return;
}

emit(
response.fold(
(failure) => const CrowdActionGetterState.noCrowdActions(),
(crowdActions) =>
CrowdActionGetterState.fetched(crowdActions),
CrowdActionGetterState.success(
crowdActions: paginatedCrowdActions.crowdActions,
pageInfo: paginatedCrowdActions.pageInfo,
),
);
} catch (e) {
emit(
const CrowdActionGetterState.noCrowdActions(),
); // TODO: Consider implementing error state
}
},
);
},
);
},
);
},
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ part of 'crowdaction_getter_bloc.dart';

@freezed
class CrowdActionGetterEvent with _$CrowdActionGetterEvent {
const factory CrowdActionGetterEvent.getMore(int? amount) = _GetMore;
const factory CrowdActionGetterEvent.init() = _Init;
const factory CrowdActionGetterEvent.getCrowdActions({
@Default(1) int pageNumber,
}) = _GetCrowdActions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ part of 'crowdaction_getter_bloc.dart';
class CrowdActionGetterState with _$CrowdActionGetterState {
const factory CrowdActionGetterState.initial() = _Initial;

const factory CrowdActionGetterState.fetchingCrowdActions() =
_FetchingCrowdActions;
const factory CrowdActionGetterState.loading() = _Loading;

const factory CrowdActionGetterState.noCrowdActions() = _NoCrowdActions;
const factory CrowdActionGetterState.success({
required List<CrowdAction> crowdActions,
required PageInfo pageInfo,
}) = _Success;

const factory CrowdActionGetterState.fetched(List<CrowdAction> crowdActions) =
_Fetched;
const factory CrowdActionGetterState.finished({
required List<CrowdAction> crowdActions,
}) = _Finished;
const factory CrowdActionGetterState.failure(CrowdActionFailure failure) =
_Failure;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:bloc/bloc.dart';
import 'package:collaction_app/domain/participation/i_participation_repository.dart';
import 'package:collaction_app/domain/participation/paginated_participations.dart';
import 'package:collaction_app/domain/participation/participation.dart';
import 'package:collaction_app/domain/participation/participation_failures.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';

import '../../../domain/core/page_info.dart';

part 'crowdaction_participants_bloc.freezed.dart';
part 'crowdaction_participants_event.dart';
part 'crowdaction_participants_state.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:bloc/bloc.dart';
import 'package:collaction_app/domain/participation/i_participation_repository.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';

import '../../../domain/participation/participation.dart';

part 'top_participants_event.dart';
part 'top_participants_state.dart';
part 'top_participants_bloc.freezed.dart';

@injectable
class TopParticipantsBloc
extends Bloc<TopParticipantsEvent, TopParticipantsState> {
final IParticipationRepository _participationRepository;

TopParticipantsBloc(this._participationRepository) : super(const _Initial()) {
on<TopParticipantsEvent>((event, emit) async {
await event.map(
fetchParticipants: (event) async {
emit(const TopParticipantsState.fetching());

final participantsOrFailure = await _participationRepository
.getTopParticipants(crowdActionId: event.crowdActionId);

participantsOrFailure.fold(
(failure) => emit(const TopParticipantsState.failure()),
(participants) => emit(TopParticipantsState.fetched(participants)),
);
},
);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
part of 'top_participants_bloc.dart';

@freezed
class TopParticipantsEvent with _$TopParticipantsEvent {
const factory TopParticipantsEvent.fetchParticipants({
required String crowdActionId,
}) = _FetchParticipants;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of 'top_participants_bloc.dart';

@freezed
class TopParticipantsState with _$TopParticipantsState {
const factory TopParticipantsState.initial() = _Initial;
const factory TopParticipantsState.fetching() = _Fetching;
const factory TopParticipantsState.fetched(
List<Participation> topParticipants,
) = _Fetched;
const factory TopParticipantsState.failure() = _Failure;
}
15 changes: 15 additions & 0 deletions lib/domain/core/page_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'page_info.freezed.dart';

@freezed
class PageInfo with _$PageInfo {
const PageInfo._();

const factory PageInfo({
required int page,
required int pageSize,
required int totalPages,
required int totalItems,
}) = _PageInfo;
}
12 changes: 11 additions & 1 deletion lib/domain/crowdaction/crowdaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ class CrowdAction with _$CrowdAction {
}

bool get isOpen => joinStatus == JoinStatus.open;
bool get isEnded => joinStatus == JoinStatus.closed;
bool get isRunning => status == Status.started;
bool get isClosed => status == Status.ended;
bool get isWaiting => status == Status.waiting;

String get statusChipLabel => isOpen
? 'Now open'
: isRunning
? 'Currently running'
: isWaiting
? 'Starting soon'
: 'Finished';
}

@freezed
Expand Down
6 changes: 4 additions & 2 deletions lib/domain/crowdaction/i_crowdaction_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collaction_app/domain/crowdaction/paginated_crowdactions.dart';
import 'package:dartz/dartz.dart';

import 'crowdaction.dart';
Expand All @@ -9,9 +10,10 @@ abstract class ICrowdActionRepository {
String id,
);

Future<Either<CrowdActionFailure, List<CrowdAction>>> getCrowdActions({
int amount = 0,
Future<Either<CrowdActionFailure, PaginatedCrowdActions>> getCrowdActions({
int pageNumber = 1,
});

Future<Either<CrowdActionFailure, List<CrowdAction>>>
getSpotlightCrowdActions();

Expand Down
16 changes: 16 additions & 0 deletions lib/domain/crowdaction/paginated_crowdactions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../core/page_info.dart';
import 'crowdaction.dart';

part 'paginated_crowdactions.freezed.dart';

@freezed
class PaginatedCrowdActions with _$PaginatedCrowdActions {
const PaginatedCrowdActions._();

const factory PaginatedCrowdActions({
required List<CrowdAction> crowdActions,
required PageInfo pageInfo,
}) = _PaginatedCrowdActions;
}
2 changes: 1 addition & 1 deletion lib/domain/crowdaction/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ const crowdActionCommitmentIcons = {
};

IconData mapIcon(String? icon) =>
crowdActionCommitmentIcons[icon] ?? CollactionIcons.no_beef;
crowdActionCommitmentIcons[icon] ?? CollactionIcons.collaction;
4 changes: 4 additions & 0 deletions lib/domain/participation/i_participation_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ abstract class IParticipationRepository {
required String crowdActionId,
int pageNumber = 1,
});

Future<Either<ParticipationFailure, List<Participation>>> getTopParticipants({
required String crowdActionId,
});
}
14 changes: 2 additions & 12 deletions lib/domain/participation/paginated_participations.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import 'package:collaction_app/domain/participation/participation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'paginated_participations.freezed.dart';

@freezed
class PageInfo with _$PageInfo {
const PageInfo._();
import '../core/page_info.dart';

const factory PageInfo({
required int page,
required int pageSize,
required int totalPages,
required int totalItems,
}) = _PageInfo;
}
part 'paginated_participations.freezed.dart';

@freezed
class PaginatedParticipations with _$PaginatedParticipations {
Expand Down
28 changes: 28 additions & 0 deletions lib/infrastructure/core/page_info_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../domain/core/page_info.dart';

part 'page_info_dto.g.dart';
part 'page_info_dto.freezed.dart';

@freezed
class PageInfoDto with _$PageInfoDto {
const PageInfoDto._();

const factory PageInfoDto({
required int page,
required int pageSize,
required int totalPages,
required int totalItems,
}) = _PageInfoDto;

factory PageInfoDto.fromJson(Map<String, dynamic> json) =>
_$PageInfoDtoFromJson(json);

PageInfo toDomain() => PageInfo(
page: page,
pageSize: pageSize,
totalPages: totalPages,
totalItems: totalItems,
);
}
38 changes: 24 additions & 14 deletions lib/infrastructure/crowdaction/crowdaction_repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:convert';

import 'package:collaction_app/domain/core/i_settings_repository.dart';
import 'package:collaction_app/domain/crowdaction/paginated_crowdactions.dart';
import 'package:collaction_app/infrastructure/crowdaction/paginated_crowdactions_dto.dart';
import 'package:dartz/dartz.dart';
import 'package:http/http.dart' as http;
import 'package:injectable/injectable.dart';
Expand All @@ -11,6 +13,7 @@ import '../../domain/crowdaction/crowdaction.dart';
import '../../domain/crowdaction/crowdaction_failures.dart';
import '../../domain/crowdaction/crowdaction_status.dart';
import '../../domain/crowdaction/i_crowdaction_repository.dart';
import '../core/page_info_dto.dart';
import 'crowdaction_dto.dart';
import 'crowdaction_status_dto.dart';

Expand Down Expand Up @@ -51,30 +54,37 @@ class CrowdActionRepository implements ICrowdActionRepository {
}

@override
Future<Either<CrowdActionFailure, List<CrowdAction>>> getCrowdActions({
int amount = 0,
Future<Either<CrowdActionFailure, PaginatedCrowdActions>> getCrowdActions({
int pageNumber = 1,
}) async {
try {
final response = await _client.get(
Uri.parse(
'${await _settingsRepository.baseApiEndpointUrl}/v1/crowdactions',
'${await _settingsRepository.baseApiEndpointUrl}/v1/crowdactions?page=$pageNumber',
),
headers: {'Content-Type': 'application/json'},
);

if (response.statusCode == 200) {
final responseBody = jsonDecode(response.body);
final json = jsonDecode(response.body) as Map<String, dynamic>;
final itemsJson = json['items'] as List<dynamic>;
final pageInfoJson = json['pageInfo'] as Map<String, dynamic>;

final crowdActions = itemsJson
.map(
(item) => CrowdActionDto.fromJson(item as Map<String, dynamic>),
)
.toList();

final pageInfo = PageInfoDto.fromJson(pageInfoJson);

return right(
responseBody['items']
.map<CrowdAction>(
(json) => CrowdActionDto.fromJson(json as Map<String, dynamic>)
.toDomain(),
)
.toList() as List<CrowdAction>,
PaginatedCrowdActionsDto(
crowdActions: crowdActions,
pageInfo: pageInfo,
).toDomain(),
);
} else {
return left(
const CrowdActionFailure.serverError(),
);
return left(const CrowdActionFailure.serverError());
}
} catch (error) {
return left(const CrowdActionFailure.networkRequestFailed());
Expand Down
Loading

0 comments on commit f13be80

Please sign in to comment.