Skip to content

Commit

Permalink
Merge pull request #4 from no-comment/optimize-complexity
Browse files Browse the repository at this point in the history
Optimise complexity and fix bugs
  • Loading branch information
mikakruschel authored Feb 26, 2022
2 parents 0fa1ea9 + e3c089d commit a00bf23
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
.target(
name: "KeyboardToolbar",
dependencies: [],
exclude: ["Tests/ExampleViews"]
),
exclude: []
),
]
)
17 changes: 3 additions & 14 deletions Sources/KeyboardToolbar/KeyboardResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import SwiftUI

internal final class KeyboardResponder: ObservableObject {
private var notificationCenter: NotificationCenter
@Published private(set) var currentHeight: CGFloat = 0
@Published private(set) var duration: Double = 0.25
@Published private(set) var visible: Bool = false

init(center: NotificationCenter = .default) {
notificationCenter = center
Expand All @@ -21,21 +20,11 @@ internal final class KeyboardResponder: ObservableObject {
}

@objc func keyBoardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
currentHeight = keyboardSize.height
}

if let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double) {
duration = keyboardDuration
}
visible = true
}

@objc func keyBoardWillHide(notification: Notification) {
currentHeight = 0

if let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double) {
duration = keyboardDuration
}
visible = false
}
}
#endif
46 changes: 25 additions & 21 deletions Sources/KeyboardToolbar/KeyboardToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,31 @@ internal struct KeyboardToolbar: View {
}

func itemGroup(_ items: [KeyboardToolbarItem]) -> some View {
ForEach(items) { item in
Button(action: item.callback, label: {
if (item.image != nil || item.text == nil) {
(item.image ?? Image(systemName: "placeholdertext.fill"))
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundColor(item.color)
.frame(height: style.itemSize)
.frame(minWidth: max(style.itemSize, style.height))
.frame(height: style.height)
} else {
Text(item.text ?? "")
.font(.system(size: style.itemSize))
.foregroundColor(item.color)
.frame(height: style.itemSize)
.frame(minWidth: max(style.itemSize, style.height))
.frame(height: style.height)
}
})
.contentShape(Rectangle())
}
ForEach(items, content: itemButton)
}

func itemButton(_ item: KeyboardToolbarItem) -> some View {
Button(action: item.callback, label: {
if (item.image != nil || item.text == nil) {
(item.image ?? Image(systemName: "placeholdertext.fill"))
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundColor(item.color)
.frame(height: style.itemSize)
.frame(minWidth: max(style.itemSize, style.height))
.padding(.vertical, style.height - style.itemSize)
.contentShape(Rectangle())
} else {
Text(item.text ?? "")
.font(.system(size: style.itemSize + 4))
.foregroundColor(item.color)
.frame(height: style.itemSize)
.frame(minWidth: max(style.itemSize, style.height))
.padding(.vertical, style.height - style.itemSize)
.contentShape(Rectangle())
}
})
.contentShape(Rectangle())
}

var leadingItemsView: some View {
Expand Down
24 changes: 10 additions & 14 deletions Sources/KeyboardToolbar/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ internal struct AppendKeyboardToolbar: ViewModifier {
let style: KeyboardToolbarStyle

func body(content: Content) -> some View {
ZStack {
content
.padding(.bottom, (responder.currentHeight == 0 || items.isEmpty) ? 0 : style.height)
content.overlay(toolbarContainer, alignment: .bottom)
}

var toolbarContainer: some View {
VStack(spacing: 0) {
Spacer()

VStack(spacing: 0) {
Spacer()

if !items.isEmpty {
toolbar
}
if !items.isEmpty {
toolbar
}
.opacity(responder.currentHeight == 0 ? 0 : 1)
.animation(.easeOut(duration: responder.duration))
.padding(.bottom, responder.currentHeight)
}
.edgesIgnoringSafeArea(.bottom)
.animation(.easeOut(duration: responder.duration))
.opacity(responder.visible ? 1 : 0)
}

var toolbar: some View {
Expand All @@ -47,6 +42,7 @@ internal struct AppendKeyboardToolbar: ViewModifier {

KeyboardToolbar(items: items, style: style)
}
.background(style.backgroundColor.ignoresSafeArea(.all, edges: .bottom))
}
}
}
Expand Down

0 comments on commit a00bf23

Please sign in to comment.