Skip to content

Commit

Permalink
feat: add UIViewRepresentable(PlantUMLTextFieldWithCustomKeyboard) fo…
Browse files Browse the repository at this point in the history
…r managing inputView
  • Loading branch information
bsorrentino committed Sep 18, 2022
1 parent e62e205 commit beb3a38
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 111 deletions.
4 changes: 0 additions & 4 deletions PlantUML.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
A02F69B228D1E19600090D97 /* PlantUMLFramework in Frameworks */ = {isa = PBXBuildFile; productRef = A02F69B128D1E19600090D97 /* PlantUMLFramework */; };
A02F69B428D1E19A00090D97 /* PlantUMLKeyboard in Frameworks */ = {isa = PBXBuildFile; productRef = A02F69B328D1E19A00090D97 /* PlantUMLKeyboard */; };
A077460F28C7D818007C5C13 /* PlantUMLTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = A077460E28C7D818007C5C13 /* PlantUMLTextField.swift */; };
A0A42A76289ABC2D00E929EB /* PlantUMLDiagramView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0A42A75289ABC2D00E929EB /* PlantUMLDiagramView.swift */; };
A0A42A77289ABC2D00E929EB /* PlantUMLDiagramView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0A42A75289ABC2D00E929EB /* PlantUMLDiagramView.swift */; };
A0A42A79289AC37C00E929EB /* PlantUMLDiagramObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0A42A78289AC37C00E929EB /* PlantUMLDiagramObject.swift */; };
Expand Down Expand Up @@ -44,7 +43,6 @@
/* Begin PBXFileReference section */
A01552A228CF47DF00F2B8A1 /* PlantUMLFramework */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PlantUMLFramework; sourceTree = "<group>"; };
A077460D28C7D49D007C5C13 /* PlantUMLKeyboard */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PlantUMLKeyboard; sourceTree = "<group>"; };
A077460E28C7D818007C5C13 /* PlantUMLTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlantUMLTextField.swift; sourceTree = "<group>"; };
A077461028C7DF19007C5C13 /* PlantUMLKeyboard */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PlantUMLKeyboard; sourceTree = "<group>"; };
A077461128C7DF74007C5C13 /* PlantUMLKeyboard */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PlantUMLKeyboard; sourceTree = "<group>"; };
A077461728C7F279007C5C13 /* PlantUMLKeyboard */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PlantUMLKeyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -144,7 +142,6 @@
children = (
A0D3C64928984A0E000838D7 /* PlantUMLApp.swift */,
A0D3C64B28984A0E000838D7 /* PlantUMLDocument.swift */,
A077460E28C7D818007C5C13 /* PlantUMLTextField.swift */,
A0D3C64D28984A0E000838D7 /* PlantUMLEditorView.swift */,
A0A42A75289ABC2D00E929EB /* PlantUMLDiagramView.swift */,
A0A42A78289AC37C00E929EB /* PlantUMLDiagramObject.swift */,
Expand Down Expand Up @@ -319,7 +316,6 @@
files = (
A0A42A79289AC37C00E929EB /* PlantUMLDiagramObject.swift in Sources */,
A0D3C64A28984A0E000838D7 /* PlantUMLApp.swift in Sources */,
A077460F28C7D818007C5C13 /* PlantUMLTextField.swift in Sources */,
A0D3C64C28984A0E000838D7 /* PlantUMLDocument.swift in Sources */,
A0D3C64E28984A0E000838D7 /* PlantUMLEditorView.swift in Sources */,
A0A42A76289ABC2D00E929EB /* PlantUMLDiagramView.swift in Sources */,
Expand Down
11 changes: 5 additions & 6 deletions PlantUML/PlantUMLEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ struct PlantUMLEditorView: View {
AddCloneButton( theItem: item )
}
}
PlantUMLTextField( value: item.rawValue,
showKeyboard: $customKeyboard.showKeyboard,
onChange: updateItem )

PlantUMLTextFieldWithCustomKeyboard()
// PlantUMLTextField( value: item.rawValue,
// customKeyboard: customKeyboard,
// onChange: updateItem )
.focused($focusedItem, equals: .row(id: item.id))
.onSubmit(of: .text) {
// openURL( diagram.buildURL() )
}
}


Expand Down
60 changes: 0 additions & 60 deletions PlantUML/PlantUMLTextField.swift

This file was deleted.

2 changes: 1 addition & 1 deletion PlantUMLKeyboard/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/siteline/SwiftUI-Introspect.git", branch: "master" ),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import Combine
import SwiftUI

import UIKit

