Skip to content

Commit 80b491b

Browse files
committed
保存widget数据时限制长度
1 parent 1cdf194 commit 80b491b

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

Common/WidgetHistoryModels.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ enum WidgetHistoryConstants {
1313
static let widgetKind = "RecentMessagesWidget"
1414
static let displayLimit = 3
1515
static let snapshotRetentionLimit = 100
16+
static let bodyCharacterLimit = 500
17+
}
18+
19+
private extension String {
20+
func trimmedForWidget(limit: Int = WidgetHistoryConstants.bodyCharacterLimit) -> String {
21+
let normalized = trimmingCharacters(in: .whitespacesAndNewlines)
22+
guard normalized.count > limit else {
23+
return normalized
24+
}
25+
return String(normalized.prefix(limit))
26+
}
1627
}
1728

1829
struct WidgetHistoryMessage: Codable, Identifiable {
@@ -36,7 +47,7 @@ struct WidgetHistoryMessage: Codable, Identifiable {
3647
self.group = group
3748
self.title = title
3849
self.subtitle = subtitle
39-
self.body = body
50+
self.body = body?.trimmedForWidget()
4051
self.image = image
4152
self.createDate = createDate
4253
}

0 commit comments

Comments
 (0)