Skip to content

Commit

Permalink
add transition view while switching accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
mormaer committed Sep 17, 2023
1 parent e72cdce commit ed76da2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Mlem/Views/Shared/Accounts/Accounts Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct AccountsPage: View {
dismiss()
setFlow(using: account)
}
.disabled(isActiveAccount(account))
.swipeActions {
Button("Remove", role: .destructive) {
dismiss()
Expand Down Expand Up @@ -93,6 +94,11 @@ struct AccountsPage: View {
return account == currentAccount ? .secondary : .primary
}

private func isActiveAccount(_ account: SavedAccount) -> Bool {
guard let currentAccount = appState.currentActiveAccount else { return false }
return account == currentAccount
}

private func setFlow(using account: SavedAccount?) {
// this tiny delay prevents the modal dismiss animation from being cancelled
DispatchQueue.main.asyncAfter(deadline: .now() + 0.03) {
Expand Down
57 changes: 56 additions & 1 deletion Mlem/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,61 @@ struct Window: View {
}

private func setFlow(_ flow: AppFlow) {
self.flow = flow
transition(flow)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.flow = flow
}
}

/// This method changes the current application flow and places a _transition_ view across the active window while
/// - Parameter newFlow: The `AppFlow` that the application should transition into
private func transition(_ newFlow: AppFlow) {
struct TransitionView: View {
let text: String

var body: some View {
VStack(spacing: 24) {
ProgressView()
.controlSize(.large)
Text(text)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

let transitionText: String
switch newFlow {
case .onboarding:
transitionText = "See you soon 👋"
case let .account(account):
transitionText = "Welcome \(account.nickname) 🚀"
}

Task { @MainActor in

let transition = TransitionView(text: transitionText)
guard let transitionView = UIHostingController(rootView: transition).view,
let window = UIApplication.shared.firstKeyWindow else {
return
}

transitionView.alpha = 0
window.addSubview(transitionView)
UIView.animate(withDuration: 0.15) {
transitionView.alpha = 1
}

transitionView.translatesAutoresizingMaskIntoConstraints = false
transitionView.heightAnchor.constraint(equalTo: window.heightAnchor).isActive = true
transitionView.widthAnchor.constraint(equalTo: window.widthAnchor).isActive = true

DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
UIView.animate(withDuration: 0.3) {
transitionView.alpha = 0
} completion: { _ in
transitionView.removeFromSuperview()
}
}
}
}
}

0 comments on commit ed76da2

Please sign in to comment.