From 5e1840cc746900767bd4482c0f69adf837f59f2f Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Sun, 9 Oct 2022 20:09:05 +0200 Subject: [PATCH] feat: propagate showingCustomKeybiard state --- PlantUML/PlantUMLEditorView.swift | 76 ++++++++++--------- .../PlantUMLTextField+Representable.swift | 14 ++-- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/PlantUML/PlantUMLEditorView.swift b/PlantUML/PlantUMLEditorView.swift index 35c32ea..a475e38 100644 --- a/PlantUML/PlantUMLEditorView.swift +++ b/PlantUML/PlantUMLEditorView.swift @@ -26,10 +26,11 @@ struct PlantUMLEditorView: View { @FocusState private var focusedItem: Focusable? - @State private var isEditorVisible = true + @State private var isEditorVisible = true @State private var isPreviewVisible = true - @State private var isScaleToFit = true - + @State private var isScaleToFit = true + @State private var showingKeyboard = false + var body: some View { GeometryReader { geometry in HStack { @@ -67,7 +68,44 @@ struct PlantUMLEditorView: View { } } + // MARK: Editor View + func EditorView() -> some View { + + List() { + ForEach( diagram.items ) { item in + + HStack { + if editMode?.wrappedValue != .active { + Image(systemName: "plus") + .contextMenu { + AddBelowButton( theItem: item ) + AddAboveButton( theItem: item ) + AddCloneButton( theItem: item ) + } + } + + PlantUMLTextFieldWithCustomKeyboard( item: item, + showingKeyboard: $showingKeyboard, + onChange: updateItem, + onAddNew: addNewItem ) + .focused($focusedItem, equals: .row(id: item.id)) + } + + } + .onMove(perform: move) + .onDelete( perform: delete) + + } + .onChange( of: showingKeyboard ) { + print( "showingKeyboard: \($0)") + } + .font(.footnote) + .listStyle(SidebarListStyle()) + + + } + func SaveButton() -> some View { Button( action: saveToDocument ) { @@ -153,38 +191,6 @@ struct PlantUMLEditorView: View { } } - // MARK: Editor View - func EditorView() -> some View { - - List() { - ForEach( diagram.items ) { item in - - HStack { - if editMode?.wrappedValue != .active { - Image(systemName: "plus") - .contextMenu { - AddBelowButton( theItem: item ) - AddAboveButton( theItem: item ) - AddCloneButton( theItem: item ) - } - } - - PlantUMLTextFieldWithCustomKeyboard( item: item, - onChange: updateItem, - onAddNew: addNewItem ) - .focused($focusedItem, equals: .row(id: item.id)) - } - - } - .onMove(perform: move) - .onDelete( perform: delete) - - } - .font(.footnote) - .listStyle(SidebarListStyle()) - - } - } diff --git a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift index e008e3b..ac88c5f 100644 --- a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift +++ b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift @@ -21,16 +21,19 @@ public struct PlantUMLTextFieldWithCustomKeyboard : UIViewRepresentable { public typealias UIViewType = UITextField private let textField = UITextField() - public var item:SyntaxStructure public var onChange:ChangeHandler public var onAddNew:AddNewActionHandler + @Binding private var showingKeyboard: Bool + public init( item:SyntaxStructure, + showingKeyboard: Binding, onChange:@escaping ChangeHandler, onAddNew:@escaping AddNewActionHandler ) { self.item = item + self._showingKeyboard = showingKeyboard self.onChange = onChange self.onAddNew = onAddNew } @@ -47,6 +50,7 @@ public struct PlantUMLTextFieldWithCustomKeyboard : UIViewRepresentable { textField.font = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular) textField.returnKeyType = .done textField.text = item.rawValue + return textField } @@ -72,7 +76,6 @@ extension PlantUMLTextFieldWithCustomKeyboard { public class Coordinator: NSObject, UITextFieldDelegate, CustomKeyboardPresenter { - private var keyboardRect:CGRect = .zero private let owner : PlantUMLTextFieldWithCustomKeyboard private var showCustomKeyboard:Bool @@ -100,7 +103,6 @@ extension PlantUMLTextFieldWithCustomKeyboard { } - public init(textfield : PlantUMLTextFieldWithCustomKeyboard) { self.owner = textfield self.showCustomKeyboard = false @@ -112,7 +114,6 @@ extension PlantUMLTextFieldWithCustomKeyboard { updateAccesoryView() - } func updateAccesoryView() { @@ -129,10 +130,11 @@ extension PlantUMLTextFieldWithCustomKeyboard { ] bar.sizeToFit() owner.textField.inputAccessoryView = bar - + } } + public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if let text = textField.text, let range = Range(range, in: text) { @@ -164,7 +166,6 @@ extension PlantUMLTextFieldWithCustomKeyboard { self.owner.onAddNew(owner.item, .BELOW, "") } - @objc public func addAbove() { self.owner.onAddNew(owner.item, .ABOVE, "") } @@ -181,6 +182,7 @@ extension PlantUMLTextFieldWithCustomKeyboard { owner.textField.inputView = nil } owner.textField.reloadInputViews() + self.owner.showingKeyboard = showCustomKeyboard }