0

I am learning MVVM and Android architecture component.

I need to make 2 requests to server from my fragment/activity, the result from first request will be used as input parameter for second request, after those request are good, then navigate to next fragment/activity

in old way, the code in my fragment/activity will be like this

class FragmentA : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)   

       getUserData("example@email.com")


    }


    fun getUserData(email: String) {

        // making request using retrofit

        call.enqueue(object: Callback<RestaurantListBaseResponse> {

            override fun onFailure(call: Call<RestaurantListBaseResponse>, t: Throwable) {

            }

            override fun onResponse(call: Call<User>, response: Response<User>) {

                val user = response.body()!!.user

                if (user.isVerified) {
                    createPost(user.id)
                }


            }

        })

    }

    fun createPost(userID: String) {

        // making request using retrofit

        call.enqueue(object: Callback<RestaurantListBaseResponse> {

            override fun onFailure(call: Call<RestaurantListBaseResponse>, t: Throwable) {

            }

            override fun onResponse(call: Call<PostResponse>, response: Response<PostResponse>) {

               val isSuccessfull = response.body()!!.isSuccessfull

                if (isSuccessfull) {

                    // Navigate to next fragment or activity

                }


            }

        })

    }
}

but now I am confused how to convert this using livedata and viewmodel. the tutorials I watch are too simple and I am confused if I have to handle 2 request in series like this using livedata and viewmodel, I don't know the common practise to solve this

please don't use kotlin coroutine, I am a beginner :(

Java or Kotlin are okay, I can read Java as well

sarah
  • 3,819
  • 4
  • 38
  • 80

0 Answers0