62 lines
1.8 KiB
Swift
62 lines
1.8 KiB
Swift
//
|
|
// CryptoChatModelRealmChat.swift
|
|
// Wallet
|
|
//
|
|
// Created by Saveliy Stavitsky on 8/25/20.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import class RealmSwift.Object
|
|
import struct RealmSwift.Persisted
|
|
|
|
class CryptoChatModelRealmChat: Object, Codable {
|
|
@Persisted(primaryKey: true) var chatName: String
|
|
@Persisted(indexed: true) var timestamp: Date
|
|
@Persisted var data: String
|
|
@Persisted var unreadCount: Int
|
|
|
|
override required init() { }
|
|
|
|
init(chatName: String, timestamp: Date, data: String, unreadCount: Int) {
|
|
super.init()
|
|
|
|
self.chatName = chatName.lowercased()
|
|
self.timestamp = timestamp
|
|
self.data = data
|
|
self.unreadCount = unreadCount
|
|
}
|
|
|
|
var time: String {
|
|
if Calendar.current.isDateInToday(timestamp) {
|
|
let dateFormat = DateFormatter()
|
|
dateFormat.dateStyle = .none
|
|
dateFormat.timeStyle = .short
|
|
return dateFormat.string(from: timestamp)
|
|
} else if timestamp.isInThisYear {
|
|
let dateFormat = DateFormatter()
|
|
dateFormat.dateFormat = "dd MMM"
|
|
return dateFormat.string(from: timestamp)
|
|
} else {
|
|
let dateFormat = DateFormatter()
|
|
dateFormat.dateStyle = .medium
|
|
dateFormat.timeStyle = .none
|
|
return dateFormat.string(from: timestamp)
|
|
}
|
|
}
|
|
|
|
private var plainText: String? {
|
|
data.starts(with: "<plaintext>")
|
|
? String(data.dropFirst("<plaintext>".count).dropLast("</plaintext>".count))
|
|
: nil
|
|
}
|
|
|
|
private var qr: String? {
|
|
data.starts(with: "<qr>")
|
|
? String(data.dropFirst("<qr>".count).dropLast("</qr>".count))
|
|
: nil
|
|
}
|
|
|
|
var textDescription: String { plainText ?? qr ?? data }
|
|
}
|