From a4e38b4c40ea9022ebde62e821ac9f57514ae422 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 3 Oct 2022 13:03:26 +0200 Subject: [PATCH] feat: add tabview on custom keyboard --- .../PlantUMLFramework/PlantUMLScript.swift | 4 +- .../Assets.xcassets/Contents.json | 6 ++ .../Contents.json | 0 .../plantuml-sequence-arrow.png | Bin .../PlantUMLKeyboard/PlantUMLKeyboard.swift | 89 ++++++++---------- .../PlantUMLKeyboard/PlantUMLSymbol.swift | 70 ++++++++++++-- 6 files changed, 110 insertions(+), 59 deletions(-) create mode 100644 PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/Contents.json rename {PlantUML => PlantUMLKeyboard/Sources/PlantUMLKeyboard}/Assets.xcassets/plantuml-sequence-arrows.imageset/Contents.json (100%) rename {PlantUML => PlantUMLKeyboard/Sources/PlantUMLKeyboard}/Assets.xcassets/plantuml-sequence-arrows.imageset/plantuml-sequence-arrow.png (100%) diff --git a/PlantUMLFramework/Sources/PlantUMLFramework/PlantUMLScript.swift b/PlantUMLFramework/Sources/PlantUMLFramework/PlantUMLScript.swift index 0537583..8976284 100644 --- a/PlantUMLFramework/Sources/PlantUMLFramework/PlantUMLScript.swift +++ b/PlantUMLFramework/Sources/PlantUMLFramework/PlantUMLScript.swift @@ -86,8 +86,8 @@ public struct PlantUMLScript : CustomStringConvertible { } else { return """ '' STYLE START - \(hideShowCommands.joined(separator: "\n")) - \(skinparamCommands.joined(separator: "\n")) + ''\(hideShowCommands.joined(separator: "\n")) + ''\(skinparamCommands.joined(separator: "\n")) '' STYLE END """ } diff --git a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/Contents.json b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/PlantUML/Assets.xcassets/plantuml-sequence-arrows.imageset/Contents.json b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/plantuml-sequence-arrows.imageset/Contents.json similarity index 100% rename from PlantUML/Assets.xcassets/plantuml-sequence-arrows.imageset/Contents.json rename to PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/plantuml-sequence-arrows.imageset/Contents.json diff --git a/PlantUML/Assets.xcassets/plantuml-sequence-arrows.imageset/plantuml-sequence-arrow.png b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/plantuml-sequence-arrows.imageset/plantuml-sequence-arrow.png similarity index 100% rename from PlantUML/Assets.xcassets/plantuml-sequence-arrows.imageset/plantuml-sequence-arrow.png rename to PlantUMLKeyboard/Sources/PlantUMLKeyboard/Assets.xcassets/plantuml-sequence-arrows.imageset/plantuml-sequence-arrow.png diff --git a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard.swift b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard.swift index 03bda71..582f55e 100644 --- a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard.swift +++ b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard.swift @@ -10,32 +10,17 @@ struct PlantUMLKeyboardView: View { ZStack(alignment: .topLeading) { - ScrollView(.vertical, showsIndicators: false) { - - VStack(spacing: 15){ - - ForEach( Array(plantUMLSymbols.enumerated()), id: \.offset) { rowIndex, i in - - HStack(spacing: 10) { - - ForEach( Array(i.enumerated()), id: \.offset ) { cellIndex, symbol in - - Button { - - onPressSymbol(symbol) - - } label: { - - ButtonLabel( rowIndex: rowIndex, cellIndex: cellIndex, symbol: symbol ) - - } - .buttonStyle( KeyButtonStyle2() ) - } - } + TabView { + ContentView( .common ) + .tabItem { + Label( PlantUMLSymbolGroup.common.rawValue, systemImage: "list.dash") + .labelStyle(.titleOnly) + } + ContentView( .sequence ) + .tabItem { + Label( PlantUMLSymbolGroup.sequence.rawValue, systemImage: "square.and.pencil") + .labelStyle(.titleOnly) } - } - .padding(.top) - } .frame(maxWidth: .infinity ) .background(Color.gray.opacity(0.1)) @@ -45,34 +30,42 @@ struct PlantUMLKeyboardView: View { Image(systemName: "xmark").foregroundColor(.black) } .padding() + } } - // - // - // - func replaceSymbolAtCursorPosition( _ symbol: Symbol) { - /* - guard let handleToYourTextView = getFirstTextFieldResponder() else { - return - } - - print( "TextViewResponder \(handleToYourTextView)") - - // [How to programmatically enter text in UITextView at the current cursor position](https://stackoverflow.com/a/35888634/521197) - if let range = handleToYourTextView.selectedTextRange { - // From your question I assume that you do not want to replace a selection, only insert some text where the cursor is. - handleToYourTextView.replace(range, withText: symbol.value ) - } + func ContentView( _ group: PlantUMLSymbolGroup ) -> some View { + ScrollView(.vertical, showsIndicators: false) { + + VStack(spacing: 15){ + + ForEach( Array(group.symbols.enumerated()), id: \.offset) { rowIndex, i in + + HStack(spacing: 10) { + + ForEach( Array(i.enumerated()), id: \.offset ) { cellIndex, symbol in + + Button { + + onPressSymbol(symbol) + + } label: { + + ButtonLabel( for: group, row: rowIndex, cell: cellIndex, symbol: symbol ) + + } + .buttonStyle( KeyButtonStyle() ) + } + } + } + } + .padding(.top) - if let additionalValues = symbol.additionalValues { - customKeyboard.itemsToAdd = additionalValues } - */ } } -fileprivate struct KeyButtonStyle2: ButtonStyle { +fileprivate struct KeyButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label @@ -85,17 +78,17 @@ fileprivate struct KeyButtonStyle2: ButtonStyle { extension PlantUMLKeyboardView { - func ButtonLabel( rowIndex: Int, cellIndex: Int, symbol: Symbol ) -> some View { + func ButtonLabel( for group: PlantUMLSymbolGroup, row: Int, cell: Int, symbol: Symbol ) -> some View { Group { - if plantUMLImages[rowIndex].isEmpty || plantUMLImages[rowIndex].isEmpty || plantUMLImages[rowIndex][cellIndex]==nil + if group.images.isEmpty || group.images[row].isEmpty || group.images[row].isEmpty || group.images[row][cell]==nil { Text(symbol.description) .font(.system(size: 16).bold()) } else { - let img = plantUMLImages[rowIndex][cellIndex] + let img = group.images[row][cell] Image( uiImage: img! ) .resizable() .frame(width: 40, height: 20) diff --git a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLSymbol.swift b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLSymbol.swift index 230fc42..f47434e 100644 --- a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLSymbol.swift +++ b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLSymbol.swift @@ -30,19 +30,42 @@ struct Symbol : Identifiable, CustomStringConvertible { self._value = value self._additionalValues = additionalValues } + } - - -var plantUMLSymbols:[[Symbol]] = [ +// +// MARK: COMMON DIAGRAMS +// +fileprivate let common_symbols = [ + [ Symbol("title", "title my title"), Symbol("header", "header my header"), Symbol("footer", "footer my footer"), + ], + [ + Symbol("allow_mixing"), + Symbol( "hide empty members"), + Symbol( "skinparam shadowing false"), + ], + [ + Symbol("actor", "actor \"my actor\" as a1"), + ] + +] + +fileprivate let common_images:[[UIImage?]] = [] + +// +// MARK: SEQUENCE DIAGRAMS +// + +fileprivate let sequence_symbols = [ + + [ Symbol("autonumber") ], [ Symbol("participant","participant \"my participant\" as p1"), - Symbol("actor", "actor \"my actor\" as a1"), Symbol("boundary", "boundary \"my boundary\" as b1"), Symbol("control", "control \"my control\" as c1"), Symbol("entity", "entity \"my entity\" as e1"), @@ -70,14 +93,43 @@ var plantUMLSymbols:[[Symbol]] = [ Symbol("note right", "note right /' of participant '/", ["this note is displayed right", "end note"]), Symbol("note over", "note over participant1 /', participant2 '/", ["this note is displayed over participant1", "end note"]), ] - ] -var plantUMLImages:[[UIImage?]] = { +fileprivate let sequence_images:[[UIImage?]] = { + + var arrows:[UIImage?] = [] - guard let arrows = UIImage(named: "plantuml-sequence-arrows")?.extractTiles( with: CGSize( width: 158.0, height: 28.6) ) else { - return [ [], [] ,[], [] ] + if let img = UIImage(named: "plantuml-sequence-arrows", in: .module, compatibleWith: nil) { + arrows = img.extractTiles( with: CGSize( width: 158.0, height: 28.6) ) } - + return [ [], [], arrows, [] ] }() + + +enum PlantUMLSymbolGroup : String, CaseIterable { + case common = "Commons" + case sequence = "Sequence" + + + var symbols: [[ Symbol ]] { + switch self { + case .common: + return common_symbols + case .sequence: + return sequence_symbols + } + } + + var images: [[ UIImage? ]] { + switch self { + case .common: + return common_images + case .sequence: + return sequence_images + } + } +} + + +