-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1748 from planetary-social/refactor-profile-view-…
…onappear Refactor ProfileView.onAppear code
- Loading branch information
Showing
11 changed files
with
96 additions
and
73 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import SwiftUI | ||
|
||
/// A view modifier that helps track when a tab becomes visible or invisible in a TabView. | ||
struct OnTabAppearModifier: ViewModifier { | ||
@EnvironmentObject private var router: Router | ||
let tab: AppDestination | ||
let onAppear: (() async -> Void)? | ||
let onDisappear: (() async -> Void)? | ||
|
||
@State private var isVisible = false | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.onAppear { | ||
if router.selectedTab == tab { | ||
isVisible = true | ||
} | ||
} | ||
.onDisappear { isVisible = false } | ||
.onChange(of: isVisible) { | ||
if isVisible { | ||
Task { await onAppear?() } | ||
} else { | ||
Task { await onDisappear?() } | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
/// Executes an action when a specific tab becomes visible | ||
/// - Parameters: | ||
/// - tab: The tab to monitor for visibility | ||
/// - action: The action to perform when the tab becomes visible | ||
func onTabAppear(_ tab: AppDestination, perform action: @escaping () async -> Void) -> some View { | ||
modifier(OnTabAppearModifier(tab: tab, onAppear: action, onDisappear: nil)) | ||
} | ||
|
||
/// Executes an action when a specific tab is navigated away from | ||
/// - Parameters: | ||
/// - tab: The tab to monitor for visibility | ||
/// - action: The action to perform when the tab becomes invisible | ||
func onTabDisappear(_ tab: AppDestination, perform action: @escaping () async -> Void) -> some View { | ||
modifier(OnTabAppearModifier(tab: tab, onAppear: nil, onDisappear: action)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
import Combine | ||
import SwiftUI | ||
import Dependencies | ||
|
||
/// A version of the ProfileView that is displayed in the main tab bar | ||
struct ProfileTab: View { | ||
|
||
@Environment(CurrentUser.self) var currentUser | ||
@EnvironmentObject private var router: Router | ||
@Dependency(\.analytics) private var analytics | ||
@ObservedObject var author: Author | ||
|
||
@Binding var path: NavigationPath | ||
|
||
var body: some View { | ||
NosNavigationStack(path: $path) { | ||
NosNavigationStack(path: $router.profilePath) { | ||
ProfileView(author: author, addDoubleTapToPop: true) | ||
.navigationBarItems(leading: SideMenuButton()) | ||
.onTabAppear(.profile) { | ||
analytics.showedProfileTab() | ||
} | ||
} | ||
} | ||
} |
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