// ignore_for_file: library_private_types_in_public_api import 'dart:async'; import 'package:demo_ai_even/ble_manager.dart'; import 'package:demo_ai_even/services/evenai.dart'; import 'package:demo_ai_even/views/even_list_page.dart'; import 'package:demo_ai_even/views/features_page.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; class HomePage extends StatefulWidget { const HomePage({super.key}); @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State { Timer? scanTimer; bool isScanning = false; @override void initState() { super.initState(); BleManager.get().setMethodCallHandler(); BleManager.get().startListening(); BleManager.get().onStatusChanged = _refreshPage; } void _refreshPage() => setState(() {}); Future _startScan() async { setState(() => isScanning = true); await BleManager.get().startScan(); scanTimer?.cancel(); scanTimer = Timer(15.seconds, () { _stopScan(); }); } Future _stopScan() async { if (isScanning) { await BleManager.get().stopScan(); setState(() => isScanning = false); } } Widget blePairedList() => Expanded( child: ListView.separated( separatorBuilder: (context, index) => const SizedBox(height: 5), itemCount: BleManager.get().getPairedGlasses().length, itemBuilder: (context, index) { final glasses = BleManager.get().getPairedGlasses()[index]; return GestureDetector( onTap: () async { String channelNumber = glasses['channelNumber']!; await BleManager.get().connectToGlasses("Pair_$channelNumber"); _refreshPage(); }, child: Container( height: 72, padding: const EdgeInsets.only(left: 16, right: 16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(5), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('Pair: ${glasses['channelNumber']}'), Text( 'Left: ${glasses['leftDeviceName']} \nRight: ${glasses['rightDeviceName']}'), ], ), ], ), ), ); }, ), ); @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: const Text('Even AI Demo'), actions: [ InkWell( onTap: () { print("To Features Page..."); Navigator.push( context, MaterialPageRoute(builder: (context) => const FeaturesPage()), ); }, splashColor: Colors.transparent, highlightColor: Colors.transparent, child: const Padding( padding: EdgeInsets.only(left: 16, top: 12, bottom: 14, right: 16), child: Icon(Icons.menu), ), ), ], ), body: Padding( padding: const EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 44), child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ GestureDetector( onTap: () async { if (BleManager.get().getConnectionStatus() == 'Not connected') { _startScan(); } }, child: Container( height: 100, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(5), ), alignment: Alignment.center, child: Text(BleManager.get().getConnectionStatus(), style: const TextStyle(fontSize: 16)), ), ), const SizedBox(height: 16), if (BleManager.get().getConnectionStatus() == 'Not connected') blePairedList(), if (BleManager.get().isConnected) Expanded( child: Column( children: [ // Test button for displaying text on glasses Container( width: double.infinity, height: 60, margin: const EdgeInsets.only(bottom: 16), child: ElevatedButton( onPressed: () async { try { await BleManager.get().displayTextOnGlasses("Hello G1!"); ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("Text sent to glasses!")), ); } catch (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text("Error: $e")), ); } }, style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), child: const Text( "Display Text on G1 Glasses", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), ), ), // AI History Section Expanded( child: GestureDetector( onTap: () async { print("To AI History List..."); Navigator.push( context, MaterialPageRoute( builder: (context) => const EvenAIListPage(), ), ); }, child: Container( color: Colors.white, padding: const EdgeInsets.all(16), alignment: Alignment.topCenter, child: SingleChildScrollView( child: StreamBuilder( stream: EvenAI.textStream, initialData: "Press and hold left TouchBar to engage Even AI.", builder: (context, snapshot) => Obx( () => EvenAI.isEvenAISyncing.value ? const SizedBox( width: 50, height: 50, child: CircularProgressIndicator(), ) : Text( snapshot.data ?? "Loading...", style: TextStyle( fontSize: 14, color: BleManager.get().isConnected ? Colors.black : Colors.grey.withOpacity(0.5)), textAlign: TextAlign.center, ), ), ), ), ), ), ), ], ), ), ], ), ), ); @override void dispose() { scanTimer?.cancel(); isScanning = false; BleManager.get().onStatusChanged = null; super.dispose(); } }