relatica/lib/riverpod_controllers/timeline_services.dart

72 wiersze
2.2 KiB
Dart
Czysty Zwykły widok Historia

import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../models/auth/profile.dart';
import '../models/timeline.dart';
import '../models/timeline_identifiers.dart';
import 'entry_tree_item_services.dart';
part 'timeline_services.g.dart';
final _tmpLogger = Logger('TimelineManagerProvider');
enum TimelineRefreshType {
refresh,
loadOlder,
loadNewer,
}
@Riverpod(keepAlive: true)
class TimelineManager extends _$TimelineManager {
@override
Timeline build(Profile profile, TimelineIdentifiers timelineId) {
_tmpLogger.info('Building for $profile for $timelineId');
return Timeline(timelineId);
}
Future<void> updateTimeline(
TimelineRefreshType refreshType,
) async {
_tmpLogger.info(
'Updating w/$refreshType for timeline $timelineId for profile $profile ');
late final int lowestId;
late final int highestId;
late final InsertionType insertionType;
switch (refreshType) {
case TimelineRefreshType.refresh:
lowestId = 0;
highestId = 0;
insertionType = InsertionType.end;
break;
case TimelineRefreshType.loadOlder:
lowestId = state.lowestStatusId;
highestId = 0;
insertionType = InsertionType.end;
break;
case TimelineRefreshType.loadNewer:
lowestId = 0;
highestId = state.highestStatusId;
insertionType = InsertionType.beginning;
break;
}
(await ref
.read(timelineUpdaterProvider(profile).notifier)
.updateTimeline(timelineId, lowestId, highestId))
.match(onSuccess: (posts) {
_tmpLogger
.finest('Posts returned for adding to $timelineId: ${posts.length}');
final oldState = state;
final newState = state = state.addOrUpdate(
posts.map((p) => p.id).toList(),
insertionType: insertionType,
);
_tmpLogger.finest(
'Old post count: ${oldState.posts.length}, New Post count: ${newState.posts.length}');
_tmpLogger.finest('Old state == New state? ${oldState == newState}');
}, onError: (error) {
_tmpLogger.severe('Error updating timeline: $timelineId}');
});
}
}