Skip to content

Commit

Permalink
Merge pull request #129 from AntaOtk/filter128
Browse files Browse the repository at this point in the history
настроить выбор отрасли#128
  • Loading branch information
AntaOtk authored Nov 15, 2023
2 parents 02a2a00 + db0759b commit ad7b0c3
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ru.practicum.android.diploma.data.ResourceProvider
import ru.practicum.android.diploma.data.ResourceProviderImpl
import ru.practicum.android.diploma.data.db.AppDataBase
import ru.practicum.android.diploma.data.filter.local.LocalStorage
import ru.practicum.android.diploma.data.filter.local.SharedPreferensClient
import ru.practicum.android.diploma.data.filter.local.SharedPreferenceClient
import ru.practicum.android.diploma.data.network.ApiService
import ru.practicum.android.diploma.data.network.RetrofitNetworkClient
import ru.practicum.android.diploma.domain.ExternalNavigator
Expand Down Expand Up @@ -66,7 +66,7 @@ val dataModule = module {
}

single<LocalStorage> {
SharedPreferensClient(get(), get())
SharedPreferenceClient(get(), get())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ class FilterRepositoryImpl(
override fun clear(){
localStorage.clear()
}

override fun getCheckedStatus(): Boolean {
return localStorage.getCheckedStatus()
}

override fun setCheckedStatus(isChecked: Boolean) {
localStorage.setCheckedStatus(isChecked)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ interface LocalStorage {

fun setSalary(salary: String)
fun getSalary(): String

fun setSelectedCountry(country: Country?)
fun getSelectedCountry(): Country?

fun setSelectedArea(area: Area?)
fun getSelectedArea(): Area?

fun setSelectedIndustry(industry: Industry?)
fun getSelectedIndustry(): Industry?
fun clear()
fun getCheckedStatus(): Boolean
fun setCheckedStatus(isChecked: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ru.practicum.android.diploma.domain.models.filter.Area
import ru.practicum.android.diploma.domain.models.filter.Country
import ru.practicum.android.diploma.domain.models.filter.Industry

class SharedPreferensClient(val gson: Gson, private val sharedPreferences: SharedPreferences) :
class SharedPreferenceClient(val gson: Gson, private val sharedPreferences: SharedPreferences) :
LocalStorage {

private val editor = sharedPreferences.edit()
Expand Down Expand Up @@ -48,7 +48,7 @@ class SharedPreferensClient(val gson: Gson, private val sharedPreferences: Share
override fun setSelectedIndustry(industry: Industry?) {
val industryJson = gson.toJson(industry)
editor
.putString(SELECTED_AREA_KEY, industryJson)
.putString(SELECTED_INDUSTRY_KEY, industryJson)
.apply()
}

Expand All @@ -57,13 +57,21 @@ class SharedPreferensClient(val gson: Gson, private val sharedPreferences: Share
return gson.fromJson(industryJson, Industry::class.java)
}


override fun clear() {
editor.clear().apply()
}

override fun getCheckedStatus(): Boolean {
return sharedPreferences.getBoolean(SALARY_FLAG, false)
}

override fun setCheckedStatus(isChecked: Boolean) {
editor.putBoolean(SALARY_FLAG, isChecked)
}

companion object {
const val SALARY_KEY = "salary"
const val SALARY_FLAG = "salaryFlag"
const val SELECTED_COUNTRY_KEY = "selectedCountry"
const val SELECTED_AREA_KEY = "selectedArea"
const val SELECTED_INDUSTRY_KEY = "selectedIndustry"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ interface FilterInteractor {

fun setSelectedIdustries(industry: Industry?)

fun getSelectedIndustries(): Industry?
fun getSelectedIndustry(): Industry?
fun setSalaryStatus(isChecked:Boolean)
fun getSalaryStatus():Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ interface FilterRepository {

fun setSelectedIndustry(industry: Industry?)
fun clear()
fun getCheckedStatus(): Boolean
fun setCheckedStatus(isChecked: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ class FilterInteractorImpl(
val country = getSelectedCountry()
val area = getSelectedArea()
val preferSalary = getSalary()
// val industry вписать получение индустрии
// val isChecked значение галочки
val industry = getSelectedIndustry()
val isChecked = getCheckedStatus()
return Filters(
country, area, null, preferSalary, false
country, area, industry, preferSalary, isChecked
)
}

private fun getCheckedStatus(): Boolean {
return repository.getCheckedStatus()
}

override fun clearFilters() {
repository.clear()
}
Expand All @@ -102,7 +106,15 @@ class FilterInteractorImpl(
repository.setSelectedIndustry(industry)
}

override fun getSelectedIndustries(): Industry? {
override fun getSelectedIndustry(): Industry? {
return repository.getSelectedIndustry()
}

override fun setSalaryStatus(isChecked:Boolean) {
repository.setCheckedStatus(isChecked)
}

override fun getSalaryStatus(): Boolean {
return repository.getCheckedStatus()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ class FilterViewModel(private val interactor: FilterInteractor) : ViewModel() {
interactor.clearFilters()
getFilters()
}
fun setSalaryStatus(isChecked:Boolean) {
interactor.setSalaryStatus(isChecked)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SelectIndustryFragment : Fragment() {
private val binding get() = _binding!!

private val viewModel by viewModel<SelectIndustryViewModel>()

private val listIndustry = mutableListOf<Industry>()
private var industriesAdapter: IndystryAdapter? = null

override fun onCreateView(
Expand Down Expand Up @@ -89,7 +89,7 @@ class SelectIndustryFragment : Fragment() {
errorIndustryLayout.visibility = View.GONE
}
if (industriesAdapter == null) {
industriesAdapter = IndystryAdapter(industries) { industry ->
industriesAdapter = IndystryAdapter(listIndustry) { industry ->
viewModel.onIndustryClicked(industry)
val position = industries.indexOf(industry)
industries[position] = industry.copy(isChecked = !industry.isChecked)
Expand All @@ -101,9 +101,10 @@ class SelectIndustryFragment : Fragment() {
layoutManager = LinearLayoutManager(requireContext())
adapter = industriesAdapter
}
} else {
//todo
}
listIndustry.clear()
listIndustry.addAll(industries)
industriesAdapter!!.notifyDataSetChanged()
}

private fun displayError(errorText: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class SelectIndustryViewModel(private val filterInteractor: FilterInteractor) :

private suspend fun processResult(result: DataResponse<Industry>) {
if (result.data != null) {
industriesStateLiveData.value =
IndustriesState.DisplayIndustries(getFullIndustriesList(result.data))
filteredIndustries = getFullIndustriesList(result.data)
industriesStateLiveData.value = IndustriesState.DisplayIndustries(filteredIndustries)
} else {
when (result.networkError!!) {
NetworkError.BAD_CONNECTION.toString() -> industriesStateLiveData.value =
Expand All @@ -80,6 +80,6 @@ class SelectIndustryViewModel(private val filterInteractor: FilterInteractor) :
}

fun loadSelectedIndustry() {
_selectedIndustry.value = filterInteractor.getSelectedIndustries()
_selectedIndustry.value = filterInteractor.getSelectedIndustry()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class SettingsFilterFragment : Fragment() {
viewModel.clearFilters()
}

binding.doNotShowWithoutSalaryCheckBox.setOnClickListener {

}
}

private fun changeEnabled(isEnabled: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import org.koin.androidx.viewmodel.ext.android.viewModel
import ru.practicum.android.diploma.databinding.FragmentSelectAreaBinding
import ru.practicum.android.diploma.domain.models.Vacancy
import ru.practicum.android.diploma.domain.models.filter.Area
import ru.practicum.android.diploma.presentation.filter.selectArea.adaptor.AreaAdapter
import ru.practicum.android.diploma.presentation.filter.selectArea.state.AreasState
Expand Down Expand Up @@ -48,9 +47,7 @@ class SelectAreaFragment : Fragment() {
}

setupSearchInput()

viewModel.initScreen()
viewModel.loadSelectedArea()
}

private fun setupSearchInput() {
Expand All @@ -61,7 +58,6 @@ class SelectAreaFragment : Fragment() {
count: Int,
after: Int
) {
// No implementation needed
}

override fun onTextChanged(
Expand All @@ -70,8 +66,9 @@ class SelectAreaFragment : Fragment() {
before: Int,
count: Int
) {
viewModel.filterAreas(s?.toString() ?: "")
viewModel.filterAreas(s?.toString() ?: "")
}

override fun afterTextChanged(editable: Editable?) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class SelectAreaViewModel(private val areasUseCase: FilterInteractor) : ViewMode
}
}


private suspend fun getAreasList(nestedAreasList: List<Area>): ArrayList<Area> =
withContext(Dispatchers.Default) {
val extendedAreasList: ArrayList<Area> = arrayListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import org.koin.androidx.viewmodel.ext.android.viewModel
import ru.practicum.android.diploma.R
import ru.practicum.android.diploma.databinding.FragmentSelectAreaBinding
import ru.practicum.android.diploma.databinding.FragmentSelectCountryBinding
import ru.practicum.android.diploma.domain.models.filter.Country
import ru.practicum.android.diploma.presentation.filter.selectArea.adaptor.CountryAdapter
import ru.practicum.android.diploma.presentation.filter.selectArea.state.CountryState

class SelectCountryFragment : Fragment() {


private var _binding: FragmentSelectAreaBinding? = null
private var _binding: FragmentSelectCountryBinding? = null
private val binding get() = _binding!!
private val viewModel: SelectCountryViewModel by viewModel()
private var countryAdapter: CountryAdapter? = null
Expand All @@ -27,53 +26,50 @@ class SelectCountryFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSelectAreaBinding.inflate(inflater, container, false)
_binding = FragmentSelectCountryBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.chooseAreaListRecycleView
binding.chooseAreaEnterFieldEdittext.isVisible = false
binding.chooseAreaHeaderTextview.text = getString(R.string.selectionCountries)
binding.countryListRecyclerView
binding.headerTextview.text = getString(R.string.selectionCountries)
viewModel.observeState().observe(viewLifecycleOwner) { state ->
when (state) {
is CountryState.Display -> displayCountries(state.content)
is CountryState.Error -> displayError(state.errorText)
}
}
viewModel.getCountries()
binding.chooseAreaBackArrowImageview.setOnClickListener {
binding.backArrow.setOnClickListener {
findNavController().popBackStack()
}
}


private fun displayCountries(countries: List<Country>) {
binding.apply {
chooseAreaListRecycleView.visibility = View.VISIBLE
errorAreasLayout.visibility = View.GONE
countryListRecyclerView.visibility = View.VISIBLE
errorCountriesLayout.visibility = View.GONE
}
if (countryAdapter == null) {
countryAdapter = CountryAdapter(countries) { country ->
viewModel.onAreaClicked(country)
findNavController().popBackStack()
}

binding.chooseAreaListRecycleView.apply {
binding.countryListRecyclerView.apply {
layoutManager = LinearLayoutManager(requireContext())
adapter = countryAdapter
}
} else {
//todo
}
}

private fun displayError(errorText: String) {
binding.apply {
chooseAreaListRecycleView.visibility = View.INVISIBLE
errorAreasLayout.visibility = View.VISIBLE
areasErrorText.text = errorText
countryListRecyclerView.visibility = View.INVISIBLE
errorCountriesLayout.visibility = View.VISIBLE
countriesErrorText.text = errorText
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CountryAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder =
CountryViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.industry_area_item, parent, false)
.inflate(R.layout.country_item, parent, false)
)

override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ru.practicum.android.diploma.domain.models.filter.Country

class CountryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

private val name: TextView = itemView.findViewById(R.id.industry_region_title_textview)
private val name: TextView = itemView.findViewById(R.id.country_item_textview)

fun bind(item: Country) {
name.text = item.name
Expand Down
Loading

0 comments on commit ad7b0c3

Please sign in to comment.