Improved favicons load
This commit is contained in:
@@ -4,7 +4,6 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import 'package:linkdy/screens/webview/ui/webview.dart';
|
||||
import 'package:linkdy/screens/bookmarks/provider/common_functions.dart';
|
||||
import 'package:linkdy/screens/bookmarks/provider/favicon_loader.provider.dart';
|
||||
import 'package:linkdy/screens/bookmarks/model/bookmarks.model.dart';
|
||||
|
||||
import 'package:linkdy/config/sizes.dart';
|
||||
@@ -35,7 +34,6 @@ FutureOr<void> bookmarksRequest(
|
||||
);
|
||||
|
||||
if (result.successful == true) {
|
||||
ref.read(faviconStoreProvider.notifier).loadFavicons(result.content!.results!);
|
||||
ref.read(bookmarksProvider).bookmarks = result.content!.results!;
|
||||
ref.read(bookmarksProvider).maxNumber = result.content!.count!;
|
||||
ref.read(bookmarksProvider).currentPage = 0;
|
||||
@@ -65,7 +63,7 @@ FutureOr<void> bookmarksRequestLoadMore(BookmarksRequestLoadMoreRef ref) async {
|
||||
);
|
||||
|
||||
if (result.successful == true) {
|
||||
ref.read(faviconStoreProvider.notifier).loadFavicons(result.content!.results!);
|
||||
// ref.read(faviconStoreProvider.notifier).loadFavicons(result.content!.results!);
|
||||
provider.bookmarks = [...provider.bookmarks, ...result.content!.results!];
|
||||
provider.maxNumber = result.content!.count!;
|
||||
provider.currentPage = provider.currentPage + 1;
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:favicon/favicon.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import 'package:linkdy/screens/bookmarks/model/favicon_loader.model.dart';
|
||||
|
||||
import 'package:linkdy/models/favicon_item.dart';
|
||||
import 'package:linkdy/models/data/bookmarks.dart';
|
||||
|
||||
part 'favicon_loader.provider.g.dart';
|
||||
|
||||
// @riverpod
|
||||
// FutureOr<FaviconItem?> fetchFavicon(FetchFaviconRef ref, String bookmarkUrl) async {
|
||||
// final exists = ref.watch(faviconStoreProvider).favicons.where((favicon) => favicon.url == bookmarkUrl).toList();
|
||||
// if (exists.isNotEmpty) {
|
||||
// return exists[0];
|
||||
// }
|
||||
|
||||
// final faviconResult = await FaviconFinder.getBest(bookmarkUrl);
|
||||
// if (faviconResult == null) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// final result = await http.get(Uri.parse(faviconResult.url));
|
||||
// if (result.statusCode == 200) {
|
||||
// final faviconItem = FaviconItem(
|
||||
// url: bookmarkUrl,
|
||||
// favicon: faviconResult.url.contains("svg") ? result.body : base64.encode(result.bodyBytes),
|
||||
// isSvg: faviconResult.url.contains("svg"),
|
||||
// );
|
||||
// ref.read(faviconStoreProvider).favicons.add(faviconItem);
|
||||
// return faviconItem;
|
||||
// } else {
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class FaviconStore extends _$FaviconStore {
|
||||
@override
|
||||
FaviconLoaderModel build() {
|
||||
return FaviconLoaderModel(
|
||||
favicons: [],
|
||||
);
|
||||
}
|
||||
|
||||
void clearFavicons() {
|
||||
state.favicons = [];
|
||||
}
|
||||
|
||||
void loadFavicons(List<Bookmark> bookmarks) async {
|
||||
if (state.loadingFavicons == true) return;
|
||||
|
||||
state.loadingFavicons = true;
|
||||
ref.notifyListeners();
|
||||
|
||||
final mappedSaved = state.favicons.map((e) => e.url);
|
||||
final notExist = bookmarks.map((b) => b.url!).where((b) => !mappedSaved.contains(b)).toList();
|
||||
|
||||
final favicons = await compute(
|
||||
(message) => Future.wait(
|
||||
message.map(
|
||||
(e) {
|
||||
try {
|
||||
return FaviconFinder.getBest(e);
|
||||
} catch (_) {
|
||||
return Future.delayed(Duration.zero);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
notExist,
|
||||
);
|
||||
|
||||
final faviconUrls = favicons
|
||||
.asMap()
|
||||
.entries
|
||||
.map(
|
||||
(e) => e.value != null
|
||||
? FaviconItem(
|
||||
url: notExist[e.key],
|
||||
favicon: e.value!.url,
|
||||
isSvg: e.value!.url.contains('svg'),
|
||||
)
|
||||
: null,
|
||||
)
|
||||
.toList();
|
||||
faviconUrls.removeWhere((v) => v == null);
|
||||
|
||||
state.favicons = [...state.favicons, ...List<FaviconItem>.from(faviconUrls)];
|
||||
state.loadingFavicons = false;
|
||||
ref.notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'favicon_loader.provider.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$faviconStoreHash() => r'5575f5daff34748e6cae0143c8633cbbe19b6009';
|
||||
|
||||
/// See also [FaviconStore].
|
||||
@ProviderFor(FaviconStore)
|
||||
final faviconStoreProvider =
|
||||
NotifierProvider<FaviconStore, FaviconLoaderModel>.internal(
|
||||
FaviconStore.new,
|
||||
name: r'faviconStoreProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$faviconStoreHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$FaviconStore = Notifier<FaviconLoaderModel>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
@@ -3,7 +3,6 @@ import 'package:go_router/go_router.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import 'package:linkdy/screens/webview/ui/webview.dart';
|
||||
import 'package:linkdy/screens/bookmarks/provider/favicon_loader.provider.dart';
|
||||
import 'package:linkdy/screens/bookmarks/provider/bookmarks.provider.dart';
|
||||
import 'package:linkdy/screens/bookmarks/provider/common_functions.dart';
|
||||
import 'package:linkdy/screens/bookmarks/model/search_bookmarks.model.dart';
|
||||
@@ -31,7 +30,7 @@ FutureOr<void> fetchSearchBookmarks(FetchSearchBookmarksRef ref, int limit) asyn
|
||||
);
|
||||
|
||||
if (result.successful == true) {
|
||||
ref.read(faviconStoreProvider.notifier).loadFavicons(result.content!.results!);
|
||||
// ref.read(faviconStoreProvider.notifier).loadFavicons(result.content!.results!);
|
||||
ref.read(searchBookmarksProvider).bookmarks = result.content!.results!;
|
||||
ref.read(searchBookmarksProvider).maxNumber = result.content!.count!;
|
||||
ref.read(searchBookmarksProvider).currentPage = 0;
|
||||
@@ -56,7 +55,6 @@ FutureOr<void> fetchSearchBookmarksLoadMore(FetchSearchBookmarksLoadMoreRef ref)
|
||||
);
|
||||
|
||||
if (result.successful == true) {
|
||||
ref.read(faviconStoreProvider.notifier).loadFavicons(result.content!.results!);
|
||||
provider.bookmarks = [...provider.bookmarks, ...result.content!.results!];
|
||||
provider.maxNumber = result.content!.count!;
|
||||
provider.currentPage = provider.currentPage + 1;
|
||||
|
||||
Reference in New Issue
Block a user