Skip to content

Commit

Permalink
fix: handle weird unresponsiveness on FavoriteView
Browse files Browse the repository at this point in the history
  • Loading branch information
uwaisalqadri committed Feb 1, 2025
1 parent e3c00a9 commit 91ad653
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 55 deletions.
23 changes: 22 additions & 1 deletion Giffy/App/Router/Routing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public final class Routing<T: RouterIdentifiable> {
public func push(_ route: T, animated: Bool = true, needValidate: Bool = false) {
guard !(needValidate && routes.last?.key == route.key) else { return }
routes.append(route)
print("Routing pushed: \(route)")
onPush?(route, animated)
}

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

Expand Down Expand Up @@ -131,7 +133,7 @@ struct NavigationControllerHost<T: RouterIdentifiable, Screen: View>: UIViewCont
router.onPresent = { route, animated in
let viewController = UIHostingController(rootView: routeMap(route))
viewController.modalPresentationStyle = .overFullScreen
navigation.present(viewController, animated: animated)
rootViewController?.present(viewController, animated: animated)
}

router.onPopLast = { numToPop, animated in
Expand All @@ -154,6 +156,25 @@ struct NavigationControllerHost<T: RouterIdentifiable, Screen: View>: UIViewCont
}
}
}

private var rootViewController: UIViewController? {
UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.compactMap { $0.windows.first { $0.isKeyWindow } }
.first?.rootViewController.flatMap(getTopViewController)
}

private func getTopViewController(from viewController: UIViewController) -> UIViewController {
if let presented = viewController.presentedViewController {
return getTopViewController(from: presented)
} else if let navigation = viewController as? UINavigationController {
return getTopViewController(from: navigation.visibleViewController ?? viewController)
} else if let tabBar = viewController as? UITabBarController {
return getTopViewController(from: tabBar.selectedViewController ?? viewController)
} else {
return viewController
}
}

