aboutsummaryrefslogtreecommitdiffstats
path: root/lib/homescreen.dart
blob: be9c7ca0e521198b98394595567834f5299a7d3a (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_homescreen/config.dart';
import 'package:grpc/grpc.dart';
import 'package:flutter_homescreen/generated/applauncher.pbgrpc.dart';
import 'package:flutter_homescreen/page_apps.dart';
import 'package:flutter_homescreen/widget_clock.dart';
import 'package:flutter_homescreen/bottom_panel.dart';
import 'package:flutter_homescreen/vehicle-signals/vss_client.dart';
import 'package:flutter_homescreen/vehicle-signals/vss_provider.dart';

enum PageIndex { home, dashboard, hvac, media }

class Homescreen extends ConsumerStatefulWidget {
  Homescreen({Key? key}) : super(key: key);

  @override
  _HomescreenState createState() => _HomescreenState();
}

class _HomescreenState extends ConsumerState<Homescreen> with TickerProviderStateMixin {
  int _selectedIndex = 0;
  int _previousIndex = 0;

  late ClientChannel channel;
  late AppLauncherClient stub;
  List<String> apps_stack = [];
  static const agl_shell_channel = MethodChannel('flutter/agl_shell');
  late VssClient vss;

  Future<List<AppInfo>> getAppList() async {
    var response = await stub.listApplications(ListRequest());
    for (AppInfo info in response.apps) {
      debugPrint("Got app:");
      debugPrint("$info");
    }
    return response.apps;
    return [];
  }

  addAppToStack(String id) {
    if (!apps_stack.contains(id)) {
      apps_stack.add(id);
    } else {
      int current_pos = apps_stack.indexOf(id);
      if (current_pos != (apps_stack.length - 1)) {
        apps_stack.removeAt(current_pos);
        apps_stack.add(id);
      }
    }
  }

  activateApp(String id) async {
    try {
      agl_shell_channel
          .invokeMethod('activate_app', {'app_id': id, 'index': 0});
    } catch (e) {
      print('Could not invoke flutter/agl_shell/activate_app: $e');
    }
    addAppToStack(id);
  }

  deactivateApp(String id) async {
    if (apps_stack.contains(id)) {
      apps_stack.remove(id);
      if (apps_stack.isNotEmpty) {
        activateApp(apps_stack.last);
      }
    }
  }

  handleAppStatusEvents() async {
    try {
      var response = stub.getStatusEvents(StatusRequest());
      await for (var event in response) {
        if (event.hasApp()) {
          AppStatus app_status = event.app;
          debugPrint("Got app status:");
          debugPrint("$app_status");
          if (app_status.hasId() && app_status.hasStatus()) {
            if (app_status.status == "started") {
              activateApp(app_status.id);
            } else if (app_status.status == "terminated") {
              deactivateApp(app_status.id);
            }
          }
        }
      }
    } catch (e) {
      print(e);
    }
  }

  initState() {
    //debugPrint("_HomescreenState.initState!");
    channel = ClientChannel('localhost',
        port: 50052,
        options: ChannelOptions(credentials: ChannelCredentials.insecure()));

    stub = AppLauncherClient(channel);

    handleAppStatusEvents();

    vss = ref.read(vssClientProvider);
    vss.run();

    super.initState();
  }

  void startApp(String id) async {
    await stub.startApplication(StartRequest(id: id));
  }

  setNavigationIndex(int index) {
    switch (PageIndex.values[index]) {
      case PageIndex.dashboard:
        startApp("dashboard_app");
        return;
      case PageIndex.hvac:
        startApp("flutter_hvac");
        return;
      case PageIndex.media:
        startApp("mediaplayer");
        return;
      default:
        setState(() {
          _previousIndex = _selectedIndex;
          _selectedIndex = index;
        });
        activateApp("homescreen");
    }
  }

  Widget _childForIndex(int selectedIndex) {
    switch (PageIndex.values[selectedIndex]) {
      case PageIndex.home:
        return AppsPage(
            key: ValueKey(selectedIndex),
            getApps: getAppList,
            startApp: startApp);
      default:
        return Text('Undefined');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Center(
            child: LayoutBuilder(
      builder: _buildLayout,
    )));
  }

  Widget _buildLayout(BuildContext context, BoxConstraints constraints) {
    var railSize = 160.0;
    var iconSize = railSize / 2;
    var foregroundColor = Theme.of(context)
        .navigationBarTheme
        .iconTheme!
        .resolve({MaterialState.pressed})!.color!;

    return Scaffold(
      body: Column(
        children: <Widget>[
          IntrinsicHeight(
            child: Row(children: <Widget>[
              Theme(
                data: Theme.of(context).copyWith(
                  // Disable indicator (for now?)
                  navigationBarTheme: NavigationBarTheme.of(context)
                      .copyWith(indicatorColor: Colors.transparent),
                  // Disable splash animations
                  splashFactory: NoSplash.splashFactory,
                  hoverColor: Colors.transparent,
                ),
                child: Expanded(
                  child: NavigationBar(
                      onDestinationSelected: (int index) {
                        setState(() {
                          setNavigationIndex(index);
                        });
                      },
                      selectedIndex: _selectedIndex,
                      height: railSize,
                      animationDuration: Duration(seconds: 0),
                      destinations: <Widget>[
                        NavigationDestination(
                          icon: Icon(Icons.home, size: iconSize),
                          label: 'Home',
                        ),
                        NavigationDestination(
                          icon: Icon(Icons.drive_eta, size: iconSize),
                          label: 'Dashboard',
                        ),
                        NavigationDestination(
                          icon: Icon(Icons.thermostat, size: iconSize),
                          label: 'HVAC',
                        ),
                        NavigationDestination(
                          icon: Icon(Icons.music_note, size: iconSize),
                          label: 'Media',
                        ),
                      ]),
                ),
              ),
              SizedBox(
                  width: 128,
                  child: Container(
                      color: NavigationBarTheme.of(context).backgroundColor)),
              Container(
                  color: NavigationBarTheme.of(context).backgroundColor,
                  child: VerticalDivider(
                      width: 32,
                      thickness: 1,
                      color: foregroundColor,
                      indent: railSize / 16,
                      endIndent: railSize / 16)),
              Container(
                  color: NavigationBarTheme.of(context).backgroundColor,
                  child:
                      ClockWidget(textColor: foregroundColor, size: railSize)),
              Container(
                  color: NavigationBarTheme.of(context).backgroundColor,
                  child: VerticalDivider(
                      width: 32,
                      thickness: 1,
                      color: foregroundColor,
                      indent: railSize / 16,
                      endIndent: railSize / 16)),
              Container(
                  color: NavigationBarTheme.of(context).backgroundColor,
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Icon(Icons.bluetooth, color: foregroundColor, size: 32),
                        Icon(Icons.wifi, color: foregroundColor, size: 32),
                        Icon(Icons.signal_cellular_4_bar,
                            color: foregroundColor, size: 32),
                      ])),
              SizedBox(
                  width: 16,
                  child: Container(
                      color: Theme.of(context)
                          .navigationBarTheme
                          .backgroundColor)),
            ]),
          ),
          // This is the main content.
          Expanded(
            child: AnimatedSwitcher(
              duration: const Duration(milliseconds: 500),
              reverseDuration: const Duration(milliseconds: 500),
              switchInCurve: Curves.easeInOut,
              switchOutCurve: Curves.easeInOut,
              transitionBuilder: (Widget child, Animation<double> animation) {
                if (child.key != ValueKey(_selectedIndex)) {
                  return FadeTransition(
                    opacity:
                        Tween<double>(begin: 1.0, end: 1.0).animate(animation),
                    child: child,
                  );
                }
                Offset beginOffset = new Offset(
                    0.0, (_selectedIndex > _previousIndex ? 1.0 : -1.0));
                return SlideTransition(
                  position: Tween<Offset>(begin: beginOffset, end: Offset.zero)
                      .animate(animation),
                  child: FadeTransition(
                    opacity: Tween<double>(begin: 0.0, end: 1.0).animate(
                      CurvedAnimation(
                        parent: animation,
                        curve: Interval(0.5, 1.0),
                      ),
                    ),
                    child: child,
                  ),
                );
              },
              child: _childForIndex(_selectedIndex),
            ),
          ),
          BottomPanelWidget(
              height: railSize,
              color: NavigationBarTheme.of(context).backgroundColor
          )
      ])
    );
  }
}