Skip to content

Commit

Permalink
Declare MessageSearchResult
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Jan 5, 2025
1 parent dc04a88 commit 82a3f79
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 99 deletions.
33 changes: 33 additions & 0 deletions violet/lib/component/hitomi/message_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,36 @@ class MessageSearch {
autocompleteTarget.sort((x, y) => y.$3.compareTo(x.$3));
}
}

class MessageSearchResult {
final double matchScore;
final int id;
final int page;
final double correctness;
final List<double> rect;

MessageSearchResult({
required this.matchScore,
required this.id,
required this.page,
required this.correctness,
required this.rect,
});

static List<MessageSearchResult> fromJson(String json) {
final result = jsonDecode(json) as List<dynamic>;
return result
.map(
(e) => MessageSearchResult(
matchScore: e['MatchScore'] as double,
id: e['Id'] as int,
page: e['Page'] as int,
correctness: e['Correctness'] as double,
rect: (e['Rect'] as List<dynamic>)
.map((e) => double.parse(e.toString()))
.toList(),
),
)
.toList();
}
}
85 changes: 23 additions & 62 deletions violet/lib/pages/lab/lab/search_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class LabSearchMessage extends StatefulWidget {
}

class _LabSearchMessageState extends State<LabSearchMessage> {
List<(double, int, int, double, List<double>)> messages =
<(double, int, int, double, List<double>)>[];
List<MessageSearchResult> messages = <MessageSearchResult>[];
TextEditingController text = TextEditingController(text: '은근슬쩍');
String latestSearch = '은근슬쩍';

Expand All @@ -39,20 +38,7 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
super.initState();

Future.delayed(const Duration(milliseconds: 100)).then((value) async {
var tmessages = (await VioletServer.searchMessage('contains', text.text))
as List<dynamic>;

messages = tmessages
.map((e) => (
e['MatchScore'] as double,
e['Id'] as int,
e['Page'] as int,
e['Correctness'] as double,
(e['Rect'] as List<dynamic>)
.map((e) => double.parse(e.toString()))
.toList()
))
.toList();
messages = (await VioletServer.searchMessage('contains', text.text))!;

if (_height == null) {
_height = List<double>.filled(messages.length, 0);
Expand Down Expand Up @@ -105,27 +91,25 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
cacheExtent: height * 3.0,
itemCount: messages.length,
itemBuilder: (BuildContext ctxt, int index) {
// if (messages.length == 0) return Container();
var e = messages[index];

final e = messages[index];
return FutureBuilder(
future: Future.delayed(const Duration(milliseconds: 100))
.then((value) async {
VioletImageProvider provider;
if (ProviderManager.isExists(e.$2)) {
provider = await ProviderManager.get(e.$2);
if (ProviderManager.isExists(e.id)) {
provider = await ProviderManager.get(e.id);
} else {
final query =
(await HentaiManager.idSearch(e.$2.toString()))
(await HentaiManager.idSearch(e.id.toString()))
.results;
provider = await HentaiManager.getImageProvider(query[0]);
await provider.init();
ProviderManager.insert(query[0].id(), provider);
}

return (
_urls![index] = await provider.getImageUrl(e.$3),
await provider.getHeader(e.$3)
_urls![index] = await provider.getImageUrl(e.page),
await provider.getHeader(e.page)
);
}),
builder: (context,
Expand All @@ -146,16 +130,16 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
),
),
ListTile(
title: Text('${e.$2} (${e.$3 + 1} Page)'),
subtitle: Text('Score: ${e.$1}'),
title: Text('${e.id} (${e.page + 1} Page)'),
subtitle: Text('Score: ${e.matchScore}'),
),
],
);
}
return InkWell(
onTap: () async {
FocusScope.of(context).unfocus();
showArticleInfoById(context, e.$2);
showArticleInfoById(context, e.id);
},
splashColor: Colors.white,
child: Column(
Expand Down Expand Up @@ -221,10 +205,10 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
(context, AsyncSnapshot<Size> snapshot2) {
if (!snapshot2.hasData) return Container();

var brtx = e.$5[0];
var brty = e.$5[1];
var brbx = e.$5[2];
var brby = e.$5[3];
var brtx = e.rect[0];
var brty = e.rect[1];
var brbx = e.rect[2];
var brby = e.rect[3];

var w = snapshot2.data!.width;

Expand All @@ -251,8 +235,8 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
],
),
ListTile(
title: Text('${e.$2} (${e.$3 + 1} Page)'),
subtitle: Text('Score: ${e.$1}'),
title: Text('${e.id} (${e.page + 1} Page)'),
subtitle: Text('Score: ${e.matchScore}'),
),
],
),
Expand All @@ -273,24 +257,14 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
value: selected,
onChanged: (String? value) async {
if (value == selected) return;
messages = <(double, int, int, double, List<double>)>[];
messages.clear();

setState(() {
selected = value!;
});
var tmessages = (await VioletServer.searchMessage(
selected.toLowerCase(), text.text)) as List<dynamic>;
messages = tmessages
.map((e) => (
e['MatchScore'] as double,
e['Id'] as int,
e['Page'] as int,
e['Correctness'] as double,
(e['Rect'] as List<dynamic>)
.map((e) => double.parse(e.toString()))
.toList(),
))
.toList();

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

evictImageUrls(_urls);

Expand Down Expand Up @@ -392,23 +366,10 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
Future<void> _onModifiedText() async {
if (latestSearch == text.text) return;
latestSearch = text.text;
messages = <(double, int, int, double, List<double>)>[];
messages.clear();

setState(() {});
var tmessages =
(await VioletServer.searchMessage(selected.toLowerCase(), text.text))
as List<dynamic>;
messages = tmessages
.map((e) => (
e['MatchScore'] as double,
e['Id'] as int,
e['Page'] as int,
double.parse(e['Correctness'].toString()),
(e['Rect'] as List<dynamic>)
.map((e) => double.parse(e.toString()))
.toList()
))
.toList();
messages = (await VioletServer.searchMessage('contains', text.text))!;

evictImageUrls(_urls);

Expand Down
12 changes: 6 additions & 6 deletions violet/lib/pages/viewer/vertical_viewer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,19 +411,19 @@ class _VerticalViewerPageState extends State<VerticalViewerPage>
_isExistsMessageSearchLayer(int index) {
return c.search.value &&
c.imgHeight[index] != 0 &&
c.messages.where((element) => element.$3 == index).isNotEmpty;
c.messages.where((element) => element.page == index).isNotEmpty;
}

_messageSearchLayer(int index) {
final ratio = c.imgHeight[index] / c.realImgHeight[index];
final messages =
c.messages.where((element) => element.$3 == index).toList();
c.messages.where((element) => element.page == index).toList();

final boxes = messages.map((e) {
var brtx = e.$5[0];
var brty = e.$5[1];
var brbx = e.$5[2];
var brby = e.$5[3];
var brtx = e.rect[0];
var brty = e.rect[1];
var brbx = e.rect[2];
var brby = e.rect[3];

return Positioned(
top: brty * ratio - 4,
Expand Down
28 changes: 7 additions & 21 deletions violet/lib/pages/viewer/viewer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:get/get.dart';
import 'package:violet/component/hitomi/message_search.dart';
import 'package:violet/pages/viewer/others/preload_page_view.dart';
import 'package:violet/pages/viewer/others/scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:violet/pages/viewer/viewer_page_provider.dart';
Expand Down Expand Up @@ -93,7 +94,7 @@ class ViewerController extends GetxController {
SuggestionsBoxController? suggestionsBoxController;

/// Is enabled search?
var messages = <(double, int, int, double, List<double>)>[];
var messages = <MessageSearchResult>[];
String latestSearch = '';
var messageIndex = 0.obs;

Expand Down Expand Up @@ -272,7 +273,7 @@ class ViewerController extends GetxController {
}

gotoSearchIndex() {
final index = messages[messageIndex.value - 1].$3;
final index = messages[messageIndex.value - 1].page;

jump(index);
}
Expand All @@ -281,25 +282,10 @@ class ViewerController extends GetxController {
suggestionsBoxController!.close();
if (latestSearch == searchText.text) return;
latestSearch == searchText.text;
messages = <(double, int, int, double, List<double>)>[];

final tmessages =
(await VioletServer.searchMessageWord(articleId, searchText.text))
as List<dynamic>;
messages = tmessages
.map((e) => (
e['MatchScore'] as double,
e['Id'] as int,
e['Page'] as int,
double.parse(e['Correctness'].toString()),
(e['Rect'] as List<dynamic>)
.map((e) => double.parse(e.toString()))
.toList()
))
.toList();

messages = messages.where((e) => e.$1 >= 80.0).toList();
messages.sort((a, b) => a.$3.compareTo(b.$3));
messages =
(await VioletServer.searchMessageWord(articleId, searchText.text))!;
messages = messages.where((e) => e.matchScore >= 80.0).toList();
messages.sort((a, b) => a.page.compareTo(b.page));

messageIndex.value = 1;

Expand Down
21 changes: 11 additions & 10 deletions violet/lib/server/violet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:violet/component/hitomi/message_search.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 @@ -416,45 +417,45 @@ class VioletServer {
}
}

static Future<dynamic> searchMessage(String type, String what) async {
static Future<List<MessageSearchResult>?> searchMessage(
String type, String what) async {
final gg = await http.get(
'${Settings.searchMessageAPI}/$type/${Uri.encodeFull(what)}',
headers: _vwHeader(),
);

if (gg.statusCode != 200) {
return gg.statusCode;
return null;
}

try {
final result = (jsonDecode(gg.body) as List<dynamic>);
return result;
return MessageSearchResult.fromJson(gg.body);
} catch (e, st) {
Logger.error('[API-searchMessage] E: $e\n'
'$st');

return 900;
return null;
}
}

static Future<dynamic> searchMessageWord(int articleId, String what) async {
static Future<List<MessageSearchResult>?> searchMessageWord(
int articleId, String what) async {
final gg = await http.get(
'${Settings.searchMessageAPI}/wcontains/$articleId/${Uri.encodeFull(what)}',
headers: _vwHeader(),
);

if (gg.statusCode != 200) {
return gg.statusCode;
return null;
}

try {
final result = (jsonDecode(gg.body) as List<dynamic>);
return result;
return MessageSearchResult.fromJson(gg.body);
} catch (e, st) {
Logger.error('[API-searchMessageWord] E: $e\n'
'$st');

return 900;
return null;
}
}

Expand Down

0 comments on commit 82a3f79

Please sign in to comment.