Skip to content
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

Pass the Gallery v2 Flag over from the editor #16832

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def wordpress_ui
end

def wordpress_kit
pod 'WordPressKit', '~> 4.37.0-beta'
# pod 'WordPressKit', '~> 4.37.0-beta'
# pod 'WordPressKit', :git => 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', :tag => ''
# pod 'WordPressKit', :git => 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', :branch => ''
# pod 'WordPressKit', :git => 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', :commit => ''
pod 'WordPressKit', :git => 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', :commit => 'bd0da2a4d94528193fad152717d908b85efa2db7'
# pod 'WordPressKit', :path => '../WordPressKit-iOS'
end

Expand Down Expand Up @@ -166,7 +166,7 @@ abstract_target 'Apps' do
## Gutenberg (React Native)
## =====================
##
gutenberg :tag => 'v1.57.0-alpha1'
gutenberg :commit => '6bf4531d056ea279962ffa0d79d499229467f10a'

## Third party libraries
## =====================
Expand Down
191 changes: 98 additions & 93 deletions Podfile.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import CoreData
enum BlockEditorSettingElementTypes: String {
case color
case gradient
case experimentalFeatures

var valueKey: String {
self.rawValue
}
}

enum BlockEditorExperimentalFeatureKeys: String {
case galleryRefactor
}

