Skip to content

Commit 91ad653

Browse files
committed
fix: handle weird unresponsiveness on FavoriteView
1 parent e3c00a9 commit 91ad653

File tree

8 files changed

+92
-55
lines changed

8 files changed

+92
-55
lines changed

Giffy/App/Router/Routing.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ public final class Routing<T: RouterIdentifiable> {
3737
public func push(_ route: T, animated: Bool = true, needValidate: Bool = false) {
3838
guard !(needValidate && routes.last?.key == route.key) else { return }
3939
routes.append(route)
40+
print("Routing pushed: \(route)")
4041
onPush?(route, animated)
4142
}
4243

4344
public func present(_ route: T, animated: Bool = true, needValidate: Bool = false) {
4445
guard !(needValidate && routes.last?.key == route.key) else { return }
4546
routes.append(route)
47+
print("Routing presented: \(route)")
4648
onPresent?(route, animated)
4749
}
4850

@@ -131,7 +133,7 @@ struct NavigationControllerHost<T: RouterIdentifiable, Screen: View>: UIViewCont
131133
router.onPresent = { route, animated in
132134
let viewController = UIHostingController(rootView: routeMap(route))
133135
viewController.modalPresentationStyle = .overFullScreen
134-
navigation.present(viewController, animated: animated)
136+
rootViewController?.present(viewController, animated: animated)
135137
}
136138

137139
router.onPopLast = { numToPop, animated in
@@ -154,6 +156,25 @@ struct NavigationControllerHost<T: RouterIdentifiable, Screen: View>: UIViewCont
154156
}
155157
}
156158
}
159+
160+
private var rootViewController: UIViewController? {
161+
UIApplication.shared.connectedScenes
162+
.compactMap { $0 as? UIWindowScene }
163+
.compactMap { $0.windows.first { $0.isKeyWindow } }
164+
.first?.rootViewController.flatMap(getTopViewController)
165+
}
166+
167+
private func getTopViewController(from viewController: UIViewController) -> UIViewController {
168+
if let presented = viewController.presentedViewController {
169+
return getTopViewController(from: presented)
170+
} else if let navigation = viewController as? UINavigationController {
171+
return getTopViewController(from: navigation.visibleViewController ?? viewController)
172+
} else if let tabBar = viewController as? UITabBarController {
173+
return getTopViewController(from: tabBar.selectedViewController ?? viewController)
174+
} else {
175+
return viewController
176+
}
177+
}
157178

