aboutsummaryrefslogtreecommitdiffstats
path: root/lib/main.dart
blob: 6349d8d9e4f673e158be851481ad80989bbf80a7 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import 'package:flutter/material.dart';
import 'package:flutter_voiceassistant/screens/loading_screen.dart';
import 'package:provider/provider.dart';
import 'package:flutter_svg/flutter_svg.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);
  serviceStatusProvider.setWakeWord(initialServiceStatus.wakeWord);

  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

            serviceStatusProvider
                .setWakeWord(serviceStatus.wakeWord); // Update the wake word
          }
        },
      ),
    ),
  );
}

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: 'Voice Assistant',
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: config.theme == "dark" || config.theme == "textured-dark"
            ? ColorScheme.dark(
                primary: Colors.green,
                onPrimary: Colors.black, // Text color on the primary background
                secondary: Colors.greenAccent,
                onSecondary:
                    Colors.black, // Text color on the secondary background
              )
            : ColorScheme.light(
                primary: Colors.green,
                onPrimary: Colors.white, // Text color on the primary background
                secondary: Colors.greenAccent,
                onSecondary:
                    Colors.white, // Text color on the secondary background
              ),
      ),
      home: Stack(
        children: [
          Container(
            color: config.theme == "dark" || config.theme == "textured-dark"
                ? Colors.black
                : Colors
                    .white, // Set the background color based on dark or light mode
            width: double.infinity,
            height: double.infinity,
          ),
          if (config.theme == 'textured-dark' ||
              config.theme == 'textured-light')
            SvgPicture.asset(
              'assets/background_texture.svg',
              width: double.infinity,
              height: double.infinity,
              fit: BoxFit.contain,
            ),
          Consumer<ServiceStatusProvider>(
            builder: (context, provider, child) {
              return provider.isServiceOnline
                  ? Loading(config: config, wakeWord: provider.wakeWord)
                  : ErrorScreen(
                      onRetry: onRetry,
                    );
            },
          ),
        ],
      ),
    );
  }
}