mirror of
https://github.com/Finb/Bark.git
synced 2025-12-08 21:36:01 +00:00
修复消息时间不会更新的问题。
This commit is contained in:
parent
9dbd9aec06
commit
8c88a0e897
@ -21,6 +21,7 @@
|
||||
061894C529962EB900E001C2 /* GradientButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 061894C429962EB900E001C2 /* GradientButton.swift */; };
|
||||
061894C729A75BEA00E001C2 /* Algorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 061894C629A75BEA00E001C2 /* Algorithm.swift */; };
|
||||
061C17082D1BDA4B00891D66 /* MessageGroupMoreView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 061C17072D1BDA4B00891D66 /* MessageGroupMoreView.swift */; };
|
||||
061E35862D1E5028009A2D6F /* MessageItemModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 061E35852D1E5028009A2D6F /* MessageItemModel.swift */; };
|
||||
0627DABB298B6EA2002F3F69 /* DropBoxView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0627DABA298B6EA2002F3F69 /* DropBoxView.swift */; };
|
||||
0627DABD2990D615002F3F69 /* BorderTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0627DABC2990D615002F3F69 /* BorderTextField.swift */; };
|
||||
062B98C3251B2762004562E7 /* BKButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 062B98C2251B2762004562E7 /* BKButton.swift */; };
|
||||
@ -263,6 +264,7 @@
|
||||
061894C429962EB900E001C2 /* GradientButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientButton.swift; sourceTree = "<group>"; };
|
||||
061894C629A75BEA00E001C2 /* Algorithm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithm.swift; sourceTree = "<group>"; };
|
||||
061C17072D1BDA4B00891D66 /* MessageGroupMoreView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageGroupMoreView.swift; sourceTree = "<group>"; };
|
||||
061E35852D1E5028009A2D6F /* MessageItemModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageItemModel.swift; sourceTree = "<group>"; };
|
||||
0627DABA298B6EA2002F3F69 /* DropBoxView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropBoxView.swift; sourceTree = "<group>"; };
|
||||
0627DABC2990D615002F3F69 /* BorderTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BorderTextField.swift; sourceTree = "<group>"; };
|
||||
062B98C2251B2762004562E7 /* BKButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BKButton.swift; sourceTree = "<group>"; };
|
||||
@ -536,6 +538,7 @@
|
||||
children = (
|
||||
06B1158E247BB1FB006D91FB /* Message.swift */,
|
||||
067B2EB425693E38008B6BE1 /* MessageSection.swift */,
|
||||
061E35852D1E5028009A2D6F /* MessageItemModel.swift */,
|
||||
064CABA5256BE9510018155C /* PreviewModel.swift */,
|
||||
06BD4DA92901352E003364DB /* Object+Dictionary.swift */,
|
||||
061894C629A75BEA00E001C2 /* Algorithm.swift */,
|
||||
@ -1288,6 +1291,7 @@
|
||||
06787C392A710568008ABDD7 /* GesturePassTextView.swift in Sources */,
|
||||
068EC15827ED99C900D5D11E /* ServerListViewController.swift in Sources */,
|
||||
0637FA7C20E0930E00E80174 /* BarkApi.swift in Sources */,
|
||||
061E35862D1E5028009A2D6F /* MessageItemModel.swift in Sources */,
|
||||
06C2CF252685BDB80034B127 /* SpacerCell.swift in Sources */,
|
||||
06BBB8BC2567B3AD0076F63E /* ArchiveSettingCellViewModel.swift in Sources */,
|
||||
061C17082D1BDA4B00891D66 /* MessageGroupMoreView.swift in Sources */,
|
||||
|
||||
@ -105,7 +105,7 @@ class MessageListViewModel: ViewModel, ViewModelType {
|
||||
// messages.append(result[i].freeze())
|
||||
// 不用 freeze 是还没弄明白 freeze 冻结快照释放时机,先直接copy吧
|
||||
// copy 是因为 message 可能在被删除后,还会被访问导致闪退
|
||||
messages.append(.message(model: result[i].copyMessage()))
|
||||
messages.append(.message(model: MessageItemModel(message: result[i])))
|
||||
}
|
||||
page += 1
|
||||
return messages
|
||||
@ -134,9 +134,9 @@ class MessageListViewModel: ViewModel, ViewModelType {
|
||||
messageResult = results.filter("group == nil")
|
||||
}
|
||||
|
||||
var messages: [Message] = []
|
||||
var messages: [MessageItemModel] = []
|
||||
for i in 0..<min(messageResult.count, 5) {
|
||||
messages.append(messageResult[i].copyMessage())
|
||||
messages.append(MessageItemModel(message: messageResult[i]))
|
||||
}
|
||||
if messages.count > 0 {
|
||||
items.append(.messageGroup(name: group ?? NSLocalizedString("default"), totalCount: messageResult.count, messages: messages))
|
||||
@ -188,11 +188,6 @@ class MessageListViewModel: ViewModel, ViewModelType {
|
||||
return BehaviorRelay<[String?]>(value: [])
|
||||
}()
|
||||
|
||||
// Message 转 MessageSection
|
||||
func messagesToMessageSection(messages: [Message]) -> [MessageSection] {
|
||||
let items = messages.map { MessageListCellItem.message(model: $0) }
|
||||
return [MessageSection(header: "model", messages: items)]
|
||||
}
|
||||
// 切换分组时,更新分组名
|
||||
filterGroups
|
||||
.subscribe(onNext: { filterGroups in
|
||||
|
||||
@ -25,16 +25,4 @@ class Message: Object {
|
||||
override class func indexedProperties() -> [String] {
|
||||
return ["group", "createDate"]
|
||||
}
|
||||
|
||||
func copyMessage() -> Message {
|
||||
let message = Message()
|
||||
message.id = self.id
|
||||
message.title = self.title
|
||||
message.subtitle = self.subtitle
|
||||
message.body = self.body
|
||||
message.url = self.url
|
||||
message.group = self.group
|
||||
message.createDate = self.createDate
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
93
Model/MessageItemModel.swift
Normal file
93
Model/MessageItemModel.swift
Normal file
@ -0,0 +1,93 @@
|
||||
//
|
||||
// MessageItemModel.swift
|
||||
// Bark
|
||||
//
|
||||
// Created by huangfeng on 12/27/24.
|
||||
// Copyright © 2024 Fin. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
enum MessageListCellDateStyle {
|
||||
/// 相对时间,例如 1分钟前、1小时前
|
||||
case relative
|
||||
/// 精确时间,例如 2024-01-01 12:00
|
||||
case exact
|
||||
}
|
||||
|
||||
class MessageItemModel {
|
||||
var id: String = ""
|
||||
var attributedText: NSAttributedString?
|
||||
var dateText: String?
|
||||
|
||||
var createDate: Date?
|
||||
var dateStyle: MessageListCellDateStyle = .relative {
|
||||
didSet {
|
||||
switch dateStyle {
|
||||
case .relative:
|
||||
dateText = createDate?.agoFormatString()
|
||||
case .exact:
|
||||
dateText = createDate?.formatString(format: "yyyy-MM-dd HH:mm")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init(message: Message) {
|
||||
let title = message.title ?? ""
|
||||
let subtitle = message.subtitle ?? ""
|
||||
let body = message.body ?? ""
|
||||
let url = message.url ?? ""
|
||||
|
||||
let text = NSMutableAttributedString(
|
||||
string: body,
|
||||
attributes: [.font: UIFont.preferredFont(ofSize: 14), .foregroundColor: BKColor.grey.darken4]
|
||||
)
|
||||
|
||||
if subtitle.count > 0 {
|
||||
// 插入一行空行当 spacer
|
||||
text.insert(NSAttributedString(
|
||||
string: "\n",
|
||||
attributes: [.font: UIFont.systemFont(ofSize: 6, weight: .medium)]
|
||||
), at: 0)
|
||||
|
||||
text.insert(NSAttributedString(
|
||||
string: subtitle + "\n",
|
||||
attributes: [.font: UIFont.preferredFont(ofSize: 16, weight: .medium), .foregroundColor: BKColor.grey.darken4]
|
||||
), at: 0)
|
||||
}
|
||||
|
||||
if title.count > 0 {
|
||||
// 插入一行空行当 spacer
|
||||
text.insert(NSAttributedString(
|
||||
string: "\n",
|
||||
attributes: [.font: UIFont.systemFont(ofSize: 6, weight: .medium)]
|
||||
), at: 0)
|
||||
|
||||
text.insert(NSAttributedString(
|
||||
string: title + "\n",
|
||||
attributes: [.font: UIFont.preferredFont(ofSize: 16, weight: .medium), .foregroundColor: BKColor.grey.darken4]
|
||||
), at: 0)
|
||||
}
|
||||
|
||||
if url.count > 0 {
|
||||
// 插入一行空行当 spacer
|
||||
text.append(NSAttributedString(
|
||||
string: "\n ",
|
||||
attributes: [.font: UIFont.systemFont(ofSize: 8, weight: .medium)]
|
||||
))
|
||||
|
||||
text.append(NSAttributedString(string: "\n\(url)", attributes: [
|
||||
.font: UIFont.preferredFont(ofSize: 14),
|
||||
.foregroundColor: BKColor.grey.darken4,
|
||||
.link: url
|
||||
]))
|
||||
}
|
||||
|
||||
self.id = message.id
|
||||
self.attributedText = text
|
||||
self.createDate = message.createDate
|
||||
defer {
|
||||
self.dateStyle = .relative
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,22 +13,35 @@ import RxDataSources
|
||||
|
||||
enum MessageListCellItem: Equatable {
|
||||
/// 单条消息
|
||||
case message(model: Message)
|
||||
case message(model: MessageItemModel)
|
||||
/// 一组消息,可以收缩折叠
|
||||
case messageGroup(name: String, totalCount: Int, messages: [Message])
|
||||
case messageGroup(name: String, totalCount: Int, messages: [MessageItemModel])
|
||||
|
||||
static func == (lhs: Self, rhs: Self) -> Bool {
|
||||
switch (lhs, rhs) {
|
||||
case (.message(let l), .message(let r)):
|
||||
return l == r
|
||||
case (.messageGroup(let l, _, _), .messageGroup(let r, _, _)):
|
||||
return l == r
|
||||
return l.id == r.id && l.dateText == r.dateText
|
||||
case (.messageGroup(let l, _, let lMessages), .messageGroup(let r, _, let rMessages)):
|
||||
return l == r && lMessages.first?.dateText == rMessages.first?.dateText
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension MessageListCellItem: IdentifiableType {
|
||||
typealias Identity = String
|
||||
|
||||
var identity: String {
|
||||
switch self {
|
||||
case .message(let model):
|
||||
return "list_\(model.id)"
|
||||
case .messageGroup(_, _, let messages):
|
||||
return "group_\(messages.first?.id ?? "")"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct MessageSection {
|
||||
var header: String
|
||||
var messages: [MessageListCellItem]
|
||||
@ -51,16 +64,3 @@ extension MessageSection: AnimatableSectionModelType {
|
||||
return header
|
||||
}
|
||||
}
|
||||
|
||||
extension MessageListCellItem: IdentifiableType {
|
||||
typealias Identity = String
|
||||
|
||||
var identity: String {
|
||||
switch self {
|
||||
case .message(let model):
|
||||
return model.id
|
||||
case .messageGroup(_, _, let messages):
|
||||
return messages.first?.id ?? ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,13 +8,6 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
enum MessageListCellDateStyle {
|
||||
/// 相对时间,例如 1分钟前、1小时前
|
||||
case relative
|
||||
/// 精确时间,例如 2024-01-01 12:00
|
||||
case exact
|
||||
}
|
||||
|
||||
class MessageItemView: UIView {
|
||||
let panel: UIView = {
|
||||
let view = UIView()
|
||||
@ -57,7 +50,7 @@ class MessageItemView: UIView {
|
||||
return label
|
||||
}()
|
||||
|
||||
var message: Message? = nil {
|
||||
var message: MessageItemModel? = nil {
|
||||
didSet {
|
||||
guard let message else {
|
||||
return
|
||||
@ -66,20 +59,6 @@ class MessageItemView: UIView {
|
||||
}
|
||||
}
|
||||
|
||||
var dateStyle: MessageListCellDateStyle = .relative {
|
||||
didSet {
|
||||
guard let message else {
|
||||
return
|
||||
}
|
||||
switch dateStyle {
|
||||
case .relative:
|
||||
dateLabel.text = message.createDate?.agoFormatString()
|
||||
case .exact:
|
||||
dateLabel.text = message.createDate?.formatString(format: "yyyy-MM-dd HH:mm")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var maskAlpha: CGFloat = 0 {
|
||||
didSet {
|
||||
blackMaskView.alpha = maskAlpha
|
||||
@ -107,11 +86,12 @@ class MessageItemView: UIView {
|
||||
// 切换时间显示样式
|
||||
dateLabel.gestureRecognizers?.first?.rx.event.subscribe(onNext: { [weak self] _ in
|
||||
guard let self else { return }
|
||||
if self.dateStyle != .exact {
|
||||
self.dateStyle = .exact
|
||||
if self.message?.dateStyle != .exact {
|
||||
self.message?.dateStyle = .exact
|
||||
} else {
|
||||
self.dateStyle = .relative
|
||||
self.message?.dateStyle = .relative
|
||||
}
|
||||
self.dateLabel.text = self.message?.dateText
|
||||
}).disposed(by: rx.disposeBag)
|
||||
}
|
||||
|
||||
@ -145,59 +125,8 @@ class MessageItemView: UIView {
|
||||
}
|
||||
|
||||
extension MessageItemView {
|
||||
func setMessage(message: Message) {
|
||||
let title = message.title ?? ""
|
||||
let subtitle = message.subtitle ?? ""
|
||||
let body = message.body ?? ""
|
||||
let url = message.url ?? ""
|
||||
|
||||
let text = NSMutableAttributedString(
|
||||
string: body,
|
||||
attributes: [.font: UIFont.preferredFont(ofSize: 14), .foregroundColor: BKColor.grey.darken4]
|
||||
)
|
||||
|
||||
if subtitle.count > 0 {
|
||||
// 插入一行空行当 spacer
|
||||
text.insert(NSAttributedString(
|
||||
string: "\n",
|
||||
attributes: [.font: UIFont.systemFont(ofSize: 6, weight: .medium)]
|
||||
), at: 0)
|
||||
|
||||
text.insert(NSAttributedString(
|
||||
string: subtitle + "\n",
|
||||
attributes: [.font: UIFont.preferredFont(ofSize: 16, weight: .medium), .foregroundColor: BKColor.grey.darken4]
|
||||
), at: 0)
|
||||
}
|
||||
|
||||
if title.count > 0 {
|
||||
// 插入一行空行当 spacer
|
||||
text.insert(NSAttributedString(
|
||||
string: "\n",
|
||||
attributes: [.font: UIFont.systemFont(ofSize: 6, weight: .medium)]
|
||||
), at: 0)
|
||||
|
||||
text.insert(NSAttributedString(
|
||||
string: title + "\n",
|
||||
attributes: [.font: UIFont.preferredFont(ofSize: 16, weight: .medium), .foregroundColor: BKColor.grey.darken4]
|
||||
), at: 0)
|
||||
}
|
||||
|
||||
if url.count > 0 {
|
||||
// 插入一行空行当 spacer
|
||||
text.append(NSAttributedString(
|
||||
string: "\n ",
|
||||
attributes: [.font: UIFont.systemFont(ofSize: 8, weight: .medium)]
|
||||
))
|
||||
|
||||
text.append(NSAttributedString(string: "\n\(url)", attributes: [
|
||||
.font: UIFont.preferredFont(ofSize: 14),
|
||||
.foregroundColor: BKColor.grey.darken4,
|
||||
.link: url
|
||||
]))
|
||||
}
|
||||
|
||||
self.bodyLabel.attributedText = text
|
||||
|
||||
self.dateStyle = .relative
|
||||
func setMessage(message: MessageItemModel) {
|
||||
self.bodyLabel.attributedText = message.attributedText
|
||||
self.dateLabel.text = message.dateText
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ import UIKit
|
||||
/// 单个消息 cell
|
||||
class MessageTableViewCell: UITableViewCell {
|
||||
private let messageView = MessageItemView()
|
||||
var message: Message? {
|
||||
var message: MessageItemModel? {
|
||||
get {
|
||||
return messageView.message
|
||||
}
|
||||
@ -75,7 +75,7 @@ class MessageGroupTableViewCell: UITableViewCell {
|
||||
}
|
||||
|
||||
/// 消息列表
|
||||
private var messages: [Message] = [] {
|
||||
private var messages: [MessageItemModel] = [] {
|
||||
didSet {
|
||||
for (index, item) in messageViews.enumerated() {
|
||||
if index < messages.count {
|
||||
@ -109,7 +109,7 @@ class MessageGroupTableViewCell: UITableViewCell {
|
||||
}
|
||||
}
|
||||
|
||||
var cellData: (groupName: String?, moreCount: Int, messages: [Message])? {
|
||||
var cellData: (groupName: String?, moreCount: Int, messages: [MessageItemModel])? {
|
||||
didSet {
|
||||
groupName = cellData?.groupName ?? ""
|
||||
moreCount = cellData?.moreCount ?? 0
|
||||
@ -173,7 +173,7 @@ class MessageGroupTableViewCell: UITableViewCell {
|
||||
|
||||
/// 更新UI
|
||||
private func refreshViewState() {
|
||||
self.messageViews.first?.bodyLabel.isUserInteractionEnabled = isExpanded
|
||||
self.messageViews.first?.panel.isUserInteractionEnabled = isExpanded
|
||||
self.contentView.gestureRecognizers?.first?.isEnabled = !isExpanded
|
||||
|
||||
for (index, view) in messageViews.enumerated() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user