Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
not done

ok

save content; sse

separatorBuilder

ok

optmize

save icon color
  • Loading branch information
liushihao committed Sep 26, 2023
1 parent 1af0fa2 commit 065b486
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 272 deletions.
57 changes: 27 additions & 30 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:fsearch_flutter/service/request.dart';
import 'package:fsearch_flutter/service/search_ws_no_web.dart'
if (dart.library.html) 'package:fsearch_flutter/service/search_ws_web.dart';

import 'package:fsearch_flutter/service/types.dart';
import 'package:fsearch_flutter/util/github_logo.dart';
Expand Down Expand Up @@ -57,10 +55,9 @@ class MyHomePage extends StatefulWidget {

class _MyHomePageState extends State<MyHomePage> {
NodeConfigInfo nodeConfigInfo = NodeConfigInfo();

String appName = '';
int nodeId = 0;

String textFilter = '';
List<String> get appNames => nodeConfigInfo.appNames;

List<String> appNamesWithFilter(String filter) {
Expand All @@ -73,8 +70,6 @@ class _MyHomePageState extends State<MyHomePage> {
return items;
}

List<String> get hosts => nodeConfigInfo.hosts(appName);

List<String> get allFiles => nodeConfigInfo.allFiles(appName);

List<String> get files => nodeConfigInfo.files(appName, nodeId);
Expand Down Expand Up @@ -128,11 +123,11 @@ class _MyHomePageState extends State<MyHomePage> {
}
setState(() {});
},
title: SelectableText(items[index - 1]));
title: Text(items[index - 1]));
});
}

String textFilter = '';


