2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// NotificationView+ViewModel.swift
|
2022-11-21 14:46:49 +01:00
|
|
|
//
|
2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-1-21.
|
|
|
|
//
|
|
|
|
|
2022-11-21 14:46:49 +01:00
|
|
|
import os.log
|
|
|
|
import UIKit
|
2022-01-27 14:23:39 +01:00
|
|
|
import Combine
|
2022-11-21 14:46:49 +01:00
|
|
|
import Meta
|
|
|
|
import MastodonSDK
|
2022-11-21 14:39:08 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2022-11-21 14:46:49 +01:00
|
|
|
import MastodonExtension
|
|
|
|
import MastodonCore
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2022-11-21 14:46:49 +01:00
|
|
|
extension NotificationView {
|
|
|
|
public final class ViewModel: ObservableObject {
|
2022-01-27 14:23:39 +01:00
|
|
|
public var disposeBag = Set<AnyCancellable>()
|
2022-06-30 09:02:24 +02:00
|
|
|
public var objects = Set<NSManagedObject>()
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2022-05-16 13:42:03 +02:00
|
|
|
let logger = Logger(subsystem: "NotificationView", category: "ViewModel")
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
@Published public var authContext: AuthContext?
|
2022-11-09 22:33:54 +01:00
|
|
|
|
|
|
|
@Published public var type: MastodonNotificationType?
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published public var notificationIndicatorText: MetaContent?
|
|
|
|
|
|
|
|
@Published public var authorAvatarImage: UIImage?
|
|
|
|
@Published public var authorAvatarImageURL: URL?
|
|
|
|
@Published public var authorName: MetaContent?
|
|
|
|
@Published public var authorUsername: String?
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published public var isMyself = false
|
|
|
|
@Published public var isMuting = false
|
|
|
|
@Published public var isBlocking = false
|
2022-11-30 16:38:31 +01:00
|
|
|
@Published public var isTranslated = false
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published public var timestamp: Date?
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-06-30 09:02:24 +02:00
|
|
|
@Published public var followRequestState = MastodonFollowRequestState(state: .none)
|
|
|
|
@Published public var transientFollowRequestState = MastodonFollowRequestState(state: .none)
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common)
|
|
|
|
.autoconnect()
|
|
|
|
.share()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationView.ViewModel {
|
|
|
|
func bind(notificationView: NotificationView) {
|
|
|
|
bindAuthor(notificationView: notificationView)
|
|
|
|
bindAuthorMenu(notificationView: notificationView)
|
2022-06-30 09:02:24 +02:00
|
|
|
bindFollowRequest(notificationView: notificationView)
|
2022-11-09 21:50:36 +01:00
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
$authContext
|
|
|
|
.assign(to: \.authContext, on: notificationView.statusView.viewModel)
|
2022-01-27 14:23:39 +01:00
|
|
|
.store(in: &disposeBag)
|
2022-10-09 14:07:57 +02:00
|
|
|
$authContext
|
|
|
|
.assign(to: \.authContext, on: notificationView.quoteStatusView.viewModel)
|
2022-01-27 14:23:39 +01:00
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
private func bindAuthor(notificationView: NotificationView) {
|
|
|
|
// avatar
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
$authorAvatarImage,
|
|
|
|
$authorAvatarImageURL
|
|
|
|
)
|
|
|
|
.sink { image, url in
|
|
|
|
let configuration: AvatarImageView.Configuration = {
|
|
|
|
if let image = image {
|
|
|
|
return AvatarImageView.Configuration(image: image)
|
|
|
|
} else {
|
|
|
|
return AvatarImageView.Configuration(url: url)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
notificationView.avatarButton.avatarImageView.configure(configuration: configuration)
|
|
|
|
notificationView.avatarButton.avatarImageView.configure(cornerConfiguration: .init(corner: .fixed(radius: 12)))
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
// name
|
|
|
|
$authorName
|
|
|
|
.sink { metaContent in
|
|
|
|
let metaContent = metaContent ?? PlaintextMetaContent(string: " ")
|
|
|
|
notificationView.authorNameLabel.configure(content: metaContent)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
// username
|
|
|
|
$authorUsername
|
|
|
|
.map { text -> String in
|
|
|
|
guard let text = text else { return "" }
|
|
|
|
return "@\(text)"
|
|
|
|
}
|
|
|
|
.sink { username in
|
|
|
|
let metaContent = PlaintextMetaContent(string: username)
|
|
|
|
notificationView.authorUsernameLabel.configure(content: metaContent)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
// timestamp
|
2022-11-09 21:50:36 +01:00
|
|
|
let formattedTimestamp = Publishers.CombineLatest(
|
2022-01-27 14:23:39 +01:00
|
|
|
$timestamp,
|
|
|
|
timestampUpdatePublisher.prepend(Date()).eraseToAnyPublisher()
|
|
|
|
)
|
2022-11-09 21:50:36 +01:00
|
|
|
.map { timestamp, _ in
|
|
|
|
timestamp?.localizedTimeAgoSinceNow ?? ""
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
2022-11-21 14:40:04 +01:00
|
|
|
.removeDuplicates()
|
2022-11-09 21:50:36 +01:00
|
|
|
|
|
|
|
formattedTimestamp
|
|
|
|
.sink { timestamp in
|
|
|
|
notificationView.dateLabel.configure(content: PlaintextMetaContent(string: timestamp))
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// notification type indicator
|
|
|
|
$notificationIndicatorText
|
|
|
|
.sink { text in
|
|
|
|
if let text = text {
|
|
|
|
notificationView.notificationTypeIndicatorLabel.configure(content: text)
|
|
|
|
} else {
|
|
|
|
notificationView.notificationTypeIndicatorLabel.reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-11-09 21:50:36 +01:00
|
|
|
|
|
|
|
Publishers.CombineLatest4(
|
|
|
|
$authorName,
|
|
|
|
$authorUsername,
|
|
|
|
$notificationIndicatorText,
|
|
|
|
formattedTimestamp
|
|
|
|
)
|
|
|
|
.sink { name, username, type, timestamp in
|
|
|
|
notificationView.accessibilityLabel = [
|
|
|
|
"\(name?.string ?? "") \(type?.string ?? "")",
|
|
|
|
username.map { "@\($0)" } ?? "",
|
2022-11-21 14:46:49 +01:00
|
|
|
timestamp
|
2022-11-09 21:50:36 +01:00
|
|
|
].joined(separator: ", ")
|
|
|
|
if !notificationView.statusView.isHidden {
|
|
|
|
notificationView.accessibilityLabel! += ", " + (notificationView.statusView.accessibilityLabel ?? "")
|
|
|
|
}
|
|
|
|
if !notificationView.quoteStatusViewContainerView.isHidden {
|
|
|
|
notificationView.accessibilityLabel! += ", " + (notificationView.quoteStatusView.accessibilityLabel ?? "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-11-09 22:33:54 +01:00
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
$authorAvatarImage,
|
|
|
|
$type
|
|
|
|
)
|
|
|
|
.sink { avatarImage, type in
|
|
|
|
var actions = [UIAccessibilityCustomAction]()
|
|
|
|
|
|
|
|
// these notifications can be directly actioned to view the profile
|
|
|
|
if type != .follow, type != .followRequest {
|
|
|
|
actions.append(
|
|
|
|
UIAccessibilityCustomAction(
|
|
|
|
name: L10n.Common.Controls.Status.showUserProfile,
|
|
|
|
image: avatarImage
|
|
|
|
) { [weak notificationView] _ in
|
|
|
|
guard let notificationView = notificationView, let delegate = notificationView.delegate else { return false }
|
|
|
|
delegate.notificationView(notificationView, authorAvatarButtonDidPressed: notificationView.avatarButton)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if type == .followRequest {
|
|
|
|
actions.append(
|
|
|
|
UIAccessibilityCustomAction(
|
|
|
|
name: L10n.Common.Controls.Actions.confirm,
|
|
|
|
image: Asset.Editing.checkmark20.image
|
|
|
|
) { [weak notificationView] _ in
|
|
|
|
guard let notificationView = notificationView, let delegate = notificationView.delegate else { return false }
|
|
|
|
delegate.notificationView(notificationView, acceptFollowRequestButtonDidPressed: notificationView.acceptFollowRequestButton)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
actions.append(
|
|
|
|
UIAccessibilityCustomAction(
|
|
|
|
name: L10n.Common.Controls.Actions.delete,
|
|
|
|
image: Asset.Circles.forbidden20.image
|
|
|
|
) { [weak notificationView] _ in
|
|
|
|
guard let notificationView = notificationView, let delegate = notificationView.delegate else { return false }
|
|
|
|
delegate.notificationView(notificationView, rejectFollowRequestButtonDidPressed: notificationView.rejectFollowRequestButton)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationView.notificationActions = actions
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
private func bindAuthorMenu(notificationView: NotificationView) {
|
|
|
|
Publishers.CombineLatest4(
|
|
|
|
$authorName,
|
|
|
|
$isMuting,
|
|
|
|
$isBlocking,
|
2022-11-30 16:38:31 +01:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
$isMyself,
|
|
|
|
$isTranslated
|
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
2022-12-07 16:03:52 +01:00
|
|
|
.sink { authorName, isMuting, isBlocking, isMyselfIsTranslated in
|
2022-01-27 14:23:39 +01:00
|
|
|
guard let name = authorName?.string else {
|
|
|
|
notificationView.menuButton.menu = nil
|
|
|
|
return
|
|
|
|
}
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-12-07 16:03:52 +01:00
|
|
|
let (isMyself, isTranslated) = isMyselfIsTranslated
|
2022-11-30 16:38:31 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let menuContext = NotificationView.AuthorMenuContext(
|
|
|
|
name: name,
|
|
|
|
isMuting: isMuting,
|
|
|
|
isBlocking: isBlocking,
|
2022-09-15 15:28:40 +02:00
|
|
|
isMyself: isMyself,
|
2022-11-30 16:38:31 +01:00
|
|
|
isBookmarking: false, // no bookmark action display for notification item
|
|
|
|
isTranslated: isTranslated,
|
|
|
|
statusLanguage: ""
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
2022-11-09 22:33:54 +01:00
|
|
|
let (menu, actions) = notificationView.setupAuthorMenu(menuContext: menuContext)
|
|
|
|
notificationView.menuButton.menu = menu
|
|
|
|
notificationView.authorActions = actions
|
2022-01-27 14:23:39 +01:00
|
|
|
notificationView.menuButton.showsMenuAsPrimaryAction = true
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
notificationView.menuButton.isHidden = menuContext.isMyself
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-06-30 09:02:24 +02:00
|
|
|
private func bindFollowRequest(notificationView: NotificationView) {
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
$followRequestState,
|
|
|
|
$transientFollowRequestState
|
|
|
|
)
|
|
|
|
.sink { followRequestState, transientFollowRequestState in
|
|
|
|
switch followRequestState.state {
|
|
|
|
case .isAccept:
|
|
|
|
notificationView.rejectFollowRequestButtonShadowBackgroundContainer.isHidden = true
|
|
|
|
notificationView.acceptFollowRequestButton.isUserInteractionEnabled = false
|
|
|
|
notificationView.acceptFollowRequestButton.setImage(nil, for: .normal)
|
2022-06-30 09:58:09 +02:00
|
|
|
notificationView.acceptFollowRequestButton.setTitle(L10n.Scene.Notification.FollowRequest.accepted, for: .normal)
|
2022-06-30 09:02:24 +02:00
|
|
|
case .isReject:
|
|
|
|
notificationView.acceptFollowRequestButtonShadowBackgroundContainer.isHidden = true
|
|
|
|
notificationView.rejectFollowRequestButton.isUserInteractionEnabled = false
|
|
|
|
notificationView.rejectFollowRequestButton.setImage(nil, for: .normal)
|
2022-06-30 09:58:09 +02:00
|
|
|
notificationView.rejectFollowRequestButton.setTitle(L10n.Scene.Notification.FollowRequest.rejected, for: .normal)
|
2022-06-30 09:02:24 +02:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-06-30 09:02:24 +02:00
|
|
|
let state = transientFollowRequestState.state
|
|
|
|
if state == .isAccepting {
|
|
|
|
notificationView.acceptFollowRequestActivityIndicatorView.startAnimating()
|
|
|
|
notificationView.acceptFollowRequestButton.tintColor = .clear
|
2022-07-13 11:44:47 +02:00
|
|
|
notificationView.acceptFollowRequestButton.setTitleColor(.clear, for: .normal)
|
2022-06-30 09:02:24 +02:00
|
|
|
} else {
|
|
|
|
notificationView.acceptFollowRequestActivityIndicatorView.stopAnimating()
|
|
|
|
notificationView.acceptFollowRequestButton.tintColor = .white
|
2022-07-13 11:44:47 +02:00
|
|
|
notificationView.acceptFollowRequestButton.setTitleColor(.white, for: .normal)
|
2022-06-30 09:02:24 +02:00
|
|
|
}
|
|
|
|
if state == .isRejecting {
|
|
|
|
notificationView.rejectFollowRequestActivityIndicatorView.startAnimating()
|
|
|
|
notificationView.rejectFollowRequestButton.tintColor = .clear
|
2022-07-13 11:44:47 +02:00
|
|
|
notificationView.rejectFollowRequestButton.setTitleColor(.clear, for: .normal)
|
2022-06-30 09:02:24 +02:00
|
|
|
} else {
|
|
|
|
notificationView.rejectFollowRequestActivityIndicatorView.stopAnimating()
|
2022-07-13 11:44:47 +02:00
|
|
|
notificationView.rejectFollowRequestButton.tintColor = .black
|
|
|
|
notificationView.rejectFollowRequestButton.setTitleColor(.black, for: .normal)
|
2022-06-30 09:02:24 +02:00
|
|
|
}
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-06-30 09:02:24 +02:00
|
|
|
UIView.animate(withDuration: 0.3) {
|
|
|
|
if state == .isAccept {
|
|
|
|
notificationView.rejectFollowRequestButtonShadowBackgroundContainer.isHidden = true
|
|
|
|
}
|
|
|
|
if state == .isReject {
|
|
|
|
notificationView.acceptFollowRequestButtonShadowBackgroundContainer.isHidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
2022-11-21 14:46:49 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|