mirror of
https://gitlab.com/mysocialportal/relatica
synced 2024-10-19 16:23:37 +00:00
73 lines
2.1 KiB
Dart
73 lines
2.1 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:result_monad/result_monad.dart';
|
|
|
|
import '../friendica_client/friendica_client.dart';
|
|
import '../globals.dart';
|
|
import '../models/auth/profile.dart';
|
|
import '../models/connection.dart';
|
|
import '../models/exec_error.dart';
|
|
import '../utils/active_profile_selector.dart';
|
|
import 'entry_manager_service.dart';
|
|
|
|
class InteractionsManager extends ChangeNotifier {
|
|
final _likesByStatusId = <String, List<Connection>>{};
|
|
final _resharesByStatusId = <String, List<Connection>>{};
|
|
|
|
final Profile profile;
|
|
|
|
InteractionsManager(this.profile);
|
|
|
|
void clear() {
|
|
_likesByStatusId.clear();
|
|
_resharesByStatusId.clear();
|
|
notifyListeners();
|
|
}
|
|
|
|
List<Connection> getLikes(String statusId) {
|
|
if (!_likesByStatusId.containsKey(statusId)) {
|
|
updateLikesForStatus(statusId);
|
|
return [];
|
|
}
|
|
|
|
return _likesByStatusId[statusId]!;
|
|
}
|
|
|
|
List<Connection> getReshares(String statusId) {
|
|
if (!_resharesByStatusId.containsKey(statusId)) {
|
|
updateResharesForStatus(statusId);
|
|
return [];
|
|
}
|
|
|
|
return _resharesByStatusId[statusId]!;
|
|
}
|
|
|
|
FutureResult<List<Connection>, ExecError> updateLikesForStatus(
|
|
String statusId) async {
|
|
final idForCall = _mapStatusId(statusId);
|
|
final likesResult = await InteractionsClient(profile).getLikes(idForCall);
|
|
if (likesResult.isSuccess) {
|
|
_likesByStatusId[statusId] = likesResult.value;
|
|
notifyListeners();
|
|
}
|
|
return likesResult;
|
|
}
|
|
|
|
FutureResult<List<Connection>, ExecError> updateResharesForStatus(
|
|
String statusId) async {
|
|
final idForCall = _mapStatusId(statusId);
|
|
final resharesResult =
|
|
await InteractionsClient(profile).getReshares(idForCall);
|
|
if (resharesResult.isSuccess) {
|
|
_resharesByStatusId[statusId] = resharesResult.value;
|
|
notifyListeners();
|
|
}
|
|
return resharesResult;
|
|
}
|
|
|
|
String _mapStatusId(String statusId) {
|
|
return getIt<ActiveProfileSelector<EntryManagerService>>()
|
|
.getForProfile(profile)
|
|
.transform((m) => m.mapInteractionId(statusId))
|
|
.getValueOrElse(() => statusId);
|
|
}
|
|
}
|