LifeMate — Personal, Family & Learning Companion
test/personal-notification-settings
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | import 'dart:async'; |
| 2 | |
| 3 | import 'package:flutter/foundation.dart'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'package:webview_flutter/webview_flutter.dart'; |
| 6 | import 'package:webview_flutter_android/webview_flutter_android.dart'; |
| 7 | |
| 8 | import 'aparat_player_policy.dart'; |
| 9 | import 'aparat_player_port.dart'; |
| 10 | import 'aparat_player_fullscreen.dart'; |
| 11 | |
| 12 | const AparatPlayerPort defaultAparatPlayerPort = _NativePlayerPort(); |
| 13 | |
| 14 | class _NativePlayerPort implements AparatPlayerPort { |
| 15 | const _NativePlayerPort(); |
| 16 | |
| 17 | @override |
| 18 | bool get isSupported => |
| 19 | defaultTargetPlatform == TargetPlatform.android || |
| 20 | defaultTargetPlatform == TargetPlatform.iOS; |
| 21 | |
| 22 | @override |
| 23 | Widget buildFrame(BuildContext context, |
| 24 | {required Uri uri, |
| 25 | required String title, |
| 26 | required VoidCallback onFrameReady, |
| 27 | required ValueChanged<String> onError}) => |
| 28 | _AparatWebView(uri: uri, onFrameReady: onFrameReady, onError: onError); |
| 29 | } |
| 30 | |
| 31 | class _AparatWebView extends StatefulWidget { |
| 32 | const _AparatWebView( |
| 33 | {required this.uri, required this.onFrameReady, required this.onError}); |
| 34 | |
| 35 | final Uri uri; |
| 36 | final VoidCallback onFrameReady; |
| 37 | final ValueChanged<String> onError; |
| 38 | |
| 39 | @override |
| 40 | State<_AparatWebView> createState() => _AparatWebViewState(); |
| 41 | } |
| 42 | |
| 43 | class _AparatWebViewState extends State<_AparatWebView> { |
| 44 | WebViewController? _controller; |
| 45 | Timer? _timeout; |
| 46 | bool _failed = false; |
| 47 | AparatFullscreenPresenter? _fullscreen; |
| 48 | |
| 49 | @override |
| 50 | void initState() { |
| 51 | super.initState(); |
| 52 | unawaited(_initialize()); |
| 53 | } |
| 54 | |
| 55 | void _fail(String code) { |
| 56 | if (!mounted || _failed) return; |
| 57 | _failed = true; |
| 58 | _timeout?.cancel(); |
| 59 | widget.onError(code); |
| 60 | } |
| 61 | |
| 62 | Future<void> _initialize() async { |
| 63 | try { |
| 64 | final hash = widget.uri.pathSegments[4]; |
| 65 | if (!isAllowedAparatNavigation(widget.uri.toString(), hash)) { |
| 66 | _fail('invalid_frame_address'); |
| 67 | return; |
| 68 | } |
| 69 | final controller = WebViewController( |
| 70 | onPermissionRequest: (request) => unawaited(request.deny())); |
| 71 | _controller = controller; |
| 72 | await controller.setJavaScriptMode(JavaScriptMode.unrestricted); |
| 73 | if (controller.platform is AndroidWebViewController) { |
| 74 | final android = controller.platform as AndroidWebViewController; |
| 75 | await android.setMediaPlaybackRequiresUserGesture(true); |
| 76 | await android.setGeolocationEnabled(false); |
| 77 | await android.setMixedContentMode(MixedContentMode.neverAllow); |
| 78 | final cookies = WebViewCookieManager(); |
| 79 | await (cookies.platform as AndroidWebViewCookieManager) |
| 80 | .setAcceptThirdPartyCookies(android, false); |
| 81 | await android.setCustomWidgetCallbacks( |
| 82 | onShowCustomWidget: (view, returnNativeView) { |
| 83 | if (!mounted) { |
| 84 | returnNativeView(); |
| 85 | return; |
| 86 | } |
| 87 | _fullscreen ??= |
| 88 | AparatFullscreenPresenter(navigator: Navigator.of(context)); |
| 89 | _fullscreen!.show(view, returnNativeView); |
| 90 | }, |
| 91 | onHideCustomWidget: () => _fullscreen?.hide()); |
| 92 | } |
| 93 | await controller.setNavigationDelegate(NavigationDelegate( |
| 94 | onNavigationRequest: (request) => !request.isMainFrame || |
| 95 | isAllowedAparatNavigation(request.url, hash) |
| 96 | ? NavigationDecision.navigate |
| 97 | : NavigationDecision.prevent, |
| 98 | onPageFinished: (url) { |
| 99 | if (!mounted || _failed || !isAllowedAparatNavigation(url, hash)) { |
| 100 | return; |
| 101 | } |
| 102 | _timeout?.cancel(); |
| 103 | widget.onFrameReady(); |
| 104 | }, |
| 105 | onWebResourceError: (error) { |
| 106 | if (error.isForMainFrame == true || |
| 107 | (error.isForMainFrame == null && |
| 108 | error.url == widget.uri.toString())) { |
| 109 | _fail('frame_load_failed'); |
| 110 | } |
| 111 | }, |
| 112 | onHttpError: (error) { |
| 113 | if (error.request?.uri == widget.uri) { |
| 114 | _fail('frame_http_error'); |
| 115 | } |
| 116 | }, |
| 117 | onSslAuthError: (error) => unawaited(error.cancel()))); |
| 118 | if (!mounted) return; |
| 119 | setState(() {}); |
| 120 | _timeout = |
| 121 | Timer(const Duration(seconds: 30), () => _fail('frame_load_timeout')); |
| 122 | // This is the only controller-initiated remote load; no app headers, |
| 123 | // JavaScript bridge, cached auth or service-issued media URL is added. |
| 124 | await controller.loadRequest(widget.uri); |
| 125 | if (!mounted) await controller.loadHtmlString(''); |
| 126 | } catch (_) { |
| 127 | _fail('frame_initialization_failed'); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @override |
| 132 | Widget build(BuildContext context) => _controller == null |
| 133 | ? const Center(child: Text('در حال بارگذاری قاب آپارات…')) |
| 134 | : WebViewWidget(controller: _controller!); |
| 135 | |
| 136 | @override |
| 137 | void dispose() { |
| 138 | _timeout?.cancel(); |
| 139 | _fullscreen?.dispose(); |
| 140 | // Blank the external document as the native platform view is removed. |
| 141 | // Controller loads are not navigation-delegate redirects. |
| 142 | unawaited(_controller?.loadHtmlString('').catchError((_) {})); |
| 143 | super.dispose(); |
| 144 | } |
| 145 | } |