relatica/lib/data/memory/memory_connections_repo.dart

149 lines
3.9 KiB
Dart

import 'dart:collection';
import 'package:result_monad/result_monad.dart';
import '../../models/connection.dart';
import '../../models/exec_error.dart';
import '../../models/group_data.dart';
import '../interfaces/connections_repo_intf.dart';
class MemoryConnectionsRepo implements IConnectionsRepo {
final _connectionsById = <String, Connection>{};
final _connectionsByName = <String, Connection>{};
final _connectionsByProfileUrl = <String, Connection>{};
final _groupsForConnection = <String, List<GroupData>>{};
final _myGroups = <GroupData>{};
final _myContacts = <Connection>[];
@override
bool addAllConnections(Iterable<Connection> newConnections) {
bool result = true;
for (final connection in newConnections) {
result &= addConnection(connection);
}
return result;
}
@override
bool addConnection(Connection connection) {
if (_connectionsById.containsKey(connection.id)) {
return false;
}
return updateConnection(connection);
}
@override
UnmodifiableListView<Connection> getKnownUsersByName(String name) {
return UnmodifiableListView(_connectionsByName.values.where((it) {
final normalizedHandle = it.handle.toLowerCase();
final normalizedName = it.name.toLowerCase();
final normalizedQuery = name.toLowerCase();
return normalizedHandle.contains(normalizedQuery) ||
normalizedName.contains(normalizedQuery);
}));
}
@override
bool updateConnection(Connection connection) {
_connectionsById[connection.id] = connection;
_connectionsByName[connection.name] = connection;
_connectionsByProfileUrl[connection.profileUrl.toString()] = connection;
int index = _myContacts.indexWhere((c) => c.id == connection.id);
if (index >= 0) {
_myContacts.removeAt(index);
}
switch (connection.status) {
case ConnectionStatus.youFollowThem:
case ConnectionStatus.theyFollowYou:
case ConnectionStatus.mutual:
if (index > 0) {
_myContacts.insert(index, connection);
} else {
_myContacts.add(connection);
}
break;
default:
break;
}
return true;
}
@override
UnmodifiableListView<Connection> getMyContacts() {
return UnmodifiableListView(_myContacts);
}
@override
Result<List<GroupData>, ExecError> getGroupsForUser(String id) {
if (!_groupsForConnection.containsKey(id)) {
return Result.error(ExecError(
type: ErrorType.notFound,
message: '$id not a known user ID',
));
}
return Result.ok(UnmodifiableListView(_groupsForConnection[id]!));
}
@override
Result<Connection, ExecError> getById(String id) {
final result = _connectionsById[id];
if (result == null) {
return Result.error(ExecError(
type: ErrorType.notFound,
message: '$id not found',
));
}
return Result.ok(result);
}
@override
Result<Connection, ExecError> getByName(String name) {
final result = _connectionsByName[name];
if (result == null) {
return Result.error(ExecError(
type: ErrorType.notFound,
message: '$name not found',
));
}
return Result.ok(result);
}
@override
UnmodifiableListView<GroupData> getMyGroups() {
return UnmodifiableListView(_myGroups);
}
@override
Result<Connection, ExecError> getByProfileUrl(String url) {
final result = _connectionsByProfileUrl[url];
if (result == null) {
return Result.error(ExecError(
type: ErrorType.notFound,
message: '$url not found',
));
}
return Result.ok(result);
}
@override
void clearGroups() {
_myGroups.clear();
}
@override
void addAllGroups(List<GroupData> groups) {
_myGroups.addAll(groups);
}
@override
bool updateConnectionGroupData(String id, List<GroupData> currentGroups) {
_groupsForConnection[id] = currentGroups;
return true;
}
}