Skip to content

Commit

Permalink
IOS-719 MiddlewareEventConverter | Move event handler methods
Browse files Browse the repository at this point in the history
IOS-719 MiddlewareEventConverter | Move event handler methods
  • Loading branch information
mgolovko committed Sep 13, 2023
2 parents d2f176d + bfd3eb8 commit 6ffbcb5
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,17 @@ final class MiddlewareEventConverter {
switch event {
case let .threadStatus(status):
return SyncStatus(status.summary.status).flatMap { .syncStatus($0) }
case let .blockSetFields(fields):
infoContainer.update(blockId: fields.id) { info in
return info.updated(fields: fields.fields.fields)
}
return .blocks(blockIds: [fields.id])
case let .blockAdd(value):
value.blocks
.compactMap(BlockInformationConverter.convert(block:))
.forEach { block in
infoContainer.add(block)
}
case let .blockSetFields(data):
infoContainer.setFields(data: data)
return .blocks(blockIds: [data.id])
case let .blockAdd(data):
infoContainer.add(data: data)
// Because blockAdd message will always come together with blockSetChildrenIds
// and it is easier to create update from those message
return nil

case let .blockDelete(value):
value.blockIds.forEach { blockId in
infoContainer.remove(id: blockId)
}
case let .blockDelete(data):
infoContainer.delete(data: data)
// Because blockDelete message will always come together with blockSetChildrenIds
// and it is easier to create update from those message
return nil
Expand All @@ -60,29 +52,22 @@ final class MiddlewareEventConverter {
return .general
case let .blockSetText(newData):
return blockSetTextUpdate(newData)
case let .blockSetBackgroundColor(updateData):
infoContainer.update(blockId: updateData.id, update: { info in
return info.updated(
backgroundColor: MiddlewareColor(rawValue: updateData.backgroundColor) ?? .default
)
})

var childIds = infoContainer.recursiveChildren(of: updateData.id).map { $0.id }
childIds.append(updateData.id)
case let .blockSetBackgroundColor(data):
infoContainer.setBackgroundColor(data: data)

var childIds = infoContainer.recursiveChildren(of: data.id).map { $0.id }
childIds.append(data.id)
return .blocks(blockIds: Set(childIds))

case let .blockSetAlign(value):
infoContainer.setAlign(data: value)

let blockId = value.id
let alignment = value.align
guard let modelAlignment = alignment.asBlockModel else {
anytypeAssertionFailure("We cannot parse alignment", info: ["value": "\(value)"])
return .general
}

infoContainer.update(blockId: blockId) { info in
info.updated(horizontalAlignment: modelAlignment)
}
return .blocks(blockIds: [blockId])

case let .objectDetailsSet(data):
Expand Down Expand Up @@ -126,16 +111,12 @@ final class MiddlewareEventConverter {
guard let details = detailsStorage.unset(data: data) else { return nil }
return .details(id: details.id)

case .objectRelationsAmend(let amend):
relationLinksStorage.amend(
relationLinks: amend.relationLinks.map { RelationLink(middlewareRelationLink: $0) }
)

case .objectRelationsAmend(let data):
relationLinksStorage.ammend(data: data)
return .general

case .objectRelationsRemove(let remove):
relationLinksStorage.remove(relationKeys: remove.relationKeys)

case .objectRelationsRemove(let data):
relationLinksStorage.remove(data: data)
return .general

case let .blockSetFile(data):
Expand Down Expand Up @@ -280,7 +261,7 @@ final class MiddlewareEventConverter {
infoContainer.updateRelation(blockId: data.id) { $0.handleSetRelation(data: data) }
return .general // Relace to `.blocks(blockIds: [data.id])` after implment task https://linear.app/anytype/issue/IOS-914
case .objectRestrictionsSet(let restrictions):
restrictionsContainer.restrinctions = MiddlewareObjectRestrictionsConverter.convertObjectRestrictions(middlewareRestrictions: restrictions.restrictions)
restrictionsContainer.set(data: restrictions)
return nil
case .blockSetWidget(let data):
infoContainer.updateWidget(blockId: data.id) { $0.handleSetWidget(data: data) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation
import ProtobufMessages

public extension InfoContainerProtocol {

func add(data: Anytype_Event.Block.Add) {
data.blocks
.compactMap(BlockInformationConverter.convert(block:))
.forEach { block in
add(block)
}
}

func setFields(data: Anytype_Event.Block.Set.Fields) {
update(blockId: data.id) { info in
return info.updated(fields: data.fields.fields)
}
}

func delete(data: Anytype_Event.Block.Delete) {
data.blockIds.forEach { blockId in
remove(id: blockId)
}
}

func setBackgroundColor(data: Anytype_Event.Block.Set.BackgroundColor) {
update(blockId: data.id, update: { info in
return info.updated(
backgroundColor: MiddlewareColor(rawValue: data.backgroundColor) ?? .default
)
})
}

func setAlign(data: Anytype_Event.Block.Set.Align) {
update(blockId: data.id) { info in
info.updated(horizontalAlignment: data.align.asBlockModel)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Foundation
import AnytypeCore
import ProtobufMessages
import SwiftProtobuf

public extension ObjectDetailsStorage {

func set(data: Anytype_Event.Object.Details.Set) -> ObjectDetails? {
guard data.hasDetails else {
anytypeAssertionFailure("No details in Object.Details.Set")
return nil
}
let id = data.id
guard id.isValidId else {
anytypeAssertionFailure("Id is empty in details", info: ["id": id])
return nil
}

let currentDetails = get(id: id) ?? ObjectDetails(id: id)
let updatedDetails = currentDetails.updated(by: data.details.fields)

add(details: updatedDetails)

return updatedDetails
}

func unset(data: Anytype_Event.Object.Details.Unset) -> ObjectDetails? {
let id = data.id
guard id.isValidId else {
anytypeAssertionFailure("Id is empty in details", info: ["id": id])
return nil
}

guard let currentDetails = get(id: id) else {
return nil
}

let updatedDetails = currentDetails.removed(keys: data.keys)
add(details: updatedDetails)

return updatedDetails
}

func amend(data: Anytype_Event.Object.Details.Amend) -> ObjectDetails? {
let id = data.id
guard id.isValidId else { return nil }

return amend(details: ObjectDetails(id: data.id, values: data.details.asDetailsDictionary))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,12 @@ public final class ObjectDetailsStorage {
storage[details.id] = details
}

public func set(data: Anytype_Event.Object.Details.Set) -> ObjectDetails? {
guard data.hasDetails else {
anytypeAssertionFailure("No details in Object.Details.Set")
return nil
}
let id = data.id
guard id.isValidId else {
anytypeAssertionFailure("Id is empty in details", info: ["id": id])
return nil
}

let currentDetails = get(id: id) ?? ObjectDetails(id: id)
let updatedDetails = currentDetails.updated(by: data.details.fields)

add(details: updatedDetails)

return updatedDetails
}

public func unset(data: Anytype_Event.Object.Details.Unset) -> ObjectDetails? {
let id = data.id
guard id.isValidId else {
anytypeAssertionFailure("Id is empty in details", info: ["id": id])
return nil
}

guard let currentDetails = get(id: id) else {
return nil
}

let updatedDetails = currentDetails.removed(keys: data.keys)
add(details: updatedDetails)

return updatedDetails
}

public func amend(data: Anytype_Event.Object.Details.Amend) -> ObjectDetails? {
let id = data.id
guard id.isValidId else { return nil }

return amend(id: id, values: data.details.asDetailsDictionary)
}

@discardableResult
public func amend(details: ObjectDetails) -> ObjectDetails {
return amend(id: details.id, values: details.values)
}

public func amend(id: BlockId, values: [String: Google_Protobuf_Value]) -> ObjectDetails {
private func amend(id: BlockId, values: [String: Google_Protobuf_Value]) -> ObjectDetails {
let currentDetails = get(id: id) ?? ObjectDetails(id: id)
let updatedDetails = currentDetails.updated(by: values)
add(details: updatedDetails)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
import ProtobufMessages

public extension ObjectRestrictionsContainer {

func set(data: Anytype_Event.Object.Restrictions.Set) {
restrinctions = MiddlewareObjectRestrictionsConverter.convertObjectRestrictions(middlewareRestrictions: data.restrictions)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation
import ProtobufMessages

public extension RelationLinksStorageProtocol {

func ammend(data: Anytype_Event.Object.Relations.Amend) {
amend(relationLinks: data.relationLinks.map { RelationLink(middlewareRelationLink: $0) })
}

func remove(data: Anytype_Event.Object.Relations.Remove) {
remove(relationKeys: data.relationKeys)
}
}

0 comments on commit 6ffbcb5

Please sign in to comment.