Skip to content

Commit

Permalink
Merge pull request #1062 from anyproto/ios-2557-migrate-code-language
Browse files Browse the repository at this point in the history
IOS-2557 DI | Migrate code language screen
  • Loading branch information
mgolovko authored Mar 27, 2024
2 parents d7b1975 + fe19015 commit 18012c4
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 80 deletions.
8 changes: 4 additions & 4 deletions Anytype.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
2A2C33212892BA36008BC326 /* RelationValueViewModelOutput.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2C33202892BA36008BC326 /* RelationValueViewModelOutput.swift */; };
2A30734928ACEF58003673DF /* AlertOpener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A30734828ACEF58003673DF /* AlertOpener.swift */; };
2A30735128AD04D7003673DF /* TopAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A30735028AD04D7003673DF /* TopAlertView.swift */; };
2A30B2022BB40E3400206806 /* BlockFields+Fields.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A30B2012BB40E3400206806 /* BlockFields+Fields.swift */; };
2A3425AC2A6A8EB3005E61F1 /* SmallImageIdPainter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A3425AB2A6A8EB3005E61F1 /* SmallImageIdPainter.swift */; };
2A3425B32A6AD415005E61F1 /* AnytypeText+Mention.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A3425B22A6AD415005E61F1 /* AnytypeText+Mention.swift */; };
2A35FF8E2AAF202E00D6006D /* CodeLanguageListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A35FF8D2AAF202E00D6006D /* CodeLanguageListView.swift */; };
Expand Down Expand Up @@ -670,7 +671,6 @@
2AB7AAEB2AF51D0C00ED3E02 /* View+PresentationBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB7AAEA2AF51D0C00ED3E02 /* View+PresentationBackground.swift */; };
2AB7AAED2AF537AB00ED3E02 /* RemoteStorageSegmentInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB7AAEC2AF537AB00ED3E02 /* RemoteStorageSegmentInfo.swift */; };
2ABA0E07283BC420008369DC /* BlockObjectsSearchInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ABA0E06283BC420008369DC /* BlockObjectsSearchInteractor.swift */; };
2ABAF2BB2938E5FA003C4730 /* CodeLanguageModuleAssembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ABAF2BA2938E5FA003C4730 /* CodeLanguageModuleAssembly.swift */; };
2ABAF2C1293DED8C003C4730 /* CreateObjectModuleAssembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ABAF2C0293DED8C003C4730 /* CreateObjectModuleAssembly.swift */; };
2ABF214329B204BE00E872D9 /* UIUserInterfaceStyle+Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ABF214229B204BE00E872D9 /* UIUserInterfaceStyle+Settings.swift */; };
2ABF214629B204F800E872D9 /* DashboardLogoutAlertModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ABF214529B204F800E872D9 /* DashboardLogoutAlertModel.swift */; };
Expand Down Expand Up @@ -2182,6 +2182,7 @@
2A2C33202892BA36008BC326 /* RelationValueViewModelOutput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelationValueViewModelOutput.swift; sourceTree = "<group>"; };
2A30734828ACEF58003673DF /* AlertOpener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertOpener.swift; sourceTree = "<group>"; };
2A30735028AD04D7003673DF /* TopAlertView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopAlertView.swift; sourceTree = "<group>"; };
2A30B2012BB40E3400206806 /* BlockFields+Fields.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BlockFields+Fields.swift"; sourceTree = "<group>"; };
2A3425AB2A6A8EB3005E61F1 /* SmallImageIdPainter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmallImageIdPainter.swift; sourceTree = "<group>"; };
2A3425B22A6AD415005E61F1 /* AnytypeText+Mention.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AnytypeText+Mention.swift"; sourceTree = "<group>"; };
2A35FF8D2AAF202E00D6006D /* CodeLanguageListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeLanguageListView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2464,7 +2465,6 @@
2AB7AAEA2AF51D0C00ED3E02 /* View+PresentationBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+PresentationBackground.swift"; sourceTree = "<group>"; };
2AB7AAEC2AF537AB00ED3E02 /* RemoteStorageSegmentInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteStorageSegmentInfo.swift; sourceTree = "<group>"; };
2ABA0E06283BC420008369DC /* BlockObjectsSearchInteractor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockObjectsSearchInteractor.swift; sourceTree = "<group>"; };
2ABAF2BA2938E5FA003C4730 /* CodeLanguageModuleAssembly.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeLanguageModuleAssembly.swift; sourceTree = "<group>"; };
2ABAF2C0293DED8C003C4730 /* CreateObjectModuleAssembly.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateObjectModuleAssembly.swift; sourceTree = "<group>"; };
2ABF214229B204BE00E872D9 /* UIUserInterfaceStyle+Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIUserInterfaceStyle+Settings.swift"; sourceTree = "<group>"; };
2ABF214529B204F800E872D9 /* DashboardLogoutAlertModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashboardLogoutAlertModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3851,6 +3851,7 @@
038FB5C922D8F32F002B90B8 /* Models */ = {
isa = PBXGroup;
children = (
2A30B2012BB40E3400206806 /* BlockFields+Fields.swift */,
2AC5B3D02BA30070002584B8 /* ObjectPermissions */,
53D4089527ECCE9000495335 /* FieldConverters */,
5347854A2743C50C0097CA30 /* Relations */,
Expand Down Expand Up @@ -6497,7 +6498,6 @@
2ABAF2B72938E548003C4730 /* CodeLanguage */ = {
isa = PBXGroup;
children = (
2ABAF2BA2938E5FA003C4730 /* CodeLanguageModuleAssembly.swift */,
2A35FF8D2AAF202E00D6006D /* CodeLanguageListView.swift */,
2A35FF8F2AAF203C00D6006D /* CodeLanguageListViewModel.swift */,
2A35FF912AAF20FD00D6006D /* CodeLanguageRowModel.swift */,
Expand Down Expand Up @@ -10713,7 +10713,6 @@
2A2B332B2A7BAEBA004BD906 /* SpaceSwitchViewModel.swift in Sources */,
EAADB1B32678EB52004B0AF2 /* MarkStyleModifier.swift in Sources */,
86C4B3782B246141003B8C51 /* SharingTipCoordinator.swift in Sources */,
2ABAF2BB2938E5FA003C4730 /* CodeLanguageModuleAssembly.swift in Sources */,
2AA8108029A3D8E8007DD64B /* WidgetObjectListRowModel.swift in Sources */,
3D2F00292B6814CC009F3AA5 /* ObjectTypeSearchView.swift in Sources */,
2A4373722906808700A1C4D6 /* InlineMarkdownListener.swift in Sources */,
Expand Down Expand Up @@ -11164,6 +11163,7 @@
2AD6792C29812EE600339624 /* HomeBottomPanelModuleOutput.swift in Sources */,
2E7E50422B6D5C3A003867A3 /* ObjectRelationListModuleOutput.swift in Sources */,
3D1CEC4527F5F566000D36BA /* Clock.swift in Sources */,
2A30B2022BB40E3400206806 /* BlockFields+Fields.swift in Sources */,
2AF205BC2ABD8E660027F3EA /* HomeBottomNavigationPanelView.swift in Sources */,
53FF7C4826F7562B00A5A592 /* AnytypeAudioPlayer.swift in Sources */,
2E22F9EC2B6167B7001EFCF3 /* SelectRelationListCoordinatorViewModel.swift in Sources */,
Expand Down
11 changes: 11 additions & 0 deletions Anytype/Sources/Models/BlockFields+Fields.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation
import Services

extension BlockFields {

var codeLanguage: CodeLanguage {
CodeLanguage.create(
middleware: self[CodeBlockFields.FieldName.codeLanguage]?.stringValue
)
}
}
4 changes: 0 additions & 4 deletions Anytype/Sources/PresentationLayer/Assemblies/ModulesDI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ final class ModulesDI: ModulesDIProtocol {
func createObject() -> CreateObjectModuleAssemblyProtocol {
return CreateObjectModuleAssembly(serviceLocator: serviceLocator)
}

func codeLanguageList() -> CodeLanguageListModuleAssemblyProtocol {
return CodeLanguageListModuleAssembly(serviceLocator: serviceLocator)
}

func newSearch() -> NewSearchModuleAssemblyProtocol {
return NewSearchModuleAssembly(uiHelpersDI: uiHelpersDI, serviceLocator: serviceLocator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ protocol ModulesDIProtocol: AnyObject {
func objectSetting() -> ObjectSettingModuleAssemblyProtocol
func search() -> SearchModuleAssemblyProtocol
func createObject() -> CreateObjectModuleAssemblyProtocol
func codeLanguageList() -> CodeLanguageListModuleAssemblyProtocol
func newSearch() -> NewSearchModuleAssemblyProtocol
func newRelation() -> NewRelationModuleAssemblyProtocol
func homeWidgets() -> HomeWidgetsModuleAssemblyProtocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct EditorPageCoordinatorView: View {
.sheet(item: $model.relationValueData) { data in
model.relationValueCoordinator(data: data)
}
.sheet(item: $model.codeLanguageData) {
CodeLanguageListView(data: $0)
}
.snackbar(toastBarData: $model.toastBarData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class EditorPageCoordinatorViewModel: ObservableObject, EditorPageModuleOu
@Published var dismiss = false
@Published var relationValueData: RelationValueData?
@Published var toastBarData: ToastBarData = .empty
@Published var codeLanguageData: CodeLanguageListData?

init(
data: EditorPageObject,
Expand Down Expand Up @@ -76,6 +77,10 @@ final class EditorPageCoordinatorViewModel: ObservableObject, EditorPageModuleOu
)
}

func onSelectCodeLanguage(objectId: String, blockId: String) {
codeLanguageData = CodeLanguageListData(documentId: objectId, blockId: blockId)
}

private func handleRelationValue(relation: Relation, objectDetails: ObjectDetails) {
let analyticsType = AnalyticsEventsRelationType.block
if relationValueProcessingService.canOpenRelationInNewModule(relation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import SwiftUI

struct CodeLanguageListView: View {

@ObservedObject var model: CodeLanguageListViewModel

@StateObject private var model: CodeLanguageListViewModel
@State private var searchText: String = ""
@Environment(\.dismiss) private var dismiss

init(data: CodeLanguageListData) {
self._model = StateObject(wrappedValue: CodeLanguageListViewModel(data: data))
}

var body: some View {
VStack(spacing: 0) {
DragIndicator()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import Foundation
import Services

struct CodeLanguageListData: Identifiable, Hashable {
let documentId: String
let blockId: String

var id: Int { hashValue }
}

final class CodeLanguageListViewModel: ObservableObject {

// MARK: - DI
@Injected(\.blockService)
private var blockService: BlockServiceProtocol
@Injected(\.documentsProvider)
private var documentsProvider: DocumentsProviderProtocol

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

// MARK: - State

@Published var rows: [CodeLanguageRowModel] = []
@Published var dismiss: Bool = false

init(document: BaseDocumentProtocol, blockId: String, selectedLanguage: CodeLanguage, blockService: BlockServiceProtocol) {
self.document = document
self.blockId = blockId
self.selectedLanguage = selectedLanguage
self.blockService = blockService
init(data: CodeLanguageListData) {
self.data = data
let info = document.infoContainer.get(id: data.blockId)
selectedLanguage = info?.fields.codeLanguage
updateRows(searchText: "")
}

Expand All @@ -30,7 +41,7 @@ final class CodeLanguageListViewModel: ObservableObject {
// MARK: - Private

private func updateRows(searchText: String) {

let languages = searchText.isEmpty
? CodeLanguage.allCases
: CodeLanguage.allCases.filter { $0.title.lowercased().contains(searchText.lowercased()) }
Expand All @@ -48,10 +59,10 @@ final class CodeLanguageListViewModel: ObservableObject {

private func onTapCodeLanguage(_ language: CodeLanguage) {
Task { @MainActor in
guard let info = document.infoContainer.get(id: blockId) else { return }
guard let info = document.infoContainer.get(id: data.blockId) else { return }
let fields = CodeBlockFields(language: language)
let newInfo = info.addFields(fields.asMiddleware())
try await blockService.setFields(objectId: document.objectId, blockId: blockId, fields: newInfo.fields)
try await blockService.setFields(objectId: document.objectId, blockId: data.blockId, fields: newInfo.fields)
dismiss.toggle()
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ final class EditorPageModuleAssembly: EditorPageModuleAssemblyProtocol {
objectSettingCoordinator: coordinatorsDI.objectSettings().make(),
searchModuleAssembly: modulesDI.search(),
toastPresenter: uiHelpersDI.toastPresenter(using: nil),
codeLanguageListModuleAssembly: modulesDI.codeLanguageList(),
newSearchModuleAssembly: modulesDI.newSearch(),
objectTypeSearchModuleAssembly: modulesDI.objectTypeSearch(),
textIconPickerModuleAssembly: modulesDI.textIconPicker(),
Expand Down Expand Up @@ -256,7 +255,8 @@ final class EditorPageModuleAssembly: EditorPageModuleAssemblyProtocol {
linkToObjectCoordinator: coordinatorsDI.linkToObject().make(output: router),
markupChanger: BlockMarkupChanger(),
slashMenuActionHandler: slashMenuActionHandler,
editorPageBlocksStateManager: blocksStateManager
editorPageBlocksStateManager: blocksStateManager,
output: output
)

blocksStateManager.blocksSelectionOverlayViewModel = blocksSelectionOverlayViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ protocol EditorPageModuleOutput: AnyObject {
func showEditorScreen(data: EditorScreenData)
func replaceEditorScreen(data: EditorScreenData)
func closeEditor()
func onSelectCodeLanguage(objectId: String, blockId: String)
func showRelationValueEditingView(document: BaseDocumentProtocol, relation: Relation)
// TODO: Refactoring templates. Delete it
func setModuleInput(input: EditorPageModuleInput, objectId: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct CodeBlockContentConfiguration: BlockConfiguration {
struct Actions {
let becomeFirstResponder: () -> ()
let textDidChange: (UITextView) -> ()
let showCodeSelection: () -> ()
let showCodeSelection: @MainActor () -> ()
}

let content: BlockText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ struct CodeBlockViewModel: BlockViewModelProtocol {
let becomeFirstResponder: (BlockInformation) -> ()
let handler: BlockActionHandlerProtocol
let editorCollectionController: EditorBlockCollectionController
let showCodeSelection: (BlockInformation, CodeLanguage) -> ()
let showCodeSelection: @MainActor (BlockInformation) -> ()

func makeContentConfiguration(maxWidth width: CGFloat) -> UIContentConfiguration {
guard case let .text(content) = info.content else {
return UnsupportedBlockViewModel(info: info).makeContentConfiguration(maxWidth: width)
}

let anytypeText = content.anytypeText(document: document)
let codeLanguage = CodeLanguage.create(
middleware: info.fields[CodeBlockFields.FieldName.codeLanguage]?.stringValue
)
let codeLanguage = info.fields.codeLanguage

return CodeBlockContentConfiguration(
content: content,
Expand All @@ -35,7 +33,7 @@ struct CodeBlockViewModel: BlockViewModelProtocol {
handler.changeText(textView.attributedText, blockId: info.id)
editorCollectionController.reconfigure(items: [.block(self)])
},
showCodeSelection: { showCodeSelection(info, codeLanguage) }
showCodeSelection: { showCodeSelection(info) }
)
).cellBlockConfiguration(
dragConfiguration: .init(id: info.id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ final class EditorPageViewModel: EditorPageViewModelProtocol, EditorBottomNaviga
for blockId in blockIds {

if blockViewModel.blockId == blockId {
guard let newViewModel = blockBuilder.build(id: blockId) else {
guard let newViewModel = blockBuilder.build(blockId: blockId) else {
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class BlockViewModelBuilder {
private let linkToObjectCoordinator: LinkToObjectCoordinatorProtocol
private let markupChanger: BlockMarkupChangerProtocol
private let slashMenuActionHandler: SlashMenuActionHandler
private weak var output: EditorPageModuleOutput?

init(
document: BaseDocumentProtocol,
Expand All @@ -47,7 +48,8 @@ final class BlockViewModelBuilder {
linkToObjectCoordinator: LinkToObjectCoordinatorProtocol,
markupChanger: BlockMarkupChangerProtocol,
slashMenuActionHandler: SlashMenuActionHandler,
editorPageBlocksStateManager: EditorPageBlocksStateManager
editorPageBlocksStateManager: EditorPageBlocksStateManager,
output: EditorPageModuleOutput?
) {
self.document = document
self.handler = handler
Expand All @@ -70,6 +72,7 @@ final class BlockViewModelBuilder {
self.markupChanger = markupChanger
self.slashMenuActionHandler = slashMenuActionHandler
self.editorPageBlocksStateManager = editorPageBlocksStateManager
self.output = output
}

func buildEditorItems(infos: [String]) -> [EditorItem] {
Expand All @@ -96,20 +99,21 @@ final class BlockViewModelBuilder {

private func build(_ ids: [String]) -> [BlockViewModelProtocol] {
ids.compactMap {
let block = build(id: $0)
let block = build(blockId: $0)
return block
}
}

func build(id: String) -> BlockViewModelProtocol? {
if let model = modelsHolder.blocksMapping[id] {
func build(blockId: String) -> BlockViewModelProtocol? {
if let model = modelsHolder.blocksMapping[blockId] {
return model
}

guard let info = infoContainer.get(id: id) else {
guard let info = infoContainer.get(id: blockId) else {
return nil
}

let documentId = document.objectId
let blockInformationProvider = BlockModelInfomationProvider(infoContainer: infoContainer, info: info)

switch info.content {
Expand All @@ -122,8 +126,8 @@ final class BlockViewModelBuilder {
becomeFirstResponder: { _ in },
handler: handler,
editorCollectionController: blockCollectionController,
showCodeSelection: { [weak self] info, codeLanguage in
self?.router.showCodeLanguage(blockId: info.id, selectedLanguage: codeLanguage)
showCodeSelection: { [weak self] info in
self?.output?.onSelectCodeLanguage(objectId: documentId, blockId: blockId)
}
)
default:
Expand Down
Loading

0 comments on commit 18012c4

Please sign in to comment.