Skip to content

Commit

Permalink
Add options for measurements in Settings view (#364)
Browse files Browse the repository at this point in the history
* add options for measurements in settings view

* PR Suggestions

Moving around the section for the units and small fixes in the MeasurementSystem

---------

Co-authored-by: Omar Hegazy <[email protected]>
Co-authored-by: Mikaela Caron <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2024
1 parent c210590 commit beb4704
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
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 = .userDefault

@Environment(\.dismiss) var dismiss

@State private var date = Date()
@State private var selectedVehicleID: String?
@State private var isMetric = false
@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
15 changes: 15 additions & 0 deletions Basic-Car-Maintenance/Shared/Settings/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ struct SettingsView: View {

@ScaledMetric(relativeTo: .largeTitle) var iconDimension = 20.0

// swiftlint:disable:next line_length
@AppStorage(AppStorageKeys.measurementSystem) private var defaultUnitSystem: MeasurementSystem = .userDefault

@State private var viewModel: SettingsViewModel
@State private var isShowingAddVehicle = false
@State private var showDeleteVehicleError = false
Expand Down Expand Up @@ -163,6 +166,18 @@ struct SettingsView: View {
Text("Vehicles", comment: "Label to display header title.")
}

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 {
NavigationLink {
AuthenticationView(viewModel: viewModel.authenticationViewModel)
Expand Down
4 changes: 4 additions & 0 deletions Basic-Car-Maintenance/Shared/Utilities/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ enum SFSymbol {

}

enum AppStorageKeys {
static let measurementSystem = "defaultMeasurementSystem"
}

// swiftlint:enable line_length
42 changes: 42 additions & 0 deletions Basic-Car-Maintenance/Shared/Utilities/MeasurementSystem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// MeasurementSystem.swift
// Basic-Car-Maintenance
//
// https://github.com/mikaelacaron/Basic-Car-Maintenance
// See LICENSE for license information.
//

import SwiftUI

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

var id: String {
return rawValue
}

var title: LocalizedStringResource {
switch self {
case .imperial:
return LocalizedStringResource(
"Imperial",
defaultValue: "Imperial",
comment: "Imperial unit system"
)
case .metric:
return LocalizedStringResource("Metric", defaultValue: "Metric", comment: "Metric unit system")
}
}

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

0 comments on commit beb4704

Please sign in to comment.