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

[SPIKE] Isolate firebase dependency #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion frontend/lib/app_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:frontend/components/sailing_club_detail_component/sailing_club_d
import 'package:frontend/components/boat_list_component/boat_list_component.dart';
import 'package:frontend/components/boat_detail_component/boat_detail_component.dart';
import 'package:frontend/store/regatta_store.dart';
import 'package:frontend/services/persistency_service.dart';
import 'package:frontend/services/firebase_service.dart';

@Component(selector: 'my-app', templateUrl: 'app_component.html', styleUrls: const [
Expand All @@ -25,7 +26,7 @@ import 'package:frontend/services/firebase_service.dart';
ROUTER_PROVIDERS,
const Provider(LocationStrategy, useClass: HashLocationStrategy),
RegattaStore,
FirebaseService,
const Provider(PersistencyService, useClass: FirebaseService)
])
@RouteConfig(const [
const Route(path: '/events', name: 'Events', component: EventListComponent, useAsDefault: true),
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/components/app_header/app_header.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:angular2/angular2.dart';
import 'package:angular2_components/angular2_components.dart';

import 'package:frontend/services/firebase_service.dart';
import 'package:frontend/services/persistency_service.dart';

@Component(
selector: 'app-header',
Expand All @@ -10,7 +10,7 @@ import 'package:frontend/services/firebase_service.dart';
directives: const [materialDirectives],
providers: const [materialProviders])
class AppHeader {
final FirebaseService fbService;
final PersistencyService fbService;

AppHeader(this.fbService);
}
14 changes: 13 additions & 1 deletion frontend/lib/services/firebase_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import 'package:frontend/models/sailing_club.dart';
import 'package:frontend/models/boat.dart';
import 'package:frontend/store/regatta_store.dart';
import 'package:frontend/store/regatta_action.dart' as actions;
import 'package:frontend/services/persistency_service.dart';

@Injectable()
class FirebaseService {
class FirebaseService implements PersistencyService {
fb.Auth _fbAuth;
fb.GoogleAuthProvider _fbGoogleAuthProvider;
fb.Database _fbDatabase;
Expand Down Expand Up @@ -57,6 +58,7 @@ class FirebaseService {
}
}

@override
Future signIn() async {
try {
await _fbAuth.signInWithPopup(_fbGoogleAuthProvider);
Expand All @@ -65,6 +67,7 @@ class FirebaseService {
}
}

@override
void signOut() {
_fbAuth.signOut();
}
Expand All @@ -75,6 +78,7 @@ class FirebaseService {
_store.dispatch(actions.addEvent(ev));
}

@override
Future addEvent(Event event) async {
try {
await _fbRefEvents.push(event.toMap()).future;
Expand All @@ -88,6 +92,7 @@ class FirebaseService {
_store.dispatch(actions.updateEvent(ev));
}

@override
Future updateEvent(Event event) async {
try {
await _fbRefEvents.child(event.key).update(event.toMap());
Expand All @@ -101,6 +106,7 @@ class FirebaseService {
_store.dispatch(actions.deleteEvent(ev));
}

@override
Future deleteEvent(Event event) async {
try {
await _fbRefEvents.child(event.key).remove();
Expand All @@ -115,6 +121,7 @@ class FirebaseService {
_store.dispatch(actions.addSailingClub(sc));
}

@override
Future addSailingClub(SailingClub sailingClub) async {
try {
await _fbRefSailingClubs.push(sailingClub.toMap()).future;
Expand All @@ -128,6 +135,7 @@ class FirebaseService {
_store.dispatch(actions.updateSailingClub(sc));
}

@override
Future updateSailingClub(SailingClub sailingClub) async {
try {
await _fbRefSailingClubs.child(sailingClub.key).update(sailingClub.toMap());
Expand All @@ -141,6 +149,7 @@ class FirebaseService {
_store.dispatch(actions.deleteSailingClub(sc));
}

@override
Future deleteSailingClub(SailingClub sailingClub) async {
try {
await _fbRefSailingClubs.child(sailingClub.key).remove();
Expand All @@ -155,6 +164,7 @@ class FirebaseService {
_store.dispatch(actions.addBoat(sc));
}

@override
Future addBoat(Boat sailingClub) async {
try {
await _fbRefBoats.push(sailingClub.toMap()).future;
Expand All @@ -168,6 +178,7 @@ class FirebaseService {
_store.dispatch(actions.updateBoat(sc));
}

@override
Future updateBoat(Boat sailingClub) async {
try {
await _fbRefBoats.child(sailingClub.key).update(sailingClub.toMap());
Expand All @@ -181,6 +192,7 @@ class FirebaseService {
_store.dispatch(actions.deleteBoat(sc));
}

@override
Future deleteBoat(Boat sailingClub) async {
try {
await _fbRefBoats.child(sailingClub.key).remove();
Expand Down
70 changes: 70 additions & 0 deletions frontend/lib/services/persistency_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'dart:async';

import 'package:frontend/models/event.dart';
import 'package:frontend/models/sailing_club.dart';
import 'package:frontend/models/boat.dart';

// TODO: Should we have a percistency Interface that is segregated from the authentication interface?

abstract class PersistencyService {
// final RegattaStore _store;

// PersistencyService(this._store);

Future signIn();
void signOut();

// These private methods are called by implementing classes
// to propagate changes in the DB to the frontend.
/*
void newEvent(Event newEvent) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I am not sure about those methods. I think they only exist for the Firebase usecase. E.g. if we would have a databse or file as a backend, we would do the saving to disk and dispatching the success in a single method without the need of an async callback.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently those are commented out and only introduced in the FirebaseService Class. But mostly because I didn't find a way to make it work as a abstract class/implementation pair.

My reasoning to have those in the PercistencyService Class was that the frontend currently assumes async communication with the persistency layer - specifically in the action design. I would then assume that we need some sort of success messages flowing back to the frontend.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial reason of defining those in the PercistencyService Class was to decouple the usage of Redux in the frontend from the usage of firebase in the backend.

If this goal is even valid might also need some discussion... :-)

_store.dispatch(actions.addEvent(newEvent));
}

void _changedEvent(Event changedEvent) {
_store.dispatch(actions.updateEvent(changedEvent));
}

void _removedEvent(Event removedEvent) {
_store.dispatch(actions.deleteEvent(removedEvent));
}

void _newSailingClub(SailingClub sc) {
_store.dispatch(actions.addSailingClub(sc));
}

void _changedSailingClub(SailingClub sc) {
_store.dispatch(actions.updateSailingClub(sc));
}

void _removedSailingClub(SailingClub sc) {
_store.dispatch(actions.deleteSailingClub(sc));
}

void _newBoat(Boat boat) {
_store.dispatch(actions.addBoat(boat));
}

void _changedBoat(Boat boat) {
_store.dispatch(actions.updateBoat(boat));
}

void _removedBoat(Boat boat) {
_store.dispatch(actions.deleteBoat(boat));
}
*/
// The following methods are called by the frontend
// to write to the DB.

Future addEvent(Event event);
Future updateEvent(Event event);
Future deleteEvent(Event event);

Future addSailingClub(SailingClub sailingClub);
Future updateSailingClub(SailingClub sailingClub);
Future deleteSailingClub(SailingClub sailingClub);

Future addBoat(Boat boat);
Future updateBoat(Boat boat);
Future deleteBoat(Boat boat);
}
Loading