Skip to content

Commit

Permalink
feature: Call Retrofit APIs using Kotlin Coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivamNagpal committed Mar 24, 2023
1 parent 84938db commit 0d21add
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package com.nagpal.shivam.vtucslab.screens.display

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.nagpal.shivam.vtucslab.VTUCSLabApplication
import com.nagpal.shivam.vtucslab.services.VtuCsLabService
import com.nagpal.shivam.vtucslab.utilities.Constants
import com.nagpal.shivam.vtucslab.utilities.NetworkUtils
import com.nagpal.shivam.vtucslab.utilities.Stages
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlinx.coroutines.launch

class DisplayViewModel(app: Application) : AndroidViewModel(app) {
var scrollX = 0
Expand All @@ -39,25 +39,25 @@ class DisplayViewModel(app: Application) : AndroidViewModel(app) {
return
}
_uiState.update { initialState }
VtuCsLabService.instance.fetchRawResponse(url)
.enqueue(object : Callback<String> {
override fun onResponse(call: Call<String>, response: Response<String>) {
viewModelScope.launch(Dispatchers.IO) {
try {
val response = VtuCsLabService.instance.fetchRawResponse(url)
if (response.isSuccessful) {
_uiState.update {
val stringResponse = response.body()!!.replace("\t", "\t\t")
DisplayState(
Stages.SUCCEEDED,
stringResponse,
null,
)
DisplayState(Stages.SUCCEEDED, stringResponse, null)
}
}

override fun onFailure(call: Call<String>, t: Throwable) {
} else {
_uiState.update {
DisplayState(Stages.FAILED, null, null)
}
}
})
} catch (throwable: Throwable) {
_uiState.update {
DisplayState(Stages.FAILED, null, null)
}
}
}
}

fun resetState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package com.nagpal.shivam.vtucslab.screens.programs

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.nagpal.shivam.vtucslab.VTUCSLabApplication
import com.nagpal.shivam.vtucslab.models.LaboratoryExperimentResponse
import com.nagpal.shivam.vtucslab.services.VtuCsLabService
import com.nagpal.shivam.vtucslab.utilities.Constants
import com.nagpal.shivam.vtucslab.utilities.NetworkUtils
import com.nagpal.shivam.vtucslab.utilities.Stages
import com.nagpal.shivam.vtucslab.utilities.StaticMethods
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlinx.coroutines.launch

class ProgramViewModel(app: Application) : AndroidViewModel(app) {
private val initialState = ProgramState(Stages.LOADING, null, null, null)
Expand All @@ -39,12 +38,10 @@ class ProgramViewModel(app: Application) : AndroidViewModel(app) {
return
}
_uiState.update { initialState }
VtuCsLabService.instance.getLaboratoryExperimentsResponse(url)
.enqueue(object : Callback<LaboratoryExperimentResponse> {
override fun onResponse(
call: Call<LaboratoryExperimentResponse>,
response: Response<LaboratoryExperimentResponse>
) {
viewModelScope.launch(Dispatchers.IO) {
try {
val response = VtuCsLabService.instance.getLaboratoryExperimentsResponse(url)
if (response.isSuccessful) {
_uiState.update {
val labResponse = response.body()!!
ProgramState(
Expand All @@ -54,14 +51,16 @@ class ProgramViewModel(app: Application) : AndroidViewModel(app) {
StaticMethods.getBaseURL(labResponse)
)
}
}

override fun onFailure(call: Call<LaboratoryExperimentResponse>, t: Throwable) {
} else {
_uiState.update {
ProgramState(Stages.FAILED, null, null, null)
}
}
})
} catch (throwable: Throwable) {
_uiState.update {
ProgramState(Stages.FAILED, null, null, null)
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package com.nagpal.shivam.vtucslab.screens.repository

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.nagpal.shivam.vtucslab.VTUCSLabApplication
import com.nagpal.shivam.vtucslab.models.LaboratoryResponse
import com.nagpal.shivam.vtucslab.services.VtuCsLabService
import com.nagpal.shivam.vtucslab.utilities.Constants
import com.nagpal.shivam.vtucslab.utilities.NetworkUtils
import com.nagpal.shivam.vtucslab.utilities.Stages
import com.nagpal.shivam.vtucslab.utilities.StaticMethods
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlinx.coroutines.launch

class RepositoryViewModel(app: Application) : AndroidViewModel(app) {
private val initialState = RepositoryState(Stages.LOADING, null, null, null)
Expand All @@ -39,12 +38,10 @@ class RepositoryViewModel(app: Application) : AndroidViewModel(app) {
return
}
_uiState.update { initialState }
VtuCsLabService.instance.getLaboratoryResponse(url)
.enqueue(object : Callback<LaboratoryResponse> {
override fun onResponse(
call: Call<LaboratoryResponse>,
response: Response<LaboratoryResponse>
) {
viewModelScope.launch(Dispatchers.IO) {
try {
val response = VtuCsLabService.instance.getLaboratoryResponse(url)
if (response.isSuccessful) {
_uiState.update {
val labResponse = response.body()!!
RepositoryState(
Expand All @@ -54,14 +51,16 @@ class RepositoryViewModel(app: Application) : AndroidViewModel(app) {
StaticMethods.getBaseURL(labResponse)
)
}
}

override fun onFailure(call: Call<LaboratoryResponse>, t: Throwable) {
} else {
_uiState.update {
RepositoryState(Stages.FAILED, null, null, null)
}
}
})
} catch (throwable: Throwable) {
_uiState.update {
RepositoryState(Stages.FAILED, null, null, null)
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import com.nagpal.shivam.vtucslab.models.LaboratoryExperimentResponse
import com.nagpal.shivam.vtucslab.models.LaboratoryResponse
import com.nagpal.shivam.vtucslab.utilities.Constants
import com.nagpal.shivam.vtucslab.utilities.StaticMethods
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Url

interface VtuCsLabService {

@GET
fun getLaboratoryResponse(@Url url: String): Call<LaboratoryResponse>
suspend fun getLaboratoryResponse(@Url url: String): Response<LaboratoryResponse>

@GET
fun getLaboratoryExperimentsResponse(@Url url: String): Call<LaboratoryExperimentResponse>
suspend fun getLaboratoryExperimentsResponse(@Url url: String): Response<LaboratoryExperimentResponse>

@GET
fun fetchRawResponse(@Url url: String): Call<String>
suspend fun fetchRawResponse(@Url url: String): Response<String>

companion object {
val instance: VtuCsLabService by lazy {
Expand Down

0 comments on commit 0d21add

Please sign in to comment.