-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements Firebase as datastore for bookmarks.
- Loading branch information
1 parent
29ba041
commit 739cdf0
Showing
8 changed files
with
131 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,37 @@ | ||
class Bookmark { | ||
Bookmark({ | ||
this.id, | ||
this.title = '', | ||
this.description = '', | ||
this.url = '', | ||
this.edit = true, | ||
this.isFresh = true, | ||
}); | ||
|
||
Bookmark.fromMap(Map bm) | ||
: this( | ||
id: bm['id'], | ||
title: bm['title'], | ||
description: bm['description'], | ||
url: bm['url'], | ||
edit: bm['edit'] ?? false, | ||
); | ||
|
||
String id; | ||
String title; | ||
String description; | ||
String url; | ||
bool edit; | ||
|
||
asMap() => { | ||
'title': title, | ||
'description': description, | ||
'url': url, | ||
}; | ||
|
||
update(Bookmark bm) { | ||
title = bm.title; | ||
description = bm.description; | ||
url = bm.url; | ||
edit = bm.edit; | ||
isFresh = bm.isFresh; | ||
} | ||
|
||
String title; | ||
String description; | ||
String url; | ||
bool edit; | ||
bool isFresh; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:angular/angular.dart'; | ||
import 'package:firebase/firebase.dart'; | ||
|
||
import '../model/bookmark.dart'; | ||
|
||
@Injectable() | ||
class BookmarkService { | ||
BookmarkService() { | ||
initializeApp( | ||
apiKey: "AIzaSyDz0_Z9cSleynEWUD0Xtx_kLDguxbrRjAU", | ||
authDomain: "bookmark-manager-1e87a.firebaseapp.com", | ||
databaseURL: "https://bookmark-manager-1e87a.firebaseio.com", | ||
projectId: "bookmark-manager-1e87a", | ||
storageBucket: "bookmark-manager-1e87a.appspot.com", | ||
messagingSenderId: "666842763609", | ||
); | ||
|
||
_db = database(); | ||
_ref = _db.ref('bookmarks').ref; | ||
} | ||
|
||
Database _db; | ||
DatabaseReference _ref; | ||
|
||
Future<List<Bookmark>> getBookmarks() async { | ||
final List<Bookmark> bookmarks = []; | ||
final queryEvent = await _ref.once('value'); | ||
final DataSnapshot snapshot = queryEvent.snapshot; | ||
final bmData = snapshot.val(); | ||
|
||
bmData.forEach((key, val) { | ||
var details = val as Map<String, dynamic>; | ||
details['id'] = key; | ||
bookmarks.add(Bookmark.fromMap(details)); | ||
}); | ||
|
||
return bookmarks; | ||
} | ||
|
||
Future addBookmark(Bookmark bm) async { | ||
var res = await _ref.push(bm.asMap()); | ||
return res.key; | ||
} | ||
|
||
Future updateBookmark(Bookmark bm) async { | ||
return await _ref.child(bm.id).set(bm.asMap()); | ||
} | ||
|
||
Future removeBookmark(Bookmark bm) async { | ||
return await _ref.child(bm.id).remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters