Created
July 12, 2023 09:07
-
-
Save senamit2708/105b46f3951b5836b77dd131e7cb83b3 to your computer and use it in GitHub Desktop.
Retrofit with Series network call having coroutine and sealed class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.coroutinelearning.retrofits | |
import com.example.coroutinelearning.entity.Root | |
import retrofit2.Response | |
import retrofit2.http.GET | |
import retrofit2.http.Query | |
interface ApiInterface { | |
@GET("/api/users") | |
suspend fun getAllUser( | |
@Query("page") currentPage: Int, @Query("per_page") pageSize: Int | |
): Response<Root> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val customerSeriesNetworkCallViewM : CustomerSeriesNetworkCallViewM by lazy { | |
ViewModelProvider(requireActivity())[CustomerSeriesNetworkCallViewM::class.java] | |
} | |
private fun customerListExampleThree() { | |
customerSeriesNetworkCallViewM.getUiState().observe(viewLifecycleOwner){ | |
when(it) { | |
is UiState.Success -> { | |
binding.progressBar.visibility = View.GONE | |
mCustomerListAdapter.setCustomerList(it.data) | |
binding.userRecyclerView.visibility = View.VISIBLE | |
} | |
is UiState.Error -> { | |
binding.progressBar.visibility = View.GONE | |
Toast.makeText(requireContext(), it.message, Toast.LENGTH_SHORT).show() | |
} | |
is UiState.loading -> { | |
binding.progressBar.visibility = View.VISIBLE | |
binding.userRecyclerView.visibility = View.GONE | |
} | |
} | |
} | |
customerSeriesNetworkCallViewM.getCustomerlist() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.coroutinelearning.repositories | |
import android.app.Application | |
import android.util.Log | |
import com.example.coroutinelearning.database.AppDatabase | |
import com.example.coroutinelearning.database.CustomerDao | |
import com.example.coroutinelearning.entity.Customer | |
import com.example.coroutinelearning.entity.Root | |
import com.example.coroutinelearning.retrofits.RetrofitHelper | |
import retrofit2.Response | |
class CustomerRepository(application: Application) { | |
private val TAG = CustomerRepository::class.simpleName | |
private val appDatabase: AppDatabase | |
private val customerDao: CustomerDao | |
init { | |
appDatabase = AppDatabase.getDatabase(application) | |
customerDao = appDatabase.getCustomerDao() | |
} | |
suspend fun getCustomerList(currentPage: Int = 1): List<Customer>? { | |
val result = RetrofitHelper.getServices().getAllUser(currentPage, 12) | |
val customerData = result.body()?.data | |
saveCustomerDataToDB(customerData) | |
return customerData | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.coroutinelearning.viewModels | |
import android.app.Application | |
import androidx.lifecycle.AndroidViewModel | |
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.viewModelScope | |
import com.example.coroutinelearning.entity.Customer | |
import com.example.coroutinelearning.repositories.CustomerRepository | |
import com.example.coroutinelearning.sealed.UiState | |
import kotlinx.coroutines.launch | |
import java.lang.Exception | |
class CustomerSeriesNetworkCallViewM(application: Application) : AndroidViewModel(application) { | |
private val TAG = CustomerSeriesNetworkCallViewM::class.simpleName | |
private val uiState = MutableLiveData<UiState<List<Customer>>>() | |
private val repository: CustomerRepository | |
init { | |
repository = CustomerRepository(application) | |
} | |
fun getCustomerlist() { | |
viewModelScope.launch { | |
uiState.postValue(UiState.loading) | |
try { | |
val customrePageOne = repository.getCustomerList(currentPage = 1) | |
val customerpageTwo = repository.getCustomerList(currentPage = 2) | |
val allCustomerList = mutableListOf<Customer>() | |
customrePageOne?.let { | |
allCustomerList.addAll(it) | |
} | |
customerpageTwo?.let { | |
allCustomerList.addAll(it) | |
} | |
uiState.postValue(UiState.Success(allCustomerList)) | |
} catch (e: Exception) { | |
uiState.postValue(UiState.Error("something went wrong: ${e.localizedMessage}")) | |
} | |
} | |
} | |
fun getUiState(): LiveData<UiState<List<Customer>>> { | |
return uiState | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.coroutinelearning.retrofits | |
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
object RetrofitHelper{ | |
private const val baseUrl = "https://reqres.in" | |
private val mHttpLoginInterceptor = HttpLoggingInterceptor() | |
.setLevel(HttpLoggingInterceptor.Level.BODY) | |
private val mOkHttpClient = OkHttpClient.Builder() | |
.addInterceptor(mHttpLoginInterceptor) | |
.build() | |
fun getInstance() : Retrofit { | |
return Retrofit.Builder() | |
.baseUrl(baseUrl) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.client(mOkHttpClient) | |
.build() | |
} | |
fun getServices() : ApiInterface{ | |
return getInstance().create(ApiInterface::class.java) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.coroutinelearning.sealed | |
sealed interface UiState<out T> { | |
data class Success<T>(val data: T) : UiState<T> | |
data class Error(val message: String) : UiState<Nothing> | |
object loading : UiState<Nothing> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment