-2

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: "dbjdb@dedde.de", 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
    }
}

}

SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
  • Please see this... https://stackoverflow.com/questions/56595542/use-navigationbutton-with-a-server-request-in-swiftui You can't do this... – Fogmeister Jun 14 '19 at 13:30

1 Answers1

1

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()
        )
    }
}
Ugo Arangino
  • 2,802
  • 1
  • 18
  • 19
  • a little bit too complicated for such a basic feature apple... Thanks – SwiftiSwift Jun 14 '19 at 17:43
  • 2
    Why do you think it is to complicated. Apple offers us a other way to create UIs. But this other way requires a (total) different thinking. So *think different*. After learning the new paradigms we can enjoy the easiness (and hopefully also the simplicity) of declarative UIs. – Ugo Arangino Jun 14 '19 at 23:36
  • somehow I can't set the showModal property to true when data by the api is received in SwiftUI. See question again – SwiftiSwift Jun 14 '19 at 23:57
  • can you have a look? – SwiftiSwift Jun 15 '19 at 14:48