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

Update Message-timestamp-updating #2270

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
return false
}

private weak var timer: Timer?

private lazy var navBarTap: UITapGestureRecognizer = {
UITapGestureRecognizer(target: self, action: #selector(chatProfilePressed))
}()
Expand Down Expand Up @@ -284,37 +282,11 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
return UIApplication.shared.statusBarFrame.height + navigationBarHeight
}

private func startTimer() {
stopTimer()
timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
// reload table
DispatchQueue.main.async { [weak self] in
guard let self,
let appDelegate = UIApplication.shared.delegate as? AppDelegate
else { return }

if appDelegate.appIsInForeground() {
self.messageIds = self.dcContext.getChatMsgs(chatId: self.chatId)
self.reloadData()
} else {
logger.warning("startTimer() must not be executed in background")
}
}
}
}

public func activateSearchOnAppear() {
activateSearch = true
navigationItem.searchController = self.searchController
}

private func stopTimer() {
if let timer = timer {
timer.invalidate()
}
timer = nil
}

private func configureEmptyStateView() {
emptyStateView.addCenteredTo(parentView: view)
}
Expand Down Expand Up @@ -568,10 +540,7 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
func handleUserVisibility(isVisible: Bool) {
isVisibleToUser = isVisible
if isVisible {
startTimer()
markSeenMessagesInVisibleArea()
} else {
stopTimer()
}
}

Expand Down
31 changes: 21 additions & 10 deletions deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class BaseMessageCell: UITableViewCell {

public var showBottomLabelBackground: Bool {
didSet {
bottomLabel.backgroundColor = showBottomLabelBackground ?
statusView.backgroundColor = showBottomLabelBackground ?
DcColors.systemMessageBackgroundColor :
UIColor(alpha: 0, red: 0, green: 0, blue: 0)
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public class BaseMessageCell: UITableViewCell {
return button
}()

let bottomLabel = StatusView()
let statusView = StatusView()

lazy var messageBackgroundContainer: BackgroundContainer = {
let container = BackgroundContainer()
Expand All @@ -173,13 +173,14 @@ public class BaseMessageCell: UITableViewCell {
let reactionsView: ReactionsView

private var showSelectionBackground: Bool
private var timer: Timer?

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {

reactionsView = ReactionsView()
reactionsView.translatesAutoresizingMaskIntoConstraints = false

bottomLabel.translatesAutoresizingMaskIntoConstraints = false
statusView.translatesAutoresizingMaskIntoConstraints = false

showSelectionBackground = false
showBottomLabelBackground = false
Expand All @@ -202,7 +203,7 @@ public class BaseMessageCell: UITableViewCell {
messageBackgroundContainer.addSubview(mainContentView)
messageBackgroundContainer.addSubview(topLabel)
messageBackgroundContainer.addSubview(actionButton)
messageBackgroundContainer.addSubview(bottomLabel)
messageBackgroundContainer.addSubview(statusView)
contentView.addSubview(avatarView)

contentView.addConstraints([
Expand All @@ -215,10 +216,10 @@ public class BaseMessageCell: UITableViewCell {
topLabel.constraintAlignTrailingMaxTo(messageBackgroundContainer, paddingTrailing: 8),
messageBackgroundContainer.constraintAlignTopTo(contentView, paddingTop: 3),
actionButton.constraintAlignLeadingTo(messageBackgroundContainer, paddingLeading: 12),
bottomLabel.constraintAlignLeadingMaxTo(messageBackgroundContainer, paddingLeading: 8),
bottomLabel.constraintAlignTrailingTo(messageBackgroundContainer, paddingTrailing: 8),
bottomLabel.constraintToBottomOf(actionButton, paddingTop: 8, priority: .defaultHigh),
bottomLabel.constraintAlignBottomTo(messageBackgroundContainer, paddingBottom: 6)
statusView.constraintAlignLeadingMaxTo(messageBackgroundContainer, paddingLeading: 8),
statusView.constraintAlignTrailingTo(messageBackgroundContainer, paddingTrailing: 8),
statusView.constraintToBottomOf(actionButton, paddingTop: 8, priority: .defaultHigh),
statusView.constraintAlignBottomTo(messageBackgroundContainer, paddingBottom: 6)
])

leadingConstraint = messageBackgroundContainer.constraintAlignLeadingTo(contentView, paddingLeading: 6)
Expand Down Expand Up @@ -439,7 +440,15 @@ public class BaseMessageCell: UITableViewCell {
} else {
tintColor = DcColors.incomingMessageSecondaryTextColor
}
bottomLabel.update(message: msg, tintColor: tintColor)

statusView.update(message: msg, tintColor: tintColor)
let timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
guard let self else { return }

self.statusView.dateLabel.text = msg.formattedSentDate()
}

self.timer = timer
}

if let quoteText = msg.quoteText {
Expand Down Expand Up @@ -577,7 +586,7 @@ public class BaseMessageCell: UITableViewCell {
topLabel.attributedText = nil
avatarView.reset()
messageBackgroundContainer.prepareForReuse()
bottomLabel.prepareForReuse()
statusView.prepareForReuse()
baseDelegate = nil
messageLabel.text = nil
messageLabel.attributedText = nil
Expand All @@ -586,6 +595,8 @@ public class BaseMessageCell: UITableViewCell {
actionButton.isEnabled = true
showSelectionBackground = false
reactionsView.prepareForReuse()
timer?.invalidate()
timer = nil
}

@objc func reactionsViewTapped(_ sender: Any?) {
Expand Down
2 changes: 1 addition & 1 deletion deltachat-ios/Chat/Views/StatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import DcCore

public class StatusView: UIView {
private let contentStackView: UIStackView
private let dateLabel: UILabel
let dateLabel: UILabel
private let padlockView: UIImageView
private let locationView: UIImageView
private let stateView: UIImageView
Expand Down
Loading