2022-11-17 16:04:14 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:result_monad/result_monad.dart';
|
|
|
|
|
|
|
|
import '../globals.dart';
|
|
|
|
import '../models/TimelineIdentifiers.dart';
|
|
|
|
import '../models/entry_tree_item.dart';
|
|
|
|
import '../models/exec_error.dart';
|
|
|
|
import '../models/timeline.dart';
|
|
|
|
import 'entry_manager_service.dart';
|
|
|
|
|
|
|
|
class TimelineManager extends ChangeNotifier {
|
|
|
|
static final _logger = Logger('$TimelineManager');
|
|
|
|
|
|
|
|
final cachedTimelines = <TimelineIdentifiers, Timeline>{};
|
|
|
|
|
2022-11-21 03:26:49 +00:00
|
|
|
void clear() {
|
|
|
|
cachedTimelines.clear();
|
|
|
|
getIt<EntryManagerService>().clear();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:04:14 +00:00
|
|
|
// refresh timeline gets statuses newer than the newest in that timeline
|
|
|
|
Result<List<EntryTreeItem>, ExecError> getTimeline(TimelineIdentifiers type) {
|
|
|
|
final posts = cachedTimelines[type]?.posts;
|
|
|
|
if (posts != null) {
|
|
|
|
return Result.ok(posts);
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshTimeline(type);
|
|
|
|
|
|
|
|
return Result.ok([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refreshTimeline(TimelineIdentifiers type) async {
|
2022-11-18 23:31:28 +00:00
|
|
|
final timeline = cachedTimelines.putIfAbsent(type, () => Timeline(type));
|
|
|
|
(await getIt<EntryManagerService>()
|
|
|
|
.updateTimeline(type, timeline.highestStatusId))
|
|
|
|
.match(onSuccess: (posts) {
|
2022-11-18 21:50:15 +00:00
|
|
|
_logger.finest('Posts returned for adding to $type: ${posts.length}');
|
2022-11-21 03:26:49 +00:00
|
|
|
timeline.addOrUpdate(posts);
|
2022-11-17 16:04:14 +00:00
|
|
|
notifyListeners();
|
|
|
|
}, onError: (error) {
|
|
|
|
_logger.severe('Error getting timeline: $type}');
|
|
|
|
});
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
// All statuses get dumped into the entity mangager and get full assembled posts out of it
|
|
|
|
// Timeline keeps track of posts level only so can query timeline manager for those
|
|
|
|
// Should put backing store on timelines and entity manager so can recover from restart faster
|
|
|
|
// Have a purge caches button to start that over from scratch
|
|
|
|
// Should have a contacts manager with backing store as well
|
|
|
|
// Timeline view is new control that knows how to load timeline, scrolling around with refresh and get more
|
|
|
|
// Timeline Item view displays itself and children
|
|
|
|
// Has "Add Comment" value
|
|
|
|
// Has like/dislke
|
|
|
|
// Has reshare/quote reshare (if can get that working somehow)
|
|
|
|
// If our own has delete
|
|
|
|
|
|
|
|
}
|