-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RNMobile] Support Theme Colors and Gradients #14085
Merged
+299
−76
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
16b7e6e
Point to develop branch
chipsnyder ce7c504
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder 7da72a8
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder dd88567
Adds baseline theme support store to provide ground work to add theme…
chipsnyder 7d54903
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder 020cc51
Fetch and Parse color palette colors for the theme
chipsnyder 5ad5b41
Rename ThemeSupportStore to EditorThemeStore, and removed clear theme…
chipsnyder 1d38c5d
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder ef4ae30
Remove unused method calls
chipsnyder 480f548
Add support for custom gradients for gutenberg
chipsnyder f218bd9
Enable local cache for theme supprt
chipsnyder 2436309
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder 24de75d
Update gutenberg referecne
chipsnyder 383e2a4
Update gutenberg reference
chipsnyder 508103d
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder 4dc5f81
Update GB reference
chipsnyder 39fe593
Refactor to make code more readable
chipsnyder fb480ca
Refactor file layout
chipsnyder aae9ffb
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder 06352af
Merge remote-tracking branch 'origin/develop' into rnmobile/issue/174…
chipsnyder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Foundation | ||
import Gutenberg | ||
|
||
struct EditorTheme: Codable, Equatable { | ||
static func == (lhs: EditorTheme, rhs: EditorTheme) -> Bool { | ||
return lhs.description == rhs.description | ||
} | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case themeSupport = "theme_supports" | ||
case version | ||
case stylesheet | ||
} | ||
|
||
let themeSupport: EditorThemeSupport? | ||
let version: String? | ||
let stylesheet: String? | ||
|
||
var description: String { | ||
return "\(stylesheet ?? "")-\(version ?? "")" | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let map = try decoder.container(keyedBy: CodingKeys.self) | ||
self.themeSupport = try? map.decode(EditorThemeSupport.self, forKey: .themeSupport) | ||
self.version = try? map.decode(String.self, forKey: .version) | ||
self.stylesheet = try? map.decode(String.self, forKey: .stylesheet) | ||
} | ||
} | ||
|
||
struct EditorThemeSupport: Codable, GutenbergEditorTheme { | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case colors = "editor-color-palette" | ||
case gradients = "editor-gradient-presets" | ||
} | ||
|
||
let colors: [[String: String]]? | ||
let gradients: [[String: String]]? | ||
|
||
init(from decoder: Decoder) throws { | ||
let map = try decoder.container(keyedBy: CodingKeys.self) | ||
self.colors = try? map.decode([[String: String]].self, forKey: .colors) | ||
self.gradients = try? map.decode([[String: String]].self, forKey: .gradients) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import WordPressFlux | ||
|
||
struct EditorThemeQuery { | ||
let blog: Blog | ||
} | ||
|
||
enum EditorThemeStoreState { | ||
typealias StoredThemes = [String: EditorTheme] | ||
case empty | ||
case loaded(StoredThemes) | ||
|
||
static func key(forBlog blog: Blog) -> String? { | ||
return blog.hostname as String? | ||
} | ||
|
||
func editorTheme(forBlog blog: Blog) -> EditorTheme? { | ||
guard let themeKey = EditorThemeStoreState.key(forBlog: blog) else { | ||
return nil | ||
} | ||
|
||
switch self { | ||
case .loaded(let themes): | ||
return themes[themeKey] | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func storedThemes() -> StoredThemes { | ||
switch self { | ||
case .loaded(let themes): | ||
return themes | ||
default: | ||
return [:] | ||
} | ||
} | ||
} | ||
|
||
extension EditorThemeStoreState: Codable { | ||
|
||
enum Key: CodingKey { | ||
case rawValue | ||
case associatedValue | ||
} | ||
|
||
enum CodingError: Error { | ||
case unknownValue | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: Key.self) | ||
let rawValue = try container.decode(Int.self, forKey: .rawValue) | ||
switch rawValue { | ||
case 0: | ||
self = .empty | ||
case 1: | ||
let themes = try container.decode(StoredThemes.self, forKey: .associatedValue) | ||
self = .loaded(themes) | ||
default: | ||
throw CodingError.unknownValue | ||
} | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: Key.self) | ||
switch self { | ||
case .empty: | ||
try container.encode(0, forKey: .rawValue) | ||
case .loaded(let themes): | ||
try container.encode(1, forKey: .rawValue) | ||
try container.encode(themes, forKey: .associatedValue) | ||
} | ||
} | ||
} | ||
|
||
class EditorThemeStore: QueryStore<EditorThemeStoreState, EditorThemeQuery> { | ||
|
||
private enum ErrorCode: Int { | ||
case processingError | ||
} | ||
|
||
init(dispatcher: ActionDispatcher = .global) { | ||
super.init(initialState: .empty, dispatcher: dispatcher) | ||
} | ||
|
||
override func queriesChanged() { | ||
|
||
activeQueries.forEach { (query) in | ||
fetchTheme(for: query.blog) | ||
} | ||
} | ||
|
||
override func logError(_ error: String) { | ||
DDLogError("Error loading active theme: \(error)") | ||
} | ||
} | ||
|
||
private extension EditorThemeStore { | ||
|
||
func fetchTheme(for blog: Blog) { | ||
let requestPath = "/wp/v2/themes?status=active" | ||
GutenbergNetworkRequest(path: requestPath, blog: blog).request { [weak self] result in | ||
switch result { | ||
case .success(let response): | ||
self?.processResponse(response, for: blog) | ||
case .failure(let error): | ||
DDLogError("Error loading active theme: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func processResponse(_ response: Any, for blog: Blog) { | ||
guard | ||
let responseData = try? JSONSerialization.data(withJSONObject: response, options: []), | ||
let themeKey = EditorThemeStoreState.key(forBlog: blog), | ||
let themeSupports = try? JSONDecoder().decode([EditorTheme].self, from: responseData), | ||
let newTheme = themeSupports.first | ||
else { return } | ||
|
||
var existingThemes = state.storedThemes() | ||
if newTheme != existingThemes[themeKey] { | ||
existingThemes[themeKey] = newTheme | ||
state = .loaded(existingThemes) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we using
try?
here because this key can be optional?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah if the colors or gradients are missing then the JSON will have
editor-color-palette: false
oreditor-gradient-presets: false
sotry?
here is to protect against the type change.It's then left as an optional to make sure we don't send an empty array to Gutenberg and override the default colors that we might want.