public class CustomKeyboardObject : ObservableObject {

@Published public var showKeyboard = false

@Published public var itemsToAdd:[String] = []

private var controller:UIHostingController<PlantUMLKeyboardView>?

private var keyboardRect:CGRect = .zero

private var cancellable:AnyCancellable?

var keyboardView:UIView {

let controller = UIHostingController( rootView: PlantUMLKeyboardView( customKeyboard: self ) )

let MAGIC_NUMBER = 50.0 // magic number .. height of keyboard top bar
var customKeyboardRect = keyboardRect
customKeyboardRect.origin.y += MAGIC_NUMBER
customKeyboardRect.size.height -= MAGIC_NUMBER
controller.view.frame = customKeyboardRect
return controller.view

}
public init() {

NotificationCenter.default.addObserver(
Expand Down Expand Up @@ -41,17 +51,17 @@ public class CustomKeyboardObject : ObservableObject {

}

cancellable = _showKeyboard.projectedValue.sink { value in
print( "showKeyboard: \(value)" )
if( value ) {
self.show()
}
else {
self.hide()
}
}
// cancellable = _showKeyboard.projectedValue.sink { value in
// print( "showKeyboard: \(value)" )
//
// if( value ) {
// self.show()
// }
// else {
// self.hide()
// }
//
// }

}

Expand All @@ -68,30 +78,4 @@ public class CustomKeyboardObject : ObservableObject {
}
)
}

private func show() {

guard !self.showKeyboard && self.keyboardRect != .zero, let keyboardWindow = getKeyboardWindow() else {
return
}

print( "keyboardRect: \(keyboardRect)")

let controller = UIHostingController( rootView: PlantUMLKeyboardView( customKeyboard: self ) )
self.controller = controller

let MAGIC_NUMBER = 50.0 // magic number .. height of keyboard top bar
var customKeyboardRect = keyboardRect
customKeyboardRect.origin.y += MAGIC_NUMBER
customKeyboardRect.size.height -= MAGIC_NUMBER
controller.view.frame = customKeyboardRect
keyboardWindow.addSubview( controller.view )



}

private func hide() {
controller?.view.removeFromSuperview()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// File.swift
//
//
// Created by Bartolomeo Sorrentino on 18/09/22.
//

import UIKit
import SwiftUI

public struct PlantUMLTextFieldWithCustomKeyboard : UIViewRepresentable {

public typealias UIViewType = UITextField

private let textField = UITextField()

public init() {

}

public func makeCoordinator() -> PlantUMLTextFieldWithCustomKeyboard.Coordinator {
Coordinator(textfield: self)
}

public func makeUIView(context: Context) -> UITextField {

textField.delegate = context.coordinator
return textField
}

public func updateUIView(_ uiView: UITextField, context: Context) {

}


public class Coordinator: NSObject, UITextFieldDelegate {

private let parent : PlantUMLTextFieldWithCustomKeyboard

public init(textfield : PlantUMLTextFieldWithCustomKeyboard) {
self.parent = textfield
}

}

}
71 changes: 71 additions & 0 deletions PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// PlantUMLTextField.swift
// PlantUML
//
// Created by Bartolomeo Sorrentino on 06/09/22.
//

import SwiftUI

public struct PlantUMLTextField: View {

public typealias ChangeHandler = ( String ) -> Void

@ObservedObject var customKeyboard: CustomKeyboardObject

@State var value: String

@FocusState private var isFocused: Bool

var onChange:ChangeHandler

public init( value: String, customKeyboard: CustomKeyboardObject, onChange: @escaping ChangeHandler ) {
self.value = value
self.customKeyboard = customKeyboard
self.onChange = onChange
}

public var body: some View {

VStack {

HStack(spacing: 15) {

TextField( "", text: $value )
.keyboardType(.asciiCapableNumberPad)
.textInputAutocapitalization(.never)
.font(Font.system(size: 15).monospaced())
.submitLabel(.done)
.focused($isFocused)
.onChange(of: value, perform: onChange )
// .introspectTextField { textField in
// print( "==> introspectTextField: \(value)" )
// }


if( isFocused ) {
ShowKeyboardAccessoryButton( show: $customKeyboard.showKeyboard )
}
}

}
.edgesIgnoringSafeArea(.all)

}
}

struct PlantUMLTextField_Previews: PreviewProvider {

struct PlantUMLTextFieldProxy : View {
@ObservedObject var customKeyboard = CustomKeyboardObject()

public var body : some View {
PlantUMLTextField(value: "test", customKeyboard: customKeyboard, onChange: { (v) in } )
}
}

static var previews: some View {
PlantUMLTextFieldProxy()
}

}

0 comments on commit beb3a38

Please sign in to comment.