Lazy load search bookmarks

This commit is contained in:
Juan Gilsanz Polo
2024-02-25 03:07:26 +01:00
parent 2c857b3276
commit 5b3b6fc109
6 changed files with 297 additions and 49 deletions

View File

@@ -98,7 +98,7 @@ class AddBookmark extends _$AddBookmark {
processModal.close();
if (result.successful == true) {
ref.invalidate(bookmarksRequestProvider);
ref.read(bookmarksProvider.notifier).refresh();
ref.watch(routerProvider).pop();
} else {
showSnacbkar(

View File

@@ -175,7 +175,7 @@ final getTagsProvider =
);
typedef GetTagsRef = AutoDisposeFutureProviderRef<ApiResponse<TagsResponse>>;
String _$addBookmarkHash() => r'7a2a1c6c6597aa28fa478d2ef9dc3bc4d71c143d';
String _$addBookmarkHash() => r'ace81358d08fd833b2aa06edddcb10f00728c30c';
/// See also [AddBookmark].
@ProviderFor(AddBookmark)

View File

@@ -1,28 +1,67 @@
import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:linkdy/screens/bookmarks/model/search_bookmarks.model.dart';
import 'package:linkdy/models/api_response.dart';
import 'package:linkdy/models/data/bookmarks.dart';
import 'package:linkdy/constants/enums.dart';
import 'package:linkdy/providers/api_client_provider.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'search_bookmarks.provider.g.dart';
@riverpod
FutureOr<ApiResponse<BookmarksResponse>?> fetchSearchBookmarks(FetchSearchBookmarksRef ref) async {
FutureOr<void> fetchSearchBookmarks(FetchSearchBookmarksRef ref, int limit) async {
if (ref.watch(searchBookmarksProvider).searchTerm == "") return null;
final result = await ref.read(apiClientProvider)!.fetchBookmarks(q: ref.watch(searchBookmarksProvider).searchTerm);
return result;
final result = await ref.read(apiClientProvider)!.fetchBookmarks(
q: ref.watch(searchBookmarksProvider).searchTerm,
limit: limit,
offset: 0,
);
if (result.successful == true) {
ref.read(searchBookmarksProvider).bookmarks = result.content!.results!;
ref.read(searchBookmarksProvider).maxNumber = result.content!.count!;
ref.read(searchBookmarksProvider).currentPage = 0;
ref.read(searchBookmarksProvider).loadingMore = false;
ref.read(searchBookmarksProvider).inialLoadStatus = LoadStatus.loaded;
} else {
ref.read(searchBookmarksProvider).inialLoadStatus = LoadStatus.error;
}
ref.read(searchBookmarksProvider.notifier).notifyListeners();
}
@riverpod
FutureOr<void> fetchSearchBookmarksLoadMore(FetchSearchBookmarksLoadMoreRef ref) async {
final provider = ref.watch(searchBookmarksProvider);
final newOffset = provider.limit * (provider.currentPage + 1);
final result = await ref.watch(apiClientProvider)!.fetchBookmarks(
q: provider.searchTerm,
limit: provider.limit,
offset: newOffset,
);
await Future.delayed(Duration(seconds: 3));
if (result.successful == true) {
provider.bookmarks = [...provider.bookmarks, ...result.content!.results!];
provider.maxNumber = result.content!.count!;
provider.currentPage = provider.currentPage + 1;
}
ref.read(searchBookmarksProvider.notifier).setLoadingMore(false);
}
@riverpod
class SearchBookmarks extends _$SearchBookmarks {
@override
SearchBookmarksModel build() {
return SearchBookmarksModel(
final model = SearchBookmarksModel(
searchController: TextEditingController(),
bookmarks: [],
);
ref.read(fetchSearchBookmarksProvider(model.limit));
return model;
}
void clearSearch() {
@@ -35,8 +74,22 @@ class SearchBookmarks extends _$SearchBookmarks {
ref.notifyListeners();
}
void setSearchTerm(String value) {
state.searchTerm = value;
void setSearchTerm() {
state.searchTerm = state.searchController.text;
ref.notifyListeners();
}
void setLoadingMore(bool value) {
state.loadingMore = value;
ref.notifyListeners();
}
void setInitialLoadStatus(LoadStatus status) {
state.inialLoadStatus = status;
ref.notifyListeners();
}
void notifyListeners() {
ref.notifyListeners();
}
}

View File

@@ -7,24 +7,174 @@ part of 'search_bookmarks.provider.dart';
// **************************************************************************
String _$fetchSearchBookmarksHash() =>
r'2b871e9d1f75fb7f7b969f819cfdd9442593c4cb';
r'dea1740df4ad970bc1ffd90d85aa0d1d2cf94bf2';
/// Copied from Dart SDK
class _SystemHash {
_SystemHash._();
static int combine(int hash, int value) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + value);
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
static int finish(int hash) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
// ignore: parameter_assignments
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
}
/// See also [fetchSearchBookmarks].
@ProviderFor(fetchSearchBookmarks)
final fetchSearchBookmarksProvider =
AutoDisposeFutureProvider<ApiResponse<BookmarksResponse>?>.internal(
fetchSearchBookmarks,
name: r'fetchSearchBookmarksProvider',
const fetchSearchBookmarksProvider = FetchSearchBookmarksFamily();
/// See also [fetchSearchBookmarks].
class FetchSearchBookmarksFamily extends Family<AsyncValue<void>> {
/// See also [fetchSearchBookmarks].
const FetchSearchBookmarksFamily();
/// See also [fetchSearchBookmarks].
FetchSearchBookmarksProvider call(
int limit,
) {
return FetchSearchBookmarksProvider(
limit,
);
}
@override
FetchSearchBookmarksProvider getProviderOverride(
covariant FetchSearchBookmarksProvider provider,
) {
return call(
provider.limit,
);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'fetchSearchBookmarksProvider';
}
/// See also [fetchSearchBookmarks].
class FetchSearchBookmarksProvider extends AutoDisposeFutureProvider<void> {
/// See also [fetchSearchBookmarks].
FetchSearchBookmarksProvider(
int limit,
) : this._internal(
(ref) => fetchSearchBookmarks(
ref as FetchSearchBookmarksRef,
limit,
),
from: fetchSearchBookmarksProvider,
name: r'fetchSearchBookmarksProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$fetchSearchBookmarksHash,
dependencies: FetchSearchBookmarksFamily._dependencies,
allTransitiveDependencies:
FetchSearchBookmarksFamily._allTransitiveDependencies,
limit: limit,
);
FetchSearchBookmarksProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.limit,
}) : super.internal();
final int limit;
@override
Override overrideWith(
FutureOr<void> Function(FetchSearchBookmarksRef provider) create,
) {
return ProviderOverride(
origin: this,
override: FetchSearchBookmarksProvider._internal(
(ref) => create(ref as FetchSearchBookmarksRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
limit: limit,
),
);
}
@override
AutoDisposeFutureProviderElement<void> createElement() {
return _FetchSearchBookmarksProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is FetchSearchBookmarksProvider && other.limit == limit;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, limit.hashCode);
return _SystemHash.finish(hash);
}
}
mixin FetchSearchBookmarksRef on AutoDisposeFutureProviderRef<void> {
/// The parameter `limit` of this provider.
int get limit;
}
class _FetchSearchBookmarksProviderElement
extends AutoDisposeFutureProviderElement<void>
with FetchSearchBookmarksRef {
_FetchSearchBookmarksProviderElement(super.provider);
@override
int get limit => (origin as FetchSearchBookmarksProvider).limit;
}
String _$fetchSearchBookmarksLoadMoreHash() =>
r'4c085e4dc487554a209fa73170294b5b8e279c24';
/// See also [fetchSearchBookmarksLoadMore].
@ProviderFor(fetchSearchBookmarksLoadMore)
final fetchSearchBookmarksLoadMoreProvider =
AutoDisposeFutureProvider<void>.internal(
fetchSearchBookmarksLoadMore,
name: r'fetchSearchBookmarksLoadMoreProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$fetchSearchBookmarksHash,
: _$fetchSearchBookmarksLoadMoreHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef FetchSearchBookmarksRef
= AutoDisposeFutureProviderRef<ApiResponse<BookmarksResponse>?>;
String _$searchBookmarksHash() => r'56efd123e03f6a080f4a61cc91794aab2e563199';
typedef FetchSearchBookmarksLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$searchBookmarksHash() => r'db9f8d8cc488e820791e22c594ad9046da73ec25';
/// See also [SearchBookmarks].
@ProviderFor(SearchBookmarks)