forked from Yandex-Practicum/practicum-android-diploma
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Реализация Data слоя поиска #36
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8b7db1
Добавил все нужные data классы
G-dev-ui f7fec8e
Добавил все нужные data классы
G-dev-ui edbe85e
Merge remote-tracking branch 'origin/CareerHub_add-Data-search' into …
G-dev-ui a3f7ced
Финальная версия
G-dev-ui 050c301
Merge remote-tracking branch 'origin/CareerHub_add-Data-search' into …
G-dev-ui ae351d5
Финальная версия
G-dev-ui bddec2d
Финальная версия + fix detect
G-dev-ui 385eab8
Финальная версия + fix detect(2)
G-dev-ui c15ff8a
Убрал класс конвертора и подправил ошибки с респонзом
G-dev-ui 9055618
Вернул обратно класс конвертора из за детекта
G-dev-ui 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
3 changes: 3 additions & 0 deletions
3
app/src/main/java/ru/practicum/android/diploma/data/components/Address.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.components | ||
|
||
data class Address(val raw: String?) |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ru/practicum/android/diploma/data/components/Area.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.components | ||
|
||
data class Area( | ||
val id: Int, | ||
val name: String | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/practicum/android/diploma/data/components/Contacts.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.components | ||
|
||
data class Contacts( | ||
val email: String, | ||
val name: String, | ||
val phones: List<Phone>?, | ||
) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/ru/practicum/android/diploma/data/components/Employer.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,8 @@ | ||
package ru.practicum.android.diploma.data.components | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class Employer( | ||
val name: String, | ||
@SerializedName("logo_urls") val logoUrls: LogoUrls? | ||
) |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ru/practicum/android/diploma/data/components/Employment.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.components | ||
|
||
data class Employment( | ||
val id: String, | ||
val name: String | ||
) |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ru/practicum/android/diploma/data/components/Experience.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.components | ||
|
||
data class Experience( | ||
val id: String, | ||
val name: String | ||
) |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/ru/practicum/android/diploma/data/components/KeySkill.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.components | ||
|
||
data class KeySkill( | ||
val name: String | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/practicum/android/diploma/data/components/LogoUrls.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.components | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class LogoUrls( | ||
@SerializedName("240") val logo240: String? | ||
) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/ru/practicum/android/diploma/data/components/Phone.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,8 @@ | ||
package ru.practicum.android.diploma.data.components | ||
|
||
data class Phone( | ||
val city: String, | ||
val comment: String?, | ||
val country: String, | ||
val formatted: String, | ||
) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/ru/practicum/android/diploma/data/components/Salary.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,8 @@ | ||
package ru.practicum.android.diploma.data.components | ||
|
||
data class Salary( | ||
val currency: String?, | ||
val from: Int?, | ||
val to: Int?, | ||
val gross: Boolean? | ||
) |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ru/practicum/android/diploma/data/components/Schedule.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.components | ||
|
||
data class Schedule( | ||
val id: String, | ||
val name: String | ||
) |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/ru/practicum/android/diploma/data/dto/Converters.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,16 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
import ru.practicum.android.diploma.domain.models.Vacancy | ||
|
||
fun VacancyDto.toVacancy(): Vacancy { | ||
return Vacancy( | ||
id = id, | ||
name = name, | ||
company = employer.name, | ||
currency = salary?.currency.orEmpty(), | ||
salaryFrom = salary?.from, | ||
salaryTo = salary?.to, | ||
area = area.name, | ||
icon = employer.logoUrls?.logo240 ?: "" | ||
) | ||
} |
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
8 changes: 8 additions & 0 deletions
8
app/src/main/java/ru/practicum/android/diploma/data/dto/SearchResponse.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,8 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
data class SearchResponse( | ||
val items: List<VacancyDto>, | ||
val found: Int, | ||
val pages: Int, | ||
val page: Int, | ||
) : Response() |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/ru/practicum/android/diploma/data/dto/VacancyDto.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,13 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
import ru.practicum.android.diploma.data.components.Area | ||
import ru.practicum.android.diploma.data.components.Employer | ||
import ru.practicum.android.diploma.data.components.Salary | ||
|
||
data class VacancyDto( | ||
val id: Int, | ||
val name: String, | ||
val employer: Employer, | ||
val salary: Salary?, | ||
val area: Area | ||
) |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/ru/practicum/android/diploma/data/dto/VacancyResponse.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,30 @@ | ||
package ru.practicum.android.diploma.data.dto | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import ru.practicum.android.diploma.data.components.Address | ||
import ru.practicum.android.diploma.data.components.Area | ||
import ru.practicum.android.diploma.data.components.Contacts | ||
import ru.practicum.android.diploma.data.components.Employer | ||
import ru.practicum.android.diploma.data.components.Employment | ||
import ru.practicum.android.diploma.data.components.Experience | ||
import ru.practicum.android.diploma.data.components.KeySkill | ||
import ru.practicum.android.diploma.data.components.Salary | ||
import ru.practicum.android.diploma.data.components.Schedule | ||
|
||
data class VacancyResponse( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Может лучше id сделать Long? Я думаю id может быть большим числом. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
val id: Int, | ||
val name: String, | ||
val employer: Employer, | ||
val salary: Salary?, | ||
val area: Area, | ||
@SerializedName("alternate_url") | ||
val alternateUrl: String, | ||
val description: String, | ||
val employment: Employment?, | ||
val experience: Experience?, | ||
val schedule: Schedule?, | ||
val contacts: Contacts?, | ||
@SerializedName("key_skills") | ||
val keySkills: List<KeySkill>, | ||
val address: Address? | ||
) : Response() |
58 changes: 58 additions & 0 deletions
58
app/src/main/java/ru/practicum/android/diploma/data/impl/SearchRepositoryImpl.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,58 @@ | ||
package ru.practicum.android.diploma.data.impl | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
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.Response | ||
import ru.practicum.android.diploma.data.dto.SearchRequest | ||
import ru.practicum.android.diploma.data.dto.SearchResponse | ||
import ru.practicum.android.diploma.data.dto.VacancyDto | ||
import ru.practicum.android.diploma.data.dto.toVacancy | ||
import ru.practicum.android.diploma.data.network.NetworkClient | ||
import ru.practicum.android.diploma.domain.api.SearchRepository | ||
import ru.practicum.android.diploma.domain.models.VacanciesResponse | ||
import ru.practicum.android.diploma.util.Options | ||
import ru.practicum.android.diploma.util.ResponseData | ||
|
||
class SearchRepositoryImpl(private val networkClient: NetworkClient) : SearchRepository { | ||
|
||
override fun search(options: Options): Flow<ResponseData<VacanciesResponse>> = flow { | ||
emit( | ||
when (val response = networkClient.doRequest(SearchRequest(Options.toMap(options)))) { | ||
is SearchResponse -> { | ||
with(response) { | ||
val vacanciesResponse = VacanciesResponse( | ||
items.map(VacancyDto::toVacancy), | ||
found, | ||
page, | ||
pages, | ||
) | ||
ResponseData.Data(vacanciesResponse) | ||
} | ||
} | ||
|
||
else -> { | ||
responseToError(response) | ||
} | ||
} | ||
) | ||
} | ||
|
||
private fun responseToError(response: Response): ResponseData<VacanciesResponse> = | ||
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 | ||
} | ||
} | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/ru/practicum/android/diploma/data/interceptors/HeaderInterceptor.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,24 @@ | ||
package ru.practicum.android.diploma.data.interceptors | ||
|
||
import okhttp3.Headers | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import ru.practicum.android.diploma.BuildConfig | ||
|
||
object HeaderInterceptor : Interceptor { | ||
private const val USER_AGENT_AUTHORIZATION = "Authorization: Bearer ${BuildConfig.HH_ACCESS_TOKEN}" | ||
private const val USER_AGENT_APP_NAME = "HH-User-Agent: CareerHub ([email protected])" | ||
|
||
private val headers = Headers.Builder() | ||
.add(USER_AGENT_AUTHORIZATION) | ||
.add(USER_AGENT_APP_NAME) | ||
.build() | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request = chain.request().newBuilder() | ||
.headers(headers) | ||
.build() | ||
|
||
return chain.proceed(request) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/java/ru/practicum/android/diploma/data/interceptors/LoggingInterceptor.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,48 @@ | ||
package ru.practicum.android.diploma.data.interceptors | ||
|
||
import android.util.Log | ||
import okhttp3.Interceptor | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okio.Buffer | ||
import java.nio.charset.Charset | ||
import kotlin.math.min | ||
|
||
object LoggingInterceptor : Interceptor { | ||
private val CHARSET: Charset = Charset.forName("UTF-8") | ||
private const val MAX_LOGCAT_SIZE = 4_000L | ||
private const val LOGCAT_TAG = "LoggingInterceptor" | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request = chain.request() | ||
logRequest(request) | ||
|
||
val response = chain.proceed(request) | ||
logResponse(response) | ||
return response | ||
} | ||
|
||
private fun logRequest(request: Request) { | ||
logD("request =>\n${request.url()}") | ||
val buffer = Buffer() | ||
request.body()?.writeTo(buffer) | ||
while (!buffer.exhausted()) { | ||
logD(buffer.readString(min(MAX_LOGCAT_SIZE, buffer.size()), CHARSET)) | ||
} | ||
} | ||
|
||
private fun logResponse(response: Response) { | ||
response.body()?.apply { | ||
source().request(Long.MAX_VALUE) | ||
val buffer = source().buffer.clone() | ||
logD("response =>\n") | ||
while (!buffer.exhausted()) { | ||
logD(buffer.readString(min(MAX_LOGCAT_SIZE, buffer.size()), CHARSET)) | ||
} | ||
} | ||
} | ||
|
||
private fun logD(message: String) { | ||
Log.d(LOGCAT_TAG, message) | ||
} | ||
} |
14 changes: 4 additions & 10 deletions
14
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,21 +1,15 @@ | ||
package ru.practicum.android.diploma.data.network | ||
|
||
import retrofit2.http.GET | ||
import retrofit2.http.Headers | ||
import retrofit2.http.Path | ||
import retrofit2.http.QueryMap | ||
import ru.practicum.android.diploma.BuildConfig | ||
import ru.practicum.android.diploma.data.dto.Response | ||
|
||
const val USER_AGENT_AUTHORIZATION = "Authorization: Bearer ${BuildConfig.HH_ACCESS_TOKEN}" | ||
const val USER_AGENT_APP_NAME = "HH-User-Agent: CareerHub ([email protected])" | ||
import ru.practicum.android.diploma.data.dto.SearchResponse | ||
import ru.practicum.android.diploma.data.dto.VacancyResponse | ||
|
||
interface HHApiService { | ||
@Headers(USER_AGENT_AUTHORIZATION, USER_AGENT_APP_NAME) | ||
@GET("vacancies/{vacancy_id}") | ||
suspend fun getVacancy(@Path("vacancy_id") id: Int): Response | ||
suspend fun getVacancy(@Path("vacancy_id") id: Int): VacancyResponse | ||
|
||
@Headers(USER_AGENT_AUTHORIZATION, USER_AGENT_APP_NAME) | ||
@GET("vacancies") | ||
suspend fun searchVacancies(@QueryMap options: Map<String, String>): Response | ||
suspend fun searchVacancies(@QueryMap options: Map<String, String>): SearchResponse | ||
} |
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
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
10 changes: 10 additions & 0 deletions
10
app/src/main/java/ru/practicum/android/diploma/domain/api/SearchRepository.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,10 @@ | ||
package ru.practicum.android.diploma.domain.api | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import ru.practicum.android.diploma.domain.models.VacanciesResponse | ||
import ru.practicum.android.diploma.util.Options | ||
import ru.practicum.android.diploma.util.ResponseData | ||
|
||
interface SearchRepository { | ||
fun search(options: Options): Flow<ResponseData<VacanciesResponse>> | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/ru/practicum/android/diploma/domain/models/VacanciesResponse.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,8 @@ | ||
package ru.practicum.android.diploma.domain.models | ||
|
||
data class VacanciesResponse( | ||
val results: List<Vacancy>, | ||
val foundVacancies: Int, | ||
val page: Int, | ||
val pages: Int, | ||
) |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/ru/practicum/android/diploma/domain/models/Vacancy.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,12 @@ | ||
package ru.practicum.android.diploma.domain.models | ||
|
||
data class Vacancy( | ||
val id: Int, | ||
val name: String, | ||
val company: String, | ||
val currency: String, | ||
val salaryFrom: Int?, | ||
val salaryTo: Int?, | ||
val area: String, | ||
val icon: String | ||
) |
Oops, something went wrong.
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.
Мы же вроде догаваривались что не будем писать конвертеры, а делать все через .map? Как например в файле data class Options