From 36a7b905dee79e2640da98f0463745b0cd337e36 Mon Sep 17 00:00:00 2001 From: Marcin Szalek Date: Sun, 27 Aug 2017 00:20:56 +0200 Subject: [PATCH] Added firebase database integration --- android/app/build.gradle | 4 +- android/app/src/main/AndroidManifest.xml | 2 +- .../weight_tracker/MainActivity.java | 2 +- android/build.gradle | 1 + lib/home_page.dart | 63 +++++++++++++------ lib/main.dart | 6 +- lib/model/weight_entry.dart | 20 +++++- pubspec.yaml | 15 ++--- 8 files changed, 75 insertions(+), 38 deletions(-) rename android/app/src/main/java/com/{yourcompany => mszalek}/weight_tracker/MainActivity.java (89%) diff --git a/android/app/build.gradle b/android/app/build.gradle index 9aa9bc7..bc09fbf 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -12,6 +12,8 @@ if (flutterRoot == null) { } apply plugin: 'com.android.application' +apply plugin: 'com.google.gms.google-services' + apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { @@ -26,7 +28,7 @@ android { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "com.yourcompany.weight_tracker" + applicationId "com.mszalek.weight_tracker" } buildTypes { diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index b0e78c9..93fee44 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ diff --git a/android/app/src/main/java/com/yourcompany/weight_tracker/MainActivity.java b/android/app/src/main/java/com/mszalek/weight_tracker/MainActivity.java similarity index 89% rename from android/app/src/main/java/com/yourcompany/weight_tracker/MainActivity.java rename to android/app/src/main/java/com/mszalek/weight_tracker/MainActivity.java index ca00c84..f8d638d 100644 --- a/android/app/src/main/java/com/yourcompany/weight_tracker/MainActivity.java +++ b/android/app/src/main/java/com/mszalek/weight_tracker/MainActivity.java @@ -1,4 +1,4 @@ -package com.yourcompany.weight_tracker; +package com.mszalek.weight_tracker; import android.os.Bundle; import io.flutter.app.FlutterActivity; diff --git a/android/build.gradle b/android/build.gradle index ee5325d..f7c11f0 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -5,6 +5,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:2.2.3' + classpath 'com.google.gms:google-services:3.1.0' } } diff --git a/lib/home_page.dart b/lib/home_page.dart index 960090a..cef974a 100644 --- a/lib/home_page.dart +++ b/lib/home_page.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/material.dart'; import 'package:weight_tracker/model/weight_entry.dart'; import 'package:weight_tracker/weight_entry_dialog.dart'; @@ -13,11 +14,18 @@ class HomePage extends StatefulWidget { _HomePageState createState() => new _HomePageState(); } +final mainReference = FirebaseDatabase.instance.reference(); + class _HomePageState extends State { List weightSaves = new List(); ScrollController _listViewScrollController = new ScrollController(); double _itemExtent = 50.0; + _HomePageState() { + mainReference.onChildAdded.listen(_onEntryAdded); + mainReference.onChildChanged.listen(_onEntryEdited); + } + @override Widget build(BuildContext context) { return new Scaffold( @@ -35,7 +43,7 @@ class _HomePageState extends State { ? 0.0 : weightSaves[index].weight - weightSaves[index - 1].weight; return new InkWell( - onTap: () => _editEntry(weightSaves[index]), + onTap: () => _openEditEntryDialog(weightSaves[index]), child: new WeightListItem(weightSaves[index], difference)); }, ), @@ -47,45 +55,60 @@ class _HomePageState extends State { ); } - void _addWeightSave(WeightEntry weightSave) { - setState(() { - weightSaves.add(weightSave); - _listViewScrollController.animateTo( - weightSaves.length * _itemExtent, - duration: const Duration(microseconds: 1), - curve: new ElasticInCurve(0.01), - ); - }); - } - - _editEntry(WeightEntry weightSave) { + _openEditEntryDialog(WeightEntry weightEntry) { Navigator .of(context) .push( new MaterialPageRoute( builder: (BuildContext context) { - return new WeightEntryDialog.edit(weightSave); + return new WeightEntryDialog.edit(weightEntry); }, fullscreenDialog: true, ), ) - .then((newSave) { - if (newSave != null) { - setState(() => weightSaves[weightSaves.indexOf(weightSave)] = newSave); + .then((WeightEntry newEntry) { + if (newEntry != null) { + mainReference.child(weightEntry.key).set(newEntry.toJson()); } }); } Future _openAddEntryDialog() async { - WeightEntry save = + WeightEntry entry = await Navigator.of(context).push(new MaterialPageRoute( builder: (BuildContext context) { return new WeightEntryDialog.add( weightSaves.isNotEmpty ? weightSaves.last.weight : 60.0); }, fullscreenDialog: true)); - if (save != null) { - _addWeightSave(save); + if (entry != null) { + mainReference.push().set(entry.toJson()); } } + + _onEntryAdded(Event event) { + setState(() { + weightSaves.add(new WeightEntry.fromSnapshot(event.snapshot)); + weightSaves.sort((we1, we2) => we1.dateTime.compareTo(we2.dateTime)); + }); + _scrollToTop(); + } + + _onEntryEdited(Event event) { + var oldValue = + weightSaves.singleWhere((entry) => entry.key == event.snapshot.key); + setState(() { + weightSaves[weightSaves.indexOf(oldValue)] = + new WeightEntry.fromSnapshot(event.snapshot); + weightSaves.sort((we1, we2) => we1.dateTime.compareTo(we2.dateTime)); + }); + } + + _scrollToTop() { + _listViewScrollController.animateTo( + weightSaves.length * _itemExtent, + duration: const Duration(microseconds: 1), + curve: new ElasticInCurve(0.01), + ); + } } diff --git a/lib/main.dart b/lib/main.dart index 32873da..4b95052 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,13 +1,13 @@ +import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/material.dart'; import 'package:weight_tracker/home_page.dart'; - void main() { + FirebaseDatabase.instance.setPersistenceEnabled(true); runApp(new MyApp()); } class MyApp extends StatelessWidget { - @override Widget build(BuildContext context) { return new MaterialApp( @@ -18,4 +18,4 @@ class MyApp extends StatelessWidget { home: new HomePage(title: 'Weight Tracker'), ); } -} \ No newline at end of file +} diff --git a/lib/model/weight_entry.dart b/lib/model/weight_entry.dart index 0be90a6..07c72f0 100644 --- a/lib/model/weight_entry.dart +++ b/lib/model/weight_entry.dart @@ -1,7 +1,25 @@ +import 'package:firebase_database/firebase_database.dart'; + class WeightEntry { + String key; DateTime dateTime; double weight; String note; WeightEntry(this.dateTime, this.weight, this.note); -} \ No newline at end of file + + WeightEntry.fromSnapshot(DataSnapshot snapshot) + : key = snapshot.key, + dateTime = + new DateTime.fromMillisecondsSinceEpoch(snapshot.value["date"]), + weight = snapshot.value["weight"].toDouble(), + note = snapshot.value["note"]; + + toJson() { + return { + "weight": weight, + "date": dateTime.millisecondsSinceEpoch, + "note": note + }; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 53003eb..e3e5f35 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,23 +5,16 @@ dependencies: flutter: sdk: flutter numberpicker: "^0.1.0" +# google_sign_in: 0.3.1 +# firebase_analytics: 0.0.4 +# firebase_auth: 0.2.0 + firebase_database: 0.0.12 -# For information on the generic Dart part of this file, see the -# following page: https://www.dartlang.org/tools/pub/pubspec - -# The following section is specific to Flutter. flutter: - # The following line ensures that the Material Icons font is - # included with your application, so that you can use the icons in - # the Icons class. uses-material-design: true - - # To add assets to your application, add an assets section here, in - # this "flutter" section, as in: assets: - assets/scale-bathroom.png - # - images/a_dot_ham.jpeg # To add assets from package dependencies, first ensure the asset # is in the lib/ directory of the dependency. Then,