From 0ec02a6f86c5515d426d97a1fbdd938d25360218 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Wed, 9 Aug 2023 06:51:01 +0200 Subject: [PATCH] Various fixes --- .../Sources/LimitsPageComponent.swift | 37 ++++++++++++-- .../Sources/PremiumIntroScreen.swift | 21 ++++++-- .../Sources/PremiumLimitsListScreen.swift | 50 +++++++++++++++---- .../Sources/StoriesPageComponent.swift | 30 +++++++++-- 4 files changed, 114 insertions(+), 24 deletions(-) diff --git a/submodules/PremiumUI/Sources/LimitsPageComponent.swift b/submodules/PremiumUI/Sources/LimitsPageComponent.swift index 9002cf506f..1a52d6db39 100644 --- a/submodules/PremiumUI/Sources/LimitsPageComponent.swift +++ b/submodules/PremiumUI/Sources/LimitsPageComponent.swift @@ -281,11 +281,13 @@ private final class LimitsListComponent: CombinedComponent { typealias EnvironmentType = (Empty, ScrollChildEnvironment) let context: AccountContext + let theme: PresentationTheme let topInset: CGFloat let bottomInset: CGFloat - init(context: AccountContext, topInset: CGFloat, bottomInset: CGFloat) { + init(context: AccountContext, theme: PresentationTheme, topInset: CGFloat, bottomInset: CGFloat) { self.context = context + self.theme = theme self.topInset = topInset self.bottomInset = bottomInset } @@ -294,6 +296,9 @@ private final class LimitsListComponent: CombinedComponent { if lhs.context !== rhs.context { return false } + if lhs.theme !== rhs.theme { + return false + } if lhs.topInset != rhs.topInset { return false } @@ -401,13 +406,17 @@ final class LimitsPageComponent: CombinedComponent { typealias EnvironmentType = DemoPageEnvironment let context: AccountContext + let theme: PresentationTheme + let neighbors: PageNeighbors let bottomInset: CGFloat let updatedBottomAlpha: (CGFloat) -> Void let updatedDismissOffset: (CGFloat) -> Void let updatedIsDisplaying: (Bool) -> Void - init(context: AccountContext, bottomInset: CGFloat, updatedBottomAlpha: @escaping (CGFloat) -> Void, updatedDismissOffset: @escaping (CGFloat) -> Void, updatedIsDisplaying: @escaping (Bool) -> Void) { + init(context: AccountContext, theme: PresentationTheme, neighbors: PageNeighbors, bottomInset: CGFloat, updatedBottomAlpha: @escaping (CGFloat) -> Void, updatedDismissOffset: @escaping (CGFloat) -> Void, updatedIsDisplaying: @escaping (Bool) -> Void) { self.context = context + self.theme = theme + self.neighbors = neighbors self.bottomInset = bottomInset self.updatedBottomAlpha = updatedBottomAlpha self.updatedDismissOffset = updatedDismissOffset @@ -418,6 +427,12 @@ final class LimitsPageComponent: CombinedComponent { if lhs.context !== rhs.context { return false } + if lhs.theme !== rhs.theme { + return false + } + if lhs.neighbors != rhs.neighbors { + return false + } if lhs.bottomInset != rhs.bottomInset { return false } @@ -456,6 +471,8 @@ final class LimitsPageComponent: CombinedComponent { } } + var neighbors = PageNeighbors(leftIsList: false, rightIsList: false) + init(updateBottomAlpha: @escaping (CGFloat) -> Void, updateDismissOffset: @escaping (CGFloat) -> Void, updateIsDisplaying: @escaping (Bool) -> Void) { self.updateBottomAlpha = updateBottomAlpha self.updateDismissOffset = updateDismissOffset @@ -465,8 +482,16 @@ final class LimitsPageComponent: CombinedComponent { } func updateAlpha() { - let dismissPosition = min(1.0, abs(self.position ?? 0.0) / 1.3333) - let position = min(1.0, abs(self.position ?? 0.0)) + var dismissToLeft = false + if let position = self.position, position > 0.0 { + dismissToLeft = true + } + var dismissPosition = min(1.0, abs(self.position ?? 0.0) / 1.3333) + var position = min(1.0, abs(self.position ?? 0.0)) + if position > 0.001, (dismissToLeft && self.neighbors.leftIsList) || (!dismissToLeft && self.neighbors.rightIsList) { + dismissPosition = 0.0 + position = 0.0 + } self.updateDismissOffset(dismissPosition) let verticalPosition = 1.0 - min(30.0, self.bottomContentOffset) / 30.0 @@ -493,11 +518,12 @@ final class LimitsPageComponent: CombinedComponent { let state = context.state let environment = context.environment[DemoPageEnvironment.self].value + state.neighbors = context.component.neighbors state.resetScroll = resetScroll state.position = environment.position state.isDisplaying = environment.isDisplaying - let theme = context.component.context.sharedContext.currentPresentationData.with { $0 }.theme + let theme = context.component.theme let strings = context.component.context.sharedContext.currentPresentationData.with { $0 }.strings let topInset: CGFloat = 56.0 @@ -507,6 +533,7 @@ final class LimitsPageComponent: CombinedComponent { content: AnyComponent( LimitsListComponent( context: context.component.context, + theme: context.component.theme, topInset: topInset, bottomInset: context.component.bottomInset ) diff --git a/submodules/PremiumUI/Sources/PremiumIntroScreen.swift b/submodules/PremiumUI/Sources/PremiumIntroScreen.swift index 1af8884c30..cb9f6def4f 100644 --- a/submodules/PremiumUI/Sources/PremiumIntroScreen.swift +++ b/submodules/PremiumUI/Sources/PremiumIntroScreen.swift @@ -1276,6 +1276,7 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent { let context: AccountContext let source: PremiumSource + let forceDark: Bool let isPremium: Bool? let justBought: Bool let otherPeerName: String? @@ -1291,6 +1292,7 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent { init( context: AccountContext, source: PremiumSource, + forceDark: Bool, isPremium: Bool?, justBought: Bool, otherPeerName: String?, @@ -1305,6 +1307,7 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent { ) { self.context = context self.source = source + self.forceDark = forceDark self.isPremium = isPremium self.justBought = justBought self.otherPeerName = otherPeerName @@ -1328,6 +1331,9 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent { if lhs.isPremium != rhs.isPremium { return false } + if lhs.forceDark != rhs.forceDark { + return false + } if lhs.justBought != rhs.justBought { return false } @@ -1718,7 +1724,8 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent { } } } - + + let forceDark = context.component.forceDark let layoutPerks = { var i = 0 var perksItems: [SectionGroupComponent.Item] = [] @@ -1780,9 +1787,8 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent { } let isPremium = state?.isPremium == true - var dismissImpl: (() -> Void)? - let controller = PremiumLimitsListScreen(context: accountContext, subject: demoSubject, source: .intro(state?.price), order: state?.configuration.perks, buttonText: isPremium ? strings.Common_OK : (state?.isAnnual == true ? strings.Premium_SubscribeForAnnual(state?.price ?? "—").string : strings.Premium_SubscribeFor(state?.price ?? "–").string), isPremium: isPremium) + let controller = PremiumLimitsListScreen(context: accountContext, subject: demoSubject, source: .intro(state?.price), order: state?.configuration.perks, buttonText: isPremium ? strings.Common_OK : (state?.isAnnual == true ? strings.Premium_SubscribeForAnnual(state?.price ?? "—").string : strings.Premium_SubscribeFor(state?.price ?? "–").string), isPremium: isPremium, forceDark: forceDark) controller.action = { [weak state] in dismissImpl?() if state?.isPremium == false { @@ -2002,14 +2008,16 @@ private final class PremiumIntroScreenComponent: CombinedComponent { let context: AccountContext let source: PremiumSource + let forceDark: Bool let updateInProgress: (Bool) -> Void let present: (ViewController) -> Void let push: (ViewController) -> Void let completion: () -> Void - init(context: AccountContext, source: PremiumSource, updateInProgress: @escaping (Bool) -> Void, present: @escaping (ViewController) -> Void, push: @escaping (ViewController) -> Void, completion: @escaping () -> Void) { + init(context: AccountContext, source: PremiumSource, forceDark: Bool, updateInProgress: @escaping (Bool) -> Void, present: @escaping (ViewController) -> Void, push: @escaping (ViewController) -> Void, completion: @escaping () -> Void) { self.context = context self.source = source + self.forceDark = forceDark self.updateInProgress = updateInProgress self.present = present self.push = push @@ -2023,6 +2031,9 @@ private final class PremiumIntroScreenComponent: CombinedComponent { if lhs.source != rhs.source { return false } + if lhs.forceDark != rhs.forceDark { + return false + } return true } @@ -2549,6 +2560,7 @@ private final class PremiumIntroScreenComponent: CombinedComponent { content: AnyComponent(PremiumIntroScreenContentComponent( context: context.component.context, source: context.component.source, + forceDark: context.component.forceDark, isPremium: state.isPremium, justBought: state.justBought, otherPeerName: state.otherPeerName, @@ -2782,6 +2794,7 @@ public final class PremiumIntroScreen: ViewControllerComponentContainer { super.init(context: context, component: PremiumIntroScreenComponent( context: context, source: source, + forceDark: forceDark, updateInProgress: { inProgress in updateInProgressImpl?(inProgress) }, diff --git a/submodules/PremiumUI/Sources/PremiumLimitsListScreen.swift b/submodules/PremiumUI/Sources/PremiumLimitsListScreen.swift index b339dde55a..4e7c1bb32a 100644 --- a/submodules/PremiumUI/Sources/PremiumLimitsListScreen.swift +++ b/submodules/PremiumUI/Sources/PremiumLimitsListScreen.swift @@ -349,6 +349,7 @@ public class PremiumLimitsListScreen: ViewController { self.updated(transition: transition) } + private var indexPosition: CGFloat? func updated(transition: Transition) { guard let controller = self.controller else { return @@ -384,12 +385,33 @@ public class PremiumLimitsListScreen: ViewController { let textColor = theme.actionSheet.primaryTextColor var availableItems: [PremiumPerk: DemoPagerComponent.Item] = [:] + + var storiesIndex: Int? + var limitsIndex: Int? + var storiesNeighbors = PageNeighbors(leftIsList: false, rightIsList: false) + var limitsNeighbors = PageNeighbors(leftIsList: false, rightIsList: false) + if let order = controller.order { + storiesIndex = order.firstIndex(where: { $0 == .stories }) + limitsIndex = order.firstIndex(where: { $0 == .doubleLimits }) + if let limitsIndex, let storiesIndex { + if limitsIndex == storiesIndex + 1 { + storiesNeighbors.rightIsList = true + limitsNeighbors.leftIsList = true + } else if limitsIndex == storiesIndex - 1 { + limitsNeighbors.rightIsList = true + storiesNeighbors.leftIsList = true + } + } + } + availableItems[.doubleLimits] = DemoPagerComponent.Item( AnyComponentWithIdentity( id: PremiumDemoScreen.Subject.doubleLimits, component: AnyComponent( LimitsPageComponent( context: context, + theme: self.presentationData.theme, + neighbors: limitsNeighbors, bottomInset: self.footerNode.frame.height, updatedBottomAlpha: { [weak self] alpha in if let strongSelf = self { @@ -402,8 +424,11 @@ public class PremiumLimitsListScreen: ViewController { } }, updatedIsDisplaying: { [weak self] isDisplaying in - if let strongSelf = self, strongSelf.isExpanded && !isDisplaying { - strongSelf.update(isExpanded: false, transition: .animated(duration: 0.2, curve: .easeInOut)) + if let self, self.isExpanded && !isDisplaying { + if let storiesIndex, let indexPosition = self.indexPosition, abs(CGFloat(storiesIndex) - indexPosition) < 0.1 { + } else { + self.update(isExpanded: false, transition: .animated(duration: 0.2, curve: .easeInOut)) + } } } ) @@ -417,6 +442,7 @@ public class PremiumLimitsListScreen: ViewController { StoriesPageComponent( context: context, theme: self.presentationData.theme, + neighbors: storiesNeighbors, bottomInset: self.footerNode.frame.height, updatedBottomAlpha: { [weak self] alpha in if let strongSelf = self { @@ -429,8 +455,11 @@ public class PremiumLimitsListScreen: ViewController { } }, updatedIsDisplaying: { [weak self] isDisplaying in - if let strongSelf = self, strongSelf.isExpanded && !isDisplaying { - strongSelf.update(isExpanded: false, transition: .animated(duration: 0.2, curve: .easeInOut)) + if let self, self.isExpanded && !isDisplaying { + if let limitsIndex, let indexPosition = self.indexPosition, abs(CGFloat(limitsIndex) - indexPosition) < 0.1 { + } else { + self.update(isExpanded: false, transition: .animated(duration: 0.2, curve: .easeInOut)) + } } } ) @@ -678,15 +707,15 @@ public class PremiumLimitsListScreen: ViewController { if let order = controller.order { var items: [DemoPagerComponent.Item] = order.compactMap { availableItems[$0] } - let index: Int + let initialIndex: Int switch controller.source { case .intro, .gift: - index = items.firstIndex(where: { (controller.subject as AnyHashable) == $0.content.id }) ?? 0 + initialIndex = items.firstIndex(where: { (controller.subject as AnyHashable) == $0.content.id }) ?? 0 case .other: items = items.filter { item in return item.content.id == (controller.subject as AnyHashable) } - index = 0 + initialIndex = 0 } let pagerSize = self.pagerView.update( @@ -694,11 +723,12 @@ public class PremiumLimitsListScreen: ViewController { component: AnyComponent( DemoPagerComponent( items: items, - index: index, + index: initialIndex, nextAction: nextAction, updated: { [weak self] position, count in - if let strongSelf = self { - strongSelf.footerNode.updatePosition(position, count: count) + if let self { + self.indexPosition = position * CGFloat(count) + self.footerNode.updatePosition(position, count: count) } } ) diff --git a/submodules/PremiumUI/Sources/StoriesPageComponent.swift b/submodules/PremiumUI/Sources/StoriesPageComponent.swift index 3a1b3d0b5c..a1369162ed 100644 --- a/submodules/PremiumUI/Sources/StoriesPageComponent.swift +++ b/submodules/PremiumUI/Sources/StoriesPageComponent.swift @@ -453,20 +453,26 @@ private final class StoriesListComponent: CombinedComponent { } } +struct PageNeighbors: Equatable { + var leftIsList: Bool + var rightIsList: Bool +} final class StoriesPageComponent: CombinedComponent { typealias EnvironmentType = DemoPageEnvironment let context: AccountContext let theme: PresentationTheme + let neighbors: PageNeighbors let bottomInset: CGFloat let updatedBottomAlpha: (CGFloat) -> Void let updatedDismissOffset: (CGFloat) -> Void let updatedIsDisplaying: (Bool) -> Void - init(context: AccountContext, theme: PresentationTheme, bottomInset: CGFloat, updatedBottomAlpha: @escaping (CGFloat) -> Void, updatedDismissOffset: @escaping (CGFloat) -> Void, updatedIsDisplaying: @escaping (Bool) -> Void) { + init(context: AccountContext, theme: PresentationTheme, neighbors: PageNeighbors, bottomInset: CGFloat, updatedBottomAlpha: @escaping (CGFloat) -> Void, updatedDismissOffset: @escaping (CGFloat) -> Void, updatedIsDisplaying: @escaping (Bool) -> Void) { self.context = context self.theme = theme + self.neighbors = neighbors self.bottomInset = bottomInset self.updatedBottomAlpha = updatedBottomAlpha self.updatedDismissOffset = updatedDismissOffset @@ -480,6 +486,9 @@ final class StoriesPageComponent: CombinedComponent { if lhs.theme !== rhs.theme { return false } + if lhs.neighbors != rhs.neighbors { + return false + } if lhs.bottomInset != rhs.bottomInset { return false } @@ -518,6 +527,8 @@ final class StoriesPageComponent: CombinedComponent { } } + var neighbors = PageNeighbors(leftIsList: false, rightIsList: false) + init(updateBottomAlpha: @escaping (CGFloat) -> Void, updateDismissOffset: @escaping (CGFloat) -> Void, updateIsDisplaying: @escaping (Bool) -> Void) { self.updateBottomAlpha = updateBottomAlpha self.updateDismissOffset = updateDismissOffset @@ -527,8 +538,16 @@ final class StoriesPageComponent: CombinedComponent { } func updateAlpha() { - let dismissPosition = min(1.0, abs(self.position ?? 0.0) / 1.3333) - let position = min(1.0, abs(self.position ?? 0.0)) + var dismissToLeft = false + if let position = self.position, position > 0.0 { + dismissToLeft = true + } + var dismissPosition = min(1.0, abs(self.position ?? 0.0) / 1.3333) + var position = min(1.0, abs(self.position ?? 0.0)) + if position > 0.001, (dismissToLeft && self.neighbors.leftIsList) || (!dismissToLeft && self.neighbors.rightIsList) { + dismissPosition = 0.0 + position = 1.0 + } self.updateDismissOffset(dismissPosition) let verticalPosition = 1.0 - min(30.0, self.bottomContentOffset) / 30.0 @@ -556,6 +575,7 @@ final class StoriesPageComponent: CombinedComponent { let state = context.state let environment = context.environment[DemoPageEnvironment.self].value + state.neighbors = context.component.neighbors state.resetScroll = resetScroll state.position = environment.position state.isDisplaying = environment.isDisplaying @@ -660,9 +680,9 @@ final class StoriesPageComponent: CombinedComponent { let titleBottomOriginY: CGFloat = 144.0 let titleOriginDelta = titleTopOriginY - titleBottomOriginY - let fraction = max(0.0, min(1.0, abs(state.topContentOffset / titleOriginDelta))) + let fraction = min(1.0, state.topContentOffset / abs(titleOriginDelta)) let titleOriginY: CGFloat = titleBottomOriginY + fraction * titleOriginDelta - let titleScale = 1.0 - fraction * 0.2 + let titleScale = 1.0 - max(0.0, fraction * 0.2) let titleAlpha: CGFloat if fraction > 0.78 {