Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove NetworkTables Singleton #42

Merged
merged 42 commits into from
Jul 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d0bb545
Removed singleton NT instance
Gold872 Apr 22, 2024
b908c3d
Began work on passing shared instances
Gold872 Apr 22, 2024
e5c9d4c
Separately pass nt instance and preferences
Gold872 Apr 23, 2024
18154e6
Pass a shared preferences instance to every widget
Gold872 Apr 23, 2024
1013556
Change IP address mode to work off of shared preferences
Gold872 Apr 23, 2024
4b0fddb
Fixed unit tests
Gold872 Apr 23, 2024
8d0bfc9
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Apr 29, 2024
c926da0
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 May 18, 2024
6655d8c
Began adding unit tests to nt widgets
Gold872 May 18, 2024
45d44f8
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 May 19, 2024
6f4a20b
Manage subscription sharing through nt connection wrapper
Gold872 May 22, 2024
049a4bb
Added encoder widget test
Gold872 May 25, 2024
9b3da1a
Added FMSInfo test
Gold872 May 25, 2024
0b47d85
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 May 25, 2024
a26d988
Removed broken method
Gold872 May 25, 2024
ef6c48e
Added tests for gyro, motor controller, network alerts, and pid contr…
Gold872 May 25, 2024
3d8dee3
Added tests for pdp, profiled pid controller, and relay
Gold872 May 25, 2024
41fe484
Added test for robot preferences and fixed search bug
Gold872 May 25, 2024
893e4f8
Added remaining multi topic widget tests
Gold872 May 25, 2024
9ecf59b
Make NTWidgetBuilder constructor private
Gold872 May 25, 2024
60a5349
Added boolean box, graph, and match time tests
Gold872 May 26, 2024
51c5c25
Added more single topic widget tests
Gold872 May 26, 2024
0ee90fc
Fixed number slider tests
Gold872 May 26, 2024
69c28ef
Added text display test
Gold872 May 26, 2024
4f7e64c
added bool and bool array tests for text display
Gold872 May 26, 2024
bc9a76a
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 May 26, 2024
2d6b7c6
Replaced deprecated APIs
Gold872 May 26, 2024
87bd0a4
Merge branch 'replace-deprecated-apis' of https://github.com/Gold872/…
Gold872 May 26, 2024
73de192
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 May 26, 2024
b9f1fe6
Added remaining single topic widget tests
Gold872 May 26, 2024
9e8d40c
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Jun 25, 2024
0f43387
Refactor notifications to new API changes
Gold872 Jun 25, 2024
c28656d
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Jun 25, 2024
ecef2d9
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Jun 25, 2024
160e933
Restructure tab grid state (#47)
Gold872 Jun 25, 2024
cb6cba1
Fixed unit tests
Gold872 Jun 25, 2024
d669002
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Jun 29, 2024
b0d5f56
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Jul 12, 2024
942f7b0
Create unit tests for robot notifications (#51)
EmeraldWither Jul 12, 2024
9bea89e
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Jul 18, 2024
8c40ba8
Changed location of default theme variables
Gold872 Jul 18, 2024
c598093
Merge branch 'main' of https://github.com/Gold872/elastic-dashboard i…
Gold872 Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Separately pass nt instance and preferences
Gold872 committed Apr 23, 2024
commit e5c9d4c2c922698f7ba9019aa596c2a86ae3dcd2
24 changes: 13 additions & 11 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -108,10 +108,8 @@ void main() async {

runApp(
Elastic(
elasticData: (
ntConnection: ntConnection,
preferences: preferences,
),
ntConnection: ntConnection,
preferences: preferences,
version: packageInfo.version,
),
);
@@ -197,19 +195,23 @@ Future<void> _restorePreferencesFromBackup(String appFolderPath) async {
}

class Elastic extends StatefulWidget {
final ElasticSharedData elasticData;
final NTConnection ntConnection;
final SharedPreferences preferences;
final String version;

const Elastic({super.key, required this.elasticData, required this.version});
const Elastic(
{super.key,
required this.ntConnection,
required this.preferences,
required this.version});

@override
State<Elastic> createState() => _ElasticState();
}

class _ElasticState extends State<Elastic> {
late Color teamColor = Color(
widget.elasticData.preferences.getInt(PrefKeys.teamColor) ??
Colors.blueAccent.value);
widget.preferences.getInt(PrefKeys.teamColor) ?? Colors.blueAccent.value);

@override
Widget build(BuildContext context) {
@@ -223,12 +225,12 @@ class _ElasticState extends State<Elastic> {
title: 'Elastic',
theme: theme,
home: DashboardPage(
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
version: widget.version,
onColorChanged: (color) => setState(() {
teamColor = color;
widget.elasticData.preferences
.setInt(PrefKeys.teamColor, color.value);
widget.preferences.setInt(PrefKeys.teamColor, color.value);
}),
),
);
66 changes: 42 additions & 24 deletions lib/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
@@ -33,16 +33,16 @@ import 'package:elastic_dashboard/widgets/settings_dialog.dart';
import 'package:elastic_dashboard/widgets/tab_grid.dart';
import '../widgets/draggable_containers/models/layout_container_model.dart';

typedef ElasticSharedData = ({NTConnection ntConnection, SharedPreferences preferences});

class DashboardPage extends StatefulWidget {
final String version;
final Function(Color color)? onColorChanged;
final ElasticSharedData elasticData;
final NTConnection ntConnection;
final SharedPreferences preferences;

const DashboardPage({
super.key,
required this.elasticData,
required this.ntConnection,
required this.preferences,
required this.version,
this.onColorChanged,
});
@@ -52,7 +52,7 @@ class DashboardPage extends StatefulWidget {
}

class _DashboardPageState extends State<DashboardPage> with WindowListener {
late final SharedPreferences _preferences = widget.elasticData.preferences;
late final SharedPreferences _preferences = widget.preferences;
late final UpdateChecker _updateChecker;

final List<TabGrid> _grids = [];
@@ -82,7 +82,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {

_setupShortcuts();

widget.elasticData.ntConnection.dsClientConnect(
widget.ntConnection.dsClientConnect(
onIPAnnounced: (ip) async {
if (Settings.ipAddressMode != IPAddressMode.driverStation) {
return;
@@ -94,7 +94,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
return;
}

widget.elasticData.ntConnection.changeIPAddress(ip);
widget.ntConnection.changeIPAddress(ip);
},
onDriverStationDockChanged: (docked) {
if (Settings.autoResizeToDS && docked) {
@@ -105,15 +105,15 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
},
);

widget.elasticData.ntConnection.addConnectedListener(() {
widget.ntConnection.addConnectedListener(() {
setState(() {
for (TabGrid grid in _grids) {
grid.onNTConnect();
}
});
});

widget.elasticData.ntConnection.addDisconnectedListener(() {
widget.ntConnection.addDisconnectedListener(() {
setState(() {
for (TabGrid grid in _grids) {
grid.onNTDisconnect();
@@ -122,7 +122,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
});

ShuffleboardNTListener apiListener = ShuffleboardNTListener(
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onTabChanged: (tab) {
int? parsedTabIndex = int.tryParse(tab);

@@ -162,7 +163,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
_grids.add(
TabGrid(
key: GlobalKey(),
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onAddWidgetPressed: _displayAddWidgetDialog,
),
);
@@ -185,7 +187,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
_tabData.add(TabData(name: tabName));
_grids.add(TabGrid(
key: GlobalKey(),
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onAddWidgetPressed: _displayAddWidgetDialog,
));

@@ -207,7 +210,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
Future.delayed(const Duration(seconds: 1), () {
apiListener.initializeSubscriptions();
apiListener.initializeListeners();
widget.elasticData.ntConnection.recallTopicAnnounceListeners();
widget.ntConnection.recallTopicAnnounceListeners();
});

Future(() => _checkForUpdates(notifyIfLatest: false, notifyIfError: false));
@@ -569,7 +572,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
_grids.add(
TabGrid.fromJson(
key: GlobalKey(),
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
jsonData: data['grid_layout'],
onAddWidgetPressed: _displayAddWidgetDialog,
onJsonLoadingWarning: _showJsonLoadingWarning,
@@ -596,12 +600,14 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
_grids.addAll([
TabGrid(
key: GlobalKey(),
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onAddWidgetPressed: _displayAddWidgetDialog,
),
TabGrid(
key: GlobalKey(),
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onAddWidgetPressed: _displayAddWidgetDialog,
),
]);
@@ -751,7 +757,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
_grids.add(
TabGrid(
key: GlobalKey(),
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onAddWidgetPressed: _displayAddWidgetDialog,
),
);
@@ -875,7 +882,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
showDialog(
context: context,
builder: (context) => SettingsDialog(
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onTeamNumberChanged: (String? data) async {
if (data == null) {
return;
@@ -915,7 +923,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {

switch (mode) {
case IPAddressMode.driverStation:
String? lastAnnouncedIP = widget.elasticData.ntConnection.dsClient.lastAnnouncedIP;
String? lastAnnouncedIP =
widget.ntConnection.dsClient.lastAnnouncedIP;

if (lastAnnouncedIP == null) {
break;
@@ -1040,7 +1049,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
},
onResizeToDSChanged: (value) async {
setState(() {
if (value && widget.elasticData.ntConnection.dsClient.driverStationDocked) {
if (value && widget.ntConnection.dsClient.driverStationDocked) {
_onDriverStationDocked();
} else {
_onDriverStationUndocked();
@@ -1101,7 +1110,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
await _preferences.setString(PrefKeys.ipAddress, newIPAddress);

setState(() {
widget.elasticData.ntConnection.changeIPAddress(newIPAddress);
widget.ntConnection.changeIPAddress(newIPAddress);
});
}

@@ -1473,7 +1482,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
_tabData.add(tab);
_grids.add(TabGrid(
key: GlobalKey(),
elasticData: widget.elasticData,
ntConnection: widget.ntConnection,
preferences: widget.preferences,
onAddWidgetPressed: _displayAddWidgetDialog,
));
});
@@ -1505,6 +1515,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
tabViews: _grids,
),
_AddWidgetDialog(
ntConnection: widget.ntConnection,
preferences: widget.preferences,
grid: () => _grids[_currentTabIndex],
visible: _addWidgetDialogVisible,
onNTDragUpdate: (globalPosition, widget) {
@@ -1538,7 +1550,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
children: [
Expanded(
child: StreamBuilder(
stream: widget.elasticData.ntConnection.connectionStatus(),
stream: widget.ntConnection.connectionStatus(),
builder: (context, snapshot) {
bool connected = snapshot.data ?? false;

@@ -1563,7 +1575,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
),
Expanded(
child: StreamBuilder(
stream: widget.elasticData.ntConnection.latencyStream(),
stream: widget.ntConnection.latencyStream(),
builder: (context, snapshot) {
int latency = snapshot.data ?? 0;

@@ -1585,6 +1597,8 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
}

class _AddWidgetDialog extends StatefulWidget {
final NTConnection ntConnection;
final SharedPreferences preferences;
final TabGrid Function() _grid;
final bool _visible;

@@ -1599,6 +1613,8 @@ class _AddWidgetDialog extends StatefulWidget {
final Function()? _onClose;

const _AddWidgetDialog({
required this.ntConnection,
required this.preferences,
required TabGrid Function() grid,
required bool visible,
required dynamic Function(Offset, WidgetContainerModel) onNTDragUpdate,
@@ -1656,6 +1672,8 @@ class _AddWidgetDialogState extends State<_AddWidgetDialog> {
child: TabBarView(
children: [
NetworkTableTree(
ntConnection: widget.ntConnection,
preferences: widget.preferences,
listLayoutBuilder: (
{required title, required children}) {
return widget._grid().createListLayout(
8 changes: 8 additions & 0 deletions lib/services/nt_connection.dart
Original file line number Diff line number Diff line change
@@ -153,6 +153,14 @@ class NTConnection {
return _ntClient.getTopicFromName(topic);
}

void publishTopic(NT4Topic topic) {
_ntClient.publishTopic(topic);
}

NT4Topic publishNewTopic(String name, String type) {
return _ntClient.publishNewTopic(name, type);
}

bool isTopicPublished(NT4Topic? topic) {
return _ntClient.isTopicPublished(topic);
}
22 changes: 14 additions & 8 deletions lib/services/nt_widget_builder.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:elastic_dashboard/pages/dashboard_page.dart';
import 'package:flutter/material.dart';

import 'package:dot_cast/dot_cast.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:elastic_dashboard/services/log.dart';
import 'package:elastic_dashboard/services/nt_connection.dart';
import 'package:elastic_dashboard/services/settings.dart';
import 'package:elastic_dashboard/widgets/draggable_containers/draggable_widget_container.dart';
import 'package:elastic_dashboard/widgets/nt_widgets/multi-topic/accelerometer.dart';
@@ -50,7 +51,7 @@ class NTWidgetBuilder {
static final Map<
String,
NTWidgetModel Function({
required ElasticSharedData elasticData,
required NTConnection ntConnection,
required String topic,
String dataType,
double period,
@@ -59,7 +60,7 @@ class NTWidgetBuilder {
static final Map<
String,
NTWidgetModel Function({
required ElasticSharedData elasticData,
required NTConnection ntConnection,
required Map<String, dynamic> jsonData,
})> _modelJsonBuildMap = {};

@@ -321,6 +322,7 @@ class NTWidgetBuilder {
}

static NTWidgetModel buildNTModelFromType(
NTConnection ntConnection,
String type,
String topic, {
String dataType = 'Unknown',
@@ -332,34 +334,38 @@ class NTWidgetBuilder {

if (_modelNameBuildMap.containsKey(type)) {
return _modelNameBuildMap[type]!(
ntConnection: ntConnection,
topic: topic,
dataType: dataType,
period: period,
);
}

return NTWidgetModel.createDefault(
ntConnection: ntConnection,
type: type,
topic: topic,
dataType: dataType,
period: period,
);
}

static NTWidgetModel buildNTModelFromJson(
ElasticSharedData elasticData,
String type, Map<String, dynamic> jsonData,
static NTWidgetModel buildNTModelFromJson(NTConnection ntConnection,
SharedPreferences preferences, String type, Map<String, dynamic> jsonData,
{Function(String message)? onWidgetTypeNotFound}) {
ensureInitialized();

if (_modelJsonBuildMap.containsKey(type)) {
return _modelJsonBuildMap[type]!(jsonData: jsonData);
return _modelJsonBuildMap[type]!(
ntConnection: ntConnection,
jsonData: jsonData,
);
}

onWidgetTypeNotFound
?.call('Unknown widget type: \'$type\', defaulting to Empty Model.');
return NTWidgetModel.createDefault(
elasticData: elasticData,
ntConnection: ntConnection,
type: type,
topic: tryCast(jsonData['topic']) ?? '',
dataType: tryCast(jsonData['data_type']) ?? 'Unknown',
Loading