Move notification-timestamp away from viewmodel (IOS-192)

This commit is contained in:
Nathan Mattes 2024-01-20 21:51:04 +01:00
parent e720db2a31
commit 304eb8b7ff
4 changed files with 25 additions and 36 deletions

View File

@ -83,8 +83,6 @@ extension NotificationView {
let metaUsername = PlaintextMetaContent(string: "@\(author.acct)")
authorUsernameLabel.configure(content: metaUsername)
viewModel.timestamp = notification.entity.createdAt
let visibility = notification.entity.status?.mastodonVisibility ?? ._other("")
visibilityIconImageView.image = visibility.image
@ -174,9 +172,15 @@ extension NotificationView {
}
timestampUpdatePublisher
.prepend(Date())
.eraseToAnyPublisher()
.sink { [weak self] now in
guard let self else { return }
viewModel.followRequestState = notification.followRequestState
viewModel.transientFollowRequestState = notification.transientFollowRequestState
let timestamp = now.localizedTimeAgo(since: notification.entity.createdAt)
dateLabel.configure(content: PlaintextMetaContent(string: timestamp))
}
.store(in: &disposeBag)
}
}

View File

@ -33,19 +33,11 @@ extension NotificationView {
@Published public var followRequestState = MastodonFollowRequestState(state: .none)
@Published public var transientFollowRequestState = MastodonFollowRequestState(state: .none)
let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common)
.autoconnect()
.share()
.eraseToAnyPublisher()
}
}
extension NotificationView.ViewModel {
func bind(notificationView: NotificationView) {
bindAuthor(notificationView: notificationView)
bindFollowRequest(notificationView: notificationView)
$authContext
.assign(to: \.authContext, on: notificationView.statusView.viewModel)
.store(in: &disposeBag)
@ -56,32 +48,19 @@ extension NotificationView.ViewModel {
private func bindAuthor(notificationView: NotificationView) {
// timestamp
let formattedTimestamp = Publishers.CombineLatest(
$timestamp,
timestampUpdatePublisher.prepend(Date()).eraseToAnyPublisher()
)
.map { timestamp, _ in
timestamp?.localizedTimeAgoSinceNow ?? ""
}
.removeDuplicates()
formattedTimestamp
.sink { timestamp in
notificationView.dateLabel.configure(content: PlaintextMetaContent(string: timestamp))
}
.store(in: &disposeBag)
Publishers.CombineLatest4(
$authorName,
$authorUsername,
$notificationIndicatorText,
formattedTimestamp
$timestamp
)
.sink { name, username, type, timestamp in
let formattedTimestamp = timestamp?.localizedSlowedTimeAgoSinceNow ?? ""
notificationView.accessibilityLabel = [
"\(name?.string ?? "") \(type?.string ?? "")",
username.map { "@\($0)" } ?? "",
timestamp
formattedTimestamp
].joined(separator: ", ")
if !notificationView.statusView.isHidden {
notificationView.accessibilityLabel! += ", " + (notificationView.statusView.accessibilityLabel ?? "")
@ -138,7 +117,7 @@ extension NotificationView.ViewModel {
}
.store(in: &disposeBag)
}
private func bindFollowRequest(notificationView: NotificationView) {
Publishers.CombineLatest(
$followRequestState,
@ -159,7 +138,7 @@ extension NotificationView.ViewModel {
default:
break
}
let state = transientFollowRequestState.state
if state == .isAccepting {
notificationView.acceptFollowRequestActivityIndicatorView.startAnimating()
@ -179,7 +158,7 @@ extension NotificationView.ViewModel {
notificationView.rejectFollowRequestButton.tintColor = .black
notificationView.rejectFollowRequestButton.setTitleColor(.black, for: .normal)
}
UIView.animate(withDuration: 0.3) {
if state == .isAccept {
notificationView.rejectFollowRequestButtonShadowBackgroundContainer.isHidden = true
@ -193,3 +172,4 @@ extension NotificationView.ViewModel {
}
}

View File

@ -176,7 +176,12 @@ public final class NotificationView: UIView {
public let quoteStatusViewContainerView = UIView()
public let quoteBackgroundView = UIView()
public let quoteStatusView = StatusView()
let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common)
.autoconnect()
.share()
.eraseToAnyPublisher()
public func prepareForReuse() {
disposeBag.removeAll()

View File

@ -32,10 +32,10 @@ extension Date {
}
public var localizedTimeAgoSinceNow: String {
return self.localizedTimeAgo(since: Date(), isSlowed: false, isAbbreviated: false)
return self.localizedTimeAgo(since: Date())
}
public func localizedTimeAgo(since date: Date, isSlowed: Bool, isAbbreviated: Bool) -> String {
public func localizedTimeAgo(since date: Date, isSlowed: Bool = false, isAbbreviated: Bool = false) -> String {
let earlierDate = date < self ? date : self
let latestDate = earlierDate == date ? self : date