Skip to content

Commit

Permalink
Added linking with google account
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinusX committed Oct 31, 2018
1 parent bcbd3f2 commit c995f0c
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 130 deletions.
8 changes: 8 additions & 0 deletions android/app/google-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
"certificate_hash": "4cfcaae693f041e62c51224b17ff5920ca45db7c"
}
},
{
"client_id": "123595545975-qlq2eju9tpm89igcgj13f96dne92dd12.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.mszalek.weight_tracker",
"certificate_hash": "52f32b889e107f33b006c376421992370f90510c"
}
},
{
"client_id": "123595545975-jvcpng9krrieeokh79673e448cekctnj.apps.googleusercontent.com",
"client_type": 3
Expand Down
Binary file added assets/google.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 19 additions & 9 deletions lib/logic/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ import 'package:weight_tracker/model/weight_entry.dart';

class UserLoadedAction {
final FirebaseUser firebaseUser;
final List<WeightEntry> cachedEntries;

UserLoadedAction(this.firebaseUser);
UserLoadedAction(this.firebaseUser, {this.cachedEntries = const []});
}

class AddDatabaseReferenceAction {
final DatabaseReference databaseReference;
final List<WeightEntry> cachedEntries;

AddDatabaseReferenceAction(this.databaseReference);
AddDatabaseReferenceAction(this.databaseReference,
{this.cachedEntries = const []});
}

class GetSavedWeightNote {
}
class GetSavedWeightNote {}

class AddWeightFromNotes {
final double weight;

AddWeightFromNotes(this.weight);
}

class ConsumeWeightFromNotes {
}
class ConsumeWeightFromNotes {}

class AddEntryAction {
final WeightEntry weightEntry;
Expand Down Expand Up @@ -88,8 +89,7 @@ class UpdateActiveWeightEntry {
UpdateActiveWeightEntry(this.weightEntry);
}

class OpenAddEntryDialog {
}
class OpenAddEntryDialog {}

class OpenEditEntryDialog {
final WeightEntry weightEntry;
Expand All @@ -101,4 +101,14 @@ class ChangeProgressChartStartDate {
final DateTime dateTime;

ChangeProgressChartStartDate(this.dateTime);
}
}

class LoginWithGoogle {
final List<WeightEntry> cachedEntries;

LoginWithGoogle({this.cachedEntries = const []});
}

class LogoutAction {
LogoutAction();
}
95 changes: 79 additions & 16 deletions lib/logic/middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:redux/redux.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:weight_tracker/logic/actions.dart';
import 'package:weight_tracker/logic/constants.dart';
import 'package:weight_tracker/logic/redux_state.dart';
import 'package:weight_tracker/model/weight_entry.dart';

final GoogleSignIn _googleSignIn = new GoogleSignIn();

middleware(Store<ReduxState> store, action, NextDispatcher next) {
print(action.runtimeType);
if (action is InitAction) {
Expand All @@ -28,19 +31,69 @@ middleware(Store<ReduxState> store, action, NextDispatcher next) {
_handleGetSavedWeightNote(store);
} else if (action is AddWeightFromNotes) {
_handleAddWeightFromNotes(store, action);
} else if (action is LoginWithGoogle) {
_handleLoginWithGoogle(store, action);
} else if (action is LogoutAction) {
_handleLogoutAction(store, action);
}
next(action);
if (action is UserLoadedAction) {
_handleUserLoadedAction(store);
_handleUserLoadedAction(store, action);
} else if (action is AddDatabaseReferenceAction) {
_handleAddedDatabaseReference(store);
_handleAddedDatabaseReference(store, action);
}
}

_handleLogoutAction(Store<ReduxState> store, LogoutAction action) {
_googleSignIn.signOut();
FirebaseAuth.instance.signOut().then((_) => FirebaseAuth.instance
.signInAnonymously()
.then((user) => store.dispatch(UserLoadedAction(user))));
}

_handleLoginWithGoogle(Store<ReduxState> store, LoginWithGoogle action) async {
GoogleSignInAccount googleUser = await _getGoogleUser();
GoogleSignInAuthentication credentials = await googleUser.authentication;

bool hasLinkingFailed = false;
try {
await FirebaseAuth.instance.linkWithGoogleCredential(
idToken: credentials.idToken,
accessToken: credentials.accessToken,
);
} catch (e) {
await FirebaseAuth.instance.signInWithGoogle(
idToken: credentials.idToken,
accessToken: credentials.accessToken,
);
hasLinkingFailed = true;
}
await FirebaseAuth.instance.updateProfile(new UserUpdateInfo()
..photoUrl = googleUser.photoUrl
..displayName = googleUser.displayName);
FirebaseUser newUser = await FirebaseAuth.instance.currentUser();

store.dispatch(new UserLoadedAction(
newUser,
cachedEntries: hasLinkingFailed ? action.cachedEntries : [],
));
}

Future<GoogleSignInAccount> _getGoogleUser() async {
GoogleSignInAccount googleUser = _googleSignIn.currentUser;
if (googleUser == null) {
googleUser = await _googleSignIn.signInSilently();
}
if (googleUser == null) {
googleUser = await _googleSignIn.signIn();
}
return googleUser;
}

_handleAddWeightFromNotes(Store<ReduxState> store, AddWeightFromNotes action) {
if (store.state.firebaseState?.mainReference != null) {
WeightEntry weightEntry =
new WeightEntry(new DateTime.now(), action.weight, null);
new WeightEntry(new DateTime.now(), action.weight, null);
store.dispatch(new AddEntryAction(weightEntry));
action = new AddWeightFromNotes(null);
}
Expand Down Expand Up @@ -68,32 +121,42 @@ Future<double> _getSavedWeightNote() async {
return null;
}

_handleAddedDatabaseReference(Store<ReduxState> store) {
_handleAddedDatabaseReference(
Store<ReduxState> store, AddDatabaseReferenceAction action) {
//maybe add cached entries
if (action.cachedEntries?.isNotEmpty ?? false) {
action.cachedEntries
.forEach((entry) => store.dispatch(AddEntryAction(entry)));
}
//maybe add height from notes
double weight = store.state.weightFromNotes;
if (weight != null) {
if (store.state.unit == 'lbs') {
weight = weight / KG_LBS_RATIO;
}
if (weight >= MIN_KG_VALUE && weight <= MAX_KG_VALUE) {
WeightEntry weightEntry =
new WeightEntry(new DateTime.now(), weight, null);
new WeightEntry(new DateTime.now(), weight, null);
store.dispatch(new AddEntryAction(weightEntry));
store.dispatch(new ConsumeWeightFromNotes());
}
}
}

_handleUserLoadedAction(Store<ReduxState> store) {
store.dispatch(new AddDatabaseReferenceAction(FirebaseDatabase.instance
.reference()
.child(store.state.firebaseState.firebaseUser.uid)
.child("entries")
..onChildAdded
.listen((event) => store.dispatch(new OnAddedAction(event)))
..onChildChanged
.listen((event) => store.dispatch(new OnChangedAction(event)))
..onChildRemoved
.listen((event) => store.dispatch(new OnRemovedAction(event)))));
_handleUserLoadedAction(Store<ReduxState> store, UserLoadedAction action) {
store.dispatch(new AddDatabaseReferenceAction(
FirebaseDatabase.instance
.reference()
.child(store.state.firebaseState.firebaseUser.uid)
.child("entries")
..onChildAdded
.listen((event) => store.dispatch(new OnAddedAction(event)))
..onChildChanged
.listen((event) => store.dispatch(new OnChangedAction(event)))
..onChildRemoved
.listen((event) => store.dispatch(new OnRemovedAction(event))),
cachedEntries: action.cachedEntries,
));
}

_handleSetUnitAction(SetUnitAction action, Store<ReduxState> store) {
Expand Down
13 changes: 8 additions & 5 deletions lib/logic/reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ ReduxState reduce(ReduxState state, action) {
String unit = _reduceUnit(state, action);
RemovedEntryState removedEntryState = _reduceRemovedEntryState(state, action);
WeightEntryDialogReduxState weightEntryDialogState =
_reduceWeightEntryDialogState(state, action);
_reduceWeightEntryDialogState(state, action);
FirebaseState firebaseState = _reduceFirebaseState(state, action);
MainPageReduxState mainPageState = _reduceMainPageState(state, action);
DateTime progressChartStartDate = _reduceProgressChartStartDate(state, action);
DateTime progressChartStartDate =
_reduceProgressChartStartDate(state, action);
double weightFromNotes = _reduceWeightFromNotes(state, action);

return new ReduxState(
Expand Down Expand Up @@ -78,8 +79,8 @@ RemovedEntryState _reduceRemovedEntryState(ReduxState reduxState, action) {
return newState;
}

WeightEntryDialogReduxState _reduceWeightEntryDialogState(ReduxState reduxState,
action) {
WeightEntryDialogReduxState _reduceWeightEntryDialogState(
ReduxState reduxState, action) {
WeightEntryDialogReduxState newState = reduxState.weightEntryDialogState;
if (action is UpdateActiveWeightEntry) {
newState = newState.copyWith(
Expand Down Expand Up @@ -107,7 +108,7 @@ List<WeightEntry> _reduceEntries(ReduxState state, action) {
} else if (action is OnChangedAction) {
WeightEntry newValue = new WeightEntry.fromSnapshot(action.event.snapshot);
WeightEntry oldValue =
entries.singleWhere((entry) => entry.key == newValue.key);
entries.singleWhere((entry) => entry.key == newValue.key);
entries
..[entries.indexOf(oldValue)] = newValue
..sort((we1, we2) => we2.dateTime.compareTo(we1.dateTime));
Expand All @@ -117,6 +118,8 @@ List<WeightEntry> _reduceEntries(ReduxState state, action) {
entries
..remove(removedEntry)
..sort((we1, we2) => we2.dateTime.compareTo(we1.dateTime));
} else if (action is UserLoadedAction) {
entries = [];
}
return entries;
}
Expand Down
26 changes: 2 additions & 24 deletions lib/screens/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter_redux/flutter_redux.dart';
import 'package:weight_tracker/logic/actions.dart';
import 'package:weight_tracker/logic/redux_state.dart';
import 'package:weight_tracker/screens/history_page.dart';
import 'package:weight_tracker/screens/profile_screen.dart';
import 'package:weight_tracker/screens/settings_screen.dart';
import 'package:weight_tracker/screens/statistics_page.dart';
import 'package:weight_tracker/screens/weight_entry_dialog.dart';
Expand Down Expand Up @@ -136,32 +135,11 @@ class MainPageState extends State<MainPage>
}

List<Widget> _buildMenuActions(BuildContext context) {
List<Widget> actions = [
new IconButton(
return [
IconButton(
icon: new Icon(Icons.settings),
onPressed: () => _openSettingsPage(context)),
];
bool showProfile = false;
if (showProfile) {
actions.add(new PopupMenuButton<String>(
onSelected: (val) {
if (val == "Profile") {
Navigator.of(context).push(new MaterialPageRoute(
builder: (context) => new ProfileScreen(),
));
}
},
itemBuilder: (context) {
return [
new PopupMenuItem<String>(
value: "Profile",
child: new Text("Profile"),
),
];
},
));
}
return actions;
}

_scrollToTop() {
Expand Down
53 changes: 0 additions & 53 deletions lib/screens/profile_screen.dart

This file was deleted.

Loading

0 comments on commit c995f0c

Please sign in to comment.