Mehedi Hassan Piash | Senior Software Engineer | Android | iOS | KMP | Ktor | Jetpack Compose | React-Native.

July 19, 2020

Api Call with architecture component [live data]

July 19, 2020 Posted by Piash No comments
- Add gradle dependency:
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.activity:activity-ktx:1.0.0"


- Resource
data class Resource<out T>(val status: Status, val data: T?, val msg: String?) {
    companion object {
        fun <T> success(data: T?): Resource<T> {
            return Resource(Status.SUCCESS, data, null)
        }

        fun <T> error(msg: String, data: T? = null): Resource<T> {
            return Resource(Status.ERROR, data, msg)
        }

        fun <T> loading(data: T? = null): Resource<T> {
            return Resource(Status.LOADING, data, null)
        }
    }
}

enum class Status {
    SUCCESS,    ERROR,    LOADING}

- ViewModel
class BasicViewModel : ViewModel() {
    fun saveCard(tokenId: String) =
        liveData(Dispatchers.IO) {            emit(Resource.loading())
            val api = ApiClient.createService(ApiService::class.java)
            val response = api.saveCard(tokenId)
            if (response.isSuccessful) {
                emit(Resource.success(response.body()))
            } else {
                emit(Resource.error(Constants.ERROR, response.errorBody()))
            }
        }
}

- Implementation in Activity of fragment

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity"    private val viewModel: PayjpViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
      
        apiCall()
    }
   

    private fun apiCall(limit: String, offset: String) {
        viewModel.paymentList(limit, offset)
            .observe(this, androidx.lifecycle.Observer { resources ->                when (resources.status) {
                    Status.LOADING -> {
                        Log.d(TAG, "Loading")
                    }

                    Status.SUCCESS -> {
                        Log.d(TAG, "PaymentInformation success : ${resources.data}")
                    }

                    Status.ERROR -> {
                        Log.d(TAG, "Error :${resources.data}")
                    }
                }
            })
    }
}


0 comments:

Post a Comment