extension BlockEditorSettingElement {

@nonobjc public class func fetchRequest() -> NSFetchRequest<BlockEditorSettingElement> {
Expand Down Expand Up @@ -53,12 +58,21 @@ extension BlockEditorSettingElement: Identifiable {
}

convenience init(fromRawRepresentation rawObject: [String: String], type: BlockEditorSettingElementTypes, order: Int, context: NSManagedObjectContext) {
self.init(name: rawObject[ #keyPath(BlockEditorSettingElement.name)],
value: rawObject[type.valueKey],
slug: rawObject[#keyPath(BlockEditorSettingElement.slug)],
type: type,
order: order,
context: context)
}

convenience init(name: String?, value: String?, slug: String?, type: BlockEditorSettingElementTypes, order: Int, context: NSManagedObjectContext) {
self.init(context: context)

self.type = type.rawValue
self.value = rawObject[type.valueKey] ?? ""
self.slug = rawObject[#keyPath(BlockEditorSettingElement.slug)] ?? ""
self.name = rawObject[ #keyPath(BlockEditorSettingElement.name)] ?? ""
self.value = value ?? ""
self.slug = slug ?? ""
self.name = name ?? ""
self.order = order
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ extension BlockEditorSettings: GutenbergEditorSettings {
elementsByType(.gradient)
}

public var galleryRefactor: Bool {
return experimentalFeature(.galleryRefactor)
}

private func elementsByType(_ type: BlockEditorSettingElementTypes) -> [[String: String]]? {
return elements?.sorted(by: { (lhs, rhs) -> Bool in
return lhs.order >= rhs.order
Expand All @@ -19,6 +23,15 @@ extension BlockEditorSettings: GutenbergEditorSettings {
return element.rawRepresentation
})
}

private func experimentalFeature(_ feature: BlockEditorExperimentalFeatureKeys) -> Bool {
guard let experimentalFeature = elements?.first(where: { (element) -> Bool in
guard element.type == BlockEditorSettingElementTypes.experimentalFeatures.rawValue else { return false }
return element.slug == feature.rawValue
}) else { return false }

return Bool(experimentalFeature.value) ?? false
}
}

extension BlockEditorSettings {
Expand Down Expand Up @@ -60,6 +73,16 @@ extension BlockEditorSettings {
parsedElements.insert(BlockEditorSettingElement(fromRawRepresentation: gradient, type: .gradient, order: index, context: context))
})

// Experimental Features
let galleryKey = BlockEditorExperimentalFeatureKeys.galleryRefactor.rawValue
let galleryRefactor = BlockEditorSettingElement(name: galleryKey,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this flag is expected to be temporary I'm storing it in the BlockEditorSettingElement as a new type experimentalFeatures

value: "\(remoteSettings.galleryRefactor)",
slug: galleryKey,
type: .experimentalFeatures,
order: 0,
context: context)
parsedElements.insert(galleryRefactor)

self.elements = parsedElements
}
}
96 changes: 63 additions & 33 deletions WordPress/Classes/Services/BlockEditorSettingsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import Foundation
import WordPressKit

class BlockEditorSettingsService {
typealias BlockEditorSettingsServiceCompletion = (_ hasChanges: Bool, _ blockEditorSettings: BlockEditorSettings?) -> Void
struct SettingsServiceResult {
let hasChanges: Bool
let blockEditorSettings: BlockEditorSettings?
}

enum BlockEditorSettingsServiceError: Int, Error {
case blogNotFound
}

typealias BlockEditorSettingsServiceCompletion = (Swift.Result<SettingsServiceResult, Error>) -> Void

let blog: Blog
let remote: BlockEditorSettingsServiceRemote
Expand Down Expand Up @@ -54,8 +63,9 @@ private extension BlockEditorSettingsService {
let originalChecksum = self.blog.blockEditorSettings?.checksum ?? ""
self.updateEditorThemeCache(originalChecksum: originalChecksum, editorTheme: editorTheme, completion: completion)
}
case .failure(let error):
DDLogError("Error loading active theme: \(error)")
case .failure(let err):
DDLogError("Error loading active theme: \(err)")
completion(.failure(err))
}
}
}
Expand All @@ -64,7 +74,8 @@ private extension BlockEditorSettingsService {
let newChecksum = editorTheme?.checksum ?? ""
guard originalChecksum != newChecksum else {
/// The fetched Editor Theme is the same as the cached one so respond with no new changes.
completion(false, self.blog.blockEditorSettings)
let result = SettingsServiceResult(hasChanges: false, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
return
}

Expand All @@ -76,27 +87,29 @@ private extension BlockEditorSettingsService {

/// The fetched Editor Theme is different than the cached one so persist the new one and delete the old one.
context.perform {
self.persistEditorThemeToCoreData(blogID: self.blog.objectID, editorTheme: editorTheme) { success in
guard success else {
completion(false, nil)
return
}

self.context.perform {
completion(true, self.blog.blockEditorSettings)
self.persistEditorThemeToCoreData(blogID: self.blog.objectID, editorTheme: editorTheme) { callback in
switch callback {
case .success:
self.context.perform {
let result = SettingsServiceResult(hasChanges: true, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
}
case .failure(let err):
completion(.failure(err))
}
}
}
}

func persistEditorThemeToCoreData(blogID: NSManagedObjectID, editorTheme: RemoteEditorTheme, completion: @escaping (_ success: Bool) -> Void) {
func persistEditorThemeToCoreData(blogID: NSManagedObjectID, editorTheme: RemoteEditorTheme, completion: @escaping (Swift.Result<Void, Error>) -> Void) {
let parsingContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
parsingContext.parent = context
parsingContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump

parsingContext.perform {
guard let blog = parsingContext.object(with: blogID) as? Blog else {
completion(false)
let err = BlockEditorSettingsServiceError.blogNotFound
completion(.failure(err))
return
}

Expand All @@ -106,25 +119,33 @@ private extension BlockEditorSettingsService {
}

blog.blockEditorSettings = BlockEditorSettings(editorTheme: editorTheme, context: parsingContext)
try? parsingContext.save()
completion(true)
do {
try parsingContext.save()
} catch let err {
completion(.failure(err))
}

completion(.success(()))
}
}
}

// MARK: Editor Global Styles support
private extension BlockEditorSettingsService {
func fetchBlockEditorSettings(_ completion: @escaping BlockEditorSettingsServiceCompletion) {
remote.fetchBlockEditorSettings { [weak self] (response) in
remote.fetchBlockEditorSettings(forSiteID: blog.dotComID?.intValue) { [weak self] (response) in
guard let `self` = self else { return }
switch response {
case .success(let remoteSettings):
self.context.perform {
let originalChecksum = self.blog.blockEditorSettings?.checksum ?? ""
self.updateBlockEditorSettingsCache(originalChecksum: originalChecksum, remoteSettings: remoteSettings, completion: completion)
}
case .failure(let error):
DDLogError("Error loading Block Editor Settings: \(error)")
case .failure(let err):
DDLogError("Error fetching editor settings: \(err)")
// The user may not have the gutenberg plugin installed so try /wp/v2/themes to maintain feature support.
// In WP 5.9 we may be able to skip this attempt.
self.fetchTheme(completion)
}
}
}
Expand All @@ -133,7 +154,8 @@ private extension BlockEditorSettingsService {
let newChecksum = remoteSettings?.checksum ?? ""
guard originalChecksum != newChecksum else {
/// The fetched Block Editor Settings is the same as the cached one so respond with no new changes.
completion(false, self.blog.blockEditorSettings)
let result = SettingsServiceResult(hasChanges: false, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
return
}

Expand All @@ -145,27 +167,29 @@ private extension BlockEditorSettingsService {

/// The fetched Block Editor Settings is different than the cached one so persist the new one and delete the old one.
context.perform {
self.persistBlockEditorSettingsToCoreData(blogID: self.blog.objectID, remoteSettings: remoteSettings) { success in
guard success else {
completion(false, nil)
return
}

self.context.perform {
completion(true, self.blog.blockEditorSettings)
self.persistBlockEditorSettingsToCoreData(blogID: self.blog.objectID, remoteSettings: remoteSettings) { callback in
switch callback {
case .success:
self.context.perform {
let result = SettingsServiceResult(hasChanges: true, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
}
case .failure(let err):
completion(.failure(err))
}
}
}
}

func persistBlockEditorSettingsToCoreData(blogID: NSManagedObjectID, remoteSettings: RemoteBlockEditorSettings, completion: @escaping (_ success: Bool) -> Void) {
func persistBlockEditorSettingsToCoreData(blogID: NSManagedObjectID, remoteSettings: RemoteBlockEditorSettings, completion: @escaping (Swift.Result<Void, Error>) -> Void) {
let parsingContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
parsingContext.parent = context
parsingContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump

parsingContext.perform {
guard let blog = parsingContext.object(with: blogID) as? Blog else {
completion(false)
let err = BlockEditorSettingsServiceError.blogNotFound
completion(.failure(err))
return
}

Expand All @@ -175,8 +199,13 @@ private extension BlockEditorSettingsService {
}

blog.blockEditorSettings = BlockEditorSettings(remoteSettings: remoteSettings, context: parsingContext)
try? parsingContext.save()
completion(true)
do {
try parsingContext.save()
} catch let err {
completion(.failure(err))
}

completion(.success(()))
}
}
}
Expand All @@ -189,7 +218,8 @@ private extension BlockEditorSettingsService {
// Block Editor Settings nullify on delete
self.context.delete(blockEditorSettings)
}
completion(true, nil)
let result = SettingsServiceResult(hasChanges: true, blockEditorSettings: nil)
completion(.success(result))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1206,9 +1206,16 @@ extension GutenbergViewController {
}

private func fetchBlockSettings() {
editorSettingsService?.fetchSettings({ [weak self] (hasChanges, settings) in
guard hasChanges, let `self` = self else { return }
self.gutenberg.updateEditorSettings(settings)
editorSettingsService?.fetchSettings({ [weak self] result in
guard let `self` = self else { return }
switch result {
case .success(let response):
if response.hasChanges {
self.gutenberg.updateEditorSettings(response.blockEditorSettings)
}
case .failure(let err):
DDLogError("Error fetching settings: \(err)")
}
})
}
}
Loading