Skip to content

Commit

Permalink
feat: change sheet height dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnhon committed May 24, 2024
1 parent 43461cb commit 29b7645
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
8 changes: 4 additions & 4 deletions iOS/Scenes/Homepage/HomepageCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 813,13 @@ extension HomepageCoordinator {
}

func publicLink(item: ItemContent) {
let view = PublicLinkView(viewModel: .init(itemContent: item))
let viewModel = PublicLinkViewModel(itemContent: item)
let view = PublicLinkView(viewModel: viewModel)
let viewController = UIHostingController(rootView: view)

viewController.setDetentType(.medium,
viewController.setDetentType(.custom(PublicLinkViewModelState.default.sheetHeight),
parentViewController: rootViewController)

viewController.sheetPresentationController?.prefersGrabberVisible = true
viewModel.sheetPresentation = viewController.sheetPresentationController
present(viewController)
}

Expand Down
1 change: 0 additions & 1 deletion iOS/Scenes/Sharing/PublicLinkView/PublicLinkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 55,6 @@ private extension PublicLinkView {
.showSpinner(viewModel.loading)
.animation(.default, value: viewModel.link)
.animation(.default, value: viewModel.viewCount)
.scrollViewEmbeded(maxWidth: .infinity)
.background(PassColor.backgroundNorm.toColor)
.toolbar { toolbarContent }
}
Expand Down
51 changes: 51 additions & 0 deletions iOS/Scenes/Sharing/PublicLinkView/PublicLinkViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,7 @@ import Entities
import Factory
import Foundation
import Macro
import UIKit

enum SecureLinkExpiration: Sendable, Hashable, Identifiable {
case hour(Int)
Expand Down Expand Up @@ -55,20 56,70 @@ enum SecureLinkExpiration: Sendable, Hashable, Identifiable {
}
}

enum PublicLinkViewModelState {
case creationWithoutRestriction
case creationWithRestriction
case created

var sheetHeight: CGFloat {
switch self {
case .creationWithoutRestriction:
280
case .creationWithRestriction:
350
case .created:
250
}
}

static var `default`: Self { .creationWithoutRestriction }
}

@MainActor
final class PublicLinkViewModel: ObservableObject, Sendable {
@Published private(set) var link: SharedPublicLink?
@Published var selectedExpiration: SecureLinkExpiration = .day(7)
@Published var loading = false
@Published var viewCount = 0

private var state = PassthroughSubject<PublicLinkViewModelState, Never>()
private var cancellables = Set<AnyCancellable>()

let router = resolve(\SharedRouterContainer.mainUIKitSwiftUIRouter)
let createItemSharingPublicLink = resolve(\SharedUseCasesContainer.createItemSharingPublicLink)

let itemContent: ItemContent
weak var sheetPresentation: UISheetPresentationController?

init(itemContent: ItemContent) {
self.itemContent = itemContent

Publishers.CombineLatest($link, $viewCount)
.sink { [weak self] link, count in
guard let self else { return }
if link != nil {
state.send(.created)
} else if count == 0 { // swiftlint:disable:this empty_count
state.send(.creationWithoutRestriction)
} else {
state.send(.creationWithRestriction)
}
}
.store(in: &cancellables)

state
.receive(on: DispatchQueue.main)
.sink { [weak self] state in
guard let self else { return }
let detent = UISheetPresentationController.Detent.custom { _ in
CGFloat(state.sheetHeight)
}
sheetPresentation?.animateChanges { [weak self] in
guard let self else { return }
sheetPresentation?.detents = [detent]
}
}
.store(in: &cancellables)
}

func createLink() {
Expand Down

0 comments on commit 29b7645

Please sign in to comment.