Skip to content

Commit

Permalink
Remove NetworkTables Singleton (#42)
Browse files Browse the repository at this point in the history
- Pass in an NT instance to every widget requiring Network Tables data
- Improve test coverage

---------

Co-authored-by: EmeraldWither <68785503+EmeraldWither@users.noreply.github.com>
  • Loading branch information
Gold872 and EmeraldWither authored Jul 19, 2024
1 parent beb5b24 commit 153046f
Show file tree
Hide file tree
Showing 102 changed files with 7,274 additions and 1,160 deletions.
57 changes: 23 additions & 34 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import 'package:window_manager/window_manager.dart';

import 'package:elastic_dashboard/pages/dashboard_page.dart';
import 'package:elastic_dashboard/services/field_images.dart';
import 'package:elastic_dashboard/services/ip_address_util.dart';
import 'package:elastic_dashboard/services/log.dart';
import 'package:elastic_dashboard/services/nt_connection.dart';
import 'package:elastic_dashboard/services/nt_widget_builder.dart';
import 'package:elastic_dashboard/services/settings.dart';
import 'package:elastic_dashboard/widgets/settings_dialog.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -57,33 +55,12 @@ void main() async {

await windowManager.ensureInitialized();

Settings.teamNumber =
preferences.getInt(PrefKeys.teamNumber) ?? Settings.teamNumber;
Settings.ipAddressMode =
IPAddressMode.fromIndex(preferences.getInt(PrefKeys.ipAddressMode));

Settings.layoutLocked =
preferences.getBool(PrefKeys.layoutLocked) ?? Settings.layoutLocked;
Settings.gridSize =
preferences.getInt(PrefKeys.gridSize) ?? Settings.gridSize;
Settings.showGrid =
preferences.getBool(PrefKeys.showGrid) ?? Settings.showGrid;
Settings.cornerRadius =
preferences.getDouble(PrefKeys.cornerRadius) ?? Settings.cornerRadius;
Settings.autoResizeToDS =
preferences.getBool(PrefKeys.autoResizeToDS) ?? Settings.autoResizeToDS;
Settings.defaultPeriod =
preferences.getDouble(PrefKeys.defaultPeriod) ?? Settings.defaultPeriod;
Settings.defaultGraphPeriod =
preferences.getDouble(PrefKeys.defaultGraphPeriod) ??
Settings.defaultGraphPeriod;

NTWidgetBuilder.ensureInitialized();

Settings.ipAddress =
preferences.getString(PrefKeys.ipAddress) ?? Settings.ipAddress;
String ipAddress =
preferences.getString(PrefKeys.ipAddress) ?? Defaults.ipAddress;

ntConnection.nt4Connect(Settings.ipAddress);
NTConnection ntConnection = NTConnection(ipAddress);

await FieldImages.loadFields('assets/fields/');

Expand All @@ -107,7 +84,13 @@ void main() async {
await windowManager.show();
await windowManager.focus();

runApp(Elastic(version: packageInfo.version, preferences: preferences));
runApp(
Elastic(
ntConnection: ntConnection,
preferences: preferences,
version: packageInfo.version,
),
);
}

Future<void> _restoreWindowPosition(SharedPreferences preferences,
Expand Down Expand Up @@ -190,10 +173,15 @@ Future<void> _restorePreferencesFromBackup(String appFolderPath) async {
}

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

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

@override
State<Elastic> createState() => _ElasticState();
Expand All @@ -202,7 +190,7 @@ class Elastic extends StatefulWidget {
class _ElasticState extends State<Elastic> {
late Color teamColor = Color(
widget.preferences.getInt(PrefKeys.teamColor) ?? Colors.blueAccent.value);
late FlexSchemeVariant flexSchemeVariant = FlexSchemeVariant.values
late FlexSchemeVariant themeVariant = FlexSchemeVariant.values
.firstWhereOrNull((element) =>
element.variantName ==
widget.preferences.getString(PrefKeys.themeVariant)) ??
Expand All @@ -215,25 +203,26 @@ class _ElasticState extends State<Elastic> {
colorScheme: SeedColorScheme.fromSeeds(
primaryKey: teamColor,
brightness: Brightness.dark,
variant: Settings.themeVariant,
variant: themeVariant,
),
);
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Elastic',
theme: theme,
home: DashboardPage(
ntConnection: widget.ntConnection,
preferences: widget.preferences,
version: widget.version,
onColorChanged: (color) => setState(() {
teamColor = color;
widget.preferences.setInt(PrefKeys.teamColor, color.value);
}),
onThemeVariantChanged: (variant) async {
flexSchemeVariant = variant;
if (variant == SettingsDialog.defaultVariant) {
await widget.preferences.setString(
PrefKeys.themeVariant, SettingsDialog.defaultVariantName);
themeVariant = variant;
if (variant == Defaults.themeVariant) {
await widget.preferences
.setString(PrefKeys.themeVariant, Defaults.defaultVariantName);
} else {
await widget.preferences
.setString(PrefKeys.themeVariant, variant.variantName);
Expand Down
Loading

0 comments on commit 153046f

Please sign in to comment.