Skip to content

Commit

Permalink
Extracts a new control from the Favorites section of the OriginDestin…
Browse files Browse the repository at this point in the history
…ationSheetView

This change DRYs up the Favorites section a bit
  • Loading branch information
aaronbrethorst committed Jul 31, 2024
1 parent fafc43b commit 007e48f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
2 changes: 0 additions & 2 deletions OTPKit/Controls/SectionHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 45 additions & 0 deletions OTPKit/Features/OriginDestination/FavoriteView.swift
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
10 changes: 10 additions & 0 deletions OTPKit/Miscellaneous/Utilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Utilities.swift
// OTPKit
//
// Created by Aaron Brethorst on 7/31/24.
//

import Foundation

typealias VoidBlock = () -> Void

0 comments on commit 007e48f

Please sign in to comment.