Skip to content

Commit f984f70

Browse files
committed
User based notes added
1 parent ecfa631 commit f984f70

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

lib/extensions/list/filter.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extension Filter<T> on Stream<List<T>> {
2+
Stream<List<T>> filter(bool Function(T) where) => map((items) => items.where(where).toList());
3+
}

lib/services/crud/crud_exceptions.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ class CouldNotDeleteNoteException implements Exception {}
1414

1515
class CouldNotFindNoteException implements Exception {}
1616

17-
class CouldNotUpdateNoteException implements Exception {}
17+
class CouldNotUpdateNoteException implements Exception {}
18+
19+
class UserShouldBeSetBeforeReadingAllNotesException implements Exception {}

lib/services/crud/notes_service.dart

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22
import 'package:flutter/foundation.dart';
3+
import 'package:mynoteapp/extensions/list/filter.dart';
34
import 'package:mynoteapp/services/crud/crud_exceptions.dart';
45
import 'package:sqflite/sqflite.dart';
56
import 'package:path_provider/path_provider.dart'
@@ -10,10 +11,12 @@ class NotesService {
1011
Database? _db;
1112
List<DatabaseNote> _notes = [];
1213

14+
DatabaseUser? _user;
15+
1316
static final NotesService _shared = NotesService._sharedInstance();
1417
NotesService._sharedInstance() {
1518
_notesStreamController = StreamController<List<DatabaseNote>>.broadcast(
16-
onListen:() {
19+
onListen: () {
1720
_notesStreamController.sink.add(_notes);
1821
},
1922
);
@@ -23,15 +26,30 @@ class NotesService {
2326

2427
late final StreamController<List<DatabaseNote>> _notesStreamController;
2528

26-
Stream<List<DatabaseNote>> get allNotes => _notesStreamController.stream;
29+
Stream<List<DatabaseNote>> get allNotes => _notesStreamController.stream.filter((note) {
30+
final currentUser = _user;
31+
if (currentUser != null) {
32+
return note.userId == currentUser.id;
33+
} else {
34+
throw UserShouldBeSetBeforeReadingAllNotesException();
35+
}
36+
});
2737

28-
Future<DatabaseUser> getOrCreateUser({required String email}) async {
38+
Future<DatabaseUser> getOrCreateUser({
39+
required String email,
40+
bool setAsCurrentUser = true,
41+
}) async {
2942
try {
3043
final user = await getUser(email: email);
44+
if (setAsCurrentUser) {
45+
_user = user;
46+
}
3147
return user;
3248
} on CouldNotFindUserException {
3349
final createdUser = await createUser(email: email);
34-
50+
if (setAsCurrentUser) {
51+
_user = createdUser;
52+
}
3553
return createdUser;
3654
} catch (e) {
3755
rethrow;
@@ -55,10 +73,12 @@ class NotesService {
5573

5674
await getNote(id: note.id); // not yoksa exception döndürsün
5775

58-
final updatesCount = await db.update(noteTable, {
59-
textColumn: text,
60-
isSyncedWithCloudColumn: 0,
61-
});
76+
final updatesCount = await db.update(
77+
noteTable,
78+
{textColumn: text, isSyncedWithCloudColumn: 0},
79+
where: 'id = ?',
80+
whereArgs: [note.id],
81+
);
6282

6383
if (updatesCount == 0) {
6484
throw CouldNotUpdateNoteException();

0 commit comments

Comments
 (0)