Skip to content

Commit

Permalink
Merge pull request #9 from forem/fdoxyz/themes-as-enums
Browse files Browse the repository at this point in the history
Adds supported themes as enum
  • Loading branch information
fdocr committed Nov 6, 2020
2 parents 80a0dd4 + 4c807ee commit 1d103d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 4 additions & 3 deletions ExampleTests/ExampleTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import XCTest
import WebKit
import ForemWebView
@testable import Example

class ExampleTests: XCTestCase {
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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()
}
}
Expand All @@ -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()
}
}
Expand Down
19 changes: 16 additions & 3 deletions ForemWebView/ForemUserData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions ForemWebView/ForemWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 1d103d7

Please sign in to comment.