Skip to content

Commit

Permalink
Refactor getImageProvider related code
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Jan 5, 2025
1 parent 82a3f79 commit fea35a4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 69 deletions.
23 changes: 5 additions & 18 deletions violet/lib/pages/bookmark/crop_bookmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import 'package:material_design_icons_flutter/material_design_icons_flutter.dart
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:pull_down_button/pull_down_button.dart';
import 'package:violet/component/hentai.dart';
import 'package:violet/component/image_provider.dart';
import 'package:violet/database/user/bookmark.dart';
import 'package:violet/database/user/record.dart';
import 'package:violet/log/log.dart';
Expand Down Expand Up @@ -150,22 +148,11 @@ class _CropBookmarkPageState extends State<CropBookmarkPage> {
return FutureBuilder(
future:
Future.delayed(const Duration(milliseconds: 100)).then((value) async {
VioletImageProvider provider;

if (ProviderManager.isExists(articleId)) {
provider = await ProviderManager.get(articleId);
} else {
final query =
(await HentaiManager.idSearch(articleId.toString())).results;
provider = await HentaiManager.getImageProvider(query[0]);
await provider.init();
ProviderManager.insert(query[0].id(), provider);
}

return (
imagesUrlForEvict![index] = await provider.getImageUrl(page),
await provider.getHeader(page)
);
final provider = await getImageProviderFromId(articleId);
final image = await provider.getImageUrl(page);
final header = await provider.getHeader(page);
imagesUrlForEvict![index] = image;
return (image, header);
}),
builder:
(context, AsyncSnapshot<(String, Map<String, String>)> snapshot) {
Expand Down
32 changes: 23 additions & 9 deletions violet/lib/pages/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ Future showArticleInfoRaw({
queryResult = await HentaiManager.idQueryWeb('$id');
}

late final VioletImageProvider provider;
if (ProviderManager.isExists(id)) {
provider = await ProviderManager.get(id);
} else {
provider = await HentaiManager.getImageProvider(queryResult);
await provider.init();
ProviderManager.insert(id, provider);
}

final provider = await getImageProvider(queryResult);
final thumbnail = await provider.getThumbnailUrl();
final headers = await provider.getHeader(0);

Expand Down Expand Up @@ -100,3 +92,25 @@ Future showArticleInfoRaw({
},
);
}

Future<VioletImageProvider> getImageProviderFromId(int id) async {
if (ProviderManager.isExists(id)) {
return await ProviderManager.get(id);
}

final query = (await HentaiManager.idSearch(id.toString())).results;
return getImageProvider(query[0]);
}

Future<VioletImageProvider> getImageProvider(QueryResult queryResult) async {
final id = queryResult.id();
if (ProviderManager.isExists(id)) {
return await ProviderManager.get(id);
}

final provider = await HentaiManager.getImageProvider(queryResult);
await provider.init();
ProviderManager.insert(id, provider);

return provider;
}
42 changes: 12 additions & 30 deletions violet/lib/pages/lab/lab/search_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,12 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
return FutureBuilder(
future: Future.delayed(const Duration(milliseconds: 100))
.then((value) async {
VioletImageProvider provider;
if (ProviderManager.isExists(e.id)) {
provider = await ProviderManager.get(e.id);
} else {
final query =
(await HentaiManager.idSearch(e.id.toString()))
.results;
provider = await HentaiManager.getImageProvider(query[0]);
await provider.init();
ProviderManager.insert(query[0].id(), provider);
}
final provider = await getImageProviderFromId(e.id);
final image = await provider.getImageUrl(e.page);
final header = await provider.getHeader(e.page);
_urls![index] = image;

return (
_urls![index] = await provider.getImageUrl(e.page),
await provider.getHeader(e.page)
);
return (image, header);
}),
builder: (context,
AsyncSnapshot<(String, Map<String, String>)> snapshot) {
Expand Down Expand Up @@ -258,22 +248,10 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
onChanged: (String? value) async {
if (value == selected) return;
messages.clear();

setState(() {
selected = value!;
});

messages = (await VioletServer.searchMessage(
'contains', text.text))!;

evictImageUrls(_urls);

_height = List<double>.filled(messages.length, 0);
_keys = List<GlobalKey>.generate(
messages.length, (index) => GlobalKey());
_urls = List<String>.filled(messages.length, '');

setState(() {});
_doSearch();
},
),
),
Expand Down Expand Up @@ -367,9 +345,13 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
if (latestSearch == text.text) return;
latestSearch = text.text;
messages.clear();

setState(() {});
messages = (await VioletServer.searchMessage('contains', text.text))!;
_doSearch();
}

_doSearch() async {
messages =
(await VioletServer.searchMessage(selected.toLowerCase(), text.text))!;

evictImageUrls(_urls);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:html_unescape/html_unescape.dart';
import 'package:intl/intl.dart';
import 'package:violet/component/hentai.dart';
import 'package:violet/component/image_provider.dart';
import 'package:violet/database/user/bookmark.dart';
import 'package:violet/database/user/record.dart';
import 'package:violet/model/article_list_item.dart';
import 'package:violet/pages/common/utils.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/widgets/article_item/image_provider_manager.dart';

class ArticleListItemWidgetController extends GetxController {
final ArticleListItem articleListItem;
Expand Down Expand Up @@ -130,15 +128,7 @@ class ArticleListItemWidgetController extends GetxController {
}

setProvider() async {
VioletImageProvider provider;

if (!ProviderManager.isExists(articleListItem.queryResult.id())) {
provider =
await HentaiManager.getImageProvider(articleListItem.queryResult);
ProviderManager.insert(articleListItem.queryResult.id(), provider);
} else {
provider = await ProviderManager.get(articleListItem.queryResult.id());
}
final provider = await getImageProvider(articleListItem.queryResult);

thumbnail.value = await provider.getThumbnailUrl();
headers.value = await provider.getHeader(0);
Expand Down

0 comments on commit fea35a4

Please sign in to comment.