From 85d6ff7987582c66b3d85417928fc621298f3f43 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 22 Jun 2023 08:08:14 +0400 Subject: [PATCH] Update header for profile stories --- .../Sources/StoryContent.swift | 4 +- .../StoryItemSetContainerComponent.swift | 4 +- .../Sources/StoryChatContent.swift | 8 +- .../Sources/StoryPositionInfoComponent.swift | 88 +++++++++++++++++++ 4 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryPositionInfoComponent.swift diff --git a/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryContent.swift b/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryContent.swift index 3ddae3087e..e49dfa2213 100644 --- a/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryContent.swift +++ b/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryContent.swift @@ -60,7 +60,7 @@ public final class StoryContentItem { } public let id: AnyHashable - public let position: Int + public let position: Int? public let component: AnyComponent public let centerInfoComponent: AnyComponent? public let rightInfoComponent: AnyComponent? @@ -70,7 +70,7 @@ public final class StoryContentItem { public init( id: AnyHashable, - position: Int, + position: Int?, component: AnyComponent, centerInfoComponent: AnyComponent?, rightInfoComponent: AnyComponent?, diff --git a/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift b/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift index bb43c558f3..ea37453c28 100644 --- a/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift +++ b/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift @@ -2136,12 +2136,10 @@ public final class StoryItemSetContainerComponent: Component { self.ignoreScrolling = false self.updateScrolling(transition: transition) - if let focusedItem, let visibleItem = self.visibleItems[focusedItem.storyItem.id] { + if let focusedItem, let visibleItem = self.visibleItems[focusedItem.storyItem.id], let index = focusedItem.position { let navigationStripSideInset: CGFloat = 8.0 let navigationStripTopInset: CGFloat = 8.0 - let index = focusedItem.position - let _ = self.navigationStrip.update( transition: transition, component: AnyComponent(MediaNavigationStripComponent( diff --git a/submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryChatContent.swift b/submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryChatContent.swift index 3a805b6d12..aaa422da7b 100644 --- a/submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryChatContent.swift +++ b/submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryChatContent.swift @@ -1122,16 +1122,16 @@ public final class PeerStoryListContentContextImpl: StoryContentContext { additionalPeerData: additionalPeerData, item: StoryContentItem( id: AnyHashable(item.id), - position: focusedIndex, + position: nil, component: AnyComponent(StoryItemContentComponent( context: context, peer: peer, item: item )), - centerInfoComponent: AnyComponent(StoryAuthorInfoComponent( + centerInfoComponent: AnyComponent(StoryPositionInfoComponent( context: context, - peer: peer, - timestamp: item.timestamp + position: focusedIndex, + totalCount: state.totalCount )), rightInfoComponent: AnyComponent(StoryAvatarInfoComponent( context: context, diff --git a/submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryPositionInfoComponent.swift b/submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryPositionInfoComponent.swift new file mode 100644 index 0000000000..6cf01b4e34 --- /dev/null +++ b/submodules/TelegramUI/Components/Stories/StoryContentComponent/Sources/StoryPositionInfoComponent.swift @@ -0,0 +1,88 @@ +import Foundation +import UIKit +import Display +import ComponentFlow +import AccountContext +import TelegramCore +import TelegramStringFormatting + +final class StoryPositionInfoComponent: Component { + let context: AccountContext + let position: Int + let totalCount: Int + + init(context: AccountContext, position: Int, totalCount: Int) { + self.context = context + self.position = position + self.totalCount = totalCount + } + + static func ==(lhs: StoryPositionInfoComponent, rhs: StoryPositionInfoComponent) -> Bool { + if lhs.context !== rhs.context { + return false + } + if lhs.position != rhs.position { + return false + } + if lhs.totalCount != rhs.totalCount { + return false + } + return true + } + + final class View: UIView { + private let title = ComponentView() + + private var component: StoryPositionInfoComponent? + private weak var state: EmptyComponentState? + + override init(frame: CGRect) { + super.init(frame: frame) + + self.isUserInteractionEnabled = false + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func update(component: StoryPositionInfoComponent, availableSize: CGSize, state: EmptyComponentState, environment: Environment, transition: Transition) -> CGSize { + self.component = component + self.state = state + + let size = availableSize + + let presentationData = component.context.sharedContext.currentPresentationData.with({ $0 }) + + let position = max(0, min(component.position + 1, component.totalCount)) + let title = presentationData.strings.Items_NOfM("\(position)", "\(component.totalCount)").string + let titleSize = self.title.update( + transition: .immediate, + component: AnyComponent(Text(text: title, font: Font.with(size: 17.0, weight: .semibold, traits: .monospacedNumbers), color: .white)), + environment: {}, + containerSize: availableSize + ) + + let contentHeight: CGFloat = titleSize.height + let titleFrame = CGRect(origin: CGPoint(x: floor((availableSize.width - titleSize.width) * 0.5), y: floor((availableSize.height - contentHeight) * 0.5)), size: titleSize) + + if let titleView = self.title.view { + if titleView.superview == nil { + titleView.isUserInteractionEnabled = false + self.addSubview(titleView) + } + transition.setFrame(view: titleView, frame: titleFrame) + } + + return size + } + } + + func makeView() -> View { + return View(frame: CGRect()) + } + + func update(view: View, availableSize: CGSize, state: EmptyComponentState, environment: Environment, transition: Transition) -> CGSize { + return view.update(component: self, availableSize: availableSize, state: state, environment: environment, transition: transition) + } +}