Skip to content

Commit

Permalink
Added firebase database integration
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinusX committed Aug 26, 2017
1 parent 18f95d5 commit 36a7b90
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 38 deletions.
4 changes: 3 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourcompany.weight_tracker"
package="com.mszalek.weight_tracker"
android:versionCode="1"
android:versionName="0.0.1">

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yourcompany.weight_tracker;
package com.mszalek.weight_tracker;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
Expand Down
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ buildscript {

dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.1.0'
}
}

Expand Down
63 changes: 43 additions & 20 deletions lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,11 +14,18 @@ class HomePage extends StatefulWidget {
_HomePageState createState() => new _HomePageState();
}

final mainReference = FirebaseDatabase.instance.reference();

class _HomePageState extends State<HomePage> {
List<WeightEntry> 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(
Expand All @@ -35,7 +43,7 @@ class _HomePageState extends State<HomePage> {
? 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));
},
),
Expand All @@ -47,45 +55,60 @@ class _HomePageState extends State<HomePage> {
);
}

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<WeightEntry>(
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<WeightEntry>(
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),
);
}
}
6 changes: 3 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -18,4 +18,4 @@ class MyApp extends StatelessWidget {
home: new HomePage(title: 'Weight Tracker'),
);
}
}
}
20 changes: 19 additions & 1 deletion lib/model/weight_entry.dart
Original file line number Diff line number Diff line change
@@ -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);
}

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
};
}
}
15 changes: 4 additions & 11 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 36a7b90

Please sign in to comment.