Skip to content

Commit 958e91a

Browse files
authored
Merge pull request #612 from NBA2K1/main
Various fixes
2 parents 6e53221 + 30c7442 commit 958e91a

File tree

5 files changed

+59
-28
lines changed

5 files changed

+59
-28
lines changed

lib/main.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ DiscordRPC? discordRpc;
5050
WebViewEnvironment? webViewEnvironment;
5151
String? customDns;
5252
void main(List<String> args) async {
53-
cfResolutionWebviewServer();
5453
WidgetsFlutterBinding.ensureInitialized();
5554
if (Platform.isLinux && runWebViewTitleBarWidget(args)) return;
5655
MediaKit.ensureInitialized();
@@ -93,6 +92,7 @@ Future<void> _postLaunchInit(StorageProvider storage) async {
9392
await discordRpc?.initialize();
9493
}
9594
await storage.deleteBtDirectory();
95+
await cfResolutionWebviewServer();
9696
}
9797

9898
class MyApp extends ConsumerStatefulWidget {
@@ -119,6 +119,8 @@ class _MyAppState extends ConsumerState<MyApp> {
119119

120120
WidgetsBinding.instance.addPostFrameCallback((_) {
121121
if (ref.read(clearChapterCacheOnAppLaunchStateProvider)) {
122+
// Watch before calling clearcache to keep it alive, so that _getTotalDiskSpace completes safely
123+
ref.watch(totalChapterCacheSizeStateProvider);
122124
ref
123125
.read(totalChapterCacheSizeStateProvider.notifier)
124126
.clearCache(showToast: false);
@@ -157,6 +159,7 @@ class _MyAppState extends ConsumerState<MyApp> {
157159
void dispose() {
158160
_linkSubscription?.cancel();
159161
discordRpc?.destroy();
162+
stopCfResolutionWebviewServer();
160163
AppLogger.dispose();
161164
super.dispose();
162165
}

lib/modules/anime/anime_player_view.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -964,30 +964,31 @@ mp.register_script_message('call_button_${button.id}_long', button${button.id}lo
964964
@override
965965
void dispose() {
966966
_currentPosition.removeListener(_updateRpcTimestamp);
967+
_subDelayController.removeListener(_onSubDelayChanged);
968+
_subSpeedController.removeListener(_onSubSpeedChanged);
967969
WidgetsBinding.instance.removeObserver(this);
968970
_setCurrentPosition(true);
969-
_player.dispose();
971+
_player.stop();
972+
_completed.cancel();
970973
_currentPositionSub.cancel();
971974
_currentTotalDurationSub.cancel();
972-
_completed.cancel();
975+
_currentPosition.dispose();
976+
_currentTotalDuration.dispose();
973977
_video.dispose();
974978
_playbackSpeed.dispose();
975979
_isDoubleSpeed.dispose();
976-
_currentTotalDuration.dispose();
977980
_showFitLabel.dispose();
978981
_isCompleted.dispose();
979982
_tempPosition.dispose();
980983
_fit.dispose();
981-
if (!_isDesktop) {
982-
_setLandscapeMode(false);
983-
}
984984
_skipPhase.dispose();
985-
discordRpc?.showIdleText();
986-
discordRpc?.showOriginalTimestamp();
987-
_currentPosition.dispose();
988985
_subDelayController.dispose();
989986
_subSpeedController.dispose();
987+
if (!_isDesktop) _setLandscapeMode(false);
988+
discordRpc?.showIdleText();
989+
discordRpc?.showOriginalTimestamp();
990990
_streamController.keepAliveLink?.close();
991+
_player.dispose();
991992
super.dispose();
992993
}
993994

lib/modules/manga/detail/providers/update_manga_detail_providers.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Future<dynamic> updateMangaDetail(
3535

3636
final genre =
3737
getManga.genre
38-
?.map((e) => e.toString().trim().trimLeft().trimRight())
38+
?.map((e) => e.toString().trim())
3939
.toList()
4040
.toSet()
4141
.toList() ??
@@ -80,7 +80,7 @@ Future<dynamic> updateMangaDetail(
8080
for (var i = 0; i < newChapsIndex; i++) {
8181
final chapter = Chapter(
8282
name: chaps[i].name!,
83-
url: chaps[i].url!.trim().trimLeft().trimRight(),
83+
url: chaps[i].url!.trim(),
8484
dateUpload: chaps[i].dateUpload == null
8585
? DateTime.now().millisecondsSinceEpoch.toString()
8686
: chaps[i].dateUpload.toString(),
@@ -171,8 +171,8 @@ Future<dynamic> updateMangaDetail(
171171

172172
extension DefaultValueExtension on String? {
173173
String? trimmedOrDefault(String? defaultValue) {
174-
if (this?.trim().trimLeft().trimRight().isNotEmpty ?? false) {
175-
return this!.trim().trimLeft().trimRight();
174+
if (this?.trim().isNotEmpty ?? false) {
175+
return this!.trim();
176176
}
177177
return defaultValue;
178178
}

lib/modules/more/data_and_storage/providers/storage_usage.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ part 'storage_usage.g.dart';
1414
class TotalChapterCacheSizeState extends _$TotalChapterCacheSizeState {
1515
@override
1616
String build() {
17-
_getTotalDiskSpace().then((value) => state = _formatBytes(value));
17+
_getTotalDiskSpace().then((value) {
18+
if (!ref.mounted) return;
19+
state = _formatBytes(value);
20+
});
1821
return "0.00 B";
1922
}
2023

lib/services/http/m_client.dart

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,44 @@ class ResolveCloudFlareChallenge extends RetryPolicy {
292292
}
293293

294294
int cfPort = 0;
295-
void cfResolutionWebviewServer() async {
296-
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, cfPort);
297-
cfPort = server.port;
295+
HttpServer? _cfServer;
298296

299-
server.listen((HttpRequest request) {
300-
if (request.method == 'POST' && request.uri.path == '/resolve_cf') {
301-
_handleResolveCf(request);
302-
} else {
303-
request.response
304-
..statusCode = HttpStatus.notFound
305-
..write('Not Found')
306-
..close();
307-
}
308-
});
297+
/// Cloudflare Resolution Webview Server
298+
Future<void> cfResolutionWebviewServer() async {
299+
try {
300+
_cfServer = await HttpServer.bind(InternetAddress.loopbackIPv4, cfPort);
301+
cfPort = _cfServer!.port;
302+
_cfServer!.listen(
303+
(HttpRequest request) {
304+
if (request.method == 'POST' && request.uri.path == '/resolve_cf') {
305+
_handleResolveCf(request);
306+
} else {
307+
request.response
308+
..statusCode = HttpStatus.notFound
309+
..write('Not Found')
310+
..close();
311+
}
312+
},
313+
onError: (e, st) {
314+
debugPrint("CF server listener error: $e\n$st");
315+
},
316+
cancelOnError: false,
317+
);
318+
} catch (e, st) {
319+
debugPrint("Couldn't start Cloudflare Resolution Webview Server: $e\n$st");
320+
botToast("Couldn't start Cloudflare Resolution Webview Server.");
321+
}
322+
}
323+
324+
Future<void> stopCfResolutionWebviewServer() async {
325+
final server = _cfServer;
326+
if (server == null) return;
327+
try {
328+
await server.close(force: true);
329+
} finally {
330+
_cfServer = null;
331+
cfPort = 0;
332+
}
309333
}
310334

311335
void _handleResolveCf(HttpRequest request) async {

0 commit comments

Comments
 (0)