Skip to content

Commit 492c20f

Browse files
authored
Merge pull request #136 from ElchinGasymov/careehub_2iteraton_fix2
Careehub 2iteraton fix2
2 parents 01ee51b + 60902a9 commit 492c20f

File tree

5 files changed

+60
-62
lines changed

5 files changed

+60
-62
lines changed

app/src/main/java/ru/practicum/android/diploma/data/db/converters/ConverterIntoEntity.kt

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,47 @@ class ConverterIntoEntity {
4040
}
4141

4242
fun intoPhoneEntity(vacancy: VacancyDetails): List<PhoneEntity> {
43-
val listPhone = ArrayList<PhoneEntity>()
44-
vacancy.contacts?.phones?.forEach { phonesModel ->
45-
listPhone.add(
46-
PhoneEntity(
47-
idVacancy = vacancy.id,
48-
cityCode = phonesModel.cityCode,
49-
comment = phonesModel.comment,
50-
countryCode = phonesModel.countryCode,
51-
formatted = phonesModel.formatted,
52-
number = phonesModel.number
43+
return buildList {
44+
vacancy.contacts?.phones?.forEach { phonesModel ->
45+
add(
46+
PhoneEntity(
47+
idVacancy = vacancy.id,
48+
cityCode = phonesModel.cityCode,
49+
comment = phonesModel.comment,
50+
countryCode = phonesModel.countryCode,
51+
formatted = phonesModel.formatted,
52+
number = phonesModel.number
53+
)
5354
)
54-
)
55+
}
5556
}
56-
return listPhone
5757
}
5858

5959
fun intoKeySkillEntity(vacancy: VacancyDetails): List<KeySkillEntity> {
60-
val listKey = ArrayList<KeySkillEntity>()
61-
vacancy.keySkills?.forEach { keySkillsModel ->
62-
listKey.add(
63-
KeySkillEntity(
64-
idVacancy = vacancy.id,
65-
name = keySkillsModel.name
60+
return buildList {
61+
vacancy.keySkills?.forEach { keySkillsModel ->
62+
add(
63+
KeySkillEntity(
64+
idVacancy = vacancy.id,
65+
name = keySkillsModel.name
66+
)
6667
)
67-
)
68+
}
6869
}
69-
return listKey
7070
}
7171

7272
fun intoAreaEntity(vacancy: VacancyDetails): List<AreaEntity> {
73-
val listArea = ArrayList<AreaEntity>()
74-
vacancy.area?.areas?.forEach { area ->
75-
listArea.add(
76-
AreaEntity(
77-
idVacancy = vacancy.id,
78-
idArea = area.id,
79-
name = area.name,
80-
countryId = area.countryId
73+
return buildList {
74+
vacancy.area?.areas?.forEach { area ->
75+
add(
76+
AreaEntity(
77+
idVacancy = vacancy.id,
78+
idArea = area.id,
79+
name = area.name,
80+
countryId = area.countryId
81+
)
8182
)
82-
)
83+
}
8384
}
84-
return listArea
8585
}
8686
}

app/src/main/java/ru/practicum/android/diploma/di/DataModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import ru.practicum.android.diploma.data.network.RetrofitNetworkClient
2020
const val HISTORY_MAIN = "historyMain"
2121
const val BASE_URL = "https://api.hh.ru/"
2222
val dataModule = module {
23-
factory<NetworkClient> {
23+
single<NetworkClient> {
2424
RetrofitNetworkClient(
2525
hhApiService = get(),
2626
context = androidContext()

app/src/main/java/ru/practicum/android/diploma/presentation/viewmodels/SearchViewModel.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class SearchViewModel(
2525
const val TWO_SECONDS = 2000L
2626
}
2727

28+
private val _vacanciesLiveData = MutableLiveData<List<Vacancy>>()
29+
val vacanciesLiveData: LiveData<List<Vacancy>> get() = _vacanciesLiveData
30+
2831
private val screenStateLiveData = MutableLiveData<SearchScreenState>()
2932
private var currentPage = 0
3033
private var maxPages = 1
@@ -41,7 +44,7 @@ class SearchViewModel(
4144
false
4245
)
4346
private val _vacancyIsClickable = MutableLiveData(true)
44-
var vacancyIsClickable: LiveData<Boolean> = _vacancyIsClickable
47+
val vacancyIsClickable: LiveData<Boolean> = _vacancyIsClickable
4548

4649
private val vacancySearchDebounce =
4750
debounce<String>(TWO_SECONDS, viewModelScope, true) { query ->
@@ -69,6 +72,10 @@ class SearchViewModel(
6972
return mainRequest
7073
}
7174

75+
private fun updateVacancies(vacancies: List<Vacancy>) {
76+
_vacanciesLiveData.postValue(vacancies)
77+
}
78+
7279
private fun searchVacancies(request: String) {
7380
if (request != this.mainRequest) {
7481
currentPage = 0
@@ -182,11 +189,13 @@ class SearchViewModel(
182189
if (data.isEmpty()) {
183190
setScreenState(SearchScreenState.NothingFound)
184191
} else {
185-
setScreenState(
186-
SearchScreenState.Success(data, quantity)
187-
)
192+
setScreenState(SearchScreenState.Success(data, quantity))
193+
updateVacancies(data)
188194
}
189195
} else {
196+
val currentVacancies = _vacanciesLiveData.value.orEmpty().toMutableList()
197+
currentVacancies.addAll(data)
198+
updateVacancies(currentVacancies)
190199
setScreenState(SearchScreenState.LoadNextPage(data))
191200
}
192201
}

app/src/main/java/ru/practicum/android/diploma/ui/fragments/SearchFragment.kt

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.view.inputmethod.InputMethodManager
1010
import android.widget.TextView
1111
import androidx.appcompat.content.res.AppCompatResources
1212
import androidx.core.os.bundleOf
13+
import androidx.core.view.isGone
1314
import androidx.core.view.isVisible
1415
import androidx.core.widget.doOnTextChanged
1516
import androidx.fragment.app.Fragment
@@ -39,11 +40,8 @@ class SearchFragment : Fragment() {
3940
private val binding: FragmentSearchBinding by viewBinding(CreateMethod.INFLATE)
4041
private val viewModel by viewModel<SearchViewModel>()
4142

42-
private val listOfVacancies = ArrayList<Vacancy>()
4343
private val adapter = VacancyAdapter({ setOnItemClicked(it) })
4444

45-
private var amountVacancies = 0
46-
4745
override fun onCreateView(
4846
inflater: LayoutInflater,
4947
container: ViewGroup?,
@@ -55,12 +53,16 @@ class SearchFragment : Fragment() {
5553
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
5654
super.onViewCreated(view, savedInstanceState)
5755

58-
if (listOfVacancies.isNotEmpty()) {
59-
binding.searchDefaultPlaceholder.isVisible = false
60-
binding.textUnderSearch.text = getCorrectAmountText(amountVacancies)
61-
binding.textUnderSearch.isVisible = true
56+
viewModel.vacanciesLiveData.observe(viewLifecycleOwner) { vacancies ->
57+
adapter.setVacancies(vacancies)
58+
binding.textUnderSearch.text = getCorrectAmountText(vacancies.size)
59+
binding.textUnderSearch.isVisible = vacancies.isNotEmpty()
60+
binding.searchDefaultPlaceholder.isGone = vacancies.isNotEmpty()
6261
}
6362

63+
binding.searchRecycleView.layoutManager = LinearLayoutManager(requireContext())
64+
binding.searchRecycleView.adapter = adapter
65+
6466
binding.filterIc.setOnClickListener {
6567
findNavController().navigate(R.id.action_searchFragment_to_filterFragment)
6668
}
@@ -126,14 +128,10 @@ class SearchFragment : Fragment() {
126128

127129
is SearchScreenState.LoadNextPage -> {
128130
binding.progressBarNextPage.isVisible = false
129-
listOfVacancies.addAll(state.vacancies)
130-
adapter.setVacancies(listOfVacancies)
131131
}
132132

133133
SearchScreenState.Loading -> {
134-
amountVacancies = 0
135-
listOfVacancies.clear()
136-
adapter.setVacancies(listOfVacancies)
134+
clearList()
137135
removePlaceholders()
138136
startProgressBar()
139137
}
@@ -144,14 +142,8 @@ class SearchFragment : Fragment() {
144142
}
145143

146144
is SearchScreenState.Success -> {
147-
listOfVacancies.clear()
148145
stopProgressBar()
149146
removePlaceholders()
150-
listOfVacancies.addAll(state.vacancies)
151-
adapter.setVacancies(listOfVacancies)
152-
amountVacancies = state.found
153-
binding.textUnderSearch.text = getCorrectAmountText(amountVacancies)
154-
binding.textUnderSearch.isVisible = true
155147
}
156148

157149
SearchScreenState.LoadingNextPage -> {
@@ -188,9 +180,6 @@ class SearchFragment : Fragment() {
188180
}
189181
viewModel.getOptions()
190182

191-
val vacanciesRecyclerView = binding.searchRecycleView
192-
vacanciesRecyclerView.layoutManager = LinearLayoutManager(requireContext())
193-
vacanciesRecyclerView.adapter = adapter
194183
initResultListeners()
195184
}
196185

@@ -204,12 +193,10 @@ class SearchFragment : Fragment() {
204193
}
205194

206195
private fun clearList() {
207-
listOfVacancies.clear()
208-
adapter.setVacancies(listOfVacancies)
196+
adapter.setVacancies(emptyList())
209197
}
210198

211199
private fun removePlaceholders() {
212-
binding.textUnderSearch.isVisible = false
213200
binding.noVacancyToShow.isVisible = false
214201
binding.noVacancyToShowText.isVisible = false
215202
binding.noConnectionText.isVisible = false

app/src/main/java/ru/practicum/android/diploma/ui/fragments/VacancyFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,16 @@ class VacancyFragment : Fragment() {
143143
binding.experience.text = vacancyDetails.experience?.name
144144
binding.scheduleEmployment.text = vacancyDetails.employment?.name
145145
binding.dutiesSubtitle.text = Html.fromHtml(vacancyDetails.description, Html.FROM_HTML_MODE_COMPACT)
146+
146147
if (vacancyDetails.keySkills?.isNotEmpty() == true) {
147148
binding.keySkillsGroup.isVisible = true
148-
var keySkillsText = ""
149+
val keySkillsBuilder = StringBuilder()
149150
vacancyDetails.keySkills.forEach {
150-
keySkillsText += StringBuilder().append("").append(it.name).append("\n").toString()
151+
keySkillsBuilder.append("").append(it.name).append("\n")
151152
}
152-
binding.coreSkills.text = keySkillsText
153+
binding.coreSkills.text = keySkillsBuilder.toString()
153154
}
155+
154156
setVacancyContacts(vacancyDetails)
155157
binding.vacancyDetailsLayout.isVisible = true
156158

0 commit comments

Comments
 (0)