From 007e48fe5d72adf6291710c0ea1f6b83f359e278 Mon Sep 17 00:00:00 2001 From: Aaron Brethorst Date: Wed, 31 Jul 2024 15:13:36 -0700 Subject: [PATCH] Extracts a new control from the Favorites section of the OriginDestinationSheetView This change DRYs up the Favorites section a bit --- OTPKit/Controls/SectionHeaderView.swift | 2 - .../OriginDestination/FavoriteView.swift | 45 +++++++++++++++++++ .../Sheets/OriginDestinationSheetView.swift | 34 ++------------ OTPKit/Miscellaneous/Utilities.swift | 10 +++++ 4 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 OTPKit/Features/OriginDestination/FavoriteView.swift create mode 100644 OTPKit/Miscellaneous/Utilities.swift diff --git a/OTPKit/Controls/SectionHeaderView.swift b/OTPKit/Controls/SectionHeaderView.swift index 7708aa0..3a9a33c 100644 --- a/OTPKit/Controls/SectionHeaderView.swift +++ b/OTPKit/Controls/SectionHeaderView.swift @@ -7,8 +7,6 @@ import SwiftUI -typealias VoidBlock = () -> Void - /// The view that appears above a section on the `OriginDestinationSheetView`. /// For instance, the header for the Recents and Favorites sections. struct SectionHeaderView: View { diff --git a/OTPKit/Features/OriginDestination/FavoriteView.swift b/OTPKit/Features/OriginDestination/FavoriteView.swift new file mode 100644 index 0000000..7a6c181 --- /dev/null +++ b/OTPKit/Features/OriginDestination/FavoriteView.swift @@ -0,0 +1,45 @@ +// +// FavoriteView.swift +// OTPKit +// +// Created by Aaron Brethorst on 7/31/24. +// + +import SwiftUI + +/// A button that wraps a circle with an icon above a line of text. +struct FavoriteView: View { + private let title: String + private let imageName: String + private let action: VoidBlock? + + init(title: String, imageName: String, action: VoidBlock? = nil) { + self.title = title + self.imageName = imageName + self.action = action + } + var body: some View { + Button(action: { + action?() + }, label: { + VStack(alignment: .center) { + Image(systemName: imageName) + .frame(width: 48, height: 48) + .background(Color.gray.opacity(0.5)) + .clipShape(Circle()) + + Text(title) + .font(.caption) + .frame(width: 64) + .lineLimit(1) + .truncationMode(.tail) + } + .padding(.all, 4) + .foregroundStyle(.black) + }) + } +} + +#Preview { + FavoriteView(title: "Hello, world!", imageName: "mappin") +} diff --git a/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift b/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift index 7c4a308..35e3741 100644 --- a/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift +++ b/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift @@ -61,41 +61,15 @@ public struct OriginDestinationSheetView: View { ScrollView(.horizontal) { HStack { ForEach(sheetEnvironment.favoriteLocations, content: { location in - Button(action: { + FavoriteView(title: location.title, imageName: "mappin") { sheetEnvironment.selectedDetailFavoriteLocation = location isFavoriteLocationDetailSheetOpen.toggle() - }, label: { - VStack(alignment: .center) { - Image(systemName: "mappin") - .frame(width: 48, height: 48) - .background(Color.gray.opacity(0.5)) - .clipShape(Circle()) - - Text(location.title) - .frame(width: 64) - .lineLimit(1) - .truncationMode(.tail) - } - .padding(.all, 4) - .foregroundStyle(Color.black) - }) - + } }) - Button(action: { + FavoriteView(title: "Add", imageName: "plus") { isAddSavedLocationsSheetOpen.toggle() - }, label: { - VStack { - Image(systemName: "plus") - .frame(width: 48, height: 48) - .background(Color.gray.opacity(0.5)) - .clipShape(Circle()) - - Text("Add") - .foregroundStyle(Color.black) - } - .padding(.all, 4) - }) + } } } }, header: { diff --git a/OTPKit/Miscellaneous/Utilities.swift b/OTPKit/Miscellaneous/Utilities.swift new file mode 100644 index 0000000..c97ae8c --- /dev/null +++ b/OTPKit/Miscellaneous/Utilities.swift @@ -0,0 +1,10 @@ +// +// Utilities.swift +// OTPKit +// +// Created by Aaron Brethorst on 7/31/24. +// + +import Foundation + +typealias VoidBlock = () -> Void