-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure TabIdentifiers are matched when switching between container ki…
…nds, and add ability to manually map identifiers between modes
- Loading branch information
Showing
6 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Example/AdaptiveTabExample/AdaptiveTabExample/Tabs/FolderTabView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// FolderTabView.swift | ||
// AdaptiveTabExample | ||
// | ||
// Created by Mark DiFranco on 2023-05-02. | ||
// | ||
|
||
import SwiftUI | ||
import AdaptiveTabView | ||
|
||
extension FolderTabView { | ||
static let identifier = TabIdentifier("FolderTabView") | ||
} | ||
|
||
struct FolderTabView: View, TitleImageProviding { | ||
let title = "Folders" | ||
let systemImageName = "folder" | ||
let id = FolderTabView.identifier | ||
|
||
private let identifiers = [1, 2, 3, 4, 5] | ||
|
||
var body: some View { | ||
List { | ||
Section { | ||
ForEach(identifiers, id: \.self) { (identifier) in | ||
NavigationLink { | ||
ContentView(title: "Folder \(identifier)") | ||
} label: { | ||
Label("Folder \(identifier)", systemImage: "folder") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct FolderTabView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
FolderTabView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
Sources/AdaptiveTabView/Environment/SelectedTabTransformer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// SelectedTabTransformer.swift | ||
// | ||
// | ||
// Created by Mark DiFranco on 2023-05-02. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// A struct meant to hold transformation logic for the selected tab when the ``AdaptiveTabView`` switches between different ``AdaptiveTabViewContainerKind``s. | ||
public struct SelectedTabTransformer { | ||
/// A closure to use to transform ``TabIdentifier``s between the different ``AdaptiveTabViewContainerKind``s. | ||
/// - parameter toKind: The container kind that the ``AdaptiveTabView`` will transform into. | ||
/// - parameter tabIdentifier: The ``TabIdentifier`` currently selected in the previous ``AdaptiveTabViewContainerKind``. | ||
public let transformer: (_ toKind: AdaptiveTabViewContainerKind, _ tabIdentifier: TabIdentifier) -> TabIdentifier | ||
|
||
public init(transformer: @escaping (AdaptiveTabViewContainerKind, TabIdentifier) -> TabIdentifier) { | ||
self.transformer = transformer | ||
} | ||
} | ||
|
||
extension SelectedTabTransformer: EnvironmentKey { | ||
public static var defaultValue = SelectedTabTransformer { (sizeClass, tabIdentifier) in | ||
return tabIdentifier | ||
} | ||
} | ||
|
||
public extension EnvironmentValues { | ||
/// An environment value that holds logic for transforming the selected tab in an ``AdaptiveTabView``. | ||
var selectedTabTransformer: SelectedTabTransformer { | ||
get {self[SelectedTabTransformer.self]} | ||
set {self[SelectedTabTransformer.self] = newValue} | ||
} | ||
} | ||
|
||
public extension View { | ||
/// Provide a transformer for converting the selected tab in an ``AdaptiveTabView`` when switching between ``AdaptiveTabViewContainerKind``s. | ||
/// - parameter transformer: The transformer to use when converting the selected tab. | ||
func selectedTabTransformer(_ transformer: SelectedTabTransformer) -> some View { | ||
environment(\.selectedTabTransformer, transformer) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters