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

Merge resonant frequency calculator improvement #14

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:coiler_app/arguments/HelicalCalculatorArgs.dart';
import 'package:coiler_app/dao/DriftCoilDao.dart';
import 'package:coiler_app/database/drift_coil_database.dart';
import 'package:coiler_app/providers/CoilProvider.dart';
import 'package:coiler_app/providers/FrequencyProvider.dart';
import 'package:coiler_app/providers/HelicalCalculatorProvider.dart';
import 'package:coiler_app/screens/calculators/capacitor_screen.dart';
import 'package:coiler_app/screens/calculators/helical_coil_screen.dart';
Expand Down Expand Up @@ -73,7 +74,10 @@ class MyApp extends StatelessWidget {
),
CalculatorsScreen.id: (context) => const CalculatorsScreen(),
CapacitorScreen.id: (context) => const CapacitorScreen(),
ResonantFrequencyScreen.id: (context) => const ResonantFrequencyScreen(),
ResonantFrequencyScreen.id: (context) => ChangeNotifierProvider<ResonantFrequencyProvider>(
create: (context) => ResonantFrequencyProvider(),
child: const ResonantFrequencyScreen(),
),
/*HelicalCoilCalculatorScreen.id: (context) =>
const HelicalCoilCalculatorScreen(),*/
CoilsListScreen.id: (context) => const CoilsListScreen(),
Expand Down
82 changes: 82 additions & 0 deletions lib/providers/FrequencyProvider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:coiler_app/calculator/calculator.dart';
import 'package:coiler_app/entities/Validation.dart';
import 'package:coiler_app/util/constants.dart';
import 'package:coiler_app/util/conversion.dart';
import 'package:flutter/cupertino.dart';

class ResonantFrequencyProvider extends ChangeNotifier {
var _inductanceValidation = InputValidation<double>(value: null, error: null);
var _capacitanceValidation = InputValidation<double>(value: null, error: null);

InputValidation get inductance => _inductanceValidation;
InputValidation get capacitance => _capacitanceValidation;

Units _inductanceUnit = Units.MICRO;
Units _capacitanceUnit = Units.MICRO;
Units _frequencyUnit = Units.KILO;

Units get capacitanceUnit => _capacitanceUnit;

Units get frequencyUnit => _frequencyUnit;

Units get inductanceUnit => _inductanceUnit;

double _frequency = 0.0;
String frequencyText = "";

void calculateFrequency() {
if (!validate()) {
return;
}
parseResultAndCalculate();
notifyListeners();
}

void parseResultAndCalculate() {
final inductanceTemp = Converter().convertToDefault(_inductanceValidation.value, _inductanceUnit);
final capacitanceTemp = Converter().convertToDefault(_capacitanceValidation.value, _capacitanceUnit);

final tempFreq = Calculator().calculateResFrequency(inductanceTemp, capacitanceTemp);

_frequency = tempFreq;

frequencyText = Converter().convertUnits(tempFreq, Units.DEFAULT, _frequencyUnit).toStringAsFixed(7);
}

void setFrequencyUnit(Units unit) {
_frequencyUnit = unit;
notifyListeners();
}

void setInductanceUnit(Units unit) {
_inductanceUnit = unit;
notifyListeners();
}

void setCapacitanceUnit(Units unit) {
_capacitanceUnit = unit;
notifyListeners();
}

void validateInductance(double? inductance) {
if (inductance != null && inductance > 0) {
_inductanceValidation = InputValidation(value: inductance, error: null);
} else {
_inductanceValidation = InputValidation(value: null, error: "Invalid inductance value.");
}
notifyListeners();
}

void validateCapacitance(double? capacitance) {
if (capacitance == null || capacitance <= 0) {
_capacitanceValidation = InputValidation(value: null, error: "Invalid capacitance value.");
} else {
_capacitanceValidation = InputValidation(value: capacitance, error: null);
}
notifyListeners();
}

bool validate() {
return (_inductanceValidation.value != null && _capacitanceValidation.value != null);
}
}
162 changes: 82 additions & 80 deletions lib/screens/calculators/resonant_freq_screen.dart
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
import 'package:coiler_app/calculator/calculator.dart';
import 'package:coiler_app/providers/FrequencyProvider.dart';
import 'package:coiler_app/util/color_constants.dart' as ColorUtil;
import 'package:coiler_app/util/constants.dart';
import 'package:coiler_app/util/conversion.dart';
import 'package:coiler_app/util/extensions/theme_extension.dart';
import 'package:coiler_app/util/list_constants.dart';
import 'package:coiler_app/widgets/border_container.dart';
import 'package:coiler_app/widgets/input_field_dropdown.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ResonantFrequencyScreen extends StatefulWidget {
const ResonantFrequencyScreen({Key? key}) : super(key: key);

static String id = "/calculators/resfreq";

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

