Skip to content

Commit

Permalink
fixed/removed saving multiple records
Browse files Browse the repository at this point in the history
  • Loading branch information
luhluh-17 committed May 31, 2020
1 parent d135e6a commit 7f78abb
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() async {
final appDirectory = await path.getApplicationDocumentsDirectory();
Hive.init(appDirectory.path);
Hive.registerAdapter<Record>(RecordAdapter());
await Hive.openBox<Record>('records');
await Hive.openBox<Record>(boxRecord);
runApp(
MultiProvider(
providers: [
Expand Down
2 changes: 2 additions & 0 deletions lib/models/record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:hive/hive.dart';

part 'record.g.dart';

const String boxRecord = 'record';

@HiveType(typeId: 0)
class Record {
@HiveField(0)
Expand Down
15 changes: 8 additions & 7 deletions lib/pages/calculator_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class _CalculatorPageState extends State<CalculatorPage> {
Widget build(BuildContext context) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light.copyWith(
statusBarColor: Colors.transparent,
systemNavigationBarColor: AppColors.darkShadow
),
statusBarColor: Colors.transparent,
systemNavigationBarColor: AppColors.darkShadow),
child: Material(
color: AppColors.baseColor,
child: Padding(
Expand All @@ -46,10 +45,12 @@ class _CalculatorPageState extends State<CalculatorPage> {
Hero(
tag: 'iconButton',
child: CustomIconButton(
Icons.history,
icon: Icons.history,
size: 25.0,
onPressed: () =>
Navigator.pushNamed(context, RecordsPage.id),
onPressed: () => Navigator.pushNamed(
context,
RecordsPage.id,
),
),
),
],
Expand All @@ -67,7 +68,7 @@ class _CalculatorPageState extends State<CalculatorPage> {

@override
void dispose() {
Hive.box<Record>('records')
Hive.box<Record>(boxRecord)
..compact()
..close();
super.dispose();
Expand Down
8 changes: 4 additions & 4 deletions lib/pages/records_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:neucalcu/widgets/record_container.dart';
class RecordsPage extends StatelessWidget {
static const String id = '/records';

final recordBox = Hive.box<Record>('records');
final recordBox = Hive.box<Record>(boxRecord);

@override
Widget build(BuildContext context) {
Expand All @@ -32,22 +32,22 @@ class RecordsPage extends StatelessWidget {
child: Hero(
tag: 'iconButton',
child: CustomIconButton(
Icons.arrow_back_ios,
icon: Icons.arrow_back_ios,
onPressed: () => Navigator.pop(context),
),
),
),
Text(
'Record History',
style: TextStyle(
color: Colors.white54,
color: AppColors.primaryText,
fontSize: sizeSubHead2,
),
),
Positioned(
right: 0,
child: CustomIconButton(
Icons.delete,
icon: Icons.delete,
onPressed: () {
recordBox..clear();
Navigator.pop(context);
Expand Down
15 changes: 7 additions & 8 deletions lib/providers/calculate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,25 @@ class Calculate with ChangeNotifier {
_calculateExpression(isPreviewActive: false);

if (!(_result == 'Syntax Error')) {
_saveRecord();
_equation = _result;
String equation = _equation;
String answer = _equation = _result;
_result = 'Answer';
_saveRecord(answer: answer, equation: equation);
}
}

void _saveRecord() {
_saveRecord({String answer, String equation}) {
DateTime now = new DateTime.now();
DateFormat formatter = new DateFormat('MMMM dd, yyyy');
String formattedDate = formatter.format(now);

Record record = Record(
answer: _result,
equation: _equation,
answer: answer,
equation: equation,
date: formattedDate,
);

if (!(_result == 'Answer')) {
Hive.box<Record>('records').add(record);
}
Hive.box<Record>(boxRecord).add(record);
}

_calculateExpression({bool isPreviewActive}) {
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/custom_icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomIconButton extends StatelessWidget {
final double size;
final Function onPressed;

CustomIconButton(this.icon, {this.size = 20.0, @required this.onPressed});
CustomIconButton({this.icon, this.size = 20.0, @required this.onPressed});

static const double _buttonSize = 40.0;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A new Flutter application.

publish_to: 'none'

version: 1.0.1+2
version: 1.1.0+3

environment:
sdk: ">=2.7.0 <3.0.0"
Expand Down

0 comments on commit 7f78abb

Please sign in to comment.