Skip to content

Commit

Permalink
add-sorting-parameter: add parameter to suggestions page
Browse files Browse the repository at this point in the history
  • Loading branch information
alesiaradkevich committed Aug 2, 2023
1 parent b49420b commit a9142e3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 32 deletions.
17 changes: 9 additions & 8 deletions lib/src/presentation/pages/suggestions/suggestions_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import 'package:suggest_a_feature/src/domain/entities/suggestion.dart';
import 'package:suggest_a_feature/src/presentation/di/injector.dart'
as injector;
import 'package:suggest_a_feature/src/presentation/pages/suggestions/suggestions_state.dart';
import 'package:suggest_a_feature/src/presentation/utils/typedefs.dart';

class SuggestionsCubit extends Cubit<SuggestionsState> {
final SuggestionRepository _suggestionRepository;
StreamSubscription<List<Suggestion>>? _suggestionSubscription;

SuggestionsCubit(this._suggestionRepository)
SuggestionsCubit(this._suggestionRepository, SortType sortType)
: super(
const SuggestionsState(
requests: [],
inProgress: [],
completed: [],
declined: [],
duplicated: [],
sortType: SortType.likes,
SuggestionsState(
requests: const [],
inProgress: const [],
completed: const [],
declined: const [],
duplicated: const [],
sortType: sortType,
),
) {
_init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:suggest_a_feature/src/presentation/di/injector.dart';
import 'package:suggest_a_feature/src/presentation/pages/suggestions/suggestions_cubit.dart';
import 'package:suggest_a_feature/src/presentation/utils/typedefs.dart';

class SuggestionsCubitScope extends StatelessWidget {
final Widget child;
final SortType sortType;

const SuggestionsCubitScope({
required this.child,
required this.sortType,
super.key,
});

Expand All @@ -16,6 +19,7 @@ class SuggestionsCubitScope extends StatelessWidget {
return BlocProvider(
create: (_) => SuggestionsCubit(
i.suggestionRepository,
sortType,
),
child: child,
);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/presentation/pages/suggestions/suggestions_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class SuggestionsPage extends StatefulWidget {
/// If `true` then [adminSettings] will be used instead of user account.
final bool isAdmin;

/// Initial sorting type
final SortType sortType;

SuggestionsPage({
required this.userId,
required this.suggestionsDataSource,
Expand All @@ -65,6 +68,7 @@ class SuggestionsPage extends StatefulWidget {
this.onUploadMultiplePhotos,
this.customAppBar,
this.imageHeaders,
this.sortType = SortType.upvotes,
super.key,
}) : assert(
(isAdmin && adminSettings != null) || !isAdmin,
Expand All @@ -88,6 +92,7 @@ class _SuggestionsPageState extends State<SuggestionsPage> {
@override
Widget build(BuildContext context) {
return SuggestionsCubitScope(
sortType: widget.sortType,
child: BlocBuilder<SuggestionsCubit, SuggestionsState>(
buildWhen: (previous, current) =>
previous.type != current.type ||
Expand Down
8 changes: 4 additions & 4 deletions lib/src/presentation/pages/suggestions/suggestions_state.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:equatable/equatable.dart';
import 'package:suggest_a_feature/src/domain/entities/suggestion.dart';
import 'package:suggest_a_feature/src/presentation/utils/typedefs.dart';

class SuggestionsState extends Equatable {
final List<Suggestion> requests;
Expand Down Expand Up @@ -118,13 +119,12 @@ class SortingState extends SuggestionsState {
}
}

enum SortType { likes, date }

extension SortTypeExtension on SortType {
Comparator<Suggestion> get sortFunction {
return switch (this) {
SortType.likes => (a, b) => b.upvotesCount.compareTo(a.upvotesCount),
SortType.date => (a, b) => b.creationTime.compareTo(a.creationTime),
SortType.upvotes => (a, b) => b.upvotesCount.compareTo(a.upvotesCount),
SortType.creationDate => (a, b) =>
b.creationTime.compareTo(a.creationTime),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:suggest_a_feature/src/presentation/pages/suggestions/suggestions_state.dart';
import 'package:suggest_a_feature/src/presentation/pages/widgets/bottom_sheets/base_bottom_sheet.dart';
import 'package:suggest_a_feature/src/presentation/pages/widgets/suggestions_radio_button.dart';
import 'package:suggest_a_feature/src/presentation/utils/context_utils.dart';
Expand Down Expand Up @@ -41,16 +40,16 @@ class _SortingBottomSheetState extends State<SortingBottomSheet> {
children: [
_SortRow(
title: context.localization.numberOfLikes,
value: SortType.likes,
selected: widget.value == SortType.likes,
value: SortType.upvotes,
selected: widget.value == SortType.upvotes,
onChanged: widget.onChanged,
),
const SizedBox(height: Dimensions.marginSmall),
_SortRow(
title: context.localization.creationDate,
value: SortType.date,
value: SortType.creationDate,
onChanged: widget.onChanged,
selected: widget.value == SortType.date,
selected: widget.value == SortType.creationDate,
),
],
);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/presentation/utils/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ typedef OnSaveToGalleryCallback = Future<bool?> Function(String url);
///
/// The [id] argument is an id of the author which we want to get.
typedef OnGetUserById = Future<SuggestionAuthor?> Function(String id);

/// Sorting type values
enum SortType { upvotes, creationDate }
24 changes: 9 additions & 15 deletions test/presentation/cubits/suggestions_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:suggest_a_feature/src/domain/utils/simple_behavior_subject.dart'
import 'package:suggest_a_feature/src/presentation/di/injector.dart';
import 'package:suggest_a_feature/src/presentation/pages/suggestions/suggestions_cubit.dart';
import 'package:suggest_a_feature/src/presentation/pages/suggestions/suggestions_state.dart';
import 'package:suggest_a_feature/src/presentation/utils/typedefs.dart';

import '../../utils/mocked_entities.dart';
import '../../utils/shared_mocks.mocks.dart';
Expand All @@ -17,13 +18,14 @@ void main() {
final mockSuggestionsTheme = MockSuggestionsTheme();
final mockSuggestionsDataSource = MockSuggestionsDataSource();
final mockSuggestionRepository = MockSuggestionRepositoryImpl();
const mockSortType = SortType.upvotes;
final emptySuggestionsState = SuggestionsState(
requests: [mockedRequestSuggestion, mockedRequestSuggestion2],
inProgress: [mockedInProgressSuggestion, mockedInProgressSuggestion2],
completed: [mockedCompletedSuggestion, mockedCompletedSuggestion2],
declined: const [],
duplicated: const [],
sortType: SortType.likes,
sortType: SortType.upvotes,
);
final mockedSuggestions = [
mockedRequestSuggestion,
Expand Down Expand Up @@ -51,9 +53,7 @@ void main() {
when(mockSuggestionRepository.suggestionsStream).thenAnswer(
(_) => Stream.value(mockedSuggestions),
);
return SuggestionsCubit(
mockSuggestionRepository,
);
return SuggestionsCubit(mockSuggestionRepository, mockSortType);
},
seed: () => emptySuggestionsState,
act: (cubit) => cubit.openCreateBottomSheet(),
Expand All @@ -76,9 +76,7 @@ void main() {
when(mockSuggestionRepository.suggestionsStream).thenAnswer(
(_) => Stream.value(mockedSuggestions),
);
return SuggestionsCubit(
mockSuggestionRepository,
);
return SuggestionsCubit(mockSuggestionRepository, mockSortType);
},
seed: () => CreateState(
requests: emptySuggestionsState.requests,
Expand All @@ -101,9 +99,7 @@ void main() {
when(mockSuggestionRepository.suggestionsStream).thenAnswer(
(_) => Stream.value(mockedSuggestions),
);
return SuggestionsCubit(
mockSuggestionRepository,
);
return SuggestionsCubit(mockSuggestionRepository, mockSortType);
},
seed: () => emptySuggestionsState.newState(
activeTab: SuggestionStatus.inProgress,
Expand Down Expand Up @@ -133,17 +129,15 @@ void main() {
dataStream.value = [mockedRequestSuggestion, upvotedSuggestion],
);

return SuggestionsCubit(
mockSuggestionRepository,
);
return SuggestionsCubit(mockSuggestionRepository, mockSortType);
},
seed: () => SuggestionsState(
requests: [mockedRequestSuggestion, mockedRequestSuggestion2],
inProgress: const [],
completed: const [],
declined: const [],
duplicated: const [],
sortType: SortType.likes,
sortType: SortType.upvotes,
),
act: (cubit) {
cubit.vote(SuggestionStatus.requests, 1);
Expand All @@ -155,7 +149,7 @@ void main() {
completed: const [],
declined: const [],
duplicated: const [],
sortType: SortType.likes,
sortType: SortType.upvotes,
),
],
);
Expand Down

0 comments on commit a9142e3

Please sign in to comment.