Added sorting options

This commit is contained in:
Juan Gilsanz Polo
2024-03-03 17:27:16 +01:00
parent e87d2f58cf
commit 3f02dfc950
13 changed files with 155 additions and 13 deletions

View File

@@ -14,6 +14,8 @@ class BookmarksModel {
ReadStatus readStatus;
Bookmark? selectedBookmark;
final GoRouter webViewRouter;
SortingType sortingType;
SortingWay sortingWay;
BookmarksModel({
this.currentPage = 0,
@@ -25,5 +27,7 @@ class BookmarksModel {
this.readStatus = ReadStatus.all,
this.selectedBookmark,
required this.webViewRouter,
this.sortingType = SortingType.date,
this.sortingWay = SortingWay.descendant,
});
}

View File

@@ -19,12 +19,27 @@ import 'package:linkdy/constants/enums.dart';
part 'bookmarks.provider.g.dart';
String sortingMethod(SortingType type, SortingWay way) {
if (type == SortingType.date && way == SortingWay.descendant) {
return "";
} else {
return "${type == SortingType.title ? "title" : "date"}_${way == SortingWay.ascendant ? "asc" : "desc"}";
}
}
@riverpod
FutureOr<void> bookmarksRequest(BookmarksRequestRef ref, ReadStatus readStatus, int limit) async {
FutureOr<void> bookmarksRequest(
BookmarksRequestRef ref,
ReadStatus readStatus,
SortingType type,
SortingWay way,
int limit,
) async {
final result = await ref.watch(apiClientProvider)!.fetchBookmarks(
limit: limit,
offset: 0,
unread: readStatus,
sort: sortingMethod(type, way),
);
if (result.successful == true) {
@@ -44,12 +59,16 @@ FutureOr<void> bookmarksRequest(BookmarksRequestRef ref, ReadStatus readStatus,
FutureOr<void> bookmarksRequestLoadMore(BookmarksRequestLoadMoreRef ref) async {
final provider = ref.watch(bookmarksProvider);
final sortingType = ref.watch(bookmarksProvider).sortingType;
final sortingWay = ref.watch(bookmarksProvider).sortingWay;
final newOffset = provider.limit * (provider.currentPage + 1);
final result = await ref.watch(apiClientProvider)!.fetchBookmarks(
limit: provider.limit,
offset: newOffset,
unread: ref.read(bookmarksProvider).readStatus,
sort: sortingMethod(sortingType, sortingWay),
);
if (result.successful == true) {
@@ -89,7 +108,7 @@ class Bookmarks extends _$Bookmarks {
],
),
);
ref.read(bookmarksRequestProvider(model.readStatus, model.limit));
ref.read(bookmarksRequestProvider(model.readStatus, model.sortingType, model.sortingWay, model.limit));
return model;
}
@@ -117,7 +136,19 @@ class Bookmarks extends _$Bookmarks {
void setReadStatus(ReadStatus status) {
state.readStatus = status;
ref.read(bookmarksRequestProvider(status, state.limit));
ref.read(bookmarksRequestProvider(status, state.sortingType, state.sortingWay, state.limit));
ref.notifyListeners();
}
void setSortingType(SortingType type) {
state.sortingType = type;
ref.read(bookmarksRequestProvider(state.readStatus, type, state.sortingWay, state.limit));
ref.notifyListeners();
}
void setSortingWay(SortingWay way) {
state.sortingWay = way;
ref.read(bookmarksRequestProvider(state.readStatus, state.sortingType, way, state.limit));
ref.notifyListeners();
}
@@ -148,7 +179,7 @@ class Bookmarks extends _$Bookmarks {
}
Future<void> refresh() async {
await ref.read(bookmarksRequestProvider(state.readStatus, state.limit).future);
await ref.read(bookmarksRequestProvider(state.readStatus, state.sortingType, state.sortingWay, state.limit).future);
}
void deleteBookmark(Bookmark bookmark) async {

View File

@@ -6,7 +6,7 @@ part of 'bookmarks.provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$bookmarksRequestHash() => r'eb1ddffb83edc0fa433fae76859bde4206e59940';
String _$bookmarksRequestHash() => r'c89579d46046980542ed9898867bfb39e85d1d6c';
/// Copied from Dart SDK
class _SystemHash {
@@ -41,10 +41,14 @@ class BookmarksRequestFamily extends Family<AsyncValue<void>> {
/// See also [bookmarksRequest].
BookmarksRequestProvider call(
ReadStatus readStatus,
SortingType type,
SortingWay way,
int limit,
) {
return BookmarksRequestProvider(
readStatus,
type,
way,
limit,
);
}
@@ -55,6 +59,8 @@ class BookmarksRequestFamily extends Family<AsyncValue<void>> {
) {
return call(
provider.readStatus,
provider.type,
provider.way,
provider.limit,
);
}
@@ -79,11 +85,15 @@ class BookmarksRequestProvider extends AutoDisposeFutureProvider<void> {
/// See also [bookmarksRequest].
BookmarksRequestProvider(
ReadStatus readStatus,
SortingType type,
SortingWay way,
int limit,
) : this._internal(
(ref) => bookmarksRequest(
ref as BookmarksRequestRef,
readStatus,
type,
way,
limit,
),
from: bookmarksRequestProvider,
@@ -96,6 +106,8 @@ class BookmarksRequestProvider extends AutoDisposeFutureProvider<void> {
allTransitiveDependencies:
BookmarksRequestFamily._allTransitiveDependencies,
readStatus: readStatus,
type: type,
way: way,
limit: limit,
);
@@ -107,10 +119,14 @@ class BookmarksRequestProvider extends AutoDisposeFutureProvider<void> {
required super.debugGetCreateSourceHash,
required super.from,
required this.readStatus,
required this.type,
required this.way,
required this.limit,
}) : super.internal();
final ReadStatus readStatus;
final SortingType type;
final SortingWay way;
final int limit;
@override
@@ -127,6 +143,8 @@ class BookmarksRequestProvider extends AutoDisposeFutureProvider<void> {
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
readStatus: readStatus,
type: type,
way: way,
limit: limit,
),
);
@@ -141,6 +159,8 @@ class BookmarksRequestProvider extends AutoDisposeFutureProvider<void> {
bool operator ==(Object other) {
return other is BookmarksRequestProvider &&
other.readStatus == readStatus &&
other.type == type &&
other.way == way &&
other.limit == limit;
}
@@ -148,6 +168,8 @@ class BookmarksRequestProvider extends AutoDisposeFutureProvider<void> {
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, readStatus.hashCode);
hash = _SystemHash.combine(hash, type.hashCode);
hash = _SystemHash.combine(hash, way.hashCode);
hash = _SystemHash.combine(hash, limit.hashCode);
return _SystemHash.finish(hash);
@@ -158,6 +180,12 @@ mixin BookmarksRequestRef on AutoDisposeFutureProviderRef<void> {
/// The parameter `readStatus` of this provider.
ReadStatus get readStatus;
/// The parameter `type` of this provider.
SortingType get type;
/// The parameter `way` of this provider.
SortingWay get way;
/// The parameter `limit` of this provider.
int get limit;
}
@@ -169,11 +197,15 @@ class _BookmarksRequestProviderElement
@override
ReadStatus get readStatus => (origin as BookmarksRequestProvider).readStatus;
@override
SortingType get type => (origin as BookmarksRequestProvider).type;
@override
SortingWay get way => (origin as BookmarksRequestProvider).way;
@override
int get limit => (origin as BookmarksRequestProvider).limit;
}
String _$bookmarksRequestLoadMoreHash() =>
r'1a206bd974677fd862eb4d5b5f453df9f566d995';
r'cea7de930dc5cb77281b68e91dfa66ff3ecf57b5';
/// See also [bookmarksRequestLoadMore].
@ProviderFor(bookmarksRequestLoadMore)
@@ -189,7 +221,7 @@ final bookmarksRequestLoadMoreProvider =
);
typedef BookmarksRequestLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$bookmarksHash() => r'84c91bf9112343e4680040bfe25b5e17fb2f92d2';
String _$bookmarksHash() => r'ed3a350dc6f4818adf146c1dab84005fec8c9be4';
/// See also [Bookmarks].
@ProviderFor(Bookmarks)

View File

@@ -6,7 +6,7 @@ part of 'favicon_loader.provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$faviconStoreHash() => r'459949315bf6a236946c8b03ba90ce047b691b7e';
String _$faviconStoreHash() => r'5575f5daff34748e6cae0143c8633cbbe19b6009';
/// See also [FaviconStore].
@ProviderFor(FaviconStore)

View File

@@ -174,7 +174,7 @@ final fetchSearchBookmarksLoadMoreProvider =
);
typedef FetchSearchBookmarksLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$searchBookmarksHash() => r'5ebbd8dbfe705cbca9552fc60d251372c771d738';
String _$searchBookmarksHash() => r'a70324693484f2c7731fcebf4df007443f1c30e9';
/// See also [SearchBookmarks].
@ProviderFor(SearchBookmarks)

View File

@@ -70,6 +70,45 @@ class VisualizationModal extends ConsumerWidget {
hoverColor: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 8),
SectionLabel(
label: t.bookmarks.sorting,
padding: const EdgeInsets.symmetric(
horizontal: 0,
vertical: 16,
),
),
SegmentedButtonSlide(
entries: [
SegmentedButtonSlideEntry(label: t.bookmarks.date),
SegmentedButtonSlideEntry(label: t.bookmarks.title),
],
selectedEntry: ref.watch(bookmarksProvider).sortingType.index,
onChange: (v) => ref.read(bookmarksProvider.notifier).setSortingType(SortingType.values[v]),
colors: SegmentedButtonSlideColors(
barColor: Theme.of(context).colorScheme.primary.withOpacity(0.2),
backgroundSelectedColor: Theme.of(context).colorScheme.primary,
foregroundSelectedColor: Theme.of(context).colorScheme.onPrimary,
foregroundUnselectedColor: Theme.of(context).colorScheme.onSurface,
hoverColor: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 16),
SegmentedButtonSlide(
entries: [
SegmentedButtonSlideEntry(label: t.bookmarks.descendant),
SegmentedButtonSlideEntry(label: t.bookmarks.ascendant),
],
selectedEntry: ref.watch(bookmarksProvider).sortingWay.index,
onChange: (v) => ref.read(bookmarksProvider.notifier).setSortingWay(SortingWay.values[v]),
colors: SegmentedButtonSlideColors(
barColor: Theme.of(context).colorScheme.primary.withOpacity(0.2),
backgroundSelectedColor: Theme.of(context).colorScheme.primary,
foregroundSelectedColor: Theme.of(context).colorScheme.onPrimary,
foregroundUnselectedColor: Theme.of(context).colorScheme.onSurface,
hoverColor: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
),

View File

@@ -372,7 +372,7 @@ final filteredBookmarksRequestLoadMoreProvider =
typedef FilteredBookmarksRequestLoadMoreRef
= AutoDisposeFutureProviderRef<void>;
String _$filteredBookmarksHash() => r'e58baf5c41c61233e76edea16830f1a47d6a645a';
String _$filteredBookmarksHash() => r'2239ac5e2eb27ffee118a5c513f191702faea8de';
/// See also [FilteredBookmarks].
@ProviderFor(FilteredBookmarks)

View File

@@ -6,7 +6,7 @@ part of 'webview.provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$webViewHash() => r'ae307e7a5014a44777bd6af7fd9cae7f96970610';
String _$webViewHash() => r'777a5c7245e32f3334c676aa2bf18d953be1233a';
/// See also [WebView].
@ProviderFor(WebView)