Skip to content

Commit

Permalink
Add setting to automatically show submit button (#200)
Browse files Browse the repository at this point in the history
- Adds a toggle that automatically shows submit button.
Off by default
addresses #199

---------

Co-authored-by: Gold87 <[email protected]>
  • Loading branch information
glolichen and Gold872 authored Feb 6, 2025
1 parent d3205fb commit 864513f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,10 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
}
setState(() {});
},
onAutoSubmitButtonChanged: (value) async {
await preferences.setBool(PrefKeys.autoTextSubmitButton, value);
setState(() {});
},
onOpenAssetsFolderPressed: () async {
Uri uri = Uri.file(
'${path.dirname(Platform.resolvedExecutable)}/data/flutter_assets/assets/');
Expand Down
1 change: 1 addition & 0 deletions lib/services/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ class PrefKeys {
static String logLevel = 'log_level';
static String gridDpiOverride = 'grid_dpi_override';
static String windowPosition = 'window_position';
static String autoTextSubmitButton = 'auto_text_submit_button';
}
9 changes: 7 additions & 2 deletions lib/widgets/nt_widgets/single_topic/text_display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:dot_cast/dot_cast.dart';
import 'package:provider/provider.dart';

import 'package:elastic_dashboard/services/nt4_client.dart';
import 'package:elastic_dashboard/services/settings.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_toggle_switch.dart';
import 'package:elastic_dashboard/widgets/nt_widgets/nt_widget.dart';

Expand Down Expand Up @@ -37,8 +38,12 @@ class TextDisplayModel extends SingleTopicNTWidgetModel {
super.dataType,
super.period,
}) : super() {
showSubmitButton ??= ntConnection.getTopicFromName(topic)?.isPersistent;
showSubmitButton ??= false;
if (preferences.getBool(PrefKeys.autoTextSubmitButton) ?? false) {
showSubmitButton ??= true;
} else {
showSubmitButton ??= ntConnection.getTopicFromName(topic)?.isPersistent;
showSubmitButton ??= false;
}
_showSubmitButton = showSubmitButton;
}

Expand Down
31 changes: 30 additions & 1 deletion lib/widgets/settings_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SettingsDialog extends StatefulWidget {
final void Function(Level? level)? onLogLevelChanged;
final FutureOr<void> Function(String? value)? onGridDPIChanged;
final void Function()? onOpenAssetsFolderPressed;
final FutureOr<void> Function(bool value)? onAutoSubmitButtonChanged;

const SettingsDialog({
super.key,
Expand All @@ -72,6 +73,7 @@ class SettingsDialog extends StatefulWidget {
this.onLogLevelChanged,
this.onGridDPIChanged,
this.onOpenAssetsFolderPressed,
this.onAutoSubmitButtonChanged,
});

@override
Expand Down Expand Up @@ -142,13 +144,15 @@ class _SettingsDialogState extends State<SettingsDialog> {
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: SingleChildScrollView(
child: ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 355),
constraints: const BoxConstraints(maxHeight: 415),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
..._themeSettings(),
const Divider(),
..._gridSettings(),
const Divider(),
..._otherSettings(),
],
),
),
Expand Down Expand Up @@ -434,6 +438,31 @@ class _SettingsDialogState extends State<SettingsDialog> {
];
}

List<Widget> _otherSettings() {
return [
const Align(
alignment: Alignment.topLeft,
child: Text('Other Settings'),
),
const SizedBox(height: 5),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(
child: DialogToggleSwitch(
initialValue: false,
label: 'Auto Show Text Submit Button',
onToggle: (value) async {
await widget.onAutoSubmitButtonChanged?.call(value);
setState(() {});
},
),
)
],
),
];
}

List<Widget> _networkTablesSettings() {
return [
const Align(
Expand Down
26 changes: 24 additions & 2 deletions test/widgets/nt_widgets/single-topic/text_display_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:elastic_dashboard/services/nt4_client.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/dialog_widgets/dialog_toggle_switch.dart';
import 'package:elastic_dashboard/widgets/draggable_containers/draggable_nt_widget_container.dart';
import 'package:elastic_dashboard/widgets/draggable_containers/models/nt_widget_container_model.dart';
Expand Down Expand Up @@ -82,7 +83,7 @@ void main() {
});

group('Show submit button defaults to', () {
test('true if topic is persistent', () {
test('true if topic is persistent', () async {
NTConnection ntConnection = createMockOnlineNT4(
virtualTopics: [
NT4Topic(
Expand All @@ -108,7 +109,28 @@ void main() {

expect(textDisplayModel.showSubmitButton, isTrue);
});
test('false if topic is not persistent', () {
test('true if text auto submit button is true', () async {
SharedPreferences.setMockInitialValues({
PrefKeys.autoTextSubmitButton: true,
});
preferences = await SharedPreferences.getInstance();

TextDisplayModel textDisplayModel = TextDisplayModel(
ntConnection: ntConnection,
preferences: preferences,
topic: 'Test/Display Value',
dataType: 'double',
period: 0.100,
);

expect(textDisplayModel.showSubmitButton, isTrue);
});
test('false if auto submit button is false', () async {
SharedPreferences.setMockInitialValues({
PrefKeys.autoTextSubmitButton: false,
});
preferences = await SharedPreferences.getInstance();

TextDisplayModel textDisplayModel = TextDisplayModel(
ntConnection: ntConnection,
preferences: preferences,
Expand Down
52 changes: 52 additions & 0 deletions test/widgets/settings_dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class FakeSettingsMethods extends Mock {
void changeGridDPIOverride();

void openAssetsFolder();

void changeAutoSubmitButton();
}

void main() {
Expand Down Expand Up @@ -80,6 +82,7 @@ void main() {
PrefKeys.defaultGraphPeriod: 0.033,
PrefKeys.themeVariant: FlexSchemeVariant.chroma.variantName,
PrefKeys.logLevel: Level.trace.levelName,
PrefKeys.autoTextSubmitButton: false,
});

preferences = await SharedPreferences.getInstance();
Expand Down Expand Up @@ -740,6 +743,55 @@ void main() {
verify(fakeSettings.changeLockLayout()).called(2);
});

testWidgets('Change auto submit button', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SettingsDialog(
preferences: preferences,
ntConnection: createMockOfflineNT4(),
onAutoSubmitButtonChanged: (value) async {
fakeSettings.changeAutoSubmitButton();

await preferences.setBool(PrefKeys.autoTextSubmitButton, value);
},
),
),
),
);

await widgetTester.pumpAndSettle();

expect(appearanceSettings, findsOneWidget);
await widgetTester.tap(appearanceSettings);
await widgetTester.pumpAndSettle();

final autoSubmitButtonSwitch =
find.widgetWithText(DialogToggleSwitch, 'Auto Show Text Submit Button');

expect(autoSubmitButtonSwitch, findsOneWidget);

// Widget tester.tap will not work for some reason
final switchWidget = find
.descendant(of: autoSubmitButtonSwitch, matching: find.byType(Switch))
.evaluate()
.first
.widget as Switch;

switchWidget.onChanged?.call(true);
await widgetTester.pumpAndSettle();

expect(preferences.getBool(PrefKeys.autoTextSubmitButton), true);

switchWidget.onChanged?.call(false);
await widgetTester.pumpAndSettle();

expect(preferences.getBool(PrefKeys.autoTextSubmitButton), false);
verify(fakeSettings.changeAutoSubmitButton()).called(2);
});

testWidgets('Change log level', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;

Expand Down

0 comments on commit 864513f

Please sign in to comment.