Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

ios - Async Next Screen Presentation in SwiftUI

i want to signup users when they click the signup button in my app. When the signup is complete and successfully created a user on the server side I want to present the next screen and only then.

In normal way I have a PresentationButton and set the destination and when somebody clicked the button the next screen is presented directly, but now it's async.

How to handle that?

Currently I have this PresentationButton:

PresentationButton(
                Text(isSignIn ? "SignIn" : "Next").font(.headline).bold()
                    .frame(width: 100)
                    .padding(10)
                    .foregroundColor(.white)
                    .background(Color.blue)
                    .cornerRadius(20)
                , destination: HomeScreen()
            )

That's with the suggestion by Uro Arangino:

struct PasswordView : View {
    @State private var password = ""
    @State private var showNextScreen = false

    var loginMode: LoginType

    @ObjectBinding var signupManager = SignUpManager()

    var body: some View {
        VStack {
//            if self.signupManager.showModal { self.showNextScreen.toggle() } <- Can't do this

            TwitterNavigationView(backBtnOn: false)
            middleView()
            Spacer()
            bottomView()
        }
    }

    func bottomView() -> some View {
        return VStack(alignment: .trailing) {
            Divider()
            BindedPresentationButton(
                showModal: $showNextScreen,
                label: getCustomText((Text("Registrieren"))),
                destination: HomeTabView(),
                onTrigger: {
                    let user = User(name: "rezo", username: "ja lol ey", profileDescription: "YAS", email: "[email protected]", telephoneNumber: nil, profileImage: UIImage(named: "twitter-logo")!, bannerImage: UIImage(systemName: "star")!)
                    self.signupManager.signIn(forUser: user, password: "ultraSecure", loginMode: .email)
//                UserDefaults.standard.set(true, forKey: "loggedIn")
            })
        }.padding([.leading, .trailing]).padding(.top, 5).padding(.bottom, 10)
    }
}

My bindable object class

final class SignUpManager: BindableObject {
let didChange = PassthroughSubject<SignUpManager, Never>()

var showModal: Bool = false {
    didSet {
        didChange.send(self)
    }
}

func signIn(forUser user: User, password: String, loginMode: LoginType) {
    if loginMode == .email {
        LoginService.instance.signupWithEmail(user: user, andPassword: password, completion: handleCompletion)
    } else {
        LoginService.instance.login(withPhoneNumber: user.telephoneNumber!, completion: handleCompletion)
    }
}

private func handleCompletion(_ status: Bool) {
    if status {
        showModal = true
    }
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a presentation button with a binding.

See: https://stackoverflow.com/a/56547016/3716612

struct ContentView: View {
    @State var showModal = false

    var body: some View {
        BindedPresentationButton(
            showModal: $isSignIn,
            label: Text(isSignIn ? "SignIn" : "Next")
                .font(.headline)
                .bold()
                .frame(width: 100)
                .padding(10)
                .foregroundColor(.white)
                .background(Color.blue)
                .cornerRadius(20),
            destination: HomeScreen()
        )
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...