Basic ingredient amount adjustment
This commit is contained in:
@@ -83,11 +83,100 @@ class ObservableRecipeDetail: ObservableObject {
|
||||
)
|
||||
}
|
||||
|
||||
func ingredients(for servings: Int) -> [String] {
|
||||
for ingredient in recipeIngredient {
|
||||
// TODO: Parse ingredient strings, adjust them for yield
|
||||
/*static func modifyIngredientAmounts(in ingredient: String, withFactor factor: Double) -> String {
|
||||
// Regular expression to match numbers, including integers and decimals
|
||||
// Patterns:
|
||||
// "\\b\\d+(\\.\\d+)?\\b" works only if there is a space following
|
||||
let regex = try! NSRegularExpression(pattern: "\\b\\d+(\\.\\d+)?\\b", options: [])
|
||||
let matches = regex.matches(in: ingredient, options: [], range: NSRange(ingredient.startIndex..., in: ingredient))
|
||||
|
||||
// Reverse the matches to replace from the end to avoid affecting indices of unprocessed matches
|
||||
let reversedMatches = matches.reversed()
|
||||
|
||||
var modifiedIngredient = ingredient
|
||||
|
||||
for match in reversedMatches {
|
||||
guard let range = Range(match.range, in: modifiedIngredient) else { continue }
|
||||
let originalNumberString = String(modifiedIngredient[range])
|
||||
if let originalNumber = Double(originalNumberString) {
|
||||
let modifiedNumber = originalNumber * factor
|
||||
// Format the number to remove trailing zeros if it's an integer after multiplication
|
||||
let formattedNumber = formatNumber(modifiedNumber)
|
||||
modifiedIngredient.replaceSubrange(range, with: formattedNumber)
|
||||
}
|
||||
}
|
||||
return []
|
||||
|
||||
return modifiedIngredient
|
||||
}*/
|
||||
static func modifyIngredientAmounts(in ingredient: String, withFactor factor: Double) -> String {
|
||||
// Regular expression to match numbers (including integers and decimals) and fractions
|
||||
let regexPattern = "\\b(\\d+(\\.\\d+)?)\\b|\\b(\\d+/\\d+)\\b"
|
||||
let regex = try! NSRegularExpression(pattern: regexPattern, options: [])
|
||||
let matches = regex.matches(in: ingredient, options: [], range: NSRange(ingredient.startIndex..., in: ingredient))
|
||||
|
||||
var modifiedIngredient = ingredient
|
||||
|
||||
// Reverse the matches to replace from the end to avoid affecting indices of unprocessed matches
|
||||
let reversedMatches = matches.reversed()
|
||||
|
||||
for match in reversedMatches {
|
||||
let fullMatchRange = match.range(at: 0)
|
||||
|
||||
// Check for a fractional match
|
||||
if match.range(at: 3).location != NSNotFound, let fractionRange = Range(match.range(at: 3), in: modifiedIngredient) {
|
||||
let fractionString = String(modifiedIngredient[fractionRange])
|
||||
let fractionParts = fractionString.split(separator: "/").compactMap { Double($0) }
|
||||
if fractionParts.count == 2, let numerator = fractionParts.first, let denominator = fractionParts.last, denominator != 0 {
|
||||
let fractionValue = numerator / denominator
|
||||
let modifiedNumber = fractionValue * factor
|
||||
let formattedNumber = formatNumber(modifiedNumber)
|
||||
modifiedIngredient.replaceSubrange(fractionRange, with: formattedNumber)
|
||||
}
|
||||
}
|
||||
// Check for an integer or decimal match
|
||||
else if let numberRange = Range(fullMatchRange, in: modifiedIngredient) {
|
||||
let numberString = String(modifiedIngredient[numberRange])
|
||||
if let number = Double(numberString) {
|
||||
let modifiedNumber = number * factor
|
||||
let formattedNumber = formatNumber(modifiedNumber)
|
||||
modifiedIngredient.replaceSubrange(numberRange, with: formattedNumber)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return modifiedIngredient
|
||||
}
|
||||
|
||||
static func formatNumber(_ value: Double) -> String {
|
||||
let integerPart = value >= 1 ? Int(value) : 0
|
||||
let decimalPart = value - Double(integerPart)
|
||||
|
||||
if integerPart >= 1 && decimalPart < 0.0001 {
|
||||
return String(format: "%.0f", value)
|
||||
}
|
||||
|
||||
// Define known fractions and their decimal equivalents
|
||||
let knownFractions: [(fraction: String, value: Double)] = [
|
||||
("1/8", 0.125), ("1/6", 0.167), ("1/4", 0.25), ("1/3", 0.33), ("1/2", 0.5), ("2/3", 0.66), ("3/4", 0.75)
|
||||
]
|
||||
|
||||
// Find the known fraction closest to the given value
|
||||
let closest = knownFractions.min(by: { abs($0.value - decimalPart) < abs($1.value - decimalPart) })!
|
||||
|
||||
// Check if the value is close enough to a known fraction to be considered a match
|
||||
let threshold = 0.05
|
||||
if abs(closest.value - decimalPart) <= threshold && integerPart == 0 {
|
||||
return closest.fraction
|
||||
} else if abs(closest.value - decimalPart) <= threshold && integerPart > 0 {
|
||||
return "\(String(integerPart)) \(closest.fraction)"
|
||||
} else {
|
||||
// If no close match is found, return the original value as a string
|
||||
return String(format: "%.2f", value)
|
||||
}
|
||||
}
|
||||
|
||||
func ingredientUnitsToMetric() {
|
||||
// TODO: Convert imperial units in recipes to metric units
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
212
Nextcloud Cookbook iOS Client/Data/Units.swift
Normal file
212
Nextcloud Cookbook iOS Client/Data/Units.swift
Normal file
@@ -0,0 +1,212 @@
|
||||
//
|
||||
// Units.swift
|
||||
// Nextcloud Cookbook iOS Client
|
||||
//
|
||||
// Created by Vincent Meilinger on 11.03.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
|
||||
// MARK: - Ingredient Units
|
||||
|
||||
enum MeasurementUnit {
|
||||
// Volume Metric
|
||||
case milliLiter, centiLiter, deciLiter, liter
|
||||
|
||||
// Volume Imperial
|
||||
case teaspoon, tablespoon, cup, pint, quart, gallon, gill, fluidOunce // Please just use metric
|
||||
|
||||
// Weight Metric
|
||||
case milliGram, gram, kilogram
|
||||
|
||||
// Weight Imperial
|
||||
case ounce, pound
|
||||
|
||||
// Other
|
||||
case pinch, dash, smidgen
|
||||
|
||||
|
||||
var localizedDescription: [LocalizedStringKey] {
|
||||
switch self {
|
||||
case .milliLiter:
|
||||
return ["milliliter", "millilitre", "ml", "cc"]
|
||||
case .centiLiter:
|
||||
return ["centiliter", "centilitre", "cl"]
|
||||
case .deciLiter:
|
||||
return ["deciliter", "decilitre", "dl"]
|
||||
case .liter:
|
||||
return ["liter", "litre", "l"]
|
||||
case .teaspoon:
|
||||
return ["teaspoon", "tsp"]
|
||||
case .tablespoon:
|
||||
return ["tablespoon", "tbsp"]
|
||||
case .cup:
|
||||
return ["cup", "c"]
|
||||
case .pint:
|
||||
return ["pint", "pt"]
|
||||
case .quart:
|
||||
return ["quart", "qt"]
|
||||
case .gallon:
|
||||
return ["gallon", "gal"]
|
||||
case .gill:
|
||||
return ["gill", "gi"]
|
||||
case .fluidOunce:
|
||||
return ["fluid ounce", "fl oz"]
|
||||
case .milliGram:
|
||||
return ["milligram", "mg"]
|
||||
case .gram:
|
||||
return ["gram", "g"]
|
||||
case .kilogram:
|
||||
return ["kilogram", "kg"]
|
||||
case .ounce:
|
||||
return ["ounce", "oz"]
|
||||
case .pound:
|
||||
return ["pound", "lb"]
|
||||
case .pinch:
|
||||
return ["pinch"]
|
||||
case .dash:
|
||||
return ["dash"]
|
||||
case .smidgen:
|
||||
return ["smidgen"]
|
||||
}
|
||||
}
|
||||
|
||||
static func convert(value: Double, from fromUnit: MeasurementUnit, to toUnit: MeasurementUnit) -> Double? {
|
||||
let (baseValue, _) = MeasurementUnit.toBaseUnit(value: value, unit: fromUnit)
|
||||
return MeasurementUnit.fromBaseUnit(value: baseValue, targetUnit: toUnit)
|
||||
}
|
||||
|
||||
private static func baseUnit(of unit: MeasurementUnit) -> MeasurementUnit {
|
||||
switch unit {
|
||||
// Volume Metric (all converted to liters)
|
||||
case .milliLiter, .centiLiter, .deciLiter, .liter, .teaspoon, .tablespoon, .cup, .pint, .quart, .gallon, .gill, .fluidOunce, .dash:
|
||||
return .liter
|
||||
|
||||
// Weight (all converted to grams)
|
||||
case .milliGram, .gram, .kilogram, .ounce, .pound, .pinch, .smidgen:
|
||||
return .gram
|
||||
}
|
||||
}
|
||||
|
||||
private static func toBaseUnit(value: Double, unit: MeasurementUnit) -> (Double, MeasurementUnit) {
|
||||
guard abs(value) >= Double(1e-10) else {
|
||||
return (0, unit)
|
||||
}
|
||||
switch unit {
|
||||
case .milliLiter:
|
||||
return (value/1000, .liter)
|
||||
case .centiLiter:
|
||||
return (value/100, .liter)
|
||||
case .deciLiter:
|
||||
return (value/10, .liter)
|
||||
case .liter:
|
||||
return (value, .liter)
|
||||
case .teaspoon:
|
||||
return (value * 0.005, .liter)
|
||||
case .tablespoon:
|
||||
return (value * 0.015, .liter)
|
||||
case .cup:
|
||||
return (value * 0.25, .liter)
|
||||
case .pint:
|
||||
return (value * 0.5, .liter)
|
||||
case .quart:
|
||||
return (value * 0.946, .liter)
|
||||
case .gallon:
|
||||
return (value * 3.8, .liter)
|
||||
case .gill:
|
||||
return (value * 0.17, .liter)
|
||||
case .fluidOunce:
|
||||
return (value * 0.03, .liter)
|
||||
case .milliGram:
|
||||
return (value * 0.001, .gram)
|
||||
case .gram:
|
||||
return (value, .gram)
|
||||
case .kilogram:
|
||||
return (value * 1000, .gram)
|
||||
case .ounce:
|
||||
return (value * 30, .gram)
|
||||
case .pound:
|
||||
return (value * 450, .gram)
|
||||
case .pinch:
|
||||
return (value * 0.3, .gram)
|
||||
case .dash:
|
||||
return (value * 0.000625, .liter)
|
||||
case .smidgen:
|
||||
return (value * 0.15, .gram)
|
||||
}
|
||||
}
|
||||
|
||||
static private func fromBaseUnit(value: Double, targetUnit: MeasurementUnit) -> Double {
|
||||
guard abs(value) >= Double(1e-10) else {
|
||||
return 0
|
||||
}
|
||||
|
||||
switch targetUnit {
|
||||
case .milliLiter:
|
||||
return value * 1000
|
||||
case .centiLiter:
|
||||
return value * 100
|
||||
case .deciLiter:
|
||||
return value * 10
|
||||
case .liter:
|
||||
return value
|
||||
case .teaspoon:
|
||||
return value / 0.005
|
||||
case .tablespoon:
|
||||
return value / 0.015
|
||||
case .cup:
|
||||
return value / 0.25
|
||||
case .pint:
|
||||
return value / 0.5
|
||||
case .quart:
|
||||
return value / 0.946
|
||||
case .gallon:
|
||||
return value / 3.8
|
||||
case .gill:
|
||||
return value / 0.17
|
||||
case .fluidOunce:
|
||||
return value / 0.03
|
||||
case .milliGram:
|
||||
return value * 1000
|
||||
case .gram:
|
||||
return value
|
||||
case .kilogram:
|
||||
return value / 1000
|
||||
case .ounce:
|
||||
return value / 30
|
||||
case .pound:
|
||||
return value / 450
|
||||
case .pinch:
|
||||
return value / 0.3
|
||||
case .dash:
|
||||
return value / 0.000625
|
||||
case .smidgen:
|
||||
return value / 0.15
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum TemperatureUnit {
|
||||
case fahrenheit, celsius
|
||||
|
||||
var localizedDescription: [LocalizedStringKey] {
|
||||
switch self {
|
||||
case .fahrenheit:
|
||||
["fahrenheit", "f"]
|
||||
case .celsius:
|
||||
["celsius", "c"]
|
||||
}
|
||||
}
|
||||
|
||||
static func celsiusToFahrenheit(_ celsius: Double) -> Double {
|
||||
return celsius * 9.0 / 5.0 + 32.0
|
||||
}
|
||||
|
||||
static func fahrenheitToCelsius(_ fahrenheit: Double) -> Double {
|
||||
return (fahrenheit - 32.0) * 5.0 / 9.0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user