Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global search improvement #855

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 3 additions & 59 deletions Session/Shared/FullConversationCell.swift
Expand Up @@ -8,11 +8,6 @@ import SessionMessagingKit
public final class FullConversationCell: UITableViewCell {
public static let unreadCountViewSize: CGFloat = 20
private static let statusIndicatorSize: CGFloat = 14
// If a message is much too long, it will take forever to calculate its width and
// cause the app to be frozen. So if a search result string is longer than 100
// characters, we assume it cannot be shown within one line and need to be truncated
// to avoid the calculation.
private static let maxApproxCharactersCanBeShownInOneLine: Int = 100

// MARK: - UI

Expand Down Expand Up @@ -620,69 +615,18 @@ public final class FullConversationCell: UITableViewCell {
}
}

// We then want to truncate the content so the first matching term is visible
let startOfSnippet: String.Index = (
firstMatchRange.map {
max(
mentionReplacedContent.startIndex,
mentionReplacedContent
.index(
$0.lowerBound,
offsetBy: -10,
limitedBy: mentionReplacedContent.startIndex
)
.defaulting(to: mentionReplacedContent.startIndex)
)
} ??
mentionReplacedContent.startIndex
)

// This method determines if the content is probably too long and returns the truncated or untruncated
// content accordingly
func truncatingIfNeeded(approxWidth: CGFloat, content: NSAttributedString) -> NSAttributedString {
let approxFullWidth: CGFloat = (approxWidth + profilePictureView.size + (Values.mediumSpacing * 3))

guard ((bounds.width - approxFullWidth) < 0) else { return content }

return content.attributedSubstring(
from: NSRange(startOfSnippet..<normalizedSnippet.endIndex, in: normalizedSnippet)
)
}

// Now that we have generated the focused snippet add the author name as a prefix (if provided)
return authorName
.map { authorName -> NSAttributedString? in
guard !authorName.isEmpty else { return nil }

let authorPrefix: NSAttributedString = NSAttributedString(
string: "\(authorName): ...",
string: "\(authorName): ",
attributes: [ .foregroundColor: textColor ]
)

return authorPrefix
.appending(
truncatingIfNeeded(
approxWidth: (
authorPrefix.size().width +
(
result.length > Self.maxApproxCharactersCanBeShownInOneLine ?
bounds.width :
result.size().width
)
),
content: result
)
)
return authorPrefix.appending(result)
}
.defaulting(
to: truncatingIfNeeded(
approxWidth: (
result.length > Self.maxApproxCharactersCanBeShownInOneLine ?
bounds.width :
result.size().width
),
content: result
)
)
.defaulting(to: result)
}
}
Expand Up @@ -1075,7 +1075,7 @@ public extension SessionThreadViewModel {
\(interaction[.id]) AS \(ViewModel.interactionIdKey),
\(interaction[.variant]) AS \(ViewModel.interactionVariantKey),
\(interaction[.timestampMs]) AS \(ViewModel.interactionTimestampMsKey),
\(interaction[.body]) AS \(ViewModel.interactionBodyKey),
snippet(\(interactionFullTextSearch), -1, '', '', '...', 6) AS \(ViewModel.interactionBodyKey),

\(interaction[.authorId]),
IFNULL(\(profile[.nickname]), \(profile[.name])) AS \(ViewModel.authorNameInternalKey),
Expand Down