relatica/lib/services/connections_manager.dart
2022-12-14 16:53:46 -05:00

236 lines
7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:result_monad/result_monad.dart';
import '../globals.dart';
import '../models/connection.dart';
import '../models/exec_error.dart';
import '../models/group_data.dart';
import 'auth_service.dart';
class ConnectionsManager extends ChangeNotifier {
static final _logger = Logger('$ConnectionsManager');
final _connectionsById = <String, Connection>{};
final _connectionsByName = <String, Connection>{};
final _connectionsByProfileUrl = <Uri, Connection>{};
final _groupsForConnection = <String, List<GroupData>>{};
final _myGroups = <GroupData>{};
int get length => _connectionsById.length;
void clearCaches() {
_connectionsById.clear();
_connectionsByName.clear();
_connectionsByProfileUrl.clear();
_groupsForConnection.clear();
}
bool addConnection(Connection connection) {
if (_connectionsById.containsKey(connection.id)) {
return false;
}
return updateConnection(connection);
}
bool updateConnection(Connection connection) {
_connectionsById[connection.id] = connection;
_connectionsByName[connection.name] = connection;
_connectionsByProfileUrl[connection.profileUrl] = connection;
return true;
}
bool addAllConnections(Iterable<Connection> newConnections) {
bool result = true;
for (final connection in newConnections) {
result &= addConnection(connection);
}
return result;
}
Future<void> follow(Connection connection) async {
_logger.finest(
'Attempting to follow ${connection.name}: ${connection.status}');
await getIt<AuthService>()
.currentClient
.andThenAsync((client) => client.followConnection(connection))
.match(
onSuccess: (update) {
_logger
.finest('Successfully followed ${update.name}: ${update.status}');
updateConnection(update);
notifyListeners();
},
onError: (error) {
_logger.severe('Error following ${connection.name}');
},
);
}
Future<void> unfollow(Connection connection) async {
_logger.finest(
'Attempting to unfollow ${connection.name}: ${connection.status}');
await getIt<AuthService>()
.currentClient
.andThenAsync((client) => client.unFollowConnection(connection))
.match(
onSuccess: (update) {
_logger
.finest('Successfully unfollowed ${update.name}: ${update.status}');
updateConnection(update);
notifyListeners();
},
onError: (error) {
_logger.severe('Error following ${connection.name}');
},
);
}
List<GroupData> getMyGroups() {
if (_myGroups.isNotEmpty) {
return _myGroups.toList(growable: false);
}
_updateMyGroups(true);
return [];
}
Result<List<GroupData>, ExecError> getGroupsForUser(String id) {
if (!_groupsForConnection.containsKey(id)) {
_refreshGroupListData(id, true);
return Result.ok([]);
}
return Result.ok(_groupsForConnection[id]!);
}
FutureResult<bool, ExecError> addUserToGroup(
GroupData group, Connection connection) async {
_logger.finest('Adding ${connection.name} to group: ${group.name}');
final result = await getIt<AuthService>().currentClient.andThenAsync(
(client) => client.addConnectionToGroup(group, connection));
result.match(
onSuccess: (_) => _refreshGroupListData(connection.id, true),
onError: (error) {
_logger
.severe('Error adding ${connection.name} to group: ${group.name}');
},
);
return result.execErrorCast();
}
FutureResult<bool, ExecError> removeUserFromGroup(
GroupData group, Connection connection) async {
_logger.finest('Removing ${connection.name} from group: ${group.name}');
final result = await getIt<AuthService>().currentClient.andThenAsync(
(client) => client.removeConnectionFromGroup(group, connection));
result.match(
onSuccess: (_) => _refreshGroupListData(connection.id, true),
onError: (error) {
_logger.severe(
'Error removing ${connection.name} from group: ${group.name}');
},
);
return result.execErrorCast();
}
Result<Connection, String> getById(String id) {
final result = _connectionsById[id];
if (result == null) {
Result.error('$id not found');
}
if (result!.status == ConnectionStatus.unknown) {
_refreshConnection(result, true);
}
return Result.ok(result);
}
Result<Connection, String> getByName(String name) {
final result = _connectionsByName[name];
if (result == null) {
Result.error('$name not found');
}
if (result!.status == ConnectionStatus.unknown) {
_refreshConnection(result, true);
}
return Result.ok(result);
}
Result<Connection, String> getByProfileUrl(Uri url) {
final result = _connectionsByProfileUrl[url];
if (result == null) {
Result.error('$url not found');
}
if (result!.status == ConnectionStatus.unknown) {
_refreshConnection(result, true);
}
return Result.ok(result);
}
Future<void> fullRefresh(Connection connection) async {
await _updateMyGroups(false);
await _refreshGroupListData(connection.id, false);
await _refreshConnection(connection, false);
notifyListeners();
}
Future<void> _refreshGroupListData(String id, bool withNotification) async {
_logger.finest('Refreshing member list data for Connection $id');
await getIt<AuthService>()
.currentClient
.andThenAsync((client) => client.getMemberGroupsForConnection(id))
.match(
onSuccess: (lists) {
_groupsForConnection[id] = lists;
if (withNotification) {
notifyListeners();
}
},
onError: (error) {
_logger.severe('Error getting list data for $id: $error');
},
);
}
Future<void> _refreshConnection(
Connection connection, bool withNotification) async {
_logger.finest('Refreshing connection data for ${connection.name}');
await getIt<AuthService>()
.currentClient
.andThenAsync((client) => client.getConnectionWithStatus(connection))
.match(
onSuccess: (update) {
updateConnection(update);
if (withNotification) {
notifyListeners();
}
},
onError: (error) {
_logger.severe('Error getting updates for ${connection.name}: $error');
},
);
}
Future<void> _updateMyGroups(bool withNotification) async {
_logger.finest('Refreshing my groups list');
await getIt<AuthService>()
.currentClient
.andThenAsync((client) => client.getGroups())
.match(
onSuccess: (groups) {
_logger.finest('Got updated groups:${groups.map((e) => e.name)}');
_myGroups.clear();
_myGroups.addAll(groups);
if (withNotification) {
notifyListeners();
}
},
onError: (error) {
_logger.severe('Error getting my groups: $error');
},
);
}
}