Improved favicons load
This commit is contained in:
@@ -1,40 +1,39 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
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];
|
||||
}
|
||||
// @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 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;
|
||||
}
|
||||
}
|
||||
// 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 {
|
||||
@@ -44,4 +43,33 @@ class FaviconStore extends _$FaviconStore {
|
||||
favicons: [],
|
||||
);
|
||||
}
|
||||
|
||||
void clearFavicons() {
|
||||
state.favicons = [];
|
||||
}
|
||||
|
||||
void loadFavicons(List<Bookmark> bookmarks) async {
|
||||
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) => FaviconFinder.getBest(e))),
|
||||
notExist,
|
||||
);
|
||||
|
||||
final faviconUrls = favicons.asMap().entries.map(
|
||||
(e) => FaviconItem(
|
||||
url: notExist[e.key],
|
||||
favicon: e.value!.url,
|
||||
isSvg: e.value!.url.contains('svg'),
|
||||
),
|
||||
);
|
||||
|
||||
state.favicons = [...state.favicons, ...faviconUrls];
|
||||
state.loadingFavicons = false;
|
||||
ref.notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user