Bark/Model/MessageItemModel.swift
2025-05-22 12:11:18 +08:00

102 lines
3.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MessageItemModel.swift
// Bark
//
// Created by huangfeng on 12/27/24.
// Copyright © 2024 Fin. All rights reserved.
//
import Kingfisher
import UIKit
enum MessageListCellDateStyle {
/// 11
case relative
/// 2024-01-01 12:00
case exact
}
class MessageItemModel {
var id: String = ""
var group: String?
var attributedText: NSAttributedString?
var dateText: String?
var image: 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) {
self.id = message.id
self.group = message.group
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.attributedText = text
self.createDate = message.createDate
self.image = message.image
defer {
self.dateStyle = .relative
}
}
}