-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d70573
commit 114a9d9
Showing
12 changed files
with
252 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// TabReselectTracker.swift | ||
// Mlem | ||
// | ||
// Created by Eric Andrews on 2023-11-02. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
@Observable | ||
class TabReselectTracker { | ||
private(set) var flag: Bool = false | ||
|
||
static var main: TabReselectTracker = .init() | ||
|
||
func signal() { | ||
flag = true | ||
} | ||
|
||
func reset() { | ||
flag = false | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
Mlem/App/Globals/EnvironmentValues/EnvironmentValues+TabReselectionHashValue.swift
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
Mlem/App/Globals/EnvironmentValues/EnvironmentValues+TabSelectionHashValue.swift
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
Mlem/App/Utility/Extensions/Views/View Modifiers/View+TabReselectConsumer.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,39 @@ | ||
// | ||
// View+TabReselectionConsumer.swift | ||
// Mlem | ||
// | ||
// Created by Eric Andrews on 2024-05-11. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct TabReselectionConsumer: ViewModifier { | ||
@Environment(TabReselectTracker.self) var tabReselectTracker | ||
|
||
/// Reselect actions should only trigger when the view is shown, so we track it with this | ||
@State var displayed: Bool = false | ||
|
||
var action: () -> Void | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.onChange(of: tabReselectTracker.flag) { | ||
// Only execute the action if: | ||
// - This view is currently displayed (this prevents it from triggering while in a different tab) | ||
// - Flag is true--combined with the reset() call below, this ensures that only one consumer will consume this action, preventing the behavior where a "dismiss" action also scrolls the previous page | ||
if displayed, tabReselectTracker.flag { | ||
tabReselectTracker.reset() | ||
action() | ||
} | ||
} | ||
.onAppear { displayed = true } | ||
.onDisappear { displayed = false } | ||
} | ||
} | ||
|
||
extension View { | ||
func onReselectTab(action: @escaping () -> Void) -> some View { | ||
modifier(TabReselectionConsumer(action: 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
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
Oops, something went wrong.