Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing Multiple Decimal Values #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Ratios/Ratios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
52712496240B566D00F26EF3 /* CalculatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52712495240B566D00F26EF3 /* CalculatorTests.swift */; };
52712498240B58CF00F26EF3 /* WaterInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52712497240B58CF00F26EF3 /* WaterInput.swift */; };
5271249A240B663A00F26EF3 /* WaterDisplay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52712499240B663A00F26EF3 /* WaterDisplay.swift */; };
AE92CF722C1DAD9F005124AB /* File.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE92CF712C1DAD9F005124AB /* File.swift */; };
AEA2EDDA2C1D0A860094972B /* DecimalInputModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEA2EDCC2C1D085F0094972B /* DecimalInputModifier.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -67,6 +69,8 @@
52712495240B566D00F26EF3 /* CalculatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorTests.swift; sourceTree = "<group>"; };
52712497240B58CF00F26EF3 /* WaterInput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaterInput.swift; sourceTree = "<group>"; };
52712499240B663A00F26EF3 /* WaterDisplay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaterDisplay.swift; sourceTree = "<group>"; };
AE92CF712C1DAD9F005124AB /* File.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = File.swift; sourceTree = SOURCE_ROOT; };
AEA2EDCC2C1D085F0094972B /* DecimalInputModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecimalInputModifier.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -117,6 +121,7 @@
526BD848240771A100A24AD2 /* Ratios */ = {
isa = PBXGroup;
children = (
AEA2EDCB2C1D08380094972B /* Modifiers */,
52712490240B554D00F26EF3 /* View Models */,
526BD87C24077F3300A24AD2 /* Views */,
526BD849240771A100A24AD2 /* AppDelegate.swift */,
Expand Down Expand Up @@ -161,6 +166,7 @@
526BD87C24077F3300A24AD2 /* Views */ = {
isa = PBXGroup;
children = (
AE92CF712C1DAD9F005124AB /* File.swift */,
526BD87D24077F4200A24AD2 /* TimerView.swift */,
526BD8832408CCFE00A24AD2 /* CoffeeInput.swift */,
526BD8852408D40200A24AD2 /* RatioInput.swift */,
Expand All @@ -179,6 +185,14 @@
path = "View Models";
sourceTree = "<group>";
};
AEA2EDCB2C1D08380094972B /* Modifiers */ = {
isa = PBXGroup;
children = (
AEA2EDCC2C1D085F0094972B /* DecimalInputModifier.swift */,
);
path = Modifiers;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -242,7 +256,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1130;
LastUpgradeCheck = 1130;
LastUpgradeCheck = 1240;
ORGANIZATIONNAME = "John Peden";
TargetAttributes = {
526BD845240771A100A24AD2 = {
Expand Down Expand Up @@ -313,12 +327,14 @@
526BD8842408CCFE00A24AD2 /* CoffeeInput.swift in Sources */,
52712492240B563000F26EF3 /* CalculatorViewModel.swift in Sources */,
52712498240B58CF00F26EF3 /* WaterInput.swift in Sources */,
AEA2EDDA2C1D0A860094972B /* DecimalInputModifier.swift in Sources */,
526BD87E24077F4200A24AD2 /* TimerView.swift in Sources */,
526BD84A240771A100A24AD2 /* AppDelegate.swift in Sources */,
5271249A240B663A00F26EF3 /* WaterDisplay.swift in Sources */,
526BD84F240771A100A24AD2 /* Ratios.xcdatamodeld in Sources */,
526BD851240771A100A24AD2 /* ContentView.swift in Sources */,
526BD8862408D40200A24AD2 /* RatioInput.swift in Sources */,
AE92CF722C1DAD9F005124AB /* File.swift in Sources */,
526BD88224079A5B00A24AD2 /* TimerViewModel.swift in Sources */,
526BD84C240771A100A24AD2 /* SceneDelegate.swift in Sources */,
);
Expand Down Expand Up @@ -394,6 +410,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -454,6 +471,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down
33 changes: 33 additions & 0 deletions Ratios/Ratios/Modifiers/DecimalInputModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// DecimalInput.swift
// Ratios
//
// Created by Yoonhee Tian on 2024-06-14.
// Copyright © 2024 John Peden. All rights reserved.
//

import SwiftUI
import Combine

struct DecimalInputModifier: ViewModifier {
@Binding var text: String

func body(content: Content) -> some View {
AnyView(content)
.onReceive(Just(text)) { newValue in
var filtered = newValue.filter { "0123456789.".contains($0) }
if (filtered.filter { $0 == "." }.count > 1) {
filtered = String(filtered.dropLast())
}
if filtered != newValue {
self.text = filtered
}
}
}
}

extension View {
func decimalInput(text: Binding<String>) -> some View {
self.modifier(DecimalInputModifier(text: text))
}
}
11 changes: 1 addition & 10 deletions Ratios/Ratios/Views/CoffeeInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@ struct CoffeeInput: View {
)
.multilineTextAlignment(.center)
.keyboardType(.decimalPad)
.onReceive(Just(amount)) { newValue in



let filtered = newValue.filter { "0123456789.0".contains($0) }
if filtered != newValue {
self.amount = filtered
}
}

.decimalInput(text: $amount)

Text("grams")
.fixedSize()
Expand Down
8 changes: 2 additions & 6 deletions Ratios/Ratios/Views/WaterInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct WaterInput: View {

VStack(alignment: .center) {
TextField("", text: $amount)
.decimalInput(text: $amount)
.frame(width: CGFloat(39), height: CGFloat(39))
.overlay(
RoundedRectangle(cornerRadius: 10)
Expand All @@ -46,12 +47,7 @@ struct WaterInput: View {
.multilineTextAlignment(.center)
.font(.system(size: 24))
.keyboardType(.decimalPad)
.onReceive(Just(amount)) { newValue in
let filtered = newValue.filter { "0123456789.0".contains($0) }
if filtered != newValue {
self.amount = filtered
}
}


Text("water")
.fixedSize()
Expand Down