forked from Yandex-Practicum/practicum-android-diploma
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from ElchinGasymov/iteration_3_review
Iteration 3 review
- Loading branch information
Showing
68 changed files
with
3,273 additions
and
54 deletions.
There are no files selected for viewing
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
3 changes: 3 additions & 0 deletions
3
app/src/main/java/ru/practicum/android/diploma/data/dto/CountriesRequest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
data object CountriesRequest |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/practicum/android/diploma/data/dto/CountriesResponse.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
import ru.practicum.android.diploma.data.dto.components.CountryDto | ||
|
||
data class CountriesResponse( | ||
val countries: List<CountryDto> | ||
) : Response() |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/ru/practicum/android/diploma/data/dto/IndustriesRequest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
data object IndustriesRequest |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/practicum/android/diploma/data/dto/IndustriesResponse.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
import ru.practicum.android.diploma.data.dto.components.IndustriesDto | ||
|
||
data class IndustriesResponse( | ||
val sectors: List<IndustriesDto> | ||
) : Response() |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/ru/practicum/android/diploma/data/dto/RegionsRequest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
data class RegionsRequest( | ||
val id: String | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/practicum/android/diploma/data/dto/RegionsResponse.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
import ru.practicum.android.diploma.data.dto.components.RegionDto | ||
|
||
data class RegionsResponse( | ||
val regions: List<RegionDto> | ||
) : Response() |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/ru/practicum/android/diploma/data/dto/SaveFiltersSharedPrefsDto.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
import ru.practicum.android.diploma.data.dto.components.CountryDto | ||
import ru.practicum.android.diploma.data.dto.components.IndustriesDto | ||
import ru.practicum.android.diploma.data.dto.components.RegionDto | ||
import java.io.Serializable | ||
|
||
data class SaveFiltersSharedPrefsDto( | ||
val industries: IndustriesDto?, | ||
val country: CountryDto?, | ||
val region: RegionDto?, | ||
val currency: Int?, | ||
val noCurrency: Boolean? | ||
) : Serializable { | ||
companion object { | ||
private const val serialVersionUID = 1L | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/practicum/android/diploma/data/dto/components/CountryDto.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.practicum.android.diploma.data.dto.components | ||
|
||
data class CountryDto( | ||
val id: String, | ||
val name: String, | ||
val areas: List<RegionDto> | ||
) |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ru/practicum/android/diploma/data/dto/components/IndustriesDto.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ru.practicum.android.diploma.data.dto.components | ||
|
||
data class IndustriesDto( | ||
val id: String, | ||
val name: String | ||
) |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/ru/practicum/android/diploma/data/dto/components/RegionDto.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.practicum.android.diploma.data.dto.components | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class RegionDto( | ||
val id: String, | ||
val name: String, | ||
@SerializedName("parent_id") val parentId: Int? | ||
) |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/ru/practicum/android/diploma/data/dto/components/RegionListDto.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ru.practicum.android.diploma.data.dto.components | ||
|
||
data class RegionListDto( | ||
val areas: List<RegionDto>? = null | ||
) |
81 changes: 81 additions & 0 deletions
81
app/src/main/java/ru/practicum/android/diploma/data/impl/FilterRepositoryImpl.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ru.practicum.android.diploma.data.impl | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import ru.practicum.android.diploma.data.dto.CountriesRequest | ||
import ru.practicum.android.diploma.data.dto.CountriesResponse | ||
import ru.practicum.android.diploma.data.dto.IndustriesRequest | ||
import ru.practicum.android.diploma.data.dto.IndustriesResponse | ||
import ru.practicum.android.diploma.data.dto.RESULT_CODE_BAD_REQUEST | ||
import ru.practicum.android.diploma.data.dto.RESULT_CODE_NO_INTERNET | ||
import ru.practicum.android.diploma.data.dto.RegionsRequest | ||
import ru.practicum.android.diploma.data.dto.RegionsResponse | ||
import ru.practicum.android.diploma.data.dto.Response | ||
import ru.practicum.android.diploma.data.dto.toAllRegions | ||
import ru.practicum.android.diploma.data.dto.toCountryList | ||
import ru.practicum.android.diploma.data.dto.toRegionList | ||
import ru.practicum.android.diploma.data.dto.toSectorList | ||
import ru.practicum.android.diploma.data.network.NetworkClient | ||
import ru.practicum.android.diploma.domain.api.FilterRepository | ||
import ru.practicum.android.diploma.domain.models.Country | ||
import ru.practicum.android.diploma.domain.models.Industries | ||
import ru.practicum.android.diploma.domain.models.Region | ||
import ru.practicum.android.diploma.util.ResponseData | ||
|
||
class FilterRepositoryImpl( | ||
private val networkClient: NetworkClient | ||
) : FilterRepository { | ||
|
||
override fun getCountries(): Flow<ResponseData<List<Country>>> = flow { | ||
when (val response = networkClient.doRequest(CountriesRequest)) { | ||
is CountriesResponse -> { | ||
val countriesList = response.countries.toCountryList() | ||
emit(ResponseData.Data(countriesList)) | ||
} | ||
|
||
else -> emit(responseToError(response)) | ||
} | ||
} | ||
|
||
override fun getRegions(id: String): Flow<ResponseData<List<Region>>> = flow { | ||
when (val response = networkClient.doRequest(RegionsRequest(id))) { | ||
is RegionsResponse -> { | ||
val regionsList = response.regions.toRegionList() | ||
emit(ResponseData.Data(regionsList)) | ||
} | ||
|
||
else -> emit(responseToError(response)) | ||
} | ||
} | ||
|
||
override fun getAllRegions(): Flow<ResponseData<List<Region>>> = flow { | ||
when (val response = networkClient.doRequest(CountriesRequest)) { | ||
is CountriesResponse -> { | ||
val regionsList = response.countries.toAllRegions() | ||
emit(ResponseData.Data(regionsList)) | ||
} | ||
|
||
else -> emit(responseToError(response)) | ||
} | ||
} | ||
|
||
override fun getIndustries(): Flow<ResponseData<List<Industries>>> = flow { | ||
when (val response = networkClient.doRequest(IndustriesRequest)) { | ||
is IndustriesResponse -> { | ||
val sectorsList = response.sectors.toSectorList() | ||
emit(ResponseData.Data(sectorsList)) | ||
} | ||
|
||
else -> emit(responseToError(response)) | ||
} | ||
} | ||
|
||
private fun <T> responseToError(response: Response): ResponseData<T> = | ||
ResponseData.Error( | ||
when (response.resultCode) { | ||
RESULT_CODE_NO_INTERNET -> ResponseData.ResponseError.NO_INTERNET | ||
RESULT_CODE_BAD_REQUEST -> ResponseData.ResponseError.CLIENT_ERROR | ||
else -> ResponseData.ResponseError.SERVER_ERROR | ||
} | ||
) | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/ru/practicum/android/diploma/data/impl/SharedPrefsRepositoryImpl.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ru.practicum.android.diploma.data.impl | ||
|
||
import android.content.SharedPreferences | ||
import com.google.gson.Gson | ||
import ru.practicum.android.diploma.domain.SharedPrefsRepository | ||
import ru.practicum.android.diploma.domain.models.SaveFiltersSharedPrefs | ||
|
||
class SharedPrefsRepositoryImpl( | ||
private val sharedPreferences: SharedPreferences, | ||
private val gson: Gson | ||
) : SharedPrefsRepository { | ||
override suspend fun readSharedPrefs(): SaveFiltersSharedPrefs? { | ||
val json = sharedPreferences.getString(HISTORY, null) ?: return null | ||
/*SaveFiltersSharedPrefs( | ||
Industries("", "", false), | ||
Country("", ""), | ||
Region("", "", null), | ||
"", | ||
false | ||
)*/ | ||
return gson.fromJson(json, SaveFiltersSharedPrefs::class.java) | ||
} | ||
|
||
override suspend fun writeSharedPrefs(filters: SaveFiltersSharedPrefs) { | ||
/* val oldShared = readSharedPrefs() | ||
val newShared = oldShared.copy( | ||
industries = filters.industries ?: oldShared.industries, | ||
country = filters.country ?: oldShared.country, | ||
region = filters.region ?: oldShared.region, | ||
currency = filters.currency ?: oldShared.currency, | ||
noCurrency = filters.noCurrency | ||
)*/ | ||
sharedPreferences.edit().putString(HISTORY, gson.toJson(filters)).apply() | ||
} | ||
|
||
override suspend fun clearSharedPrefs() { | ||
sharedPreferences.edit().remove(HISTORY).apply() | ||
} | ||
|
||
companion object { | ||
private const val HISTORY = "history" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/ru/practicum/android/diploma/data/network/HHApiService.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
package ru.practicum.android.diploma.data.network | ||
|
||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
import retrofit2.http.QueryMap | ||
import ru.practicum.android.diploma.data.dto.SearchResponse | ||
import ru.practicum.android.diploma.data.dto.VacancyResponse | ||
import ru.practicum.android.diploma.data.dto.components.CountryDto | ||
import ru.practicum.android.diploma.data.dto.components.IndustriesDto | ||
import ru.practicum.android.diploma.data.dto.components.RegionListDto | ||
|
||
interface HHApiService { | ||
@GET("vacancies/{vacancy_id}") | ||
suspend fun getVacancy(@Path("vacancy_id") id: String): VacancyResponse | ||
|
||
@GET("vacancies") | ||
suspend fun searchVacancies(@QueryMap options: Map<String, String>): SearchResponse | ||
|
||
@GET("areas") | ||
suspend fun getCountries(): Response<List<CountryDto>> | ||
|
||
@GET("areas/{area_id}") | ||
suspend fun getRegions(@Path("area_id") areaId: String): Response<RegionListDto> | ||
|
||
@GET("industries") | ||
suspend fun getIndustries(): Response<List<IndustriesDto>> | ||
} |
Oops, something went wrong.