Skip to content

Commit

Permalink
chore: filter and sort tweaks (#94)
Browse files Browse the repository at this point in the history
* filter and sort minor ui tweaks

* extend enums with value method
update discover query string util
  • Loading branch information
d-reader-luka authored Jun 19, 2024
1 parent 657bbff commit d450002
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 127 deletions.
96 changes: 45 additions & 51 deletions lib/features/discover/root/presentation/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,75 @@ import 'package:d_reader_flutter/shared/presentations/providers/common/search_pr
import 'package:d_reader_flutter/shared/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

String _getSortByQueryString(SortByEnum selected, ScrollListType type) {
if (type == ScrollListType.comicList) {
if (selected == SortByEnum.likes) {
return 'sortTag=likes';
} else if (selected == SortByEnum.rating) {
return 'sortTag=rating';
} else if (selected == SortByEnum.readers) {
return 'sortTag=readers';
} else if (selected == SortByEnum.viewers) {
return 'sortTag=viewers';
} else if (selected == SortByEnum.published) {
return 'sortTag=published';
}
} else if (type == ScrollListType.issueList) {
if (selected == SortByEnum.latest) {
return 'sortTag=latest';
} else if (selected == SortByEnum.likes) {
return 'sortTag=likes';
} else if (selected == SortByEnum.rating) {
return 'sortTag=rating';
} else if (selected == SortByEnum.readers) {
return 'sortTag=readers';
} else if (selected == SortByEnum.viewers) {
return 'sortTag=viewers';
}
} else if (type == ScrollListType.creatorList) {
if (selected == SortByEnum.followers) {
return 'sortTag=followers';
}
const String _genreSlugsKey = 'genreSlugs[]';
const String _nameSubstringKey = 'nameSubstring';
const String _sortOrderKey = 'sortOrder';
const String _sortTagKey = 'sortTag';
const String _filterTagKey = 'filterTag';
const String _titleSubstringKey = 'titleSubstring';

String _prependQueryWithKey({required String key, required String value}) =>
value.isNotEmpty ? '$key=$value' : '';

bool _isCreatorTagOnly(SortByEnum selected) =>
selected == SortByEnum.followers || selected == SortByEnum.name;

String _getSortTagValue(SortByEnum selected, ScrollListType type) {
final bool isCreatorListType = type == ScrollListType.creatorList;

if (_isCreatorTagOnly(selected)) {
return isCreatorListType ? selected.value() : '';
}

if (isCreatorListType) {
return '';
}

return '';
// special handling for comic filters when latest/published is selected
if (type == ScrollListType.comicList && selected == SortByEnum.latest) {
return SortByEnum.published.value();
}
return selected.value();
}

String getFilterQueryString(WidgetRef ref, ScrollListType scrollListType) {
String search = ref.watch(searchProvider).search;
final String genreTags = ref
.read(selectedGenresProvider)
.map((genreSlug) => 'genreSlugs[]=$genreSlug')
.map((genreSlug) => '$_genreSlugsKey=$genreSlug')
.join('&');
final FilterId? selectedFilter = ref.read(selectedFilterProvider);
final SortByEnum? selectedSortBy = ref.read(selectedSortByProvider);
final SortDirection selectedSortDirection = ref.read(sortDirectionProvider);
final String tagFilter = _filterPerType(
scrollListType: scrollListType,
selectedFilter: selectedFilter,
final String filterBy = _prependQueryWithKey(
key: _filterTagKey,
value: _filterPerType(
scrollListType: scrollListType,
selectedFilter: selectedFilter,
),
);
final String sortByFilter = selectedSortBy != null
? _getSortByQueryString(
selectedSortBy,
scrollListType,
? _prependQueryWithKey(
key: _sortTagKey,
value: _getSortTagValue(
selectedSortBy,
scrollListType,
),
)
: '';
final String sortDirection = getSortDirection(selectedSortDirection);
final String common =
'sortOrder=$sortDirection&${_adjustQueryString(genreTags)}${_adjustQueryString(sortByFilter)}${_adjustQueryString(tagFilter)}';
'$_sortOrderKey=$sortDirection&${_adjustQueryString(genreTags)}${_adjustQueryString(sortByFilter)}${_adjustQueryString(filterBy)}';
final String query = scrollListType == ScrollListType.creatorList
? '$common${'nameSubstring=$search'}'
: '$common${'titleSubstring=$search'}';
? '$common${'$_nameSubstringKey=$search'}'
: '$common${'$_titleSubstringKey=$search'}';
return query;
}

String _filterPerType({
required ScrollListType scrollListType,
FilterId? selectedFilter,
}) {
if (selectedFilter == null) {
return '';
}
if (scrollListType == ScrollListType.issueList) {
return selectedFilter == FilterId.free
? 'filterTag=free'
: 'filterTag=popular';
}
return selectedFilter == FilterId.popular ? 'filterTag=popular' : '';
}
}) =>
selectedFilter == null ? '' : selectedFilter.value();

String _adjustQueryString(String query) => query.isNotEmpty ? '$query&' : '';
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FilterBottomSheet extends ConsumerWidget {
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8),
child: AppBar(
backgroundColor: ColorPalette.appBackgroundColor,
backgroundColor: Colors.transparent,
elevation: 0,
leading: Text(
'Filter',
Expand All @@ -49,36 +49,36 @@ class FilterBottomSheet extends ConsumerWidget {
padding: const EdgeInsets.all(12.0),
child: ListView(
children: [
// dont show on creators tab
ref.watch(tabBarProvider) != 2
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SectionTitle(
title: ref.watch(tabBarProvider) == 0
? 'Show comics'
: 'Show issues',
),
const SectionTitle(title: 'Show editions'),
const SizedBox(
height: 16,
),
// for comics we have popular filter only
ref.watch(tabBarProvider) == 0
? const FilterContainer(
id: FilterId.popular, text: 'Popular')
: const Row(
? FilterContainer(
id: FilterId.popular,
text: FilterId.popular.displayText(),
)
: Row(
children: [
Expanded(
child: FilterContainer(
id: FilterId.free,
text: 'Free to read',
text: FilterId.free.displayText(),
),
),
SizedBox(
const SizedBox(
width: 8,
),
Expanded(
child: FilterContainer(
id: FilterId.popular,
text: 'Popular',
text: FilterId.popular.displayText(),
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,94 +92,60 @@ class SortMenu extends ConsumerWidget {
const SizedBox(
height: 16,
),
if (ref.watch(tabBarProvider) == 0)
const ComicSortMenu()
else if (ref.watch(tabBarProvider) == 1)
const IssueSortMenu()
else if (ref.watch(tabBarProvider) == 2)
const CreatorSortMenu(),
if (ref.watch(tabBarProvider) == 2)
const _CreatorSortMenu()
else
const _DefaultSortMenu()
],
);
}
}

class ComicSortMenu extends StatelessWidget {
const ComicSortMenu({super.key});
class _DefaultSortMenu extends StatelessWidget {
const _DefaultSortMenu();

@override
Widget build(BuildContext context) {
return const Column(
children: [
FilterRadioButton(
title: 'New',
value: SortByEnum.latest,
),
FilterRadioButton(
title: 'Rating',
value: SortByEnum.rating,
),
FilterRadioButton(
title: 'Likes',
value: SortByEnum.likes,
),
FilterRadioButton(
title: 'Readers',
value: SortByEnum.readers,
),
FilterRadioButton(
title: 'Viewers',
value: SortByEnum.viewers,
),
],
);
}
}

class IssueSortMenu extends StatelessWidget {
const IssueSortMenu({super.key});

@override
Widget build(BuildContext context) {
return const Column(
return Column(
children: [
FilterRadioButton(
title: 'New',
CustomRadioButton(
title: SortByEnum.latest.displayText(),
value: SortByEnum.latest,
),
FilterRadioButton(
title: 'Rating',
CustomRadioButton(
title: SortByEnum.rating.displayText(),
value: SortByEnum.rating,
),
FilterRadioButton(
title: 'Likes',
CustomRadioButton(
title: SortByEnum.likes.displayText(),
value: SortByEnum.likes,
),
FilterRadioButton(
title: 'Readers',
CustomRadioButton(
title: SortByEnum.readers.displayText(),
value: SortByEnum.readers,
),
FilterRadioButton(
title: 'Viewers',
CustomRadioButton(
title: SortByEnum.viewers.displayText(),
value: SortByEnum.viewers,
),
],
);
}
}

class CreatorSortMenu extends StatelessWidget {
const CreatorSortMenu({super.key});
class _CreatorSortMenu extends StatelessWidget {
const _CreatorSortMenu();

@override
Widget build(BuildContext context) {
return const Column(
return Column(
children: [
FilterRadioButton(
title: 'Name',
CustomRadioButton(
title: SortByEnum.name.displayText(),
value: SortByEnum.name,
),
FilterRadioButton(
title: 'Followers',
CustomRadioButton(
title: SortByEnum.followers.displayText(),
value: SortByEnum.followers,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ChangeNetworkView extends ConsumerWidget {
}) async {
return await triggerConfirmationDialog(
context: context,
title: 'Warning, switching to $cluster',
title: 'Change Network',
subtitle: subtitle,
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class ProfileView extends HookConsumerWidget {
onTap: () async {
final result = await triggerConfirmationDialog(
context: context,
title: '',
title: 'Logout',
subtitle: 'Are you sure you want logout?',
);
if (result) {
Expand Down
34 changes: 32 additions & 2 deletions lib/shared/domain/models/enums.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
enum FilterId {
free,
popular,
popular;

String displayText() => switch (this) {
FilterId.free => 'Free to read',
FilterId.popular => 'Popular',
};

String value() =>
switch (this) { FilterId.free => 'free', FilterId.popular => 'popular' };
}

enum SortByEnum {
Expand All @@ -11,7 +19,29 @@ enum SortByEnum {
viewers,
followers,
name,
published,
published;

String displayText() => switch (this) {
SortByEnum.latest => 'New',
SortByEnum.rating => 'Rating',
SortByEnum.likes => 'Likes',
SortByEnum.readers => 'Readers',
SortByEnum.viewers => 'Viewers',
SortByEnum.followers => 'Followers',
SortByEnum.name => 'Name',
SortByEnum.published => 'Published',
};

String value() => switch (this) {
SortByEnum.latest => 'latest',
SortByEnum.rating => 'rating',
SortByEnum.likes => 'likes',
SortByEnum.readers => 'readers',
SortByEnum.viewers => 'viewers',
SortByEnum.followers => 'followers',
SortByEnum.name => 'name',
SortByEnum.published => 'published',
};
}

enum SortDirection { asc, desc }
Expand Down
4 changes: 2 additions & 2 deletions lib/shared/widgets/buttons/radio_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import 'package:d_reader_flutter/shared/theme/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class FilterRadioButton extends ConsumerWidget {
class CustomRadioButton extends ConsumerWidget {
final String title;
final SortByEnum value;
const FilterRadioButton({
const CustomRadioButton({
super.key,
required this.title,
required this.value,
Expand Down

0 comments on commit d450002

Please sign in to comment.