Files
Morgan Pretty ac9816b118 Massive refactors of the SessionThreadViewModel and MessageViewModel
The `SessionThreadViewModel` has been renamed to `ConversationInfoViewModel` and is now a simple struct (as opposed to a FetchableRecord with a handful of behemoth database queries)

The `ConversationInfoViewModel` and `MessageViewModel` now get initialised with a `ConversationDataCache` which is an object that holds a bunch of [ID -> Data] stores for various types that are needed to construct the aforementioned view models. The `ConversationDataCache` will generally be populated via the `ConversationDataHelper` which, when combined with our `ObservableEvent` system, is where the majority of the work for this refactoring has gone. This helper essentially has 4 parts:
- `determineFetchRequirements`: Reviews any events which have come through and determines whether anything needs to be fetched from the database
- `applyNonDatabaseEvents`: Updates the cached data directly based on delta information included in the `ObservableEvent` instances
- `fetchFromDatabase`: Fetches any data from the database based on the info returned from `determineFetchRequirements`, this function will also resolve any dependant data that newly fetched objects need if they aren't already in the cache
- `fetchFromLibSession`: Retrieves requested data from libSession

Using the above setup all screens which contain conversation or message data now use these two view models, and can share the state of the view models between each other to reduce the need to refetch data when entering a new screen

Some additional changes included due to the above are:
• No longer need to pass `displayNameRetriever` throughout
• Removed deprecated display name retrieval functions
• Removed some duplicate code
• Removed some synchronous database query hacks we needed until refactoring had been completed
• Refactored mentions to be handled by tags
• Refactored and optimised the searching logic (also added some caching)
• Refactored the ThreadSettingsViewModel to use the new ObservationBuilder system
• Refactored the ThreadPickerViewModel to use the new ObservationBuilder system
• Refactored the HomeViewModel to retrieve data using the `ConversationDataHelper`
• Refactored the ConversationViewModel to retrieve data using the `ConversationDataHelper`
• Fixed an issue where communities weren't getting the correct "joined" timestamp
2025-12-11 16:47:37 +11:00

141 lines
4.9 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import UIKit
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
public enum SwipeState {
case began
case ended
case cancelled
}
public class MessageCell: UITableViewCell {
var dependencies: Dependencies?
var viewModel: MessageViewModel?
weak var delegate: MessageCellDelegate?
open var contextSnapshotView: UIView? { return nil }
public lazy var tapGestureRegonizer: UITapGestureRecognizer = {
let result: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
result.numberOfTapsRequired = 1
result.require(toFail: doubleTapGestureRegonizer)
addGestureRecognizer(result)
return result
}()
public lazy var doubleTapGestureRegonizer: UITapGestureRecognizer = {
let result: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
result.numberOfTapsRequired = 2
addGestureRecognizer(result)
return result
}()
public lazy var longPressGestureRegonizer: UILongPressGestureRecognizer = {
let result: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
addGestureRecognizer(result)
return result
}()
// MARK: - Lifecycle
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpViewHierarchy()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUpViewHierarchy()
}
func setUpViewHierarchy() {
themeBackgroundColor = .clear
let selectedBackgroundView = UIView()
selectedBackgroundView.themeBackgroundColor = .clear
self.selectedBackgroundView = selectedBackgroundView
}
// MARK: - Updating
public override func prepareForReuse() {
super.prepareForReuse()
self.dependencies = nil
self.viewModel = nil
self.tapGestureRegonizer.isEnabled = false
self.doubleTapGestureRegonizer.isEnabled = false
self.longPressGestureRegonizer.isEnabled = false
}
func update(
with cellViewModel: MessageViewModel,
playbackInfo: ConversationViewModel.PlaybackInfo?,
showExpandedReactions: Bool,
shouldExpanded: Bool,
lastSearchText: String?,
tableSize: CGSize,
using dependencies: Dependencies
) {
preconditionFailure("Must be overridden by subclasses.")
}
/// This is a cut-down version of the 'update' function which doesn't re-create the UI (it should be used for dynamically-updating content
/// like playing inline audio/video)
func dynamicUpdate(with cellViewModel: MessageViewModel, playbackInfo: ConversationViewModel.PlaybackInfo?) {
preconditionFailure("Must be overridden by subclasses.")
}
// MARK: - Convenience
static func cellType(for viewModel: MessageViewModel) -> MessageCell.Type {
switch viewModel.cellType {
case .typingIndicator: return TypingIndicatorCell.self
case .dateHeader: return DateHeaderCell.self
case .unreadMarker: return UnreadMarkerCell.self
case .call: return CallMessageCell.self
case .infoMessage: return InfoMessageCell.self
case .textOnlyMessage, .mediaMessage, .audio, .voiceMessage, .genericAttachment:
return VisibleMessageCell.self
}
}
// MARK: - Gesture events
@objc
func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {}
@objc
func handleTap(_ gestureRecognizer: UITapGestureRecognizer) {}
@objc
func handleDoubleTap() {}
}
// MARK: - MessageCellDelegate
protocol MessageCellDelegate: ReactionDelegate {
func handleItemLongPressed(_ cellViewModel: MessageViewModel)
func handleItemTapped(_ cellViewModel: MessageViewModel, cell: UITableViewCell, cellLocation: CGPoint)
func handleItemDoubleTapped(_ cellViewModel: MessageViewModel)
func handleItemSwiped(_ cellViewModel: MessageViewModel, state: SwipeState)
func openUrl(_ urlString: String)
func handleReplyButtonTapped(for cellViewModel: MessageViewModel)
func showUserProfileModal(for cellViewModel: MessageViewModel)
func showReactionList(_ cellViewModel: MessageViewModel, selectedReaction: EmojiWithSkinTones?)
func needsLayout(for cellViewModel: MessageViewModel, expandingReactions: Bool)
func handleReadMoreButtonTapped(_ cell: UITableViewCell, for cellViewModel: MessageViewModel)
}
extension MessageCellDelegate {
func handleItemTapped(_ cellViewModel: MessageViewModel, cell: UITableViewCell, cellLocation: CGPoint) {
handleItemTapped(cellViewModel, cell: cell, cellLocation: cellLocation)
}
}