improve readerability handling

This commit is contained in:
Fabian Freund
2024-08-19 14:07:57 +02:00
parent 5ca9595b42
commit 8dc858af38
8 changed files with 176 additions and 136 deletions
@@ -1,33 +1,62 @@
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:lensai/features/web_view/domain/entities/consistent_controller.dart';
import 'package:lensai/features/web_view/presentation/services/readerability_script.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'readerability.g.dart';
@Riverpod()
class ReaderabilityController extends _$ReaderabilityController {
late InAppWebViewController? _controller;
late ReaderabilityScriptService _service;
late KeepAliveLink _aliveLink;
@override
FutureOr<void> build(InAppWebViewController? controller) async {
AsyncValue<({bool readerable, bool applied})> build(
ConsistentController controller,
) {
_controller = controller.value;
_service =
ref.watch(readerabilityScriptServiceProvider(controller).notifier);
_aliveLink = ref.keepAlive();
return const AsyncLoading();
}
Future<bool> isReaderable() async {
Future<bool> checkReaderable() async {
final applied = state.valueOrNull?.applied ?? false;
state = const AsyncLoading();
final result = await AsyncValue.guard(() async {
return await _service.isReaderable();
return (readerable: await _service.isReaderable(), applied: applied);
});
state = result;
return result.valueOrNull ?? false;
return result.valueOrNull?.readerable ?? false;
}
Future<void> applyReaderable() async {
Future<void> toggleReaderable() async {
final applied = state.valueOrNull?.applied ?? false;
final readerable = state.valueOrNull?.readerable ?? false;
state = const AsyncLoading();
state = await AsyncValue.guard(() async {
await _service.applyReaderable();
if (applied) {
await _controller?.reload();
} else {
await _service.applyReaderable();
}
return (readerable: readerable, applied: !applied);
});
}
void reset() {
state = const AsyncLoading();
}
void dispose() {
_aliveLink.close();
}
}