158179
private func setupInitialRoutes(in navigation: PopAwareUINavigationController) {
159180
for path in router.currentRoutes {

Giffy/Features/Favorite/FavoriteView.swift

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ import CommonUI
1313
import ComposableArchitecture
1414

1515
struct FavoriteView: View {
16+
@Environment(\.dismiss) var pop
1617
let store: StoreOf<FavoriteReducer>
1718

1819
var body: some View {
1920
WithViewStore(store, observe: { $0 }) { viewStore in
20-
NavigationView {
21+
ZStack {
2122
ScrollView(.vertical, showsIndicators: false) {
2223
SearchField { query in
2324
viewStore.send(.fetch(request: query))
2425
}
2526
.padding(.horizontal, 16)
2627
.padding(.vertical, 20)
27-
28+
.padding(.top, 62)
29+
2830
if viewStore.state.list.isEmpty {
2931
FavoriteEmptyView()
3032
.padding(.top, 50)
@@ -54,42 +56,60 @@ struct FavoriteView: View {
5456
.animation(.easeInOut(duration: 0.2), value: viewStore.list.count)
5557
.navigationBarBackButtonHidden(false)
5658
.navigationBarTitleDisplayMode(.inline)
57-
.toolbar {
58-
ToolbarItem(placement: .topBarLeading) {
59-
IconButton(
60-
iconName: "chevron.left",
61-
tint: .blue,
62-
onClick: {
63-
viewStore.send(.didBackPressed)
64-
}
65-
)
66-
}
67-
68-
ToolbarItem(placement: .principal) {
69-
Text(key: .titleFavorite)
70-
.font(.bold, size: 16)
59+
.showDialog(
60+
shouldDismissOnTapOutside: true,
61+
isShowing: viewStore.binding(
62+
get: { $0.shareImage != nil },
63+
send: .showShare(nil)
64+
)
65+
) {
66+
ShareView(store: viewStore.share)
67+
}
68+
.onAppear {
69+
viewStore.send(.fetch())
70+
}
71+
.onReceive(viewStore.state.detailDisappear) { _ in
72+
viewStore.send(.fetch())
73+
}
74+
75+
VStack {
76+
FavoriteToolbar(title: Localizable.titleFavorite.tr()) {
77+
pop()
7178
}
79+
Spacer()
7280
}
7381
}
74-
.showDialog(
75-
shouldDismissOnTapOutside: true,
76-
isShowing: viewStore.binding(
77-
get: { $0.shareImage != nil },
78-
send: .showShare(nil)
79-
)
80-
) {
81-
ShareView(store: viewStore.share)
82-
}
83-
.onAppear {
84-
viewStore.send(.fetch())
85-
}
86-
.onReceive(viewStore.state.detailDisappear) { _ in
87-
viewStore.send(.fetch())
88-
}
8982
}
9083
}
9184
}
9285

86+
struct FavoriteToolbar: View {
87+
var title: String
88+
var onBackPressed: () -> Void
89+
90+
var body: some View {
91+
HStack {
92+
IconButton(
93+
iconName: "chevron.left",
94+
tint: .Theme.red,
95+
size: 26,
96+
onClick: onBackPressed
97+
)
98+
99+
Spacer()
100+
101+
Text(title)
102+
.font(.system(size: 16, weight: .bold))
103+
104+
Spacer()
105+
106+
Spacer().frame(width: 32)
107+
}
108+
.padding(8)
109+
.background(Blur(style: .prominent).edgesIgnoringSafeArea(.top))
110+
}
111+
}
112+
93113
#Preview {
94114
FavoriteView(store: Injection.resolve())
95115
}

Giffy/Features/Home/Button.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ struct RedirectButton: View {
3636
.foregroundColor(.Theme.yellow)
3737
.frame(width: 17, height: 17)
3838
.padding(.all, 17)
39-
.background(Color.Theme.background)
40-
.clipShape(.circle)
41-
.contentShape(.circle)
42-
}.buttonStyle(.plain)
39+
.background(Circle().fill(Color.Theme.background))
40+
}
4341
}
4442
}
4543

@@ -51,18 +49,17 @@ struct FavoriteButton: View {
5149
var onClick: () -> Void
5250

5351
var body: some View {
54-
Image(systemName: isFavorite ? "heart.fill" : "heart")
55-
.resizable()
56-
.foregroundColor(!isInverted ? Color.Theme.red : Color.white)
57-
.frame(width: size.width - margin, height: size.height - margin - 4)
58-
.background(
59-
(!isInverted ? Color.Theme.background : Color.Theme.red)
60-
.clipShape(Circle())
61-
.frame(width: size.width, height: size.height)
62-
)
63-
.onTapGesture {
64-
onClick()
65-
}
52+
Button(action: onClick) {
53+
Image(systemName: isFavorite ? "heart.fill" : "heart")
54+
.resizable()
55+
.foregroundColor(!isInverted ? Color.Theme.red : Color.white)
56+
.frame(width: size.width - margin, height: size.height - margin - 4)
57+
.background(
58+
(!isInverted ? Color.Theme.background : Color.Theme.red)
59+
.clipShape(.circle)
60+
.frame(width: size.width, height: size.height)
61+
)
62+
}
6663
}
6764

6865
func frame(width: CGFloat, height: CGFloat) -> Self {

Giffy/Features/Home/GiffyRow.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ struct GiffyRow: View {
4949
FavoriteButton(isFavorite: $isFavorite, size: .init(width: 40, height: 40)) {
5050
onFavorite(giphy)
5151
}
52-
.tapScaleEffect()
5352
.padding(.trailing, 10)
5453
}
5554

@@ -64,6 +63,7 @@ struct GiffyRow: View {
6463
.animation(.linear(duration: 0.2), value: isSelected)
6564
}
6665

66+
@ViewBuilder
6767
var footer: some View {
6868
HStack {
6969
if !giphy.title.isEmpty {
@@ -84,7 +84,6 @@ struct GiffyRow: View {
8484
RedirectButton(onClick: {
8585
onTapRow?(giphy)
8686
})
87-
.tapScaleEffect()
8887
.showGiffyMenu(
8988
URL(string: giphy.url),
9089
data: downloadedImage,

Giffy/Features/Home/HomeView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct HomeView: View {
7878
) {
7979
ShareView(store: viewStore.share)
8080
}
81-
.onChange(of: viewStore.shareImage) { image, _ in
81+
.onChange(of: viewStore.shareImage) { _, image in
8282
tabState.isShowShare = image != nil
8383
}
8484
.onAppear {

Giffy/Features/Search/SearchView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct SearchView: View {
8484
) {
8585
ShareView(store: viewStore.share)
8686
}
87-
.onChange(of: viewStore.shareImage) { image, _ in
87+
.onChange(of: viewStore.shareImage) { _, image in
8888
tabState.isShowShare = image != nil
8989
}
9090
.onAppear {

Giffy/Features/Sticker/StickerView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ struct StickerView: View {
6969
) {
7070
ShareView(store: viewStore.state.share)
7171
}
72-
.onChange(of: viewStore.isCopied) { _, _ in
73-
tabState.isShowShare = viewStore.isCopied
72+
.onChange(of: viewStore.isCopied) { _, state in
73+
tabState.isShowShare = state
7474
}
7575
.navigationBarTitleDisplayMode(.inline)
7676
.toolbar {

Modules/CommonUI/CommonUI/Sources/CommonUI/Assets/en.lproj/Localizable.strings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
"profile" = "Profile";
99
"search" = "Discover GIFs!";
10-
"favorite" = "Favorite";
10+
"favorite" = "Favorites";
1111
"trending" = "Trending";
1212
"detail" = "Detail";
1313
"today_popular" = "Today's Trending";

0 commit comments

Comments
 (0)