Skip to content

Commit

Permalink
Merge branch 'feature/issue12' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Jan 7, 2023
2 parents bcee031 + 7c5162e commit 20512dd
Show file tree
Hide file tree
Showing 10 changed files with 379 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreviewsEnabled</key>
<false/>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/bsorrentino/PlantUML4iPad.git",
"state" : {
"branch" : "line_editor",
"revision" : "c90880eceaa26b08870ab34caf356dbbfa6cabc8"
"revision" : "502b2d37fa46ab0fc24936860d1edabf0342ce89"
}
}
],
Expand Down
40 changes: 40 additions & 0 deletions PlantUMLKeyboard/Sources/PlantUMLKeyboard/DebounceObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// File.swift
//
//
// Created by Bartolomeo Sorrentino on 06/01/23.
//

import Foundation
import Combine

class DebounceUpdate<T> where T : Equatable {

private var updateSubject = PassthroughSubject<T, Never>()

private var cancellabe:Cancellable?

func subscribe( debounceInSeconds seconds: Double, onUpdate update: @escaping ( T ) -> Void ) {

if self.cancellabe == nil {

self.cancellabe = updateSubject
.removeDuplicates()
.debounce(for: .seconds(seconds), scheduler: RunLoop.main)
.print()
.sink( receiveValue: update )

}

}

func request( for element:T ) {
updateSubject.send( element )
}
}

class DebounceUpdateObject<T> : ObservableObject where T : Equatable {

let update = DebounceUpdate<T>()

}
217 changes: 217 additions & 0 deletions PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard+Color.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
//
// PlantUMLKeyboard+Color.swift
//
//
// Created by Bartolomeo Sorrentino on 05/01/23.
//

import SwiftUI
import Combine

typealias RGBA = (R:Int, G:Int, B:Int, A:Int )

// MARK: CGColor extension

extension CGColor {

func rgbValue() -> RGBA? {
var output:RGBA? = nil

if let values = self.components {

switch values.count {
case 1:
output = ( Int(values[0] * 255), Int(values[0] * 255), Int(values[0] * 255),1)
break
case 2:
output = ( Int(values[0] * 255), Int(values[0] * 255), Int(values[0] * 255),Int(values[1] * 255))
break
case 3:
output = ( Int(values[0] * 255), Int(values[1] * 255), Int(values[2] * 255),1)
case 4:
output = ( Int(values[0] * 255), Int(values[1] * 255), Int(values[2] * 255),Int(values[3] * 255))
default:
break
}
}

return output
}

func hexValue() -> String? {
var output:String? = nil

if let rgba:RGBA = self.rgbValue() {
output = "#\(String(format:"%02X", rgba.R))\(String(format:"%02X", rgba.G))\(String(format:"%02X", rgba.B))\( String(format:"%02X", rgba.A))"
}

return output
}

}

// MARK: Color extension
extension Color {

func hexValue() -> String? {
self.cgColor?.hexValue()
}
}


