Add function to format bytes as readable string in KB, MB or GB

This commit is contained in:
Benoit Martins 2024-09-24 15:56:46 +02:00 committed by QuentinArguillere
parent e0e0970481
commit b715cf1cfd
4 changed files with 38 additions and 16 deletions

View file

@ -420,7 +420,7 @@ struct ChatBubbleView: View {
.frame(maxWidth: .infinity, alignment: .leading)
.lineLimit(1)
Text("2,2 Mo")
Text(eventLogMessage.message.attachments.first!.size.formatBytes())
.default_text_style_300(styleSize: 16)
.frame(maxWidth: .infinity, alignment: .leading)
.lineLimit(1)

View file

@ -84,17 +84,19 @@ public struct Attachment: Codable, Identifiable, Hashable {
public let full: URL
public let type: AttachmentType
public let duration: Int
public let size: Int
public init(id: String, name: String, thumbnail: URL, full: URL, type: AttachmentType, duration: Int = 0) {
public init(id: String, name: String, thumbnail: URL, full: URL, type: AttachmentType, duration: Int = 0, size: Int = 0) {
self.id = id
self.name = name
self.thumbnail = thumbnail
self.full = full
self.type = type
self.duration = duration
self.size = size
}
public init(id: String, name: String, url: URL, type: AttachmentType, duration: Int = 0) {
self.init(id: id, name: name, thumbnail: url, full: url, type: type, duration: duration)
public init(id: String, name: String, url: URL, type: AttachmentType, duration: Int = 0, size: Int = 0) {
self.init(id: id, name: name, thumbnail: url, full: url, type: type, duration: duration, size: size)
}
}

View file

@ -312,7 +312,8 @@ class ConversationViewModel: ObservableObject {
id: UUID().uuidString,
name: content.name!,
url: path!,
type: .fileTransfer
type: .fileTransfer,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -342,7 +343,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
url: path!,
type: typeTmp,
duration: typeTmp == . voiceRecording ? content.fileDuration : 0
duration: typeTmp == .voiceRecording ? content.fileDuration : 0,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -358,7 +360,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
thumbnail: pathThumbnail!,
full: path!,
type: .video
type: .video,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -528,7 +531,8 @@ class ConversationViewModel: ObservableObject {
id: UUID().uuidString,
name: content.name!,
url: path!,
type: .fileTransfer
type: .fileTransfer,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -558,7 +562,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
url: path!,
type: typeTmp,
duration: typeTmp == . voiceRecording ? content.fileDuration : 0
duration: typeTmp == . voiceRecording ? content.fileDuration : 0,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -574,7 +579,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
thumbnail: pathThumbnail!,
full: path!,
type: .video
type: .video,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -741,7 +747,8 @@ class ConversationViewModel: ObservableObject {
id: UUID().uuidString,
name: content.name ?? "???",
url: path!,
type: .fileTransfer
type: .fileTransfer,
size: content.fileSize
)
attachmentNameList += ", \(content.name ?? "???")"
attachmentList.append(attachment)
@ -771,7 +778,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
url: path!,
type: typeTmp,
duration: typeTmp == . voiceRecording ? content.fileDuration : 0
duration: typeTmp == . voiceRecording ? content.fileDuration : 0,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -787,7 +795,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
thumbnail: pathThumbnail!,
full: path!,
type: .video
type: .video,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -1027,7 +1036,8 @@ class ConversationViewModel: ObservableObject {
id: UUID().uuidString,
name: content.name!,
url: path!,
type: .fileTransfer
type: .fileTransfer,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -1057,7 +1067,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
url: path!,
type: typeTmp,
duration: typeTmp == . voiceRecording ? content.fileDuration : 0
duration: typeTmp == . voiceRecording ? content.fileDuration : 0,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)
@ -1073,7 +1084,8 @@ class ConversationViewModel: ObservableObject {
name: content.name!,
thumbnail: pathThumbnail!,
full: path!,
type: .video
type: .video,
size: content.fileSize
)
attachmentNameList += ", \(content.name!)"
attachmentList.append(attachment)

View file

@ -34,6 +34,14 @@ extension Int {
}
return "\(duration)\(self.getMinute(minute: minute))\(self.getSecond(second: second))"
}
public func formatBytes() -> String {
let byteCountFormatter = ByteCountFormatter()
byteCountFormatter.allowedUnits = [.useKB, .useMB, .useGB] // Allows KB, MB and KB
byteCountFormatter.countStyle = .file // Use file size style
byteCountFormatter.isAdaptive = true // Adjusts automatically to appropriate unit
return byteCountFormatter.string(fromByteCount: Int64(self))
}
private func getHour(hour: Int) -> String {
var duration = "\(hour):"