kopia lustrzana https://gitlab.com/mysocialportal/relatica
60 wiersze
1.5 KiB
Dart
60 wiersze
1.5 KiB
Dart
![]() |
import 'package:result_monad/result_monad.dart';
|
||
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||
|
|
||
|
import '../models/auth/profile.dart';
|
||
|
import '../models/exec_error.dart';
|
||
|
import '../models/timeline_entry.dart';
|
||
|
import 'rp_provider_extension.dart';
|
||
|
|
||
|
part 'timeline_entry_services.g.dart';
|
||
|
|
||
|
@Riverpod(keepAlive: true)
|
||
|
Map<String, TimelineEntry> _timelineEntries(
|
||
|
_TimelineEntriesRef ref, Profile profile) =>
|
||
|
{};
|
||
|
|
||
|
@riverpod
|
||
|
class TimelineEntryManager extends _$TimelineEntryManager {
|
||
|
late Profile userProfile;
|
||
|
var entryId = '';
|
||
|
|
||
|
@override
|
||
|
Result<TimelineEntry, ExecError> build(Profile profile, String id) {
|
||
|
ref.cacheFor(const Duration(hours: 1));
|
||
|
entryId = id;
|
||
|
userProfile = profile;
|
||
|
final entry = ref.watch(_timelineEntriesProvider(profile))[id];
|
||
|
if (entry == null) {
|
||
|
return buildErrorResult(
|
||
|
type: ErrorType.notFound,
|
||
|
message: '$id not found',
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return Result.ok(entry);
|
||
|
}
|
||
|
|
||
|
Result<TimelineEntry, ExecError> upsert(TimelineEntry entry) {
|
||
|
if (entry.id != entryId) {
|
||
|
return buildErrorResult(
|
||
|
type: ErrorType.argumentError,
|
||
|
message:
|
||
|
'Trying to add an entry to a provider that does not match the id: $id',
|
||
|
);
|
||
|
} else {
|
||
|
ref.read(_timelineEntriesProvider(userProfile))[entryId] = entry;
|
||
|
}
|
||
|
|
||
|
if (state.isFailure || entry != state.value) {
|
||
|
state = Result.ok(entry);
|
||
|
ref.invalidateSelf();
|
||
|
}
|
||
|
|
||
|
return state;
|
||
|
}
|
||
|
|
||
|
void remove() {
|
||
|
ref.read(_timelineEntriesProvider(userProfile)).remove(entryId);
|
||
|
}
|
||
|
}
|