Skip to content

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
anugrahdev committed May 30, 2020
1 parent 841784b commit ab164ca
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ import com.anugrahdev.app.klikPaket.data.network.SafeApiRequest
class CostRepository(private val api:ApiService):
SafeApiRequest() {

suspend fun getCities(query: String):Resource<List<CitiesResult>>{
val response= apiRequest { api.getCities(query) }
return Resource.Success(response.data.results)
}
suspend fun getCities(query: String) = apiRequest { api.getCities(query) }

suspend fun getDistricts(query: String):Resource<List<DistrictsResult>>{
val response = apiRequest { api.getDistricts(query) }
return Resource.Success(response.data.results)
}
suspend fun getDistricts(query: String) = apiRequest { api.getDistricts(query) }

suspend fun postShippingCosts(originId:Int, destId:Int, weight:Int, courier:String):Resource<List<ShippingCostResult>>{
val response = apiRequest { api.postShippingCost(originId,destId,weight,courier) }
return Resource.Success(response.data.results)
}
suspend fun postShippingCosts(originId:Int, destId:Int, weight:Int, courier:String)
= apiRequest { api.postShippingCost(originId,destId,weight,courier) }

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import com.anugrahdev.app.klikPaket.data.network.SafeApiRequest
class WaybillRepository (private val api:ApiService, private val db:AppDatabase):
SafeApiRequest(){

suspend fun postWaybill(waybill:String,courier:String): Resource<WaybillData>{
val response = apiRequest { api.postWaybill(waybill, courier) }
return Resource.Success(response.data)
}
suspend fun postWaybill(waybill:String,courier:String) = apiRequest { api.postWaybill(waybill, courier) }

suspend fun upsert(waybillData: WaybillData) = db.getWaybillDao().upsert(waybillData)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import com.anugrahdev.app.klikPaket.data.models.districts.DistrictsResult
import com.anugrahdev.app.klikPaket.data.models.shippingcost.ShippingCostResult
import com.anugrahdev.app.klikPaket.data.network.Resource
import com.anugrahdev.app.klikPaket.data.repositories.CostRepository
import com.anugrahdev.app.klikPaket.utils.ApiException
import com.anugrahdev.app.klikPaket.utils.NoConnectivityException
import kotlinx.coroutines.launch
import java.io.IOException

class CostViewModel(private val repository: CostRepository) : ViewModel() {

Expand All @@ -22,17 +21,15 @@ class CostViewModel(private val repository: CostRepository) : ViewModel() {
val districts: LiveData<Resource<List<DistrictsResult>>> get() = _districts

private val _shippingCost = MutableLiveData<Resource<List<ShippingCostResult>>>()
val shippingCost : LiveData<Resource<List<ShippingCostResult>>>
val shippingCost: LiveData<Resource<List<ShippingCostResult>>>
get() = _shippingCost

fun getCities(query:String) = viewModelScope.launch {
fun getCities(query: String) = viewModelScope.launch {
_cities.postValue(Resource.Loading())
try {
val response = repository.getCities(query)
_cities.postValue(response)
} catch (e: NoConnectivityException) {
_cities.postValue(Resource.Error(e.message))
} catch (e: ApiException){
_cities.postValue(Resource.Success(response.data.results))
} catch (e: IOException) {
_cities.postValue(Resource.Error(e.message))
}
}
Expand All @@ -41,28 +38,24 @@ class CostViewModel(private val repository: CostRepository) : ViewModel() {
_districts.postValue(Resource.Loading())
try {
val response = repository.getDistricts(query)
_districts.postValue(response)
} catch (e: NoConnectivityException) {
_districts.postValue(Resource.Error(e.message))
} catch (e: ApiException){
_districts.postValue(Resource.Success(response.data.results))
} catch (e: IOException) {
_districts.postValue(Resource.Error(e.message))
}
}

fun postShippingCost(originId:Int, destId:Int, weight:Int, courier:String) = viewModelScope.launch {
_shippingCost.postValue(Resource.Loading())
try{
val response = repository.postShippingCosts(originId,destId,weight,courier)
_shippingCost.postValue(response)
}catch (e: NoConnectivityException) {
_shippingCost.postValue(Resource.Error(e.message))
} catch (e: ApiException){
_shippingCost.postValue(Resource.Error(e.message))
fun postShippingCost(originId: Int, destId: Int, weight: Int, courier: String) =
viewModelScope.launch {
_shippingCost.postValue(Resource.Loading())
try {
val response = repository.postShippingCosts(originId, destId, weight, courier)
_shippingCost.postValue(Resource.Success(response.data.results))
} catch (e: IOException) {
_shippingCost.postValue(Resource.Error(e.message))
}
}
}

}




}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WaybillViewModel(private val repository: WaybillRepository) : ViewModel(){
_waybillData.postValue(Resource.Loading())
try{
val response = repository.postWaybill(waybill, courier)
_waybillData.postValue(response)
_waybillData.postValue(Resource.Success(response.data))
}catch (e: NoConnectivityException) {
_waybillData.postValue(Resource.Error(e.message))
} catch (e: ApiException){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.anugrahdev.app.klikPaket.utils

import java.io.IOException

class ApiException(message:String) : IOException(message)
class NoConnectivityException(message: String):IOException(message)
class ApiException(message: String) : IOException(message)
class NoConnectivityException(message: String) : IOException(message)
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.2.0-rc01"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-beta01"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name='iKurir'
rootProject.name='KlikPaket'
include ':app'

0 comments on commit ab164ca

Please sign in to comment.