Skip to content

Commit

Permalink
attempting to use optionals for placeholder logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ApplebaumIan committed Feb 4, 2021
1 parent e7885e9 commit 9e2784b
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions Sources/SwiftUI Apple Watch Decimal Pad/DigetPadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,31 @@ import SwiftUI

@available(iOS 13.0, watchOS 6.0, *)
public struct DigiTextView: View {
@Binding public var text: String
var placeholder: String
@Binding public var text: String?
@State public var presentingModal: Bool

var align: TextViewAlignment
public init(text: Binding<String>, presentingModal:Bool, alignment: TextViewAlignment = .center){
public init( placeholder: String, text: Binding<String?>, presentingModal:Bool, alignment: TextViewAlignment = .center){
_text = text
_presentingModal = State(initialValue: presentingModal)
self.align = alignment
self.placeholder = placeholder
}

public var body: some View {
Button(action: {
presentingModal.toggle()
}) {
Text(text)
if text != nil{
Text(text!)
.lineLimit(1)
}
else{
Text(placeholder)
.lineLimit(1)
.opacity(0.8)
}
}.buttonStyle(TextViewStyle(alignment: align))
.sheet(isPresented: $presentingModal, content: {
EnteredText(text: $text, presentedAsModal: $presentingModal)
Expand All @@ -33,9 +42,9 @@ public struct DigiTextView: View {
}
@available(iOS 13.0, watchOS 6.0, *)
public struct EnteredText: View {
@Binding var text:String
@Binding var text:String?
@Binding var presentedAsModal: Bool
public init(text: Binding<String>, presentedAsModal:
public init(text: Binding<String?>, presentedAsModal:
Binding<Bool>){
_text = text
_presentedAsModal = presentedAsModal
Expand All @@ -55,7 +64,7 @@ public struct EnteredText: View {
Button(action:{
presentedAsModal.toggle()
}){
Text(text)
Text(text ?? "")
}
.buttonStyle(PlainButtonStyle())
.multilineTextAlignment(.trailing)
Expand All @@ -73,93 +82,93 @@ public struct EnteredText: View {
@available(iOS 13.0, watchOS 6.0, *)
public struct DigetPadView: View {
public var widthSpace: CGFloat = 4.0
@Binding var text:String
public init(text: Binding<String>){
@Binding var text:String?
public init(text: Binding<String?>){
_text = text
}
public var body: some View {
VStack(spacing: 5) {
HStack(spacing: widthSpace){
Button(action: {
text.append("1")
text?.append("1")
}) {
Text("1")
.padding(0)
}
.digitKeyFrame()
Button(action: {
text.append("2")
text?.append("2")
}) {
Text("2")
}.digitKeyFrame()

Button(action: {
text.append("3")
text?.append("3")
}) {
Text("3")
}.digitKeyFrame()
}
HStack(spacing:widthSpace){
Button(action: {
text.append("4")
text?.append("4")
}) {
Text("4")
}.digitKeyFrame()
Button(action: {
text.append("5")
text?.append("5")
}) {
Text("5")
}.digitKeyFrame()

Button(action: {
text.append("6")
text?.append("6")
}) {
Text("6")
}.digitKeyFrame()
}

HStack(spacing:widthSpace){
Button(action: {
text.append("7")
text?.append("7")
}) {
Text("7")
}.digitKeyFrame()
Button(action: {
text.append("8")
text?.append("8")
}) {
Text("8")
}.digitKeyFrame()

Button(action: {
text.append("9")
text?.append("9")
}) {
Text("9")
}
.digitKeyFrame()
}
HStack(spacing:widthSpace) {
Button(action: {
if !text.contains("."){
if !(text?.contains("."))!{
if text == ""{
text.append("0.")
text?.append("0.")
}else{
text.append(".")
text?.append(".")
}
}
}) {
Text("")
}
.digitKeyFrame()
Button(action: {
text.append("0")
text?.append("0")
}) {
Text("0")
}
.digitKeyFrame()

Button(action: {
if let last = text.indices.last{
text.remove(at: last)
if let last = text?.indices.last{
text?.remove(at: last)
}
}) {
Image(systemName: "delete.left")
Expand Down Expand Up @@ -192,7 +201,7 @@ struct EnteredText_Previews: PreviewProvider {

struct Content_View_Previews: PreviewProvider {
static var previews: some View{
DigiTextView(text: .constant("boop"), presentingModal: true)
DigiTextView(placeholder: "boop placeholder", text: .constant(""), presentingModal: true)
}
}
#endif

0 comments on commit 9e2784b

Please sign in to comment.