-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetHistoryModels.swift
More file actions
78 lines (69 loc) · 2.08 KB
/
WidgetHistoryModels.swift
File metadata and controls
78 lines (69 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// WidgetHistoryModels.swift
// Bark
//
// Created by OpenCode on 2026/3/26.
//
import Foundation
enum WidgetHistoryConstants {
static let appGroupIdentifier = "group.bark"
static let snapshotFilename = "recent_messages_snapshot.json"
static let widgetKind = "RecentMessagesWidget"
static let displayLimit = 3
static let snapshotRetentionLimit = 100
static let bodyCharacterLimit = 500
}
private extension String {
func trimmedForWidget(limit: Int = WidgetHistoryConstants.bodyCharacterLimit) -> String {
let normalized = trimmingCharacters(in: .whitespacesAndNewlines)
guard normalized.count > limit else {
return normalized
}
return String(normalized.prefix(limit))
}
}
struct WidgetHistoryMessage: Codable, Identifiable {
let id: String
let group: String?
let title: String?
let subtitle: String?
let body: String?
let image: String?
let createDate: Date
init(id: String,
group: String?,
title: String?,
subtitle: String?,
body: String?,
image: String?,
createDate: Date)
{
self.id = id
self.group = group
self.title = title
self.subtitle = subtitle
self.body = body?.trimmedForWidget()
self.image = image
self.createDate = createDate
}
}
struct WidgetHistorySnapshot: Codable {
let messages: [WidgetHistoryMessage]
func recentMessages(in group: String?, limit: Int = WidgetHistoryConstants.displayLimit) -> [WidgetHistoryMessage] {
let filteredMessages: [WidgetHistoryMessage]
if let group {
filteredMessages = messages.filter { $0.group == group }
} else {
filteredMessages = messages
}
return Array(filteredMessages.prefix(limit))
}
var availableGroups: [String] {
messages.reduce(into: [String]()) { result, message in
guard let group = message.group, !group.isEmpty, !result.contains(group) else {
return
}
result.append(group)
}
}
}