import 'dart:convert'; import 'dart:developer'; import 'package:eatvio_app/core/app_config.dart'; import 'package:eatvio_app/core/locator_service.dart'; import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_cubit.dart'; import 'package:eatvio_app/features/registration/services/registration_analytics_service.dart'; import 'package:flutter/material.dart'; import 'package:webview_flutter/webview_flutter.dart'; class WebViewRegistrationScreen extends StatefulWidget { const WebViewRegistrationScreen({super.key}); @override State createState() => _WebViewRegistrationScreenState(); } class _WebViewRegistrationScreenState extends State { late final WebViewController _controller; late final AuthCubit authCubit; String? email; String? password; @override void initState() { super.initState(); authCubit = sl(); // Report webview registration started RegistrationAnalyticsService.reportRegistrationStep(18); // Webview registration step _controller = WebViewController() ..setJavaScriptMode(JavaScriptMode.unrestricted) ..addJavaScriptChannel( 'flutter_inject', onMessageReceived: (JavaScriptMessage message) { final data = jsonDecode(message.message); if (data['type'] == 'email') { email = data['value']; log("Email: $email"); } else if (data['type'] == 'password') { password = data['value']; log("Password: $password"); } }, ) ..setNavigationDelegate( NavigationDelegate( onNavigationRequest: (NavigationRequest request) { if (request.url.contains('profile')) { // Report webview registration completion RegistrationAnalyticsService.reportRegistrationComplete(); authCubit.getTokenByCredentials(email!, password!); } return NavigationDecision.navigate; }, ), ) ..loadRequest(Uri.parse('${sl().baseUrl}/registration/step1')); _injectJavaScript(); } void _injectJavaScript() async { await Future.delayed(const Duration(seconds: 2)); _controller.runJavaScript(''' function sendMessageToFlutter(data) { window.flutter_inject.postMessage(JSON.stringify(data)); } function trackInputChanges() { let emailField = document.getElementById('inputEmail'); let passwordField = document.getElementById('inputPassword'); if (emailField && passwordField) { emailField.addEventListener('input', function() { sendMessageToFlutter({ type: 'email', value: emailField.value }); }); passwordField.addEventListener('input', function() { sendMessageToFlutter({ type: 'password', value: passwordField.value }); }); } } trackInputChanges(); '''); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(toolbarHeight: 0), body: SafeArea(child: WebViewWidget(controller: _controller)), ); } }