-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
refactor(mobile): split store into repo and service #16199
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Domain Layer | ||
|
||
This directory contains the domain layer of Immich. The domain layer is responsible for the business logic of the app. It includes interfaces for repositories, models, services and utilities. This layer should never depend on anything from the presentation layer or from the infrastructure layer. | ||
|
||
## Structure | ||
|
||
- **[Interfaces](./interfaces/)**: These are the interfaces that define the contract for data operations. | ||
- **[Models](./models/)**: These are the core data classes that represent the business models. | ||
- **[Services](./services/)**: These are the classes that contain the business logic and interact with the repositories. | ||
- **[Utils](./utils/)**: These are utility classes and functions that provide common functionalities used across the domain layer. | ||
|
||
``` | ||
domain/ | ||
├── interfaces/ | ||
│ └── user.interface.dart | ||
├── models/ | ||
│ └── user.model.dart | ||
├── services/ | ||
│ └── user.service.dart | ||
└── utils/ | ||
└── date_utils.dart | ||
``` | ||
|
||
## Usage | ||
|
||
The domain layer provides services that implement the business logic by consuming repositories through dependency injection. Services are exposed through Riverpod providers in the root `providers` directory. | ||
|
||
```dart | ||
// In presentation layer | ||
final userService = ref.watch(userServiceProvider); | ||
final user = await userService.getUser(userId); | ||
``` | ||
|
||
The presentation layer should never directly use repositories, but instead interact with the domain layer through services. |
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,5 @@ | ||
abstract interface class IDatabaseRepository { | ||
Future<T> nestTxn<T>(Future<T> Function() callback); | ||
|
||
Future<T> txn<T>(Future<T> Function() callback); | ||
} |
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,17 @@ | ||
import 'package:immich_mobile/entities/store.entity.dart'; | ||
|
||
abstract interface class IStoreRepository { | ||
Future<bool> insert<T>(StoreKey<T> key, T value); | ||
|
||
Future<T?> tryGet<T>(StoreKey<T> key); | ||
|
||
Stream<T?> watch<T>(StoreKey<T> key); | ||
|
||
Stream<StoreUpdateEvent> watchAll(); | ||
|
||
Future<bool> update<T>(StoreKey<T> key, T value); | ||
|
||
Future<void> delete<T>(StoreKey<T> key); | ||
|
||
Future<void> deleteAll(); | ||
} |
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,106 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:immich_mobile/domain/interfaces/store.interface.dart'; | ||
import 'package:immich_mobile/entities/store.entity.dart'; | ||
|
||
class StoreService { | ||
final IStoreRepository _storeRepo; | ||
alextran1502 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final Map<int, dynamic> _cache = {}; | ||
late final StreamSubscription<StoreUpdateEvent> _subscription; | ||
|
||
StoreService._({ | ||
required IStoreRepository storeRepo, | ||
}) : _storeRepo = storeRepo; | ||
|
||
// TODO: Temporary typedef to make minimal changes. Remove this and make the presentation layer access store through a provider | ||
static StoreService? _instance; | ||
static StoreService get I { | ||
if (_instance == null) { | ||
throw UnsupportedError("StoreService not initialized. Call init() first"); | ||
} | ||
return _instance!; | ||
} | ||
|
||
// TODO: Replace the implementation with the one from create after removing the typedef | ||
/// Initializes the store with the given [storeRepository] | ||
static Future<StoreService> init({ | ||
required IStoreRepository storeRepo, | ||
}) async { | ||
_instance ??= await create(storeRepo: storeRepo); | ||
return _instance!; | ||
} | ||
|
||
/// Initializes the store with the given [storeRepository] | ||
static Future<StoreService> create({ | ||
required IStoreRepository storeRepo, | ||
}) async { | ||
final instance = StoreService._(storeRepo: storeRepo); | ||
await instance._populateCache(); | ||
instance._subscription = instance._listenForChange(); | ||
return instance; | ||
} | ||
|
||
/// Fills the cache with the values from the DB | ||
Future<void> _populateCache() async { | ||
for (StoreKey key in StoreKey.values) { | ||
final storeValue = await _storeRepo.tryGet(key); | ||
_cache[key.id] = storeValue; | ||
} | ||
} | ||
|
||
/// Listens for changes in the DB and updates the cache | ||
StreamSubscription<StoreUpdateEvent> _listenForChange() => | ||
_storeRepo.watchAll().listen((event) { | ||
_cache[event.key.id] = event.value; | ||
}); | ||
|
||
/// Disposes the store and cancels the subscription. To reuse the store call init() again | ||
void dispose() async { | ||
await _subscription.cancel(); | ||
_cache.clear(); | ||
} | ||
|
||
/// Returns the stored value for the given key (possibly null) | ||
T? tryGet<T>(StoreKey<T> key) => _cache[key.id]; | ||
|
||
/// Returns the stored value for the given key or if null the [defaultValue] | ||
/// Throws a [StoreKeyNotFoundException] if both are null | ||
T get<T>(StoreKey<T> key, [T? defaultValue]) { | ||
final value = tryGet(key) ?? defaultValue; | ||
if (value == null) { | ||
throw StoreKeyNotFoundException(key); | ||
} | ||
return value; | ||
} | ||
|
||
/// Asynchronously stores the value in the DB and synchronously in the cache | ||
Future<void> put<T>(StoreKey<T> key, T value) async { | ||
if (_cache[key.id] == value) return; | ||
await _storeRepo.insert(key, value); | ||
_cache[key.id] = value; | ||
} | ||
|
||
/// Watches a specific key for changes | ||
Stream<T?> watch<T>(StoreKey<T> key) => _storeRepo.watch(key); | ||
|
||
/// Removes the value asynchronously from the DB and synchronously from the cache | ||
Future<void> delete<T>(StoreKey<T> key) async { | ||
await _storeRepo.delete(key); | ||
_cache.remove(key.id); | ||
} | ||
|
||
/// Clears all values from this store (cache and DB) | ||
Future<void> clear() async { | ||
await _storeRepo.deleteAll(); | ||
_cache.clear(); | ||
} | ||
} | ||
|
||
class StoreKeyNotFoundException implements Exception { | ||
final StoreKey key; | ||
const StoreKeyNotFoundException(this.key); | ||
|
||
@override | ||
String toString() => "Key - <${key.name}> not available in Store"; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it typical to abbreviate function names in dart land? Usually I am against that, as it makes it harder to read for no benefit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. But we can follow a similar convention to the server codebase in our mobile codebase as well. Will update all the methods to not use abbreviations