aboutsummaryrefslogtreecommitdiffstats
path: root/lib/main.dart
blob: bc5b84b72148114e4f875bd89a86f26062ab5f2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'models/app_state.dart';
import 'screens/home_screen.dart';
import 'screens/error_screen.dart';
import 'providers/service_status.dart';
import 'grpc/generated/voice_agent.pbgrpc.dart';
import 'grpc/voice_agent_client.dart';
import 'utils/app_config.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final serviceStatusProvider = ServiceStatusProvider();

  // get config variables
  final config = await AppConfig.loadFromAsset();

  // Check the service status initially
  final initialServiceStatus = await checkServiceStatus(config);
  serviceStatusProvider.setServiceStatus(initialServiceStatus.status);

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => AppState()),
        ChangeNotifierProvider.value(value: serviceStatusProvider),
      ],
      child: App(
        config: config,
        onRetry: () async {
          final serviceStatus = await checkServiceStatus(
              config); // Retry connecting to the server

          if (serviceStatus.status) {
            serviceStatusProvider
                .setServiceStatus(true); // Update the service status
          }
        },
      ),
    ),
  );
}

Future<ServiceStatus> checkServiceStatus(AppConfig config) async {
  final client = VoiceAgentClient(config.grpcHost, config.grpcPort);
  final serviceStatus = await client.checkServiceStatus();
  client.shutdown();
  return serviceStatus;
}

class App extends StatelessWidget {
  final AppConfig config;
  final VoidCallback onRetry;

  const App({Key? key, required this.config, required this.onRetry});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AGL Voice Assistant',
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
      ),
      home: Consumer<ServiceStatusProvider>(
        builder: (context, provider, child) {
          return provider.isServiceOnline
              ? HomePage(config: config)
              : ErrorScreen(
                  onRetry: onRetry, // Pass the callback to the ErrorScreen
                ); // Conditionally render HomePage or ErrorScreen
        },
      ),
    );
  }
}