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 = >{}; final _resharesByStatusId = >{}; final Profile profile; InteractionsManager(this.profile); void clear() { _likesByStatusId.clear(); _resharesByStatusId.clear(); notifyListeners(); } List getLikes(String statusId) { if (!_likesByStatusId.containsKey(statusId)) { updateLikesForStatus(statusId); return []; } return _likesByStatusId[statusId]!; } List getReshares(String statusId) { if (!_resharesByStatusId.containsKey(statusId)) { updateResharesForStatus(statusId); return []; } return _resharesByStatusId[statusId]!; } FutureResult, 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, 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>() .getForProfile(profile) .transform((m) => m.mapInteractionId(statusId)) .getValueOrElse(() => statusId); } }