|
| 1 | +package com.azuresamples.msalnativeauthandroidkotlinsampleapp |
| 2 | + |
| 3 | +import android.app.AlertDialog |
| 4 | +import android.os.Bundle |
| 5 | +import android.view.LayoutInflater |
| 6 | +import android.view.View |
| 7 | +import android.view.ViewGroup |
| 8 | +import android.widget.Toast |
| 9 | +import androidx.fragment.app.Fragment |
| 10 | +import com.azuresamples.msalnativeauthandroidkotlinsampleapp.databinding.FragmentSignUpAttributesBinding |
| 11 | +import com.microsoft.identity.nativeauth.UserAttributes |
| 12 | +import com.microsoft.identity.nativeauth.parameters.NativeAuthSignInContinuationParameters |
| 13 | +import com.microsoft.identity.nativeauth.statemachine.errors.SignInContinuationError |
| 14 | +import com.microsoft.identity.nativeauth.statemachine.errors.SignUpSubmitAttributesError |
| 15 | +import com.microsoft.identity.nativeauth.statemachine.results.SignInResult |
| 16 | +import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult |
| 17 | +import com.microsoft.identity.nativeauth.statemachine.states.SignInContinuationState |
| 18 | +import com.microsoft.identity.nativeauth.statemachine.states.SignUpAttributesRequiredState |
| 19 | +import kotlinx.coroutines.CoroutineScope |
| 20 | +import kotlinx.coroutines.Dispatchers |
| 21 | +import kotlinx.coroutines.launch |
| 22 | + |
| 23 | +class SignUpAttributesFragment : Fragment() { |
| 24 | + private lateinit var currentState: SignUpAttributesRequiredState |
| 25 | + private var _binding: FragmentSignUpAttributesBinding? = null |
| 26 | + private val binding get() = _binding!! |
| 27 | + |
| 28 | + companion object { |
| 29 | + private val TAG = SignUpAttributesFragment::class.java.simpleName |
| 30 | + } |
| 31 | + |
| 32 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { |
| 33 | + _binding = FragmentSignUpAttributesBinding.inflate(inflater, container, false) |
| 34 | + |
| 35 | + val bundle = this.arguments |
| 36 | + currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpAttributesRequiredState)!! |
| 37 | + |
| 38 | + init() |
| 39 | + |
| 40 | + return binding.root |
| 41 | + } |
| 42 | + |
| 43 | + private fun init() { |
| 44 | + initializeButtonListeners() |
| 45 | + } |
| 46 | + |
| 47 | + private fun initializeButtonListeners() { |
| 48 | + binding.submitAttributes.setOnClickListener { |
| 49 | + submitAttributes() |
| 50 | + } |
| 51 | + |
| 52 | + binding.cancelAttributes.setOnClickListener { |
| 53 | + finish() |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private fun submitAttributes() { |
| 58 | + CoroutineScope(Dispatchers.Main).launch { |
| 59 | + val username = binding.usernameText.text.toString() |
| 60 | + |
| 61 | + val attributes = UserAttributes.Builder() |
| 62 | + .flatUsername(username) |
| 63 | + .build() |
| 64 | + |
| 65 | + val actionResult = currentState.submitAttributes(attributes) |
| 66 | + |
| 67 | + when (actionResult) { |
| 68 | + is SignUpResult.Complete -> { |
| 69 | + Toast.makeText(requireContext(), getString(R.string.sign_up_successful_message), Toast.LENGTH_SHORT).show() |
| 70 | + signInAfterSignUp( |
| 71 | + nextState = actionResult.nextState |
| 72 | + ) |
| 73 | + } |
| 74 | + is SignUpResult.AttributesRequired -> { |
| 75 | + displayDialog(getString(R.string.unexpected_sdk_result_title), actionResult.toString()) |
| 76 | + } |
| 77 | + is SignUpSubmitAttributesError -> { |
| 78 | + displayDialog(getString(R.string.unexpected_sdk_error_title), actionResult.exception?.message ?: actionResult.errorMessage) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private suspend fun signInAfterSignUp(nextState: SignInContinuationState) { |
| 85 | + val parameters = NativeAuthSignInContinuationParameters() |
| 86 | + val actionResult = nextState.signIn(parameters) |
| 87 | + |
| 88 | + when (actionResult) { |
| 89 | + is SignInResult.Complete -> { |
| 90 | + Toast.makeText( |
| 91 | + requireContext(), |
| 92 | + getString(R.string.sign_in_successful_message), |
| 93 | + Toast.LENGTH_SHORT |
| 94 | + ).show() |
| 95 | + finish() |
| 96 | + } |
| 97 | + is SignInContinuationError -> { |
| 98 | + displayDialog(getString(R.string.msal_exception_title), actionResult.exception?.message ?: actionResult.errorMessage) |
| 99 | + } |
| 100 | + is SignInResult.CodeRequired, |
| 101 | + is SignInResult.PasswordRequired -> { |
| 102 | + displayDialog(getString(R.string.unexpected_sdk_result_title), actionResult.toString()) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private fun displayDialog(error: String?, message: String?) { |
| 108 | + val builder = AlertDialog.Builder(requireContext()) |
| 109 | + builder.setTitle(error) |
| 110 | + .setMessage(message) |
| 111 | + val alertDialog = builder.create() |
| 112 | + alertDialog.show() |
| 113 | + } |
| 114 | + |
| 115 | + private fun finish() { |
| 116 | + requireActivity().supportFragmentManager.popBackStackImmediate() |
| 117 | + } |
| 118 | +} |
0 commit comments