Skip to content

Commit

Permalink
Merge pull request #309 from TaYaKi71751/dev-add-ignore-timeout
Browse files Browse the repository at this point in the history
Add `ignoreTimeout` for slow environment
  • Loading branch information
violet-dev authored Jan 2, 2024
2 parents 5151606 + 6bf2790 commit d1d803e
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 40 deletions.
3 changes: 2 additions & 1 deletion assets/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
3 changes: 2 additions & 1 deletion assets/locale/eo.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
3 changes: 2 additions & 1 deletion assets/locale/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
3 changes: 2 additions & 1 deletion assets/locale/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
3 changes: 2 additions & 1 deletion assets/locale/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "실시간 유저 레코드",
"manualupdate": "수동 업데이트",
"cannotuseios": "iOS에선 사용할 수 없어요 :(",
"exitTheApp": "앱 종료하기"
"exitTheApp": "앱 종료하기",
"ignoretimeout": "타임아웃 끄기"
}
3 changes: 2 additions & 1 deletion assets/locale/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
3 changes: 2 additions & 1 deletion assets/locale/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
3 changes: 2 additions & 1 deletion assets/locale/zh_Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
3 changes: 2 additions & 1 deletion assets/locale/zh_Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,6 @@
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
"exitTheApp": "Exit the app",
"ignoretimeout": "Ignore Timeout"
}
39 changes: 24 additions & 15 deletions lib/network/wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:violet/log/log.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/thread/semaphore.dart';

class HttpWrapper {
Expand Down Expand Up @@ -95,13 +96,17 @@ Future<http.Response> _ehentaiGet(String url,
StreamedResponse response;

try {
response = await client.send(request).timeout(
const Duration(seconds: 3),
onTimeout: () {
timeout = true;
throw TimeoutException('Timeout error');
},
);
final sent = client.send(request);
if (!Settings.ignoreTimeout) {
sent.timeout(
const Duration(seconds: 3),
onTimeout: () {
timeout = true;
throw TimeoutException('Timeout error');
},
);
}
response = await sent;
} catch (e, st) {
Logger.error('[Http Request] GET: $url\n'
'E:$e\n'
Expand Down Expand Up @@ -155,14 +160,18 @@ Future<http.Response> _scriptGet(String url,
var retry = 0;
do {
isTimeout = false;
res = await http.get(Uri.parse(url), headers: headers).timeout(
timeout,
onTimeout: () {
isTimeout = true;
retry++;
return http.Response('', 200);
},
);
final sent = http.get(Uri.parse(url), headers: headers);
if (!Settings.ignoreTimeout) {
sent.timeout(
timeout,
onTimeout: () {
isTimeout = true;
retry++;
return http.Response('', 200);
},
);
}
res = await sent;
} while (isTimeout && retry < 10);
}

Expand Down
7 changes: 5 additions & 2 deletions lib/pages/search/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ class _SearchPageState extends ThemeSwitchableState<SearchPage>

doInitialSearch() async {
try {
final result = await HentaiManager.search(widget.searchKeyWord ?? '')
.timeout(const Duration(seconds: 5));
final search = HentaiManager.search(widget.searchKeyWord ?? '');
if (!Settings.ignoreTimeout) {
search.timeout(const Duration(seconds: 5));
}
final result = await search;

c.latestQuery = Tuple2(result, widget.searchKeyWord ?? '');
c.queryResult = c.latestQuery!.item1!.results;
Expand Down
35 changes: 21 additions & 14 deletions lib/pages/search/search_page_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,35 @@ class SearchPageController extends GetxController {
}

loadNextQuery() async {
await _querySem.acquire().timeout(
const Duration(seconds: 5),
onTimeout: () {
showErrorToast('Semaphore acquisition failed');

throw TimeoutException('Failed to acquire the query semaphore');
},
);
final aquire = _querySem.acquire();
if (!Settings.ignoreTimeout) {
aquire.timeout(
const Duration(seconds: 5),
onTimeout: () {
showErrorToast('Semaphore acquisition failed');

throw TimeoutException('Failed to acquire the query semaphore');
},
);
}
await aquire;

try {
if (_queryEnd ||
(latestQuery!.item1 != null && latestQuery!.item1!.offset == -1)) {
return;
}

var next = await HentaiManager.search(latestQuery!.item2,
latestQuery!.item1 == null ? 0 : latestQuery!.item1!.offset)
.timeout(const Duration(seconds: 10), onTimeout: () {
Logger.error('[Search_loadNextQuery] Search Timeout');
final search = HentaiManager.search(latestQuery!.item2,
latestQuery!.item1 == null ? 0 : latestQuery!.item1!.offset);
if (!Settings.ignoreTimeout) {
search.timeout(const Duration(seconds: 10), onTimeout: () {
Logger.error('[Search_loadNextQuery] Search Timeout');

throw TimeoutException('Failed to search the query');
});
throw TimeoutException('Failed to search the query');
});
}
var next = await search;

latestQuery = Tuple2(next, latestQuery!.item2);

Expand Down
32 changes: 32 additions & 0 deletions lib/pages/settings/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,38 @@ class _SettingsPageState extends State<SettingsPage>
});
},
),
_buildDivider(),
InkWell(
customBorder: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(8.0),
bottomRight: Radius.circular(8.0))),
child: ListTile(
leading: Image.asset(
'assets/images/logo.png',
width: 25,
height: 25,
),
title: Text(Translations.of(context).trans('ignoretimeout')),
trailing: Switch(
value: Settings.ignoreTimeout,
onChanged: (newValue) async {
await Settings.setIgnoreTimeout(newValue);
setState(() {
_shouldReload = true;
});
},
activeTrackColor: Settings.majorColor,
activeColor: Settings.majorAccentColor,
),
),
onTap: () async {
await Settings.setIgnoreTimeout(!Settings.ignoreTimeout);
setState(() {
_shouldReload = true;
});
},
),
],
),
];
Expand Down
10 changes: 10 additions & 0 deletions lib/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import 'package:violet/settings/device_type.dart';
class Settings {
static late final SharedPreferences prefs;

// Timeout Settings
static late bool ignoreTimeout; // default false

// Color Settings
static late Color themeColor; // default light
static late bool themeWhat; // default false == light
Expand Down Expand Up @@ -300,6 +303,7 @@ class Settings {
searchMessageAPI = await _getString(
'searchmessageapi', 'https://koromo.xyz/api/search/msg');

ignoreTimeout = await _getBool('ignoretimeout');
useVioletServer = await _getBool('usevioletserver');
useDrawer = await _getBool('usedrawer');
showArticleProgress = await _getBool('showarticleprogress');
Expand Down Expand Up @@ -623,6 +627,12 @@ class Settings {
await prefs.setBool('searchPure', nn);
}

static Future<void> setIgnoreTimeout(bool nn) async {
ignoreTimeout = nn;

await prefs.setBool('ignoretimeout', nn);
}

static Future<void> setUseVioletServer(bool nn) async {
useVioletServer = nn;

Expand Down

0 comments on commit d1d803e

Please sign in to comment.