struct ColorKeyButton : UIViewRepresentable {

var symbol:Symbol
var onPressSymbol: (Symbol) -> Void

init(symbol: Symbol, onPressSymbol: @escaping (Symbol) -> Void) {
self.symbol = symbol
self.onPressSymbol = onPressSymbol
}

func makeCoordinator() -> Coordinator {
Coordinator( self )
}

func makeUIView(context: Context) -> UIButton {
let button = UIButton()


//
// title
//
button.setTitle(symbol.id, for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
if let label = button.titleLabel {
label.font = UIFont.systemFont(ofSize: 16, weight: .bold)
}

//
// Image
//
// if let image = UIImage(named: "paintpalette", in: .module, compatibleWith: nil) {
// result.setImage(image, for: .normal)
// }
if let image = UIImage(systemName: "paintbrush.fill") {
button.setImage(image, for: .normal)
}

//
// Border
//
button.layer.borderColor = UIColor.black.cgColor
button.layer.borderWidth = 1
button.layer.cornerRadius = 5

button.frame.size = CGSize(width: 100, height: 30)

//
// constraints
//
// button.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
// button.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true

return button
}

func updateUIView(_ button: UIButton, context: Context) {
//
// action
//
let action = UIAction(title: symbol.id ) { _ in

let colorPicker = UIColorPickerViewController()

colorPicker.delegate = context.coordinator

getRootViewController()?.presentedViewController?.present( colorPicker, animated: true, completion: nil )
}

button.addAction( action, for: .touchDown )

}



}

extension ColorKeyButton {

class Coordinator: NSObject, UIColorPickerViewControllerDelegate {

private let update = DebounceUpdate<String>()
private var owner: ColorKeyButton

init( _ owner: ColorKeyButton ) {
self.owner = owner
}

func colorPickerViewController(_ viewController: UIColorPickerViewController, didSelect color: UIColor, continuously: Bool) {

guard let hexColor = color.cgColor.hexValue() else {
return
}

update.subscribe( debounceInSeconds: 0 ) { [self] value in

viewController.dismiss(animated: true, completion: nil)

let symbol = Symbol( id: owner.symbol.id, value: value )

owner.onPressSymbol( symbol )

}

let value = String(format: owner.symbol.value, hexColor )

update.request(for: value)
}

}
}

/*
struct ColorKeyView: View {
@StateObject private var updateColor = DebounceUpdateObject<String>()

@State private var selectedColor = Color.blue.opacity(0.5)

var symbol:Symbol
var onPressSymbol: (Symbol) -> Void

var body: some View {
VStack {

ColorPicker( selection: $selectedColor, label: {
Text(symbol.id).font(.system(size: 16).bold())

})
.frame( maxWidth: 110 )
.border(.black)
.padding()
.onChange(of: selectedColor ) { color in
updateColor.update.subscribe( debounceInSeconds: 0 ) {
onPressSymbol( makeSymbol( from: $0 ) )
}
updateColor.update.request(for: color.hexValue() ?? "")

}
}

}

private func makeSymbol( from hexColor: String ) -> Symbol {

let value = String(format: symbol.value, hexColor )
return Symbol( id: symbol.id, value: value )
}
}

*/

// MARK: Preview
struct ColorKeyButton_Previews: PreviewProvider {
static var previews: some View {
ColorKeyButton( symbol:Symbol( id: "test" ), onPressSymbol: { _ in } )
}
}
59 changes: 38 additions & 21 deletions PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import LineEditor

public struct PlantUMLKeyboardView: LineEditorKeyboard {


var onHide:() -> Void
var onPressSymbol: (Symbol) -> Void

public init(onHide: @escaping () -> Void, onPressSymbol: @escaping (LineEditorKeyboardSymbol) -> Void) {
self.onHide = onHide
self.onPressSymbol = onPressSymbol
}

public var body : some View{

ZStack(alignment: .topLeading) {
Expand Down Expand Up @@ -41,6 +43,7 @@ public struct PlantUMLKeyboardView: LineEditorKeyboard {
.padding()
}


func ContentView( _ group: PlantUMLSymbolGroup ) -> some View {
ScrollView(.vertical, showsIndicators: false) {

Expand All @@ -52,39 +55,52 @@ public struct PlantUMLKeyboardView: LineEditorKeyboard {

ForEach( Array(i.enumerated()), id: \.offset ) { cellIndex, symbol in

Button {

onPressSymbol(symbol)

} label: {

ButtonLabel( for: group, row: rowIndex, cell: cellIndex, symbol: symbol )

VStack {
if symbol.type == "color" {
// ColorKeyView( symbol: symbol, onPressSymbol: onPressSymbol )
ColorKeyButton( symbol: symbol, onPressSymbol: onPressSymbol )
.frame( maxWidth: 100)

}
else {
Button {
onPressSymbol(symbol)
} label: {
ButtonLabel( for: group, row: rowIndex, cell: cellIndex, symbol: symbol )
}
.buttonStyle( KeyButtonStyle() )
}
}
.buttonStyle( KeyButtonStyle() )

}

}
}
}
.padding(.top)

}
}
}

fileprivate struct KeyButtonStyle: ButtonStyle {

func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(5)
.border( .black, width: 1)
.background( .white )
}
}


// MARK: Plain Button Extension
extension PlantUMLKeyboardView {


fileprivate struct KeyButtonStyle: ButtonStyle {

func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(5)
.background( .white )
// .border( .black, width: 1)
.overlay {
RoundedRectangle(cornerRadius: 5)
.stroke(.black, lineWidth: 1)
}
}
}

func ButtonLabel( for group: PlantUMLSymbolGroup, row: Int, cell: Int, symbol: Symbol ) -> some View {

Text(symbol.id).font(.system(size: 16).bold())
Expand All @@ -106,6 +122,7 @@ extension PlantUMLKeyboardView {
}
}


struct PlantUMLKeyboardView_Previews: PreviewProvider {

static var previews: some View {
Expand Down
Loading

0 comments on commit 20512dd

Please sign in to comment.