Skip to content

Commit

Permalink
add options for measurements in settings view
Browse files Browse the repository at this point in the history
  • Loading branch information
Omar Hegazy authored and OmarHegazy93 committed Oct 27, 2024
1 parent c210590 commit 6c57c45
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Basic-Car-Maintenance/Shared/Localizable.xcstrings
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"sourceLanguage" : "en",
"strings" : {
"" : {

},
"%@" : {
"comment" : "Maintenance list item description 'Date' formatted",
"localizations" : {
Expand Down Expand Up @@ -2719,6 +2722,9 @@
}
}
},
"Imperial" : {
"comment" : "Imperial unit system"
},
"It's open source and anyone can contribute to it." : {
"comment" : "Tells the user they can contribute to the codebase.",
"localizations" : {
Expand Down Expand Up @@ -3101,6 +3107,9 @@
}
}
},
"Metric" : {
"comment" : "Metric unit system"
},
"Mileage: %lld %@" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -3853,6 +3862,9 @@
},
"Plate: %@" : {

},
"Preferred System" : {

},
"Preferred units" : {
"comment" : "Label for units selected when adding an odometer reading",
Expand Down Expand Up @@ -5164,6 +5176,9 @@
}
}
},
"Units" : {
"comment" : "Label to represent the options for measurement units"
},
"Update" : {
"comment" : "Label for submit button on form to update an existing entry",
"localizations" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ struct AddOdometerReadingView: View {
let vehicles: [Vehicle]
let addTapped: (OdometerReading) -> Void

@AppStorage(AppStorageKeys.measurementSystem)
private var defaultUnitSystem: MeasurementSystem = .default

@Environment(\.dismiss) var dismiss

@State private var date = Date()
@State private var selectedVehicleID: String?
@State private var isMetric = false
@State private var isMetric: Bool
@State private var distance = 0

init(
vehicles: [Vehicle],
addTapped: @escaping (OdometerReading) -> Void
) {
self.vehicles = vehicles
self.addTapped = addTapped
self.isMetric = _defaultUnitSystem.wrappedValue == .metric
}

var body: some View {
NavigationStack {
Form {
Expand Down Expand Up @@ -83,6 +95,9 @@ struct AddOdometerReadingView: View {
.disabled(distance < 0)
}
}
.onChange(of: defaultUnitSystem) { _, newValue in
isMetric = newValue == .metric
}
}
.analyticsView("\(Self.self)")
}
Expand Down
14 changes: 14 additions & 0 deletions Basic-Car-Maintenance/Shared/Settings/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import UniformTypeIdentifiers
import TipKit

struct SettingsView: View {
@AppStorage(AppStorageKeys.measurementSystem) private var defaultUnitSystem: MeasurementSystem = .default

@Environment(ActionService.self) var actionService
@Environment(\.scenePhase) var scenePhase
@Environment(\.colorScheme) var colorScheme
Expand Down Expand Up @@ -88,6 +90,18 @@ struct SettingsView: View {
.foregroundStyle(.blue)
}

Section {
Picker("Preferred System", selection: $defaultUnitSystem) {
ForEach(MeasurementSystem.allCases) { unit in
Text(unit.title)
.tag(unit)
}
}
.foregroundStyle(.blue)
} header: {
Text("Units", comment: "Label to represent the options for measurement units")
}

Section {
ForEach(viewModel.vehicles) { vehicle in
VStack(alignment: .leading, spacing: 2) {
Expand Down
11 changes: 11 additions & 0 deletions Basic-Car-Maintenance/Shared/Utilities/AppStorageKeys.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// AppStorageKeys.swift
// Basic-Car-Maintenance
//
// https://github.com/mikaelacaron/Basic-Car-Maintenance
// See LICENSE for license information.
//

enum AppStorageKeys {
static let measurementSystem = "defaultMeasurementSystem"
}
35 changes: 35 additions & 0 deletions Basic-Car-Maintenance/Shared/Utilities/MeasurementSystem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// MeasurementSystem.swift
// Basic-Car-Maintenance
//
// https://github.com/mikaelacaron/Basic-Car-Maintenance
// See LICENSE for license information.
//

import Foundation

enum MeasurementSystem: String, Identifiable, CaseIterable {
case imperial
case metric

var id: UUID { UUID() }
var title: String {
switch self {
case .imperial:
return NSLocalizedString("Imperial", comment: "Imperial unit system")
case .metric:
return NSLocalizedString("Metric", comment: "Metric unit system")
}
}

static var `default`: MeasurementSystem {
switch Locale.current.measurementSystem {
case .uk:
return .metric
case .us:
return .imperial
default:
return .metric
}
}
}

0 comments on commit 6c57c45

Please sign in to comment.