-
-
Notifications
You must be signed in to change notification settings - Fork 855
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: re implemented favorites feature (#100)
- Loading branch information
Showing
16 changed files
with
696 additions
and
75 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
11 changes: 11 additions & 0 deletions
11
lib/src/features/common/data/data_sources/favorites/favorites_local_data_source.dart
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,11 @@ | ||
import 'package:flutter_ebook_app/src/features/common/data/models/category_feed.dart'; | ||
|
||
abstract class FavoritesLocalDataSource { | ||
const FavoritesLocalDataSource(); | ||
|
||
Future<void> addBook(Entry book, String id); | ||
|
||
Future<void> deleteBook(String id); | ||
|
||
Stream<List<Entry>> favoritesListStream(); | ||
} |
46 changes: 46 additions & 0 deletions
46
lib/src/features/common/data/data_sources/favorites/favorites_local_data_source_impl.dart
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,46 @@ | ||
import 'package:flutter_ebook_app/src/features/common/data/data_sources/favorites/favorites_local_data_source.dart'; | ||
import 'package:flutter_ebook_app/src/features/common/data/models/category_feed.dart'; | ||
import 'package:flutter_ebook_app/src/features/common/data/providers/database_provider.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:sembast/sembast.dart'; | ||
|
||
class FavoritesLocalDataSourceImpl implements FavoritesLocalDataSource { | ||
final Database _database; | ||
final StoreRef<String, Map<String, dynamic>> _store; | ||
|
||
const FavoritesLocalDataSourceImpl({ | ||
required Database database, | ||
required StoreRef<String, Map<String, dynamic>> store, | ||
}) : _database = database, | ||
_store = store; | ||
|
||
@override | ||
Future<void> addBook(Entry book, String id) async { | ||
await _store.record(id).put(_database, book.toJson()); | ||
} | ||
@override | ||
Future<void> deleteBook(String id) async { | ||
await _store.record(id).delete(_database); | ||
} | ||
|
||
@override | ||
Stream<List<Entry>> favoritesListStream() { | ||
return _store | ||
.query() | ||
.onSnapshots(_database) | ||
.map<List<Entry>>( | ||
(records) => records | ||
.map<Entry>((record) => Entry.fromJson(record.value)) | ||
.toList(), | ||
); | ||
} | ||
} | ||
|
||
final favoritesLocalDataSourceProvider = | ||
Provider.autoDispose<FavoritesLocalDataSource>( | ||
(ref) { | ||
final database = ref.watch(favoritesDatabaseProvider); | ||
final store = ref.watch(storeRefProvider); | ||
return FavoritesLocalDataSourceImpl(database: database, store: store); | ||
}, | ||
); |
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
10 changes: 10 additions & 0 deletions
10
lib/src/features/common/data/notifiers/favorites/favorites_state.dart
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,10 @@ | ||
part of 'favorites_state_notifier.dart'; | ||
|
||
@freezed | ||
abstract class FavoritesState with _$FavoritesState { | ||
const factory FavoritesState.started() = FavoritesStateStarted; | ||
|
||
const factory FavoritesState.listening({ | ||
required List<Entry> favorites, | ||
}) = FavoritesStateLoadListening; | ||
} |
Oops, something went wrong.