Merge origin/dev into fix/visual-consistency
This commit is contained in:
commit
96b8ff5cf7
85 files changed
+8433
-1331
No files matched your search
@@ -2,31 +2,90 @@ import '../models/contact.dart';
|
||||
|
||||
export 'contact_filter_types.dart';
|
||||
|
||||
bool matchesContactQuery(Contact contact, String query) {
|
||||
final normalizedQuery = query.trim().toLowerCase();
|
||||
if (normalizedQuery.isEmpty) return true;
|
||||
/// How closely a contact matches a search query. Lower ranks sort first.
|
||||
enum ContactQueryRank { namePrefix, nameContains, keyPrefix }
|
||||
|
||||
if (contact.name.toLowerCase().contains(normalizedQuery)) {
|
||||
return true;
|
||||
/// Rank for [query], or null when the contact does not match.
|
||||
///
|
||||
/// Names that start with the query come before other substring matches.
|
||||
/// A public-key / hop-hash prefix matches only when the name does not.
|
||||
/// Name-only half of [rankContactQuery], for a label that is not itself a
|
||||
/// contact (for example the "my contact" alias on the share sheet).
|
||||
ContactQueryRank? rankNameQuery(String name, String query) {
|
||||
final normalized = query.trim().toLowerCase();
|
||||
if (normalized.isEmpty) return ContactQueryRank.namePrefix;
|
||||
final label = name.toLowerCase();
|
||||
if (label.startsWith(normalized)) return ContactQueryRank.namePrefix;
|
||||
if (label.contains(normalized)) return ContactQueryRank.nameContains;
|
||||
return null;
|
||||
}
|
||||
|
||||
ContactQueryRank? rankContactQuery(Contact contact, String query) {
|
||||
final normalized = query.trim().toLowerCase();
|
||||
if (normalized.isEmpty) return ContactQueryRank.namePrefix;
|
||||
|
||||
final byName = rankNameQuery(contact.name, normalized);
|
||||
if (byName != null) return byName;
|
||||
if (_publicKeyPrefixMatches(contact, normalized)) {
|
||||
return ContactQueryRank.keyPrefix;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
final hexPrefix = _extractHexPrefix(normalizedQuery);
|
||||
if (hexPrefix == null) return false;
|
||||
/// Whether [name], any [aliases], or [publicKeyHex] matches [query] with the
|
||||
/// same prefix, substring, then hash order as [rankContactQuery].
|
||||
ContactQueryRank? rankLabeledKeyQuery({
|
||||
required String name,
|
||||
required String publicKeyHex,
|
||||
required String query,
|
||||
List<String> aliases = const [],
|
||||
}) {
|
||||
final normalized = query.trim().toLowerCase();
|
||||
if (normalized.isEmpty) return ContactQueryRank.namePrefix;
|
||||
ContactQueryRank? best = rankNameQuery(name, normalized);
|
||||
for (final alias in aliases) {
|
||||
final rank = rankNameQuery(alias, normalized);
|
||||
if (rank != null && (best == null || rank.index < best.index)) {
|
||||
best = rank;
|
||||
}
|
||||
}
|
||||
if (best != null) return best;
|
||||
final hexPrefix = _extractHexPrefix(normalized);
|
||||
if (hexPrefix != null && publicKeyHex.toLowerCase().startsWith(hexPrefix)) {
|
||||
return ContactQueryRank.keyPrefix;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return contact.publicKeyHex.toLowerCase().startsWith(hexPrefix);
|
||||
bool matchesContactQuery(Contact contact, String query) {
|
||||
return rankContactQuery(contact, query) != null;
|
||||
}
|
||||
|
||||
bool matchesDiscoveryContactQuery(Contact contact, String query) {
|
||||
final normalizedQuery = query.trim().toLowerCase();
|
||||
if (normalizedQuery.isEmpty) return true;
|
||||
return matchesContactQuery(contact, query);
|
||||
}
|
||||
|
||||
if (contact.name.toLowerCase().contains(normalizedQuery)) {
|
||||
return true;
|
||||
}
|
||||
/// Sorts prefix matches ahead of other substring matches, then key matches.
|
||||
///
|
||||
/// [tieBreak] orders contacts that share a rank. With an empty query every
|
||||
/// contact shares the top rank, so [tieBreak] is the whole order.
|
||||
int compareContactQueryRank(
|
||||
Contact a,
|
||||
Contact b,
|
||||
String query, [
|
||||
int Function(Contact a, Contact b)? tieBreak,
|
||||
]) {
|
||||
final left = rankContactQuery(a, query);
|
||||
final right = rankContactQuery(b, query);
|
||||
final byRank = (left?.index ?? 3).compareTo(right?.index ?? 3);
|
||||
if (byRank != 0) return byRank;
|
||||
return tieBreak?.call(a, b) ??
|
||||
a.name.toLowerCase().compareTo(b.name.toLowerCase());
|
||||
}
|
||||
|
||||
bool _publicKeyPrefixMatches(Contact contact, String normalizedQuery) {
|
||||
final hexPrefix = _extractHexPrefix(normalizedQuery);
|
||||
if (hexPrefix == null) return false;
|
||||
|
||||
if (hexPrefix == null || contact.publicKey.isEmpty) return false;
|
||||
return contact.publicKeyHex.toLowerCase().startsWith(hexPrefix);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user