private func setupInitialRoutes(in navigation: PopAwareUINavigationController) {
for path in router.currentRoutes {
Expand Down
82 changes: 51 additions & 31 deletions Giffy/Features/Favorite/FavoriteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ import CommonUI
import ComposableArchitecture

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

var body: some View {
WithViewStore(store, observe: { $0 }) { viewStore in
NavigationView {
ZStack {
ScrollView(.vertical, showsIndicators: false) {
SearchField { query in
viewStore.send(.fetch(request: query))
}
.padding(.horizontal, 16)
.padding(.vertical, 20)

.padding(.top, 62)

if viewStore.state.list.isEmpty {
FavoriteEmptyView()
.padding(.top, 50)
Expand Down Expand Up @@ -54,42 +56,60 @@ struct FavoriteView: View {
.animation(.easeInOut(duration: 0.2), value: viewStore.list.count)
.navigationBarBackButtonHidden(false)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
IconButton(
iconName: "chevron.left",
tint: .blue,
onClick: {
viewStore.send(.didBackPressed)
}
)
}

ToolbarItem(placement: .principal) {
Text(key: .titleFavorite)
.font(.bold, size: 16)
.showDialog(
shouldDismissOnTapOutside: true,
isShowing: viewStore.binding(
get: { $0.shareImage != nil },
send: .showShare(nil)
)
) {
ShareView(store: viewStore.share)
}
.onAppear {
viewStore.send(.fetch())
}
.onReceive(viewStore.state.detailDisappear) { _ in
viewStore.send(.fetch())
}

VStack {
FavoriteToolbar(title: Localizable.titleFavorite.tr()) {
pop()
}
Spacer()
}
}
.showDialog(
shouldDismissOnTapOutside: true,
isShowing: viewStore.binding(
get: { $0.shareImage != nil },
send: .showShare(nil)
)
) {
ShareView(store: viewStore.share)
}
.onAppear {
viewStore.send(.fetch())
}
.onReceive(viewStore.state.detailDisappear) { _ in
viewStore.send(.fetch())
}
}
}
}

struct FavoriteToolbar: View {
var title: String
var onBackPressed: () -> Void

var body: some View {
HStack {
IconButton(
iconName: "chevron.left",
tint: .Theme.red,
size: 26,
onClick: onBackPressed
)

Spacer()

Text(title)
.font(.system(size: 16, weight: .bold))

Spacer()

Spacer().frame(width: 32)
}
.padding(8)
.background(Blur(style: .prominent).edgesIgnoringSafeArea(.top))
}
}

#Preview {
FavoriteView(store: Injection.resolve())
}
29 changes: 13 additions & 16 deletions Giffy/Features/Home/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ struct RedirectButton: View {
.foregroundColor(.Theme.yellow)
.frame(width: 17, height: 17)
.padding(.all, 17)
.background(Color.Theme.background)
.clipShape(.circle)
.contentShape(.circle)
}.buttonStyle(.plain)
.background(Circle().fill(Color.Theme.background))
}
}
}

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

var body: some View {
Image(systemName: isFavorite ? "heart.fill" : "heart")
.resizable()
.foregroundColor(!isInverted ? Color.Theme.red : Color.white)
.frame(width: size.width - margin, height: size.height - margin - 4)
.background(
(!isInverted ? Color.Theme.background : Color.Theme.red)
.clipShape(Circle())
.frame(width: size.width, height: size.height)
)
.onTapGesture {
onClick()
}
Button(action: onClick) {
Image(systemName: isFavorite ? "heart.fill" : "heart")
.resizable()
.foregroundColor(!isInverted ? Color.Theme.red : Color.white)
.frame(width: size.width - margin, height: size.height - margin - 4)
.background(
(!isInverted ? Color.Theme.background : Color.Theme.red)
.clipShape(.circle)
.frame(width: size.width, height: size.height)
)
}
}

func frame(width: CGFloat, height: CGFloat) -> Self {
Expand Down
3 changes: 1 addition & 2 deletions Giffy/Features/Home/GiffyRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct GiffyRow: View {
FavoriteButton(isFavorite: $isFavorite, size: .init(width: 40, height: 40)) {
onFavorite(giphy)
}
.tapScaleEffect()
.padding(.trailing, 10)
}

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

@ViewBuilder
var footer: some View {
HStack {
if !giphy.title.isEmpty {
Expand All @@ -84,7 +84,6 @@ struct GiffyRow: View {
RedirectButton(onClick: {
onTapRow?(giphy)
})
.tapScaleEffect()
.showGiffyMenu(
URL(string: giphy.url),
data: downloadedImage,
Expand Down
2 changes: 1 addition & 1 deletion Giffy/Features/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct HomeView: View {
) {
ShareView(store: viewStore.share)
}
.onChange(of: viewStore.shareImage) { image, _ in
.onChange(of: viewStore.shareImage) { _, image in
tabState.isShowShare = image != nil
}
.onAppear {
Expand Down
2 changes: 1 addition & 1 deletion Giffy/Features/Search/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct SearchView: View {
) {
ShareView(store: viewStore.share)
}
.onChange(of: viewStore.shareImage) { image, _ in
.onChange(of: viewStore.shareImage) { _, image in
tabState.isShowShare = image != nil
}
.onAppear {
Expand Down
4 changes: 2 additions & 2 deletions Giffy/Features/Sticker/StickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ struct StickerView: View {
) {
ShareView(store: viewStore.state.share)
}
.onChange(of: viewStore.isCopied) { _, _ in
tabState.isShowShare = viewStore.isCopied
.onChange(of: viewStore.isCopied) { _, state in
tabState.isShowShare = state
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
"profile" = "Profile";
"search" = "Discover GIFs!";
"favorite" = "Favorite";
"favorite" = "Favorites";
"trending" = "Trending";
"detail" = "Detail";
"today_popular" = "Today's Trending";
Expand Down

0 comments on commit 91ad653

Please sign in to comment.