-
Notifications
You must be signed in to change notification settings - Fork 38
BasicArchitecture задание 1 #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gbdr73
wants to merge
5
commits into
Android-Developer-Basic:master
Choose a base branch
from
Gbdr73:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0394d13
BasicArchitecture задание 1
Gbdr73 01acc03
WizardCash перенесен в ActivityRetainedScope
Gbdr73 6d0842c
BasicArchitecture задание 2
Gbdr73 02f8fe0
Добавлены правки согласно комментариям.
Gbdr73 dba29f9
BasicArchitecture задание 3
Gbdr73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
16 changes: 16 additions & 0 deletions
16
app/src/main/java/ru/otus/basicarchitecture/AddressApiService.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| import retrofit2.Response | ||
| import retrofit2.http.Body | ||
| import retrofit2.http.Header | ||
| import retrofit2.http.Headers | ||
| import retrofit2.http.POST | ||
|
|
||
| interface AddressApiService { | ||
| @Headers("Content-Type: application/json") | ||
| @POST("suggest/address") | ||
| suspend fun suggestAddress( | ||
| @Header("Authorization") token: String, | ||
| @Body request: AddressRequestDto, | ||
| ): Response<AddressResponseDto> | ||
| } |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/ru/otus/basicarchitecture/AddressDataDto.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| data class AddressDataDto( | ||
| val country: String?, | ||
| val city: String?, | ||
| val street: String?, | ||
| val house: String?, | ||
| val block: String?, | ||
| val fullAddress: String? = null | ||
| ) |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/ru/otus/basicarchitecture/AddressMapper.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| class AddressMapper { | ||
| fun mapDtoToEntity(dto: AddressDataDto) = UserAddress( | ||
| fullAddress = dto.fullAddress ?: "", | ||
| country = dto.country ?: "", | ||
| city = dto.city ?: "", | ||
| street = dto.street ?: "", | ||
| house = dto.house ?: "", | ||
| block = dto.block ?: "" | ||
| ) | ||
| } |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/ru/otus/basicarchitecture/AddressRepository.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| interface AddressRepository { | ||
| suspend fun suggestAddress(query: String): List<UserAddress> | ||
| } |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/ru/otus/basicarchitecture/AddressRepositoryImpl.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| import android.util.Log | ||
| import java.io.IOException | ||
| import javax.inject.Inject | ||
|
|
||
| class AddressRepositoryImpl @Inject constructor( | ||
| private val addressApiService: AddressApiService | ||
| ) : AddressRepository { | ||
| override suspend fun suggestAddress(query: String): List<UserAddress> { | ||
| val token = "Token ${BuildConfig.dadata_api_key}" | ||
|
|
||
| val response = addressApiService.suggestAddress(token, AddressRequestDto(query)) | ||
|
|
||
| if (!response.isSuccessful){ | ||
| val errorResponse = response.errorBody()?.string() | ||
| Log.e("API Error", "Error response: $errorResponse") | ||
| throw IOException("Error response: $errorResponse") | ||
| } | ||
|
|
||
| val listAddressDataDto = response.body()?.suggestions?.map { suggestion -> | ||
| suggestion.data.copy(fullAddress = suggestion.unrestricted_value) | ||
| } | ||
|
|
||
| val mapper = AddressMapper() | ||
| val listUserAddress = listAddressDataDto?.map { | ||
| mapper.mapDtoToEntity(it) | ||
| } | ||
|
|
||
| return listUserAddress ?: listOf() | ||
| } | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
app/src/main/java/ru/otus/basicarchitecture/AddressRequestDto.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| data class AddressRequestDto( | ||
| val query: String | ||
| ) |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/ru/otus/basicarchitecture/AddressResponseDto.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| data class AddressResponseDto( | ||
| val suggestions: List<AddressSuggestionDto> | ||
| ) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/ru/otus/basicarchitecture/AddressSuggestUseCase.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
|
|
||
| class AddressSuggestUseCase(private val addressRepository: AddressRepository) { | ||
| suspend operator fun invoke(query: String): List<UserAddress> { | ||
| return addressRepository.suggestAddress(query) | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/otus/basicarchitecture/AddressSuggestionDto.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
| data class AddressSuggestionDto( | ||
| val value: String, | ||
| val unrestricted_value: String, | ||
| val data: AddressDataDto | ||
| ) |
70 changes: 70 additions & 0 deletions
70
app/src/main/java/ru/otus/basicarchitecture/AddressVewModel.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package ru.otus.basicarchitecture | ||
|
|
||
|
|
||
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| @HiltViewModel | ||
| class AddressViewModel @Inject constructor( | ||
| private val wizardCache: WizardCache, | ||
| private val addressSuggestUseCase: AddressSuggestUseCase | ||
| ): ViewModel() { | ||
| private var _listUserAddress = MutableLiveData<List<UserAddress>>() | ||
| val listUserAddress: LiveData<List<UserAddress>> | ||
| get() = _listUserAddress | ||
| private var _canContinue = MutableLiveData<Boolean>(false) | ||
| val canContinue: LiveData<Boolean> | ||
| get() = _canContinue | ||
| private var _errorNetwork = MutableLiveData<Boolean>() | ||
| val errorNetwork: LiveData<Boolean> | ||
| get() = _errorNetwork | ||
| private var _errorEmptyAddress = MutableLiveData<Boolean>() | ||
| val errorEmptyAddress: LiveData<Boolean> | ||
| get() = _errorEmptyAddress | ||
|
|
||
| private var current_job: Job? = null | ||
|
|
||
| fun validateData() { | ||
| val successful = checkEmptyFields() | ||
|
|
||
| if (successful == false){ | ||
| _canContinue.value = false | ||
| return | ||
| } | ||
| _canContinue.value = true | ||
| } | ||
|
|
||
| fun setAddress(fullAddress: String) { | ||
| wizardCache.userAddress.fullAddress = fullAddress | ||
| } | ||
|
|
||
| fun searchAddress(query: String) { | ||
| current_job?.cancel() | ||
|
|
||
| current_job = viewModelScope.launch { | ||
| try { | ||
| val result = addressSuggestUseCase.invoke(query) | ||
| _listUserAddress.postValue(result) | ||
| } catch (e: Exception) { | ||
| _errorNetwork.postValue(true) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun checkEmptyFields(): Boolean{ | ||
| var successful = true | ||
| if (wizardCache.userAddress.fullAddress.isBlank()){ | ||
| _errorEmptyAddress.value = true | ||
| successful = false | ||
| } else{ | ||
| _errorEmptyAddress.value = false | ||
| } | ||
| return successful | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше еще пробрасывать исключение при неуспешном ответе, чтобы ВМ могла корректно обработать ошибку и показать пользователю сообщение.