-
Notifications
You must be signed in to change notification settings - Fork 814
feat: added configuration screen for luxmeter and stored settings #2769
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ddb452c
initial luxmeter config
Yugesh-Kumar-S f6189e9
Merge branch 'flutter' of https://github.com/Yugesh-Kumar-S/pslab-and…
Yugesh-Kumar-S 988c582
removed hardcoded colors
Yugesh-Kumar-S 26eb1d1
ui change
Yugesh-Kumar-S e6d8a45
applied themeing for config page and provider for instrument
Yugesh-Kumar-S fcad5f7
made the config widgets reusable for other instrument config screens.
Yugesh-Kumar-S 81ef362
Merge branch 'flutter' into config-page
Yugesh-Kumar-S ac9e4de
Update luxmeter_config_screen.dart
Yugesh-Kumar-S 4317200
Merge branch 'flutter' into config-page
Yugesh-Kumar-S 482aec3
Merge branch 'flutter' into config-page
marcnause File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| class LuxMeterConfig { | ||
| final int updatePeriod; | ||
| final int highLimit; | ||
| final String activeSensor; | ||
| final int sensorGain; | ||
| final bool includeLocationData; | ||
|
|
||
| const LuxMeterConfig({ | ||
| this.updatePeriod = 1000, | ||
| this.highLimit = 2000, | ||
| this.activeSensor = 'In-built Sensor', | ||
| this.sensorGain = 1, | ||
| this.includeLocationData = true, | ||
| }); | ||
|
|
||
| LuxMeterConfig copyWith({ | ||
| int? updatePeriod, | ||
| int? highLimit, | ||
| String? activeSensor, | ||
| int? sensorGain, | ||
| bool? includeLocationData, | ||
| }) { | ||
| return LuxMeterConfig( | ||
| updatePeriod: updatePeriod ?? this.updatePeriod, | ||
| highLimit: highLimit ?? this.highLimit, | ||
| activeSensor: activeSensor ?? this.activeSensor, | ||
| sensorGain: sensorGain ?? this.sensorGain, | ||
| includeLocationData: includeLocationData ?? this.includeLocationData, | ||
| ); | ||
| } | ||
|
|
||
| Map<String, dynamic> toJson() { | ||
| return { | ||
| 'updatePeriod': updatePeriod, | ||
| 'highLimit': highLimit, | ||
| 'activeSensor': activeSensor, | ||
| 'sensorGain': sensorGain, | ||
| 'includeLocationData': includeLocationData, | ||
| }; | ||
| } | ||
|
|
||
| factory LuxMeterConfig.fromJson(Map<String, dynamic> json) { | ||
| return LuxMeterConfig( | ||
| updatePeriod: json['updatePeriod'] ?? 1000, | ||
| highLimit: json['highLimit'] ?? 2000, | ||
| activeSensor: json['activeSensor'] ?? 'In-built Sensor', | ||
| sensorGain: json['sensorGain'] ?? 1, | ||
| includeLocationData: json['includeLocationData'] ?? true, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'dart:convert'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
| import 'package:pslab/models/luxmeter_config.dart'; | ||
|
|
||
| class LuxMeterConfigProvider extends ChangeNotifier { | ||
| LuxMeterConfig _config = const LuxMeterConfig(); | ||
|
|
||
| LuxMeterConfig get config => _config; | ||
|
|
||
| LuxMeterConfigProvider() { | ||
| _loadConfigFromPrefs(); | ||
| } | ||
|
|
||
| Future<void> _loadConfigFromPrefs() async { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| final jsonString = prefs.getString('lux_config'); | ||
| if (jsonString != null) { | ||
| final Map<String, dynamic> jsonMap = json.decode(jsonString); | ||
| _config = LuxMeterConfig.fromJson(jsonMap); | ||
| notifyListeners(); | ||
| } | ||
| } | ||
|
|
||
| Future<void> _saveConfigToPrefs() async { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| await prefs.setString('lux_config', json.encode(_config.toJson())); | ||
| } | ||
|
|
||
| void updateConfig(LuxMeterConfig newConfig) { | ||
| _config = newConfig; | ||
| notifyListeners(); | ||
| _saveConfigToPrefs(); | ||
| } | ||
|
|
||
| void updateUpdatePeriod(int updatePeriod) { | ||
| _config = _config.copyWith(updatePeriod: updatePeriod); | ||
| notifyListeners(); | ||
| _saveConfigToPrefs(); | ||
| } | ||
|
|
||
| void updateHighLimit(int highLimit) { | ||
| _config = _config.copyWith(highLimit: highLimit); | ||
| notifyListeners(); | ||
| _saveConfigToPrefs(); | ||
| } | ||
|
|
||
| void updateActiveSensor(String activeSensor) { | ||
| _config = _config.copyWith(activeSensor: activeSensor); | ||
| notifyListeners(); | ||
| _saveConfigToPrefs(); | ||
| } | ||
|
|
||
| void updateSensorGain(int sensorGain) { | ||
| _config = _config.copyWith(sensorGain: sensorGain); | ||
| notifyListeners(); | ||
| _saveConfigToPrefs(); | ||
| } | ||
|
|
||
| void updateIncludeLocationData(bool includeLocationData) { | ||
| _config = _config.copyWith(includeLocationData: includeLocationData); | ||
| notifyListeners(); | ||
| _saveConfigToPrefs(); | ||
| } | ||
|
|
||
| void resetToDefaults() { | ||
| _config = const LuxMeterConfig(); | ||
| notifyListeners(); | ||
| _saveConfigToPrefs(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:provider/provider.dart'; | ||
| import 'package:pslab/constants.dart'; | ||
| import 'package:pslab/providers/luxmeter_config_provider.dart'; | ||
| import 'package:pslab/view/widgets/config_widgets.dart'; | ||
|
|
||
| import '../theme/colors.dart'; | ||
|
|
||
| class LuxMeterConfigScreen extends StatefulWidget { | ||
| const LuxMeterConfigScreen({super.key}); | ||
|
|
||
| @override | ||
| State<LuxMeterConfigScreen> createState() => _LuxMeterConfigScreenState(); | ||
| } | ||
|
|
||
| class _LuxMeterConfigScreenState extends State<LuxMeterConfigScreen> { | ||
| final TextEditingController _updatePeriodController = TextEditingController(); | ||
| final TextEditingController _highLimitController = TextEditingController(); | ||
| final TextEditingController _sensorGainController = TextEditingController(); | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| WidgetsBinding.instance.addPostFrameCallback((_) { | ||
| final provider = | ||
| Provider.of<LuxMeterConfigProvider>(context, listen: false); | ||
| _updatePeriodController.text = provider.config.updatePeriod.toString(); | ||
| _highLimitController.text = provider.config.highLimit.toString(); | ||
| _sensorGainController.text = provider.config.sensorGain.toString(); | ||
| }); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _updatePeriodController.dispose(); | ||
| _highLimitController.dispose(); | ||
| _sensorGainController.dispose(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
| backgroundColor: Theme.of(context).colorScheme.surface, | ||
| resizeToAvoidBottomInset: true, | ||
| appBar: AppBar( | ||
| systemOverlayStyle: SystemUiOverlayStyle(statusBarColor: appBarColor), | ||
| leading: Builder(builder: (context) { | ||
| return IconButton( | ||
| onPressed: () { | ||
| if (Navigator.canPop(context) && | ||
| ModalRoute.of(context)?.settings.name == '/luxmeter') { | ||
| Navigator.popUntil(context, ModalRoute.withName('/luxmeter')); | ||
| } else { | ||
| Navigator.pushNamedAndRemoveUntil( | ||
| context, | ||
| '/luxmeter', | ||
| (route) => route.isFirst, | ||
| ); | ||
| } | ||
| }, | ||
| icon: Icon( | ||
| Icons.arrow_back, | ||
| color: appBarContentColor, | ||
| ), | ||
| ); | ||
| }), | ||
| backgroundColor: primaryRed, | ||
| title: Text( | ||
| luxmeterConfigurations, | ||
| style: TextStyle( | ||
| color: appBarContentColor, | ||
| fontSize: 15, | ||
| ), | ||
| ), | ||
| ), | ||
| body: SafeArea( | ||
| child: Padding( | ||
| padding: const EdgeInsets.symmetric(horizontal: 16.0), | ||
| child: Consumer<LuxMeterConfigProvider>( | ||
| builder: (context, provider, child) { | ||
| return SingleChildScrollView( | ||
| child: Column( | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
| children: [ | ||
| ConfigInputItem( | ||
| title: updatePeriod, | ||
| value: '${provider.config.updatePeriod} $ms', | ||
| controller: _updatePeriodController, | ||
| onChanged: (value) { | ||
| final intValue = int.tryParse(value); | ||
| if (intValue != null && | ||
| intValue >= 100 && | ||
| intValue <= 1000) { | ||
| provider.updateUpdatePeriod(intValue); | ||
| } else { | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| SnackBar( | ||
| content: Text( | ||
| updatePeriodErrorMessage, | ||
| style: TextStyle(color: snackBarContentColor), | ||
| ), | ||
| backgroundColor: snackBarBackgroundColor), | ||
| ); | ||
| } | ||
| }, | ||
| hint: updatePeriodHint, | ||
| ), | ||
| ConfigInputItem( | ||
| title: highLimit, | ||
| value: '${provider.config.highLimit} $lx', | ||
| controller: _highLimitController, | ||
| onChanged: (value) { | ||
| final intValue = int.tryParse(value); | ||
| if (intValue != null && | ||
| intValue >= 10 && | ||
| intValue <= 10000) { | ||
| provider.updateHighLimit(intValue); | ||
| } else { | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| SnackBar( | ||
| content: Text( | ||
| highLimitErrorMessage, | ||
| style: TextStyle(color: snackBarContentColor), | ||
| ), | ||
| backgroundColor: snackBarBackgroundColor), | ||
| ); | ||
| } | ||
| }, | ||
| hint: highLimitHint, | ||
| ), | ||
| ConfigDropdownItem( | ||
| title: activeSensor, | ||
| selectedValue: provider.config.activeSensor, | ||
| options: [ | ||
| ConfigOption( | ||
| value: 'In-built Sensor', | ||
| displayName: inBuiltSensor), | ||
| ConfigOption(value: 'BH1750', displayName: 'BH1750'), | ||
| ConfigOption(value: 'TSL2561', displayName: 'TSL2561'), | ||
| ], | ||
| onChanged: (value) { | ||
| provider.updateActiveSensor(value); | ||
| }, | ||
| ), | ||
| ConfigInputItem( | ||
| title: sensorGain, | ||
| value: provider.config.sensorGain.toString(), | ||
| controller: _sensorGainController, | ||
| onChanged: (value) { | ||
| final intValue = int.tryParse(value); | ||
| if (intValue != null) { | ||
| provider.updateSensorGain(intValue); | ||
| } | ||
| }, | ||
| hint: sensorGainHint, | ||
| ), | ||
| ConfigCheckboxItem( | ||
| title: locationData, | ||
| subtitle: locationDataHint, | ||
| value: provider.config.includeLocationData, | ||
| onChanged: (value) { | ||
| provider.updateIncludeLocationData(value); | ||
| }, | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| }, | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.