Skip to content

Commit

Permalink
Merge pull request #1 from emanuelgalvao/feat/version2
Browse files Browse the repository at this point in the history
feat: version 2.0.0
  • Loading branch information
emanuelgalvao authored Mar 3, 2024
2 parents 9be31f1 + 5470b06 commit 532d2b6
Show file tree
Hide file tree
Showing 22 changed files with 479 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ class MainActivity : AppCompatActivity() {
showMessageDialog(getString(R.string.TITLE_SUCCESS), cepDetails)
}

viewModel.error.observe(this) { errorStatus ->
showMessageDialog(getString(R.string.TITLE_ERROR), errorStatus.message)
}

viewModel.validation.observe(this) { validationStatus ->
showMessageDialog(getString(R.string.TITLE_VALIDATION), validationStatus.message)
viewModel.error.observe(this) { message ->
showMessageDialog(getString(R.string.TITLE_ERROR), message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.emanuelgalvao.buscacep.SearchCep
import com.emanuelgalvao.buscacep.callback.CepCallback
import com.emanuelgalvao.buscacep.model.CepModel
import com.emanuelgalvao.buscacep.status.CepErrorStatus
import com.emanuelgalvao.buscacep.callback.CepResponse
import com.emanuelgalvao.buscacep.model.CepData
import com.emanuelgalvao.buscacep.status.CepValidationStatus
import kotlinx.coroutines.launch

Expand All @@ -17,47 +16,36 @@ class MainViewModel: ViewModel() {
val requesting: LiveData<Boolean>
get() = _requesting

private val _success: MutableLiveData<CepModel> = MutableLiveData()
val success: LiveData<CepModel>
private val _success: MutableLiveData<CepData> = MutableLiveData()
val success: LiveData<CepData>
get() = _success

private val _error: MutableLiveData<CepErrorStatus> = MutableLiveData()
val error: LiveData<CepErrorStatus>
private val _error: MutableLiveData<String> = MutableLiveData()
val error: LiveData<String>
get() = _error

private val _validation: MutableLiveData<CepValidationStatus> = MutableLiveData()
val validation: LiveData<CepValidationStatus>
get() = _validation

fun searchCep(cep: String) {

viewModelScope.launch {
_requesting.postValue(true)
val callback = SearchCep.instance.getCepData(cep)
when (callback) {
is CepCallback.Success -> {
handleSuccess(callback.cepModel)
}
is CepCallback.Error -> {
handleError(callback.cepErrorStatus)
val response = SearchCep.getCepData(cep)
when (response) {
is CepResponse.Success -> {
handleSuccess(response.data)
}
is CepCallback.Validation -> {
handleValidation(callback.cepValidationStatus)
is CepResponse.Error -> {
handleError(response.message)
}
}
_requesting.postValue(false)
}
}

private fun handleSuccess(cepModel: CepModel) {
private fun handleSuccess(cepModel: CepData) {
_success.postValue(cepModel)
}

private fun handleError(cepErrorStatus: CepErrorStatus) {
_error.postValue(cepErrorStatus)
}

private fun handleValidation(cepValidationStatus: CepValidationStatus) {
_validation.postValue(cepValidationStatus)
private fun handleError(message: String) {
_error.postValue(message)
}
}
16 changes: 8 additions & 8 deletions buscacep/src/main/java/com/emanuelgalvao/buscacep/SearchCep.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.emanuelgalvao.buscacep

import com.emanuelgalvao.buscacep.callback.CepCallback
import com.emanuelgalvao.buscacep.callback.CepResponse
import com.emanuelgalvao.buscacep.data.CepDataSource
import com.emanuelgalvao.buscacep.data.CepRepository
import com.emanuelgalvao.buscacep.manager.CepManager
import com.emanuelgalvao.buscacep.model.CepData
import com.emanuelgalvao.buscacep.network.ApiServices

class SearchCep private constructor() {
object SearchCep {

private val cepRepository: CepRepository by lazy {
CepDataSource(ApiServices.cepService)
Expand All @@ -15,13 +16,12 @@ class SearchCep private constructor() {
private val cepManager: CepManager by lazy {
CepManager(cepRepository)
}
companion object {
val instance: SearchCep by lazy {
SearchCep()
}
}

suspend fun getCepData(cep: String): CepCallback {
suspend fun getCepData(cep: String): CepResponse {
return cepManager.getCepData(cep)
}

suspend fun getCepDataOrNull(cep: String): CepData? {
return cepManager.getCepDataOrNull(cep)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.emanuelgalvao.buscacep.callback

import com.emanuelgalvao.buscacep.model.CepData

sealed interface CepResponse {
class Error(val message: String): CepResponse
class Success(val data: CepData): CepResponse
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
package com.emanuelgalvao.buscacep.data

import com.emanuelgalvao.buscacep.callback.CepCallback
import com.emanuelgalvao.buscacep.model.CepModel
import com.emanuelgalvao.buscacep.callback.CepResponse
import com.emanuelgalvao.buscacep.model.CepData
import com.emanuelgalvao.buscacep.network.CepService
import com.emanuelgalvao.buscacep.status.CepErrorStatus
import com.emanuelgalvao.buscacep.status.CepValidationStatus
import com.emanuelgalvao.buscacep.utils.CepHandler
import com.emanuelgalvao.buscacep.utils.Validator
import com.emanuelgalvao.buscacep.utils.CepValidator

internal class CepDataSource(private val cepApiService: CepService): CepRepository {
class CepDataSource(private val cepApiService: CepService): CepRepository {

override suspend fun searchCep(cep: String): CepCallback {

val cepFormatted = CepHandler.instance.formatCepToCorrectFormat(cep)

val validationStatus = Validator.instance.validateCep(cepFormatted)

return if (validationStatus == CepValidationStatus.CEP_VALID) {
doSearchRequest(cepFormatted)
override suspend fun getCepData(cep: String): CepResponse {
val validationStatus = CepValidator.validate(cep)
return if (isCepValid(validationStatus)) {
doSearchRequest(cep)
} else {
CepCallback.Validation(validationStatus)
CepResponse.Error(validationStatus.message)
}
}

private suspend fun doSearchRequest(cep: String): CepCallback {
override suspend fun getCepDataOrNull(cep: String): CepData? {
val validationStatus = CepValidator.validate(cep)
if (isCepValid(validationStatus)) {
val callback = doSearchRequest(cep)
if (callback is CepResponse.Success) {
return callback.data
}
}
return null
}

private fun isCepValid(validationStatus: CepValidationStatus): Boolean {
return validationStatus == CepValidationStatus.CEP_VALID
}

val apiResponse = cepApiService.searchCep(cep)
private suspend fun doSearchRequest(cep: String): CepResponse {
val cepFormatted = CepHandler.formatCep(cep)
val apiResponse = cepApiService.searchCep(cepFormatted)

return if (apiResponse.isSuccessful) {
apiResponse.body()?.let { cepResponse ->
if (cepResponse.erro) {
CepCallback.Error(CepErrorStatus.INVALID_CEP)
CepResponse.Error(CepErrorStatus.INVALID_CEP.message)
} else {
val cepModel = CepModel(
val cepModel = CepData(
bairro = cepResponse.bairro,
cep = cepResponse.cep,
complemento = cepResponse.complemento,
Expand All @@ -44,11 +55,11 @@ internal class CepDataSource(private val cepApiService: CepService): CepReposito
siafi = cepResponse.siafi,
uf = cepResponse.uf
)
CepCallback.Success(cepModel)
CepResponse.Success(cepModel)
}
} ?: CepCallback.Error(CepErrorStatus.SERVER_ERROR)
} ?: CepResponse.Error(CepErrorStatus.SERVER_ERROR.message)
} else {
CepCallback.Error(CepErrorStatus.SERVER_ERROR)
CepResponse.Error(CepErrorStatus.SERVER_ERROR.message)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.emanuelgalvao.buscacep.data

import com.emanuelgalvao.buscacep.callback.CepCallback
import com.emanuelgalvao.buscacep.callback.CepResponse
import com.emanuelgalvao.buscacep.model.CepData

internal interface CepRepository {
interface CepRepository {

suspend fun searchCep(cep: String): CepCallback
suspend fun getCepData(cep: String): CepResponse

suspend fun getCepDataOrNull(cep: String): CepData?
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.emanuelgalvao.buscacep.manager

import com.emanuelgalvao.buscacep.callback.CepCallback
import com.emanuelgalvao.buscacep.callback.CepResponse
import com.emanuelgalvao.buscacep.data.CepRepository
import com.emanuelgalvao.buscacep.model.CepData

internal class CepManager(private val cepRepository: CepRepository) {

suspend fun getCepData(cep: String): CepCallback {
return cepRepository.searchCep(cep)
suspend fun getCepData(cep: String): CepResponse {
return cepRepository.getCepData(cep)
}

suspend fun getCepDataOrNull(cep: String): CepData? {
return cepRepository.getCepDataOrNull(cep)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.emanuelgalvao.buscacep.model

data class CepApiResponse(
val bairro: String,
val cep: String,
val complemento: String,
val ddd: String,
val gia: String,
val ibge: String,
val localidade: String,
val logradouro: String,
val siafi: String,
val uf: String,
val erro: Boolean
)
14 changes: 14 additions & 0 deletions buscacep/src/main/java/com/emanuelgalvao/buscacep/model/CepData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.emanuelgalvao.buscacep.model

data class CepData(
val bairro: String,
val cep: String,
val complemento: String,
val ddd: String,
val gia: String,
val ibge: String,
val localidade: String,
val logradouro: String,
val siafi: String,
val uf: String
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.emanuelgalvao.buscacep.network


internal class ApiServices {

companion object {

val cepService: CepService = RetrofitClient.getClient().create(CepService::class.java)

}
internal object ApiServices {
val cepService: CepService = RetrofitClient.getClient().create(CepService::class.java)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.emanuelgalvao.buscacep.network

import com.emanuelgalvao.buscacep.model.CepResponse
import com.emanuelgalvao.buscacep.model.CepApiResponse
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path

internal interface CepService {
interface CepService {

@GET("{cep}/json/")
suspend fun searchCep(@Path(value = "cep") cep: String) : Response<CepResponse>
suspend fun searchCep(@Path(value = "cep") cep: String) : Response<CepApiResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ package com.emanuelgalvao.buscacep.network
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

internal class RetrofitClient {
internal object RetrofitClient {

companion object {
private const val BASE_URL = "https://viacep.com.br/ws/"
private lateinit var client: Retrofit

private const val BASE_URL = "https://viacep.com.br/ws/"

private lateinit var client: Retrofit

fun getClient(): Retrofit {
if (!this::client.isInitialized)
initializeClient()
return client
}
fun getClient(): Retrofit {
if (!this::client.isInitialized)
initializeClient()
return client
}

private fun initializeClient() {
client = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
}
private fun initializeClient() {
client = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
}
}
Loading

0 comments on commit 532d2b6

Please sign in to comment.