first commit
This commit is contained in:
commit
faf67cc6d8
148 changed files with 6580 additions and 0 deletions
47
lib/data/repositories/owner_repository.dart
Normal file
47
lib/data/repositories/owner_repository.dart
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:seshat/data/services/api_client.dart';
|
||||
import 'package:seshat/data/services/websocket_client.dart';
|
||||
import 'package:seshat/domain/models/owner.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
class OwnerRepository {
|
||||
OwnerRepository({
|
||||
required ApiClient apiClient,
|
||||
required WebsocketClient wsClient,
|
||||
}) : _apiClient = apiClient,
|
||||
_wsClient = wsClient;
|
||||
|
||||
final ApiClient _apiClient;
|
||||
final WebsocketClient _wsClient;
|
||||
List<Owner>? _cachedData;
|
||||
|
||||
Future<Result<List<Owner>>> getOwners() async {
|
||||
if (_cachedData == null) {
|
||||
final result = await _apiClient.getOwners();
|
||||
|
||||
if (result is Ok<List<Owner>>) {
|
||||
_cachedData = result.value;
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Result.ok(_cachedData!);
|
||||
}
|
||||
}
|
||||
|
||||
Stream<Owner> liveOwners() async* {
|
||||
await for (String data in _wsClient.connect()) {
|
||||
Map<String, dynamic> decodedData = jsonDecode(
|
||||
data,
|
||||
).cast<Map<String, dynamic>>();
|
||||
Owner owner = Owner.fromJSON(decodedData);
|
||||
if (_cachedData == null) {
|
||||
getOwners();
|
||||
} else {
|
||||
_cachedData!.add(owner);
|
||||
yield* Stream.value(owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue