Skip to content

Commit

Permalink
implement-sorting: update sort type in realtime, split states
Browse files Browse the repository at this point in the history
  • Loading branch information
alesiaradkevich committed Jul 31, 2023
1 parent 808cd92 commit e2bc6ec
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 61 deletions.
50 changes: 36 additions & 14 deletions lib/src/presentation/pages/suggestions/suggestions_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class SuggestionsCubit extends Cubit<SuggestionsState> {
completed: [],
declined: [],
duplicated: [],
isCreateBottomSheetOpened: false,
isSortingBottomSheetOpened: false,
sortType: SortType.likes,
),
) {
Expand Down Expand Up @@ -90,23 +88,47 @@ class SuggestionsCubit extends Cubit<SuggestionsState> {
: _suggestionRepository.upvote(suggestion.id);
}

void openCreateBottomSheet() =>
emit(state.newState(isCreateBottomSheetOpened: true));
void openCreateBottomSheet() => emit(
CreateState(
requests: state.requests,
inProgress: state.inProgress,
completed: state.completed,
declined: state.declined,
duplicated: state.duplicated,
sortType: state.sortType,
activeTab: state.activeTab,
),
);

void closeCreateBottomSheet() =>
emit(state.newState(isCreateBottomSheetOpened: false));
void closeBottomSheet() => emit(
SuggestionsState(
requests: state.requests,
inProgress: state.inProgress,
completed: state.completed,
declined: state.declined,
duplicated: state.duplicated,
sortType: state.sortType,
),
);

void changeActiveTab(SuggestionStatus activeTab) =>
emit(state.newState(activeTab: activeTab));

void openSortingBottomSheet() =>
emit(state.newState(isSortingBottomSheetOpened: true));
void openSortingBottomSheet() => emit(
SortingState(
requests: state.requests,
inProgress: state.inProgress,
completed: state.completed,
declined: state.declined,
duplicated: state.duplicated,
sortType: state.sortType,
),
);

void closeSortingBottomSheet(SortType sortType) {
final sortTypeChanged = sortType != state.sortType;
emit(
state.newState(isSortingBottomSheetOpened: false, sortType: sortType),
);
if (sortTypeChanged) _onNewSuggestions(_suggestionRepository.suggestions);
void onSortTypeChanged(SortType sortType) {
if (sortType != state.sortType) {
emit(state.newState(sortType: sortType));
_onNewSuggestions(_suggestionRepository.suggestions);
}
}
}
15 changes: 7 additions & 8 deletions lib/src/presentation/pages/suggestions/suggestions_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ class _SuggestionsPageState extends State<SuggestionsPage> {
return SuggestionsCubitScope(
child: BlocBuilder<SuggestionsCubit, SuggestionsState>(
buildWhen: (previous, current) =>
previous.isCreateBottomSheetOpened !=
current.isCreateBottomSheetOpened ||
previous.type != current.type ||
previous.activeTab != current.activeTab ||
previous.isSortingBottomSheetOpened !=
current.isSortingBottomSheetOpened,
previous.sortType != current.sortType,
builder: (context, state) {
final cubit = context.read<SuggestionsCubit>();
return Stack(
Expand Down Expand Up @@ -126,16 +124,17 @@ class _SuggestionsPageState extends State<SuggestionsPage> {
],
),
),
if (state.isCreateBottomSheetOpened)
if (state is CreateState)
_BottomSheet(
onSaveToGallery: widget.onSaveToGallery,
onUploadMultiplePhotos: widget.onUploadMultiplePhotos,
onCloseBottomSheet: cubit.closeCreateBottomSheet,
onCloseBottomSheet: cubit.closeBottomSheet,
),
if (state.isSortingBottomSheetOpened)
if (state is SortingState)
SortingBottomSheet(
closeSortingBottomSheet: cubit.closeSortingBottomSheet,
closeBottomSheet: cubit.closeBottomSheet,
value: state.sortType,
onChanged: cubit.onSortTypeChanged,
)
],
);
Expand Down
87 changes: 75 additions & 12 deletions lib/src/presentation/pages/suggestions/suggestions_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class SuggestionsState extends Equatable {
final List<Suggestion> declined;
final List<Suggestion> duplicated;
final SuggestionStatus activeTab;
final bool isCreateBottomSheetOpened;
final bool isSortingBottomSheetOpened;
final SortType sortType;

const SuggestionsState({
Expand All @@ -18,8 +16,6 @@ class SuggestionsState extends Equatable {
required this.completed,
required this.declined,
required this.duplicated,
required this.isCreateBottomSheetOpened,
required this.isSortingBottomSheetOpened,
required this.sortType,
this.activeTab = SuggestionStatus.requests,
});
Expand All @@ -31,8 +27,6 @@ class SuggestionsState extends Equatable {
List<Suggestion>? declined,
List<Suggestion>? duplicated,
SuggestionStatus? activeTab,
bool? isCreateBottomSheetOpened,
bool? isSortingBottomSheetOpened,
SortType? sortType,
}) {
return SuggestionsState(
Expand All @@ -42,10 +36,6 @@ class SuggestionsState extends Equatable {
declined: declined ?? this.declined,
duplicated: duplicated ?? this.duplicated,
activeTab: activeTab ?? this.activeTab,
isCreateBottomSheetOpened:
isCreateBottomSheetOpened ?? this.isCreateBottomSheetOpened,
isSortingBottomSheetOpened:
isSortingBottomSheetOpened ?? this.isSortingBottomSheetOpened,
sortType: sortType ?? this.sortType,
);
}
Expand All @@ -58,11 +48,76 @@ class SuggestionsState extends Equatable {
declined,
duplicated,
activeTab,
isCreateBottomSheetOpened,
isSortingBottomSheetOpened,
sortType,
];
}

class CreateState extends SuggestionsState {
const CreateState({
required super.requests,
required super.inProgress,
required super.completed,
required super.declined,
required super.duplicated,
required super.sortType,
super.activeTab,
});

@override
CreateState newState({
List<Suggestion>? requests,
List<Suggestion>? inProgress,
List<Suggestion>? completed,
List<Suggestion>? declined,
List<Suggestion>? duplicated,
SuggestionStatus? activeTab,
SortType? sortType,
}) {
return CreateState(
requests: requests ?? this.requests,
inProgress: inProgress ?? this.inProgress,
completed: completed ?? this.completed,
declined: declined ?? this.declined,
duplicated: duplicated ?? this.duplicated,
activeTab: activeTab ?? this.activeTab,
sortType: sortType ?? this.sortType,
);
}
}

class SortingState extends SuggestionsState {
const SortingState({
required super.requests,
required super.inProgress,
required super.completed,
required super.declined,
required super.duplicated,
required super.sortType,
super.activeTab,
});

@override
SortingState newState({
List<Suggestion>? requests,
List<Suggestion>? inProgress,
List<Suggestion>? completed,
List<Suggestion>? declined,
List<Suggestion>? duplicated,
SuggestionStatus? activeTab,
SortType? sortType,
}) {
return SortingState(
requests: requests ?? this.requests,
inProgress: inProgress ?? this.inProgress,
completed: completed ?? this.completed,
declined: declined ?? this.declined,
duplicated: duplicated ?? this.duplicated,
activeTab: activeTab ?? this.activeTab,
sortType: sortType ?? this.sortType,
);
}
}

enum SortType { likes, date }

extension SortTypeExtension on SortType {
Expand All @@ -73,3 +128,11 @@ extension SortTypeExtension on SortType {
};
}
}

extension SuggestionsStateType on SuggestionsState {
Type get type {
if (this is SortingState) return SortingState;
if (this is CreateState) return CreateState;
return SuggestionsState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import 'package:suggest_a_feature/suggest_a_feature.dart';
import 'package:wtf_sliding_sheet/wtf_sliding_sheet.dart';

class SortingBottomSheet extends StatefulWidget {
final ValueChanged<SortType> closeSortingBottomSheet;
final VoidCallback closeBottomSheet;
final ValueChanged<SortType> onChanged;
final SortType value;

const SortingBottomSheet({
required this.closeSortingBottomSheet,
required this.closeBottomSheet,
required this.value,
required this.onChanged,
super.key,
});

Expand All @@ -24,13 +26,6 @@ class SortingBottomSheet extends StatefulWidget {

class _SortingBottomSheetState extends State<SortingBottomSheet> {
final SheetController _controller = SheetController();
late SortType _value;

@override
void initState() {
super.initState();
_value = widget.value;
}

@override
Widget build(BuildContext context) {
Expand All @@ -47,27 +42,25 @@ class _SortingBottomSheetState extends State<SortingBottomSheet> {
_SortRow(
title: context.localization.numberOfLikes,
value: SortType.likes,
selected: _value == SortType.likes,
onChanged: _changeSortValue,
selected: widget.value == SortType.likes,
onChanged: widget.onChanged,
),
const SizedBox(height: Dimensions.marginSmall),
_SortRow(
title: context.localization.creationDate,
value: SortType.date,
onChanged: _changeSortValue,
selected: _value == SortType.date,
onChanged: widget.onChanged,
selected: widget.value == SortType.date,
),
],
);
},
);
}

void _changeSortValue(SortType value) => setState(() => _value = value);

Future<void> _onClose() async {
await _controller.collapse();
widget.closeSortingBottomSheet(_value);
widget.closeBottomSheet();
}
}

Expand Down
28 changes: 17 additions & 11 deletions test/presentation/cubits/suggestions_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ void main() {
completed: [mockedCompletedSuggestion, mockedCompletedSuggestion2],
declined: const [],
duplicated: const [],
isCreateBottomSheetOpened: false,
isSortingBottomSheetOpened: false,
sortType: SortType.likes,
);
final mockedSuggestions = [
Expand Down Expand Up @@ -60,8 +58,14 @@ void main() {
seed: () => emptySuggestionsState,
act: (cubit) => cubit.openCreateBottomSheet(),
expect: () => [
emptySuggestionsState.newState(
isCreateBottomSheetOpened: true,
CreateState(
requests: emptySuggestionsState.requests,
inProgress: emptySuggestionsState.inProgress,
completed: emptySuggestionsState.completed,
declined: emptySuggestionsState.declined,
duplicated: emptySuggestionsState.duplicated,
sortType: emptySuggestionsState.sortType,
activeTab: emptySuggestionsState.activeTab,
),
],
);
Expand All @@ -76,10 +80,16 @@ void main() {
mockSuggestionRepository,
);
},
seed: () => emptySuggestionsState.newState(
isCreateBottomSheetOpened: true,
seed: () => CreateState(
requests: emptySuggestionsState.requests,
inProgress: emptySuggestionsState.inProgress,
completed: emptySuggestionsState.completed,
declined: emptySuggestionsState.declined,
duplicated: emptySuggestionsState.duplicated,
sortType: emptySuggestionsState.sortType,
activeTab: emptySuggestionsState.activeTab,
),
act: (cubit) => cubit.closeCreateBottomSheet(),
act: (cubit) => cubit.closeBottomSheet(),
expect: () => [
emptySuggestionsState,
],
Expand Down Expand Up @@ -134,8 +144,6 @@ void main() {
completed: const [],
declined: const [],
duplicated: const [],
isCreateBottomSheetOpened: false,
isSortingBottomSheetOpened: false,
sortType: SortType.likes,
),
act: (cubit) {
Expand All @@ -148,8 +156,6 @@ void main() {
completed: const [],
declined: const [],
duplicated: const [],
isCreateBottomSheetOpened: false,
isSortingBottomSheetOpened: false,
sortType: SortType.likes,
),
],
Expand Down

0 comments on commit e2bc6ec

Please sign in to comment.