From 4c644d5c79dba253501e85058d495570156c79f8 Mon Sep 17 00:00:00 2001 From: forgotvas Date: Tue, 19 Mar 2024 14:22:37 +0300 Subject: [PATCH 1/3] fix: announcements --- Core/Core/Configuration/CSSInjector.swift | 12 +- .../Handouts/HandoutsUpdatesDetailView.swift | 136 ++++++++++++------ 2 files changed, 105 insertions(+), 43 deletions(-) diff --git a/Core/Core/Configuration/CSSInjector.swift b/Core/Core/Configuration/CSSInjector.swift index efd18643c..7161965c5 100644 --- a/Core/Core/Configuration/CSSInjector.swift +++ b/Core/Core/Configuration/CSSInjector.swift @@ -95,9 +95,17 @@ public class CSSInjector { var maxWidth: String switch type { case .discovery: - maxWidth = "max-width: \(screenWidth)px;" + if screenWidth == .infinity { + maxWidth = "max-width: 100%;" + } else { + maxWidth = "max-width: \(screenWidth)px;" + } case .comment: - maxWidth = "width: \(screenWidth / 1.3)px;" + if screenWidth == .infinity { + maxWidth = "width: 100%;" + } else { + maxWidth = "width: \(screenWidth / 1.3)px;" + } } func currentColor() -> String { diff --git a/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift b/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift index ac4fbeddd..ea8babd71 100644 --- a/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift +++ b/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift @@ -17,8 +17,8 @@ public struct HandoutsUpdatesDetailView: View { private var router: CourseRouter private let cssInjector: CSSInjector - public var handouts: String? - public var announcements: [CourseUpdate]? + private var handouts: String? + private var announcements: [CourseUpdate]? private let title: String public init( @@ -77,12 +77,9 @@ public struct HandoutsUpdatesDetailView: View { public var body: some View { ZStack(alignment: .top) { Theme.Colors.background - .ignoresSafeArea() - GeometryReader { reader in - + .ignoresSafeArea() // MARK: - Page Body VStack(alignment: .leading) { - // MARK: - Handouts if let handouts { let formattedHandouts = cssInjector.injectCSS( @@ -94,52 +91,109 @@ public struct HandoutsUpdatesDetailView: View { ) WebViewHtml(fixBrokenLinks(in: formattedHandouts)) - } else if let announcements { - + } else if let html = announcemetsHtml() { // MARK: - Announcements - ScrollView { - ForEach(Array(announcements.enumerated()), id: \.offset) { index, ann in - - Text(ann.date) - .font(Theme.Fonts.labelSmall) - let formattedAnnouncements = cssInjector.injectCSS( - colorScheme: colorScheme, - html: ann.content, - type: .discovery, - screenWidth: reader.size.width - ) - HStack { - HTMLFormattedText(formattedAnnouncements) - Spacer() - } - - .id(UUID()) - - if index != announcements.count - 1 { - Divider() - } - } - }.frame(height: reader.size.height - 60) - } - }.padding(.top, 8) - .padding(.horizontal, 32) - .frame( - maxHeight: .infinity, - alignment: .topLeading) - .onRightSwipeGesture { - router.back() + WebViewHtml(fixBrokenLinks(in: html)) } + } + .padding(.top, 8) + .padding(.horizontal, 32) + .frame( + maxHeight: .infinity, + alignment: .topLeading) + .onRightSwipeGesture { + router.back() + } Spacer(minLength: 84) - } } .navigationBarHidden(false) .navigationBarBackButtonHidden(false) .navigationTitle(title) - .onChange(of: colorSchemeNative) { newValue in + .onChange(of: colorSchemeNative) { _ in guard UIApplication.shared.applicationState == .active else { return } updateColorScheme() } } + + func fontsCSS(for fontFamily: String) -> String? { + if let path = Bundle(for: ThemeBundle.self).path(forResource: "fonts_file", ofType: "ttf"), + let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe) { + let fontCSS = """ + @font-face { + font-family: \(fontFamily); + src: url(data:font/truetype;charset=utf-8;base64,\(data.base64EncodedString())) format('truetype'); + font-weight: normal; + font-style: normal; + } + """ + return "" + } + return nil + } + + func titleHTML(fontFamily: String, fontSize: CGFloat, title: String) -> String { + """ +
+

+ \(title) +

+
+ """ + } + + func dividerHTML() -> String { + """ +
+
+ """ + } + + func announcemetsHtml() -> String? { + guard let announcements = announcements else {return nil} + var html: String = "" + let font = Theme.UIFonts.labelSmall() + let fontFamily = font.familyName + let fontSize = font.pointSize + + if let fontsCSS = fontsCSS(for: fontFamily) { + html.append(fontsCSS) + } + + for (index, ann) in announcements.enumerated() { + let titleHTML = titleHTML(fontFamily: fontFamily, fontSize: fontSize, title: ann.date) + html.append("
\(titleHTML)\n\(ann.content)
") + + if index != announcements.count - 1 { + html.append(dividerHTML()) + } + } + let formattedAnnouncements = cssInjector.injectCSS( + colorScheme: colorScheme, + html: html, + type: .discovery, + fontSize: 100, + screenWidth: .infinity + ) + + return """ + + \(formattedAnnouncements) + + """ + } } #if DEBUG From c769a681f86e729846a746980f46ee64ea958206 Mon Sep 17 00:00:00 2001 From: forgotvas Date: Tue, 19 Mar 2024 14:26:15 +0300 Subject: [PATCH 2/3] chore: fix divider --- .../Presentation/Handouts/HandoutsUpdatesDetailView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift b/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift index ea8babd71..2ac803a91 100644 --- a/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift +++ b/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift @@ -155,7 +155,7 @@ public struct HandoutsUpdatesDetailView: View { margin-bottom: 3px !important; background-color: \(UIColor.opaqueSeparator.cgColor.hexString ?? "") !important; width: 100%; - height: 0.3px; + height: 0.5px; "> """ From 4930522833d5ff55dd083a9eb11b80aecf68a4b6 Mon Sep 17 00:00:00 2001 From: forgotvas Date: Tue, 19 Mar 2024 14:29:41 +0300 Subject: [PATCH 3/3] chore: refactor --- .../Handouts/HandoutsUpdatesDetailView.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift b/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift index 2ac803a91..80b8ce43f 100644 --- a/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift +++ b/Course/Course/Presentation/Handouts/HandoutsUpdatesDetailView.swift @@ -189,9 +189,14 @@ public struct HandoutsUpdatesDetailView: View { ) return """ - - \(formattedAnnouncements) - + + + + + + \(formattedAnnouncements) + + """ } }