void aboutOnTap() async {
String version = "0.0.1";
Expand Down Expand Up @@ -161,6 +156,21 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

changeThemeMode() {
switch (prefs.themeMode) {
case ThemeMode.system:
break;
case ThemeMode.light:
prefs.themeMode = ThemeMode.dark;
RestartApp.restart(context);
break;
case ThemeMode.dark:
prefs.themeMode = ThemeMode.light;
RestartApp.restart(context);
break;
}
}

List<Widget> buildRadioAppNames() {
List<Widget> children = [
ListTile(
Expand All @@ -174,6 +184,10 @@ class _MyHomePageState extends State<MyHomePage> {
icon: const Icon(Icons.refresh)),
),
CupertinoSearchTextField(
style: TextStyle(
color:
prefs.themeMode == ThemeMode.light ? Colors.black : Colors.white,
),
onChanged: (v) {
textFilter = v;
setState(() {});
Expand Down Expand Up @@ -213,12 +227,12 @@ class _MyHomePageState extends State<MyHomePage> {
final infos = nodeConfigInfo.nodeInfos(appName);
for (var info in infos) {
items.add(RadioListTile<int>(
title: SelectableText(info.hostName),
title: Text(info.hostName),
value: info.nodeId,
groupValue: nodeId,
onChanged: (v) {
nodeId = v ?? nodeId;
fileCheckMap.clear();
// fileCheckMap.clear();
setState(() {});
}));
}
Expand All @@ -230,8 +244,6 @@ class _MyHomePageState extends State<MyHomePage> {
super.dispose();
}

String get searchPathWS => nodeConfigInfo.searchPathWS;

@override
void initState() {
super.initState();
Expand All @@ -241,29 +253,14 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

changeThemeMode() {
switch (prefs.themeMode) {
case ThemeMode.system:
break;
case ThemeMode.light:
prefs.themeMode = ThemeMode.dark;
RestartApp.restart(context);
break;
case ThemeMode.dark:
prefs.themeMode = ThemeMode.light;
RestartApp.restart(context);
break;
}
}

@override
Widget build(BuildContext context) {
final mediaHeight = MediaQuery.of(context).size.height;
final right = TextSearchRegion(
appName: appName,
nodeId: nodeId,
files: fileChecked,
searchPathWS: searchPathWS,
searchPathSSE: nodeConfigInfo.searchPathSSE,
);
final Widget body = Row(
mainAxisSize: MainAxisSize.min,
Expand Down
20 changes: 0 additions & 20 deletions lib/service/request.dart

This file was deleted.

70 changes: 45 additions & 25 deletions lib/service/search_ws_no_web.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:fsearch_flutter/service/service.dart';
import 'package:fsearch_flutter/service/types.dart';

import '../util/util.dart';

Future<StreamSubscription<dynamic>?> searchText({
required String appName,
required String searchPathWS,
required String searchPathSSE,
required int nodeId,
required List<String> files,
required List<String> kw,
Expand All @@ -19,37 +24,52 @@ Future<StreamSubscription<dynamic>?> searchText({
'nodeId': "$nodeId",
"files": files,
"kw": kw,
'dataType': 'json',
};
final dio = Dio();
final query = Uri(queryParameters: queryParameters).query;
myPrint(query);
final Map<String, dynamic> header = {};
header["Origin"] = "http://127.0.0.1";
WebSocket socket;
ResponseBody? data;
try {
// Because there may be a large amount of content transmitted, we use websocket
// to transmit the search result content in batches instead of using http
// please modify the url below in online environment
final String url = 'ws://127.0.0.1:9097$searchPathWS?$query';
socket = await WebSocket.connect(url,
protocols: ["mychat"],
headers: header,
customClient: HttpClient()
..userAgent = null
..connectionTimeout = const Duration(seconds: 3, milliseconds: 500));
Response<ResponseBody> response = await dio.get<ResponseBody>(
"$searchPathSSE?$query",
options: Options(
headers: {
"Accept": "text/event-stream",
"Cache-Control": "no-cache",
},
responseType: ResponseType.stream,
sendTimeout:
const Duration(seconds: 3)), // set responseType to `stream`
);
myPrint("--------------==== ${response.data}");
data = response.data;
} catch (e) {
myPrint("--------------==== $e");
return null;
}

final listener = socket.listen((dynamic event) {
if (event is String) {
onData(event);
} else if (event is List<int>) {
onData(utf8.decode(event));
} else {
myPrint("unknown data type");
socket.close();
}
if (data == null) return null;
final listener = data.stream.listen((Uint8List event) {
// myPrint(event);
onData(utf8.decode(event));
}, cancelOnError: true);
return listener;
}

Future<NodeConfigInfo> homeInfo(BuildContext context) async {
String url = const String.fromEnvironment("CONFIG_PATH", defaultValue: "");
if (url == "") {
throw "no config for CONFIG_PATH";
}
final RespData<NodeConfigInfo> result = await dioTryGet(context, url,
queryParameters: {}, fromJson: NodeConfigInfo.fromJson);
if (result.code != 0) {
myToast(context, result.message);
return NodeConfigInfo();
}
return result.data ?? NodeConfigInfo();
}

void saveContent(String content, String fileName) {
throw "implement me";
}
88 changes: 62 additions & 26 deletions lib/service/search_ws_web.dart
Original file line number Diff line number Diff line change
@@ -1,65 +1,101 @@
import 'dart:async';
import 'dart:convert';
import 'dart:html';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:fsearch_flutter/service/service.dart';
import 'package:fsearch_flutter/service/types.dart';

import '../util/util.dart';

Future<StreamSubscription<dynamic>?> searchText({
required String appName,
required String searchPathWS,
required String searchPathSSE,
required int nodeId,
required List<String> files,
required List<String> kw,
required void Function(String data) onData,
void Function()? onClose,
}) async {
// in browsers, you need to pass a http.BrowserClient:
// todo 服务端适配协议
WebSocket ws;

final params = <String, dynamic>{
'appName': appName,
'nodeId': "$nodeId",
'files': files,
'kw': kw,
'dataType': 'text',
};
final path = searchPathWS;
final path = searchPathSSE;
final query = Uri(queryParameters: params).query;
// Because there may be a large amount of content transmitted, we use websocket
// to transmit the search result content in batches instead of using http
String url = "ws://${window.location.host}$path?$query";
String url = "$path?$query";
if (kDebugMode) {
// you can modify the port 9097 to that really is
url = "ws://127.0.0.1:9097$path?$query";
url = 'http://127.0.0.1:9097$url';
}
myPrint("url is : $url");
EventSource eventSource;
try {
ws = WebSocket(url);
eventSource = EventSource(url);
} catch (e) {
myPrint("-------- $e");
return null;
}
ws.onError.listen((Event event) {
myPrint("websocket error $event");

ws.close();
});
ws.onOpen.listen((Event event) {
myPrint("websocket open");
});
ws.onClose.listen((Event event) {
myPrint("websocket close");
eventSource.onError.listen((Event event) {
myPrint(
"----========onError========---------- ${event.type}${event} ${event.path}");
eventSource.close();
if (onClose != null) {
onClose();
}
});

final listener = ws.onMessage.listen((MessageEvent event) {
eventSource.onOpen.listen((Event event) {
myPrint("----========open========---------- ${event.type} ${event.path}");
});
final listener = eventSource.onMessage.listen((MessageEvent event) {
onData(event.data);
myPrint("---------------------- websocket onMessage -------------------");
}, onError: (e) {
myPrint("-------->>> $e");
}, cancelOnError: true);
// window.alert("SSE链接成功");
return listener;
}

Future<NodeConfigInfo> homeInfo(BuildContext context) async {
String url;
const internConfig = "/_internal/config";
final pathname = window.location.pathname ?? '/';
if (pathname == "/") {
url = internConfig;
} else if (pathname.endsWith("/")) {
// /user/home/
String temp = pathname.substring(0, pathname.length - 1);
temp = temp.substring(0, temp.lastIndexOf("/"));
url = "$temp$internConfig";
} else {
// /user/home
String temp = pathname;
temp = temp.substring(0, temp.lastIndexOf("/"));
url = "$temp$internConfig";
}
// FIXME
if (kDebugMode) {
url = 'http://127.0.0.1:9097/_internal/config';
}
print("config url: $url");
final RespData<NodeConfigInfo> result = await dioTryGet(context, url,
queryParameters: {}, fromJson: NodeConfigInfo.fromJson);
if (result.code != 0) {
myToast(context, result.message);
return NodeConfigInfo();
}
return result.data ?? NodeConfigInfo();
}

void saveContent(String content, String fileName) {
final downLink = AnchorElement();
downLink.download = fileName;
final blob = Blob([content]);
downLink.href = Url.createObjectUrlFromBlob(blob);
document.body?.append(downLink);
downLink.click();
downLink.remove();
}
4 changes: 0 additions & 4 deletions lib/service/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class RespData<T> {
}
}



Future<RespData<T>> dioTryGet<T>(
BuildContext? context,
String www, {
Expand All @@ -63,8 +61,6 @@ Future<RespData<T>> dioTryGet<T>(
return RespData(code: -1);
}
final String? respData = respBody.data;
myPrint(respData);

if (respData == null) return RespData(code: -1);
return RespData.fromJson(jsonDecode(respData), fromJson);
} catch (e) {
Expand Down
Loading

0 comments on commit 065b486

Please sign in to comment.