From 4c807ee82b5f44b72f97092cea1a546f7e916993 Mon Sep 17 00:00:00 2001 From: Fernando Valverde Date: Fri, 6 Nov 2020 11:06:08 -0600 Subject: [PATCH] Adds supported themes as enum --- ExampleTests/ExampleTests.swift | 7 ++++--- ForemWebView/ForemUserData.swift | 19 ++++++++++++++++--- ForemWebView/ForemWebView.swift | 4 ++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ExampleTests/ExampleTests.swift b/ExampleTests/ExampleTests.swift index fce9fce..b15a137 100644 --- a/ExampleTests/ExampleTests.swift +++ b/ExampleTests/ExampleTests.swift @@ -1,5 +1,6 @@ import XCTest import WebKit +import ForemWebView @testable import Example class ExampleTests: XCTestCase { @@ -95,7 +96,7 @@ class ExampleTests: XCTestCase { // On top of the expectation it turns out we need to give the webView some time to load/process the HTML string DispatchQueue.main.asyncAfter(deadline: .now() + asyncAfter) { webView.fetchUserData { (userData) in - XCTAssertTrue(userData?.theme() == "default") + XCTAssertTrue(userData?.theme() == .base) promise.fulfill() } } @@ -114,7 +115,7 @@ class ExampleTests: XCTestCase { // On top of the expectation it turns out we need to give the webView some time to load/process the HTML string DispatchQueue.main.asyncAfter(deadline: .now() + asyncAfter) { webView.fetchUserData { (userData) in - XCTAssertTrue(userData?.theme() == "pink-theme") + XCTAssertTrue(userData?.theme() == .pink) promise.fulfill() } } @@ -133,7 +134,7 @@ class ExampleTests: XCTestCase { // On top of the expectation it turns out we need to give the webView some time to load/process the HTML string DispatchQueue.main.asyncAfter(deadline: .now() + asyncAfter) { webView.fetchUserData { (userData) in - XCTAssertTrue(userData?.theme() == "night-theme") + XCTAssertTrue(userData?.theme() == .night) promise.fulfill() } } diff --git a/ForemWebView/ForemUserData.swift b/ForemWebView/ForemUserData.swift index d842530..27c671c 100644 --- a/ForemWebView/ForemUserData.swift +++ b/ForemWebView/ForemUserData.swift @@ -9,14 +9,27 @@ import Foundation public var configBodyClass: String // Returns the UX theme in the logged-in user's settings - public func theme() -> String { + public func theme() -> ForemWebViewTheme { + var themeName = "" let regex = #".+-theme"# for element in configBodyClass.split(separator: " ") { if let range = element.range(of: regex, options: .regularExpression) { - return String(element[range]) + themeName = String(element[range]) } } - return "default" + + switch themeName { + case "night-theme": + return .night + case "minimal-light-theme": + return .minimal + case "pink-theme": + return .pink + case "ten-x-hacker-theme": + return .hacker + default: + return .base + } } public static func isEqual(lfi: ForemUserData, rfi: ForemUserData) -> Bool { diff --git a/ForemWebView/ForemWebView.swift b/ForemWebView/ForemWebView.swift index a71ec96..fbc0811 100644 --- a/ForemWebView/ForemWebView.swift +++ b/ForemWebView/ForemWebView.swift @@ -14,6 +14,10 @@ public enum ForemWebViewError: Error { case invalidInstance(String) } +public enum ForemWebViewTheme { + case base, night, minimal, pink, hacker +} + open class ForemWebView: WKWebView { var videoPlayerLayer: AVPlayerLayer?