From 273fe08bb2d9a9c78b7639af7593eca66b083675 Mon Sep 17 00:00:00 2001 From: David Christiandy <1299411+dvdchr@users.noreply.github.com> Date: Wed, 8 Mar 2023 16:28:25 +0700 Subject: [PATCH 1/2] Skip parsing blogging reminders schedule when backgrounded --- .../BloggingRemindersScheduleFormatter.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift b/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift index b7c77886adfc..28ef402b9ad4 100644 --- a/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift +++ b/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift @@ -113,6 +113,13 @@ private extension BloggingRemindersScheduleFormatter { } static func stringToAttributedString(_ string: String) -> NSAttributedString { + // When the app is in a background state, return the non-emphasized plain String instead. + // Parsing a HTML string in the background may lead to a crash. + // Refer to ReaderCommentsViewController.m:1088 for a similar scenario and explanation. + if UIApplication.shared.applicationState == .background { + return .init(string: string.removedHTMLEmphases()) + } + let htmlData = NSString(string: string).data(using: String.Encoding.unicode.rawValue) ?? Data() let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html] @@ -176,3 +183,13 @@ private extension BloggingRemindersScheduleFormatter { comment: "Short title telling the user they will receive a blogging reminder every day of the week.") } } + +private extension String { + /// Convenient method to remove HTML emphasis tags from a given string. + func removedHTMLEmphases() -> String { + guard let expression = try? NSRegularExpression(pattern: "", options: .caseInsensitive) else { + return self + } + return expression.stringByReplacingMatches(in: self, range: NSMakeRange(0, self.count), withTemplate: String()) + } +} From e18784ed6695d5d072732c6a992ec17cc4bac70b Mon Sep 17 00:00:00 2001 From: David Christiandy <1299411+dvdchr@users.noreply.github.com> Date: Mon, 13 Mar 2023 15:30:34 +0700 Subject: [PATCH 2/2] Update comments to refer to the PR instead --- .../Blogging Reminders/BloggingRemindersScheduleFormatter.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift b/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift index 28ef402b9ad4..7a3dae78bf50 100644 --- a/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift +++ b/WordPress/Classes/Utility/Blogging Reminders/BloggingRemindersScheduleFormatter.swift @@ -115,7 +115,7 @@ private extension BloggingRemindersScheduleFormatter { static func stringToAttributedString(_ string: String) -> NSAttributedString { // When the app is in a background state, return the non-emphasized plain String instead. // Parsing a HTML string in the background may lead to a crash. - // Refer to ReaderCommentsViewController.m:1088 for a similar scenario and explanation. + // Refer to https://github.com/wordpress-mobile/WordPress-iOS/pull/17678 for a similar scenario and explanation. if UIApplication.shared.applicationState == .background { return .init(string: string.removedHTMLEmphases()) }