Skip to content

Commit

Permalink
Merge pull request #142 from ElchinGasymov/Career_Hub-edits-3-iterations
Browse files Browse the repository at this point in the history
edits 3 iterations
  • Loading branch information
ElchinGasymov authored Aug 25, 2024
2 parents 492c20f + 7fc1940 commit fe124c9
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ru.practicum.android.diploma.data.impl

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import ru.practicum.android.diploma.data.dto.CountriesRequest
import ru.practicum.android.diploma.data.dto.CountriesResponse
import ru.practicum.android.diploma.data.dto.IndustriesRequest
Expand Down Expand Up @@ -35,7 +37,7 @@ class FilterRepositoryImpl(

else -> emit(responseToError(response))
}
}
}.flowOn(Dispatchers.IO)

override fun getRegions(id: String): Flow<ResponseData<List<Region>>> = flow {
when (val response = networkClient.doRequest(RegionsRequest(id))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,10 @@ class SharedPrefsRepositoryImpl(
) : 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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ class FilterIndustryViewModel(

private fun whenList(industry: Industries?, list: ResponseData.Data<List<Industries>>) {
if (industry != null) {
val newList = ArrayList<Industries>()
list.value.forEach { industryItem ->
if (industryItem.id == industry.id) {
newList.add(industryItem.copy(isChecked = true))
_selectedIndustry.postValue(industryItem)
val newList = list.value.map {
if (industry.id == it.id) {
_selectedIndustry.postValue(it)
_hasSelected.postValue(true)
selectedId = industryItem.id
selectedId = it.id
Industries(it.id, it.name, true)
} else {
newList.add(industryItem)
it
}
}
_industries.postValue(newList)
Expand All @@ -85,19 +84,20 @@ class FilterIndustryViewModel(
}

fun search(request: String) {
val sortedList = mutableListOf<Industries>()
val newSortedList = mutableListOf<Industries>()
sortedList.addAll(listOfIndustries)
sortedList.removeAll {
!it.name.contains(request, true)
val sortedList = listOfIndustries.mapNotNull {
if (it.name.contains(request, true)) {
it
} else {
null
}
}
sortedList.forEach {
val newSortedList = sortedList.map {
if (it.id == selectedId) {
newSortedList.add(Industries(it.id, it.name, true))
_selectedIndustry.postValue(it)
_hasSelected.postValue(true)
Industries(it.id, it.name, true)
} else {
newSortedList.add(it)
it
}
}
if (sortedList.isNotEmpty()) {
Expand All @@ -106,7 +106,6 @@ class FilterIndustryViewModel(
_industries.postValue(newSortedList)
_error.postValue(ResponseError.NOT_FOUND)
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import ru.practicum.android.diploma.domain.FilterInteractor
import ru.practicum.android.diploma.domain.models.Country
Expand All @@ -23,7 +22,7 @@ class FilterPlaceOfWorkViewModel(
}

fun getCountryName(region: Region, isSaving: Boolean) {
viewModelScope.launch(Dispatchers.IO) {
viewModelScope.launch {
filterInteractor
.getCountries()
.collect { response ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.CreateMethod
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.gson.Gson
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import ru.practicum.android.diploma.databinding.FragmentSelectCountryBinding
import ru.practicum.android.diploma.domain.models.Country
import ru.practicum.android.diploma.presentation.viewmodels.FilterCountryViewModel
import ru.practicum.android.diploma.ui.state.CountriesScreenState
import ru.practicum.android.diploma.util.COUNTRY_BUNDLE_KEY
import ru.practicum.android.diploma.util.COUNTRY_REQUEST_KEY
import ru.practicum.android.diploma.util.ResponseData
import ru.practicum.android.diploma.util.adapter.country.CountryAdapter

class FilterCountryFragment : Fragment() {
private val binding: FragmentSelectCountryBinding by viewBinding(CreateMethod.INFLATE)
private val viewModel by viewModel<FilterCountryViewModel>()
private val gson: Gson by inject()

companion object {
const val COUNTRY_REQUEST_KEY = "COUNTRY_REQUEST_KEY"
const val COUNTRY_BUNDLE_KEY = "COUNTRY_BUNDLE_KEY"
}

private val adapter = CountryAdapter {
onItemClicked(it)
Expand Down Expand Up @@ -80,7 +85,7 @@ class FilterCountryFragment : Fragment() {
}

private fun onItemClicked(country: Country) {
val json = Gson().toJson(country)
val json = gson.toJson(country)
setFragmentResult(COUNTRY_REQUEST_KEY, bundleOf(COUNTRY_BUNDLE_KEY to json))
findNavController().navigateUp()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import by.kirich1409.viewbindingdelegate.CreateMethod
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import ru.practicum.android.diploma.R
import ru.practicum.android.diploma.databinding.FragmentFilterBinding
Expand All @@ -27,20 +28,23 @@ import ru.practicum.android.diploma.domain.models.SaveFiltersSharedPrefs
import ru.practicum.android.diploma.presentation.viewmodels.FilterViewModel
import ru.practicum.android.diploma.ui.fragments.FilterIndustryFragment.Companion.INDUSTRY_ITEM_KEY
import ru.practicum.android.diploma.ui.fragments.FilterIndustryFragment.Companion.INDUSTRY_KEY
import ru.practicum.android.diploma.ui.fragments.FilterPlaceOfWorkFragment.Companion.PLACE_OF_WORK_COUNTRY_KEY
import ru.practicum.android.diploma.ui.fragments.FilterPlaceOfWorkFragment.Companion.PLACE_OF_WORK_KEY
import ru.practicum.android.diploma.ui.fragments.FilterPlaceOfWorkFragment.Companion.PLACE_OF_WORK_REGION_KEY
import ru.practicum.android.diploma.ui.state.FilterScreenState
import ru.practicum.android.diploma.util.FILTER_BUNDLE_KEY
import ru.practicum.android.diploma.util.FILTER_REQUEST_KEY
import ru.practicum.android.diploma.util.FILTER_TO_PLACE_OF_WORK_COUNTRY_KEY
import ru.practicum.android.diploma.util.FILTER_TO_PLACE_OF_WORK_KEY
import ru.practicum.android.diploma.util.FILTER_TO_PLACE_OF_WORK_REGION_KEY
import ru.practicum.android.diploma.util.PLACE_OF_WORK_COUNTRY_KEY
import ru.practicum.android.diploma.util.PLACE_OF_WORK_KEY
import ru.practicum.android.diploma.util.PLACE_OF_WORK_REGION_KEY

class FilterFragment : Fragment() {
private val binding: FragmentFilterBinding by viewBinding(CreateMethod.INFLATE)
private val viewModel by viewModel<FilterViewModel>()

private val gson: Gson by inject()

companion object {
const val FILTER_TO_PLACE_OF_WORK_KEY = "FILTER_TO_PLACE_OF_WORK_KEY"
const val FILTER_TO_PLACE_OF_WORK_COUNTRY_KEY = "FILTER_TO_PLACE_OF_WORK_COUNTRY_KEY"
const val FILTER_TO_PLACE_OF_WORK_REGION_KEY = "FILTER_TO_PLACE_OF_WORK_REGION_KEY"
const val FILTER_REQUEST_KEY = "FILTER_REQUEST_KEY"
const val FILTER_BUNDLE_KEY = "FILTER_BUNDLE_KEY"
}

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -100,7 +104,7 @@ class FilterFragment : Fragment() {
}

is FilterScreenState.FiltersSaved -> {
val json = Gson().toJson(state.filters)
val json = gson.toJson(state.filters)
setFragmentResult(FILTER_REQUEST_KEY, bundleOf(FILTER_BUNDLE_KEY to json))
findNavController().navigateUp()
}
Expand Down Expand Up @@ -337,8 +341,8 @@ class FilterFragment : Fragment() {
}

private fun navigateToPlaceOfWorkScreen() {
val jsonCountry = Gson().toJson(viewModel.getCountry())
val jsonRegion = Gson().toJson(viewModel.getRegion())
val jsonCountry = gson.toJson(viewModel.getCountry())
val jsonRegion = gson.toJson(viewModel.getRegion())
setFragmentResult(
FILTER_TO_PLACE_OF_WORK_KEY,
bundleOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.navigation.fragment.findNavController
import by.kirich1409.viewbindingdelegate.CreateMethod
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.gson.Gson
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import ru.practicum.android.diploma.databinding.FragmentSelectIndustryBinding
import ru.practicum.android.diploma.presentation.viewmodels.FilterIndustryViewModel
Expand All @@ -28,6 +29,7 @@ class FilterIndustryFragment : Fragment() {

private val binding: FragmentSelectIndustryBinding by viewBinding(CreateMethod.INFLATE)
private val viewModel: FilterIndustryViewModel by viewModel()
private val gson: Gson by inject()
private val adapter by lazy {
IndustryAdapter(
onClick = { industryList ->
Expand Down Expand Up @@ -107,7 +109,7 @@ class FilterIndustryFragment : Fragment() {

private fun setupApplyButton() {
binding.applyButton.setOnClickListener {
val json = Gson().toJson(viewModel.selectedIndustry.value)
val json = gson.toJson(viewModel.selectedIndustry.value)
setFragmentResult(INDUSTRY_KEY, bundleOf(INDUSTRY_ITEM_KEY to json))
findNavController().popBackStack()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,35 @@ import by.kirich1409.viewbindingdelegate.CreateMethod
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import ru.practicum.android.diploma.R
import ru.practicum.android.diploma.databinding.FragmentSelectPlaceOfWorkBinding
import ru.practicum.android.diploma.domain.models.Country
import ru.practicum.android.diploma.domain.models.Region
import ru.practicum.android.diploma.presentation.viewmodels.FilterPlaceOfWorkViewModel
import ru.practicum.android.diploma.ui.fragments.FilterCountryFragment.Companion.COUNTRY_BUNDLE_KEY
import ru.practicum.android.diploma.ui.fragments.FilterCountryFragment.Companion.COUNTRY_REQUEST_KEY
import ru.practicum.android.diploma.ui.fragments.FilterFragment.Companion.FILTER_TO_PLACE_OF_WORK_COUNTRY_KEY
import ru.practicum.android.diploma.ui.fragments.FilterFragment.Companion.FILTER_TO_PLACE_OF_WORK_KEY
import ru.practicum.android.diploma.ui.fragments.FilterFragment.Companion.FILTER_TO_PLACE_OF_WORK_REGION_KEY
import ru.practicum.android.diploma.ui.fragments.FilterRegionFragment.Companion.REGION_BUNDLE_KEY
import ru.practicum.android.diploma.ui.fragments.FilterRegionFragment.Companion.REGION_REQUEST_KEY
import ru.practicum.android.diploma.ui.state.PlaceOfWorkScreenState
import ru.practicum.android.diploma.util.COUNTRY_BUNDLE_KEY
import ru.practicum.android.diploma.util.COUNTRY_REQUEST_KEY
import ru.practicum.android.diploma.util.FILTER_TO_PLACE_OF_WORK_COUNTRY_KEY
import ru.practicum.android.diploma.util.FILTER_TO_PLACE_OF_WORK_KEY
import ru.practicum.android.diploma.util.FILTER_TO_PLACE_OF_WORK_REGION_KEY
import ru.practicum.android.diploma.util.PLACE_OF_WORK_COUNTRY_KEY
import ru.practicum.android.diploma.util.PLACE_OF_WORK_KEY
import ru.practicum.android.diploma.util.PLACE_OF_WORK_REGION_KEY
import ru.practicum.android.diploma.util.REGION_BUNDLE_KEY
import ru.practicum.android.diploma.util.REGION_ID_KEY
import ru.practicum.android.diploma.util.REGION_REQUEST_KEY

class FilterPlaceOfWorkFragment : Fragment() {
private val binding: FragmentSelectPlaceOfWorkBinding by viewBinding(CreateMethod.INFLATE)
private val viewModel by viewModel<FilterPlaceOfWorkViewModel>()
private var country = Country("", "")
private var region = Region("", "", null)
private val gson: Gson by inject()

companion object {
const val REGION_ID_KEY = "REGION_ID_KEY"
const val PLACE_OF_WORK_KEY = "PLACE_OF_WORK_KEY"
const val PLACE_OF_WORK_COUNTRY_KEY = "PLACE_OF_WORK_COUNTRY_KEY"
const val PLACE_OF_WORK_REGION_KEY = "PLACE_OF_WORK_REGION_KEY"
}

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -104,8 +109,8 @@ class FilterPlaceOfWorkFragment : Fragment() {
}

private fun handleSavedState(state: PlaceOfWorkScreenState.Saved) {
val jsonCountry = Gson().toJson(state.country)
val jsonRegion = Gson().toJson(state.region)
val jsonCountry = gson.toJson(state.country)
val jsonRegion = gson.toJson(state.region)
setFragmentResult(
PLACE_OF_WORK_KEY,
bundleOf(
Expand Down Expand Up @@ -134,7 +139,7 @@ class FilterPlaceOfWorkFragment : Fragment() {
setFragmentResultListener(COUNTRY_REQUEST_KEY) { _, bundle ->
val json = bundle.getString(COUNTRY_BUNDLE_KEY).toString()
val type = object : TypeToken<Country>() {}.type
country = Gson().fromJson(json, type)
country = gson.fromJson(json, type)
viewModel.setCountryName(country)
setNoRegionEndIcon()
binding.regionTextInput.text?.clear()
Expand All @@ -143,18 +148,18 @@ class FilterPlaceOfWorkFragment : Fragment() {
setFragmentResultListener(REGION_REQUEST_KEY) { _, bundle ->
val json = bundle.getString(REGION_BUNDLE_KEY).toString()
val type = object : TypeToken<Region>() {}.type
region = Gson().fromJson(json, type)
region = gson.fromJson(json, type)
viewModel.getCountryName(region, false)
viewModel.setRegionName(region)
}

setFragmentResultListener(FILTER_TO_PLACE_OF_WORK_KEY) { _, bundle ->
val json = bundle.getString(FILTER_TO_PLACE_OF_WORK_COUNTRY_KEY).toString()
val type = object : TypeToken<Country>() {}.type
country = Gson().fromJson(json, type)
country = gson.fromJson(json, type)
val jsonRegion = bundle.getString(FILTER_TO_PLACE_OF_WORK_REGION_KEY).toString()
val typeRegion = object : TypeToken<Region>() {}.type
region = Gson().fromJson(jsonRegion, typeRegion)
region = gson.fromJson(jsonRegion, typeRegion)
handleLoadedFilters()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@ import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.CreateMethod
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.gson.Gson
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import ru.practicum.android.diploma.databinding.FragmentSelectRegionBinding
import ru.practicum.android.diploma.domain.models.Region
import ru.practicum.android.diploma.presentation.viewmodels.FilterRegionViewModel
import ru.practicum.android.diploma.ui.state.RegionsScreenState
import ru.practicum.android.diploma.util.App.Companion.REGION_ID_KEY
import ru.practicum.android.diploma.util.REGION_BUNDLE_KEY
import ru.practicum.android.diploma.util.REGION_REQUEST_KEY
import ru.practicum.android.diploma.util.ResponseData
import ru.practicum.android.diploma.util.adapter.region.RegionAdapter

class FilterRegionFragment : Fragment() {
private val binding: FragmentSelectRegionBinding by viewBinding(CreateMethod.INFLATE)
private val viewModel by viewModel<FilterRegionViewModel>()
private val gson: Gson by inject()
private val adapter = RegionAdapter {
onItemClicked(it)
}

companion object {
const val REGION_REQUEST_KEY = "REGION_REQUEST_KEY"
const val REGION_BUNDLE_KEY = "REGION_BUNDLE_KEY"
const val REGION_ID_KEY = "REGION_ID_KEY"
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -109,7 +114,7 @@ class FilterRegionFragment : Fragment() {
}

private fun onItemClicked(region: Region) {
val json = Gson().toJson(region)
val json = gson.toJson(region)
setFragmentResult(
REGION_REQUEST_KEY,
bundleOf(REGION_BUNDLE_KEY to json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import ru.practicum.android.diploma.R
import ru.practicum.android.diploma.databinding.FragmentSearchBinding
import ru.practicum.android.diploma.domain.models.Vacancy
import ru.practicum.android.diploma.presentation.viewmodels.SearchViewModel
import ru.practicum.android.diploma.ui.fragments.FilterFragment.Companion.FILTER_REQUEST_KEY
import ru.practicum.android.diploma.ui.state.SearchScreenState
import ru.practicum.android.diploma.util.FILTER_REQUEST_KEY
import ru.practicum.android.diploma.util.ResponseData
import ru.practicum.android.diploma.util.adapter.VacancyAdapter

Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/ru/practicum/android/diploma/util/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@ class App : Application() {
)
}
}
companion object {
const val REGION_ID_KEY = "REGION_ID_KEY"
const val PLACE_OF_WORK_KEY = "PLACE_OF_WORK_KEY"
const val PLACE_OF_WORK_COUNTRY_KEY = "PLACE_OF_WORK_COUNTRY_KEY"
const val PLACE_OF_WORK_REGION_KEY = "PLACE_OF_WORK_REGION_KEY"
}
}
Loading

0 comments on commit fe124c9

Please sign in to comment.