Skip to content

Commit

Permalink
Merge pull request #132 from ElchinGasymov/iteration_3_review
Browse files Browse the repository at this point in the history
Iteration 3 review
  • Loading branch information
G-dev-ui authored Aug 23, 2024
2 parents 8ff3f02 + 58a9ab1 commit 45d4357
Show file tree
Hide file tree
Showing 68 changed files with 3,273 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package ru.practicum.android.diploma.data.dto

import ru.practicum.android.diploma.data.dto.components.Area
import ru.practicum.android.diploma.data.dto.components.Contacts
import ru.practicum.android.diploma.data.dto.components.CountryDto
import ru.practicum.android.diploma.data.dto.components.Employer
import ru.practicum.android.diploma.data.dto.components.Employment
import ru.practicum.android.diploma.data.dto.components.Experience
import ru.practicum.android.diploma.data.dto.components.KeySkill
import ru.practicum.android.diploma.data.dto.components.LogoUrls
import ru.practicum.android.diploma.data.dto.components.Phone
import ru.practicum.android.diploma.data.dto.components.RegionDto
import ru.practicum.android.diploma.data.dto.components.Salary
import ru.practicum.android.diploma.data.dto.components.Schedule
import ru.practicum.android.diploma.data.dto.components.IndustriesDto
import ru.practicum.android.diploma.domain.models.Country
import ru.practicum.android.diploma.domain.models.Region
import ru.practicum.android.diploma.domain.models.Industries
import ru.practicum.android.diploma.domain.models.VacanciesResponse
import ru.practicum.android.diploma.domain.models.Vacancy
import ru.practicum.android.diploma.domain.models.VacancyDetails
Expand Down Expand Up @@ -75,3 +81,40 @@ fun Contacts.toModel(): ContactsModel {
val phones = phones?.map { it.toModel() }
return ContactsModel(email, name, phones)
}

fun CountryDto.toModel(): Country {
return Country(
id = this.id,
name = this.name
)
}

fun RegionDto.toModel(): Region {
return Region(
id = this.id,
name = this.name,
parentId = this.parentId
)
}

fun List<IndustriesDto>.toSectorList(): List<Industries> {
return this.map {
Industries(
id = it.id,
name = it.name,
isChecked = false
)
}
}

fun List<CountryDto>.toCountryList(): List<Country> {
return this.map { it.toModel() }
}

fun List<RegionDto>.toRegionList(): List<Region> {
return this.map { it.toModel() }
}

fun List<CountryDto>.toAllRegions(): List<Region> {
return this.flatMap { it.areas.map { regionDto -> regionDto.toModel() } }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ru.practicum.android.diploma.data.dto

data object CountriesRequest
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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ru.practicum.android.diploma.data.dto

data object IndustriesRequest
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()
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
)
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()
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
}
}
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>
)
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
)
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?
)
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
)
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
}
)
}
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"
}
}
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>>
}
Loading

0 comments on commit 45d4357

Please sign in to comment.