Skip to content

Commit

Permalink
Merge pull request #1870 from anyproto/ios-3251-open-page-object-with…
Browse files Browse the repository at this point in the history
…-version-add-mode

iOS-3251 Version history | Refactor preview flag + add version mode
  • Loading branch information
joe-pusya authored Aug 5, 2024
2 parents 9b203ff + 6869e76 commit 9f71764
Show file tree
Hide file tree
Showing 37 changed files with 182 additions and 123 deletions.
4 changes: 4 additions & 0 deletions Anytype.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@
2E8825772C5A5B7E00FD59D3 /* VersionHistoryCoordinatorViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E8825762C5A5B7E00FD59D3 /* VersionHistoryCoordinatorViewModel.swift */; };
2E88257A2C5A61A200FD59D3 /* ObjectVersionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E8825792C5A61A200FD59D3 /* ObjectVersionView.swift */; };
2E88257C2C5A61AC00FD59D3 /* ObjectVersionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E88257B2C5A61AC00FD59D3 /* ObjectVersionViewModel.swift */; };
2E88257E2C5BDF0D00FD59D3 /* DocumentMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E88257D2C5BDF0D00FD59D3 /* DocumentMode.swift */; };
2E92219D294B8B5000968750 /* DataViewBlockViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E92219C294B8B5000968750 /* DataViewBlockViewModel.swift */; };
2E92219F294B8CDE00968750 /* DataViewBlockConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E92219E294B8CDE00968750 /* DataViewBlockConfiguration.swift */; };
2E9221A1294B8D4900968750 /* DataViewBlockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E9221A0294B8D4900968750 /* DataViewBlockView.swift */; };
Expand Down Expand Up @@ -2916,6 +2917,7 @@
2E8825762C5A5B7E00FD59D3 /* VersionHistoryCoordinatorViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionHistoryCoordinatorViewModel.swift; sourceTree = "<group>"; };
2E8825792C5A61A200FD59D3 /* ObjectVersionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectVersionView.swift; sourceTree = "<group>"; };
2E88257B2C5A61AC00FD59D3 /* ObjectVersionViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectVersionViewModel.swift; sourceTree = "<group>"; };
2E88257D2C5BDF0D00FD59D3 /* DocumentMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentMode.swift; sourceTree = "<group>"; };
2E92219C294B8B5000968750 /* DataViewBlockViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataViewBlockViewModel.swift; sourceTree = "<group>"; };
2E92219E294B8CDE00968750 /* DataViewBlockConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataViewBlockConfiguration.swift; sourceTree = "<group>"; };
2E9221A0294B8D4900968750 /* DataViewBlockView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataViewBlockView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4198,6 +4200,7 @@
2AEAEE532C077C9D0060FE4C /* BaseDocumentProtocol+Sync.swift */,
2AEAEE572C08AB7E0060FE4C /* BaseDocumentUpdate.swift */,
2A12F0A12C11B0DD00E866BA /* Relation+DependedObject.swift */,
2E88257D2C5BDF0D00FD59D3 /* DocumentMode.swift */,
);
path = Document;
sourceTree = "<group>";
Expand Down Expand Up @@ -11844,6 +11847,7 @@
2E0D0F712938CFE200674D3F /* DataviewGroupOrder+Update.swift in Sources */,
3D07ECE9261B735300C007E2 /* UINavigationController+UIGestureRecognizerDelegate.swift in Sources */,
535E02AD274FF0B100E1AF8C /* RelationValueView.swift in Sources */,
2E88257E2C5BDF0D00FD59D3 /* DocumentMode.swift in Sources */,
3D547B852B988BDE000B030A /* MembershipTierSuccessView.swift in Sources */,
2A0F6C402BF219BA0041176E /* SpacePermissions.swift in Sources */,
2EA9D72B2B3093FE001CF0DE /* EventsBunch.swift in Sources */,
Expand Down
58 changes: 38 additions & 20 deletions Anytype/Sources/Models/Documents/Document/BaseDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ final class BaseDocument: BaseDocumentProtocol {

// MARK: - Local state
let objectId: String
let forPreview: Bool
let mode: DocumentMode

@Atomic
private(set) var children = [BlockInformation]()
@Atomic
Expand All @@ -36,6 +37,8 @@ final class BaseDocument: BaseDocumentProtocol {
private var relationBuilder: any RelationsBuilderProtocol
@Injected(\.syncStatusStorage)
private var syncStatusStorage: any SyncStatusStorageProtocol
@Injected(\.historyVersionsService)
private var historyVersionsService: any HistoryVersionsServiceProtocol
private let relationDetailsStorage: any RelationDetailsStorageProtocol
private let objectTypeProvider: any ObjectTypeProviderProtocol
private let accountParticipantsStorage: any AccountParticipantsStorageProtocol
Expand All @@ -59,7 +62,7 @@ final class BaseDocument: BaseDocumentProtocol {

init(
objectId: String,
forPreview: Bool,
mode: DocumentMode,
objectLifecycleService: some ObjectLifecycleServiceProtocol,
relationDetailsStorage: some RelationDetailsStorageProtocol,
objectTypeProvider: some ObjectTypeProviderProtocol,
Expand All @@ -72,7 +75,7 @@ final class BaseDocument: BaseDocumentProtocol {
detailsStorage: ObjectDetailsStorage
) {
self.objectId = objectId
self.forPreview = forPreview
self.mode = mode
self.eventsListener = eventsListener
self.viewModelSetter = viewModelSetter
self.objectLifecycleService = objectLifecycleService
Expand All @@ -88,7 +91,7 @@ final class BaseDocument: BaseDocumentProtocol {
}

deinit {
guard !forPreview, isOpened, UserDefaultsConfig.usersId.isNotEmpty else { return }
guard mode.isHandling, isOpened, UserDefaultsConfig.usersId.isNotEmpty else { return }
Task.detached(priority: .userInitiated) { [objectLifecycleService, objectId] in
try await objectLifecycleService.close(contextId: objectId)
}
Expand All @@ -102,30 +105,33 @@ final class BaseDocument: BaseDocumentProtocol {

@MainActor
func open() async throws {
if isOpened {
return
}
guard !forPreview else {
anytypeAssertionFailure("Document created for preview. You should use openForPreview() method.")
return
switch mode {
case .handling:
guard !isOpened else { return }
let model = try await objectLifecycleService.open(contextId: objectId)
setupView(model)
case .preview:
try await updateDocumentPreview()
case .version(let versionId):
try await updateDocumentVersion(versionId)
}
let model = try await objectLifecycleService.open(contextId: objectId)
setupView(model)
}

@MainActor
func openForPreview() async throws {
guard forPreview else {
anytypeAssertionFailure("Document created for handling. You should use open() method.")
return
func update() async throws {
switch mode {
case .handling:
anytypeAssertionFailure("Document was created in `handling` mode. You can't update it")
case .preview:
try await updateDocumentPreview()
case .version(let versionId):
try await updateDocumentVersion(versionId)
}
let model = try await objectLifecycleService.openForPreview(contextId: objectId)
setupView(model)
}

@MainActor
func close() async throws {
guard !forPreview, isOpened, UserDefaultsConfig.usersId.isNotEmpty else { return }
guard mode.isHandling, isOpened, UserDefaultsConfig.usersId.isNotEmpty else { return }
try await objectLifecycleService.close(contextId: objectId)
isOpened = false
}
Expand Down Expand Up @@ -156,13 +162,25 @@ final class BaseDocument: BaseDocumentProtocol {

// MARK: - Private methods

@MainActor
private func updateDocumentPreview() async throws {
let model = try await objectLifecycleService.openForPreview(contextId: objectId)
setupView(model)
}

@MainActor
private func updateDocumentVersion(_ versionId: String) async throws {
let model = try await historyVersionsService.showVersion(objectId: objectId, versionId: versionId)
setupView(model)
}

private func setup() {
eventsListener.onUpdatesReceive = { [weak self] updates in
DispatchQueue.main.async { [weak self] in
self?.triggerSync(updates: updates)
}
}
if !forPreview {
if mode.isHandling {
eventsListener.startListening()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol BaseDocumentProtocol: AnyObject {
var isLocked: Bool { get }
var isEmpty: Bool { get }
var isOpened: Bool { get }
var forPreview: Bool { get }
var mode: DocumentMode { get }
var details: ObjectDetails? { get }
var permissions: ObjectPermissions { get }
var syncStatus: SyncStatus? { get }
Expand All @@ -22,8 +22,10 @@ protocol BaseDocumentProtocol: AnyObject {

@MainActor
func open() async throws

@MainActor
func openForPreview() async throws
func update() async throws

@MainActor
func close() async throws
}
9 changes: 9 additions & 0 deletions Anytype/Sources/Models/Documents/Document/DocumentMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum DocumentMode: Hashable, Codable {
case handling
case preview
case version(String)

var isHandling: Bool {
self == .handling
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct LinkToObjectSearchData: SearchDataProtocol {
self.description = ""
self.callout = ""
self.typeId = ""
self.editorScreenData = .page(EditorPageObject(objectId: "", spaceId: "", isOpenedForPreview: false))
self.editorScreenData = .page(EditorPageObject(objectId: "", spaceId: ""))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ final class HomeCoordinatorViewModel: ObservableObject,
case .invite(let cid, let key):
spaceJoinData = SpaceJoinModuleData(cid: cid, key: key)
case .object(let objectId, _):
let document = documentsProvider.document(objectId: objectId, forPreview: true)
try await document.openForPreview()
let document = documentsProvider.document(objectId: objectId, mode: .preview)
try await document.open()
guard let editorData = document.details?.editorScreenData() else { return }
try await push(data: editorData)
case .spaceShareTip:
Expand All @@ -325,8 +325,8 @@ final class HomeCoordinatorViewModel: ObservableObject,
editorPath.push(data)
return
}
let document = documentsProvider.document(objectId: objectId, forPreview: true)
try await document.openForPreview()
let document = documentsProvider.document(objectId: objectId, mode: .preview)
try await document.open()
guard let details = document.details else {
return
}
Expand Down Expand Up @@ -375,8 +375,8 @@ final class HomeCoordinatorViewModel: ObservableObject,
// Restore last open page
if currentSpaceId.isNil, let lastOpenPage = UserDefaultsConfig.lastOpenedPage {
if let objectId = lastOpenPage.objectId {
let document = documentsProvider.document(objectId: objectId, forPreview: true)
try await document.openForPreview()
let document = documentsProvider.document(objectId: objectId, mode: .preview)
try await document.open()
// Check space is deleted or switched
if document.spaceId == newInfo.accountSpaceId {
path.push(lastOpenPage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class CodeLanguageListViewModel: ObservableObject {

private let data: CodeLanguageListData
private lazy var document: any BaseDocumentProtocol = {
documentsProvider.document(objectId: data.documentId, forPreview: false)
documentsProvider.document(objectId: data.documentId)
}()
private var selectedLanguage: CodeLanguage?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ final class ObjectWidgetInternalViewModel: ObservableObject, WidgetInternalViewM
func onCreateObjectTap() {
guard let linkedObjectDetails else { return }
Task {
let document = documentsProvider.document(objectId: linkedObjectDetails.id, forPreview: true)
try await document.openForPreview()
let document = documentsProvider.document(objectId: linkedObjectDetails.id, mode: .preview)
try await document.open()
guard let lastBlockId = document.children.last?.id else { return }

let details = try await defaultObjectService.createDefaultObject(name: "", shouldDeleteEmptyObject: true, spaceId: widgetObject.spaceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ final class SetObjectWidgetInternalViewModel: ObservableObject {

private func updateSetDocument(objectId: String) async {
guard objectId != setDocument?.objectId else {
try? await setDocument?.openForPreview()
try? await setDocument?.update()
await updateModelState()
return
}

setDocument = documentsProvider.setDocument(objectId: objectId, forPreview: true)
try? await setDocument?.openForPreview()
setDocument = documentsProvider.setDocument(objectId: objectId, mode: .preview)
try? await setDocument?.open()

rowDetails = nil
dataviewState = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ final class SetObjectWidgetLegacyInternalViewModel: ObservableObject, WidgetData

private func updateSetDocument(objectId: String) async {
guard objectId != setDocument?.objectId else {
try? await setDocument?.openForPreview()
try? await setDocument?.update()
updateModelState()
return
}

setDocument = documentsProvider.setDocument(objectId: objectId, forPreview: true)
try? await setDocument?.openForPreview()
setDocument = documentsProvider.setDocument(objectId: objectId, mode: .preview)
try? await setDocument?.open()
updateModelState()

details = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ final class SetObjectCreationSettingsCoordinator:
data: EditorPageObject(
objectId: setting.templateId,
spaceId: setting.spaceId,
isOpenedForPreview: false,
usecase: .templateEditing
),
showHeader: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ extension TemplatesCoordinator: TemplatePickerViewModuleOutput {
data: EditorPageObject(
objectId: template.id,
spaceId: template.spaceId,
isOpenedForPreview: false,
usecase: .templateEditing
),
showHeader: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct SetObjectCreationSettingsView: View {

#Preview {
SetObjectCreationSettingsView(
setDocument: Container.shared.documentsProvider().setDocument(objectId: "", forPreview: false),
setDocument: Container.shared.documentsProvider().setDocument(objectId: ""),
viewId: "viewId",
output: nil
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ final class SetObjectCreationSettingsViewModel: ObservableObject {
}

private func retrieveObjectDetails(objectId: String) async -> ObjectDetails? {
let targetDocument = documentsProvider.document(objectId: objectId, forPreview: true)
try? await targetDocument.openForPreview()
let targetDocument = documentsProvider.document(objectId: objectId, mode: .preview)
try? await targetDocument.open()

return targetDocument.details
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class SettingsCoordinatorViewModel: ObservableObject,
}

func onChangeIconSelected(objectId: String) {
let document = documentService.document(objectId: objectId, forPreview: true)
let document = documentService.document(objectId: objectId, mode: .preview)
objectIconPickerData = ObjectIconPickerData(document: document)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class SlashMenuActionHandler {
if objectType.isListType {
return .set(EditorSetObject(objectId: objectId, spaceId: objectType.spaceId))
} else {
return .page(EditorPageObject(objectId: objectId, spaceId: objectType.spaceId, isOpenedForPreview: false))
return .page(EditorPageObject(objectId: objectId, spaceId: objectType.spaceId))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import Services
// MARK: - Init helpers

extension EditorScreenData {
init(details: ObjectDetails, isOpenedForPreview: Bool = false, blockId: String? = nil, activeViewId: String? = nil) {
init(details: ObjectDetails, mode: DocumentMode = .handling, blockId: String? = nil, activeViewId: String? = nil) {
switch details.editorViewType {
case .page:
self = .page(EditorPageObject(
details: details,
isOpenedForPreview: isOpenedForPreview,
mode: mode,
blockId: blockId
))
case .set:
self = .set(EditorSetObject(
details: details,
activeViewId: activeViewId
activeViewId: activeViewId,
mode: mode
))
}
}
Expand All @@ -24,24 +25,25 @@ extension EditorScreenData {
extension EditorPageObject {
init(
details: ObjectDetails,
isOpenedForPreview: Bool = false,
mode: DocumentMode = .handling,
blockId: String? = nil,
usecase: ObjectHeaderEmptyData.ObjectHeaderEmptyUsecase = .editor
) {
self.objectId = details.id
self.spaceId = details.spaceId
self.isOpenedForPreview = isOpenedForPreview
self.mode = mode
self.blockId = blockId
self.usecase = usecase
}
}

extension EditorSetObject {
init(details: ObjectDetails, activeViewId: String? = nil) {
init(details: ObjectDetails, activeViewId: String? = nil, mode: DocumentMode = .handling) {
self.objectId = details.id
self.spaceId = details.spaceId
self.activeViewId = activeViewId
self.inline = nil
self.mode = mode
}
}

Expand Down
Loading

0 comments on commit 9f71764

Please sign in to comment.