class _ResonantFrequencyScreenState extends State<ResonantFrequencyScreen> {
final _formKey = GlobalKey<FormState>();

String inductance = "";
/*String inductance = "";
String capacitance = "";
String frequency = "";

Units inductanceUnit = Units.MICRO;
Units capacitanceUnit = Units.MICRO;
Units frequencyUnit = Units.KILO;
Units frequencyUnit = Units.KILO;*/

TextEditingController inductanceController = TextEditingController();
TextEditingController capacitanceController = TextEditingController();

void calculateFrequency() {
/*void calculateFrequency() {
if (!_formKey.currentState!.validate()) {
return;
}
var inductanceTemp = double.tryParse(inductance);
var capacitanceTemp = double.tryParse(capacitance);

if (inductanceTemp != 0 && capacitanceTemp != 0) {
var inductance = Converter()
.convertUnits(inductanceTemp, inductanceUnit, Units.DEFAULT);
var capacitance = Converter()
.convertUnits(capacitanceTemp, capacitanceUnit, Units.DEFAULT);
var inductance = Converter().convertUnits(inductanceTemp, inductanceUnit, Units.DEFAULT);
var capacitance = Converter().convertUnits(capacitanceTemp, capacitanceUnit, Units.DEFAULT);

var frequencyTemp =
Calculator().calculateResFrequency(inductance, capacitance);
var frequency =
Converter().convertUnits(frequencyTemp, Units.DEFAULT, frequencyUnit);
var frequencyTemp = Calculator().calculateResFrequency(inductance, capacitance);
var frequency = Converter().convertUnits(frequencyTemp, Units.DEFAULT, frequencyUnit);

setState(() {
this.frequency = frequency.toStringAsFixed(4);
});
}
}

*/
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
backgroundColor: Colors.white,
backgroundColor: theme.backgroundColor,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(
Expand All @@ -67,118 +65,122 @@ class _ResonantFrequencyScreenState extends State<ResonantFrequencyScreen> {
),
child: SingleChildScrollView(
child: Form(
autovalidateMode: AutovalidateMode.always,
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Align(
alignment: Alignment.center,
child: BorderContainer(
elevated: true,
child: Column(
children: [
Image.asset(
"assets/lc_circuit.png",
height: 200,
color: context.isDarkTheme() ? Colors.white : Colors.black87,
),
Text(
"Calculate resonant frequency of your LC circuit by entering values below.",
style: normalTextStyleOpenSans14,
style: theme.textTheme.displayMedium,
textAlign: TextAlign.center,
)
],
),
),
),
SizedBox(
const SizedBox(
height: 10,
),
BorderContainer(
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Frequency: $frequency"),
Text(
"Frequency: ${Provider.of<ResonantFrequencyProvider>(context).frequencyText}",
style: theme.textTheme.displaySmall,
),
Container(
padding: EdgeInsets.symmetric(
padding: const EdgeInsets.symmetric(
horizontal: 9,
),
decoration: BoxDecoration(
color: lightBlueColor,
color: context.isDarkTheme() ? Colors.grey.shade800 : ColorUtil.lightestBlue,
borderRadius: BorderRadius.circular(9),
),
child: DropdownButton<Units>(
value: frequencyUnit,
items: frequencyDropDownList,
borderRadius: BorderRadius.circular(9),
underline: Container(),
onChanged: (value) {
setState(() {
frequencyUnit = value!;
calculateFrequency();
});
}),
child: Consumer<ResonantFrequencyProvider>(
builder: (context, provider, child) {
return DropdownButton<Units>(
value: provider.frequencyUnit,
items: frequencyDropDownList,
style: theme.textTheme.displaySmall?.copyWith(fontWeight: FontWeight.bold),
borderRadius: BorderRadius.circular(9),
underline: Container(),
onChanged: (unitValue) {
provider.setFrequencyUnit(unitValue!);
provider.calculateFrequency();
},
);
},
),
),
],
),
),
SizedBox(
const SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 9,
),
child: InputFieldDropDown(
hintText: "Enter inductance",
labelText: "Inductance",
controller: inductanceController,
onTextChanged: (text) {
setState(() {
inductance = text;
calculateFrequency();
});
},
validator: (text) {
if (text == null || text.isEmpty) {
return "Input invalid";
} else {
return null;
}
},
dropDownValue: inductanceUnit,
onDropDownChanged: (value) {
setState(() {
inductanceUnit = value!;
calculateFrequency();
});
},
dropDownList: inductanceDropDownList),
child: Consumer<ResonantFrequencyProvider>(
builder: (context, provider, child) {
return InputFieldDropDown(
hintText: "Enter inductance",
labelText: "Inductance",
controller: inductanceController,
onTextChanged: (text) {
provider.validateInductance(double.tryParse(text));
provider.calculateFrequency();
},
validator: (text) => null,
errorText: provider.inductance.error,
dropDownValue: provider.inductanceUnit,
onDropDownChanged: (value) {
provider.setInductanceUnit(value!);
provider.calculateFrequency();
},
dropDownList: inductanceDropDownList,
);
},
),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 9,
),
child: InputFieldDropDown(
hintText: "Enter capacitance",
labelText: "Capacitance",
controller: capacitanceController,
onTextChanged: (text) {
setState(() {
capacitance = text;
calculateFrequency();
});
},
validator: (text) {},
dropDownValue: capacitanceUnit,
onDropDownChanged: (value) {
setState(() {
capacitanceUnit = value!;
calculateFrequency();
});
},
dropDownList: capacitanceDropDownList),
child: Consumer<ResonantFrequencyProvider>(
builder: (context, provider, child) {
return InputFieldDropDown(
hintText: "Enter capacitance",
labelText: "Capacitance",
controller: capacitanceController,
onTextChanged: (text) {
provider.validateCapacitance(double.tryParse(text));
provider.calculateFrequency();
},
validator: (text) => null,
errorText: provider.capacitance.error,
dropDownValue: provider.capacitanceUnit,
onDropDownChanged: (value) {
provider.setCapacitanceUnit(value!);
provider.calculateFrequency();
},
dropDownList: capacitanceDropDownList,
);
},
),
),
],
),
Expand Down
Loading