Skip to content

Commit

Permalink
Filters and sortings work
Browse files Browse the repository at this point in the history
Applied on fragment start, saved in sharedpref using gson

Related: #39
  • Loading branch information
alexandr7035 committed Sep 10, 2021
1 parent cc4e3ca commit 192f92d
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 13 deletions.
26 changes: 26 additions & 0 deletions app/src/main/java/com/alexandr7035/gitstat/view/ReposFilters.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alexandr7035.gitstat.view

// Defaults in the constructor
data class ReposFilters(
var sortingType: SortingType = SortingType.BY_REPO_NAME,
var sortingOrder: SortingOrder = SortingOrder.ASCENDING_MODE,
var filterPrivacy: FilterPrivacy = FilterPrivacy.ALL_REPOS
)

{
enum class SortingType {
BY_REPO_NAME,
BY_REPO_CREATION_DATE
}

enum class SortingOrder {
ASCENDING_MODE,
DESCENDING_MODE
}

enum class FilterPrivacy {
PRIVATE_REPOS_ONLY,
PUBLIC_REPOS_ONLY,
ALL_REPOS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.view.ViewGroup
import com.alexandr7035.gitstat.databinding.FiltersDialogBinding
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class RepositoriesFiltersDialog(): BottomSheetDialogFragment() {
class RepositoriesFiltersDialog(private val currentFilters: ReposFilters, private val filtersUpdateObserver: FiltersUpdateObserver): BottomSheetDialogFragment() {

private var binding: FiltersDialogBinding? = null

Expand All @@ -16,4 +16,58 @@ class RepositoriesFiltersDialog(): BottomSheetDialogFragment() {
return binding!!.root
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// Load current filters settings
setupFiltersViews()

binding!!.applyButton.setOnClickListener {

val newFilters = ReposFilters()

when (binding!!.includeRepositoriesRadioGroup.checkedRadioButtonId) {
binding!!.includeAllBtn.id -> {
newFilters.filterPrivacy = ReposFilters.FilterPrivacy.ALL_REPOS
}

binding!!.includePrivateOnlyBtn.id -> {
newFilters.filterPrivacy = ReposFilters.FilterPrivacy.PRIVATE_REPOS_ONLY
}

binding!!.includePublicOnlyBtn.id -> {
newFilters.filterPrivacy = ReposFilters.FilterPrivacy.PUBLIC_REPOS_ONLY
}
}

filtersUpdateObserver.onFiltersUpdated(newFilters)
dismiss()
}
}


private fun setupFiltersViews() {
when (currentFilters.filterPrivacy) {
ReposFilters.FilterPrivacy.PRIVATE_REPOS_ONLY -> binding!!.includePrivateOnlyBtn.isChecked = true
ReposFilters.FilterPrivacy.PUBLIC_REPOS_ONLY -> binding!!.includePublicOnlyBtn.isChecked = true
ReposFilters.FilterPrivacy.ALL_REPOS -> binding!!.includeAllBtn.isChecked = true
}

when (currentFilters.sortingType) {
ReposFilters.SortingType.BY_REPO_NAME -> binding!!.sortByRepositoryNameBtn.isChecked = true
ReposFilters.SortingType.BY_REPO_CREATION_DATE -> binding!!.sortByCreationDateBtn.isChecked = true
}

when (currentFilters.sortingOrder) {
ReposFilters.SortingOrder.ASCENDING_MODE -> binding!!.sortAscendingBtn.isChecked = true
ReposFilters.SortingOrder.DESCENDING_MODE-> binding!!.sortDescendingBtn.isChecked = true
}
}


interface FiltersUpdateObserver {
fun onFiltersUpdated(filters: ReposFilters)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@ import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.alexandr7035.gitstat.R
import com.alexandr7035.gitstat.data.local.model.RepositoryEntity
import com.alexandr7035.gitstat.databinding.FragmentReposBinding
import com.alexandr7035.gitstat.databinding.FragmentRepositoriesListBinding
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import java.io.InputStreamReader


class RepositoriesListFragment : Fragment() {
class RepositoriesListFragment : Fragment(), RepositoriesFiltersDialog.FiltersUpdateObserver {

private var binding: FragmentRepositoriesListBinding? = null
private var sharedPreferences: SharedPreferences? = null
private var viewModel: MainViewModel? = null

// FIXME
private var filters: ReposFilters = ReposFilters()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentRepositoriesListBinding.inflate(inflater, container, false)
return binding!!.root
Expand All @@ -53,34 +52,50 @@ class RepositoriesListFragment : Fragment() {
// FIXME move to other place
val languagesColorsList: Map<String, Map<String, String>> = getLangColorsList()

// Setup adapter
val adapter = RepositoriesAdapter(languagesColorsList)
binding!!.recyclerView.adapter = adapter
binding!!.recyclerView.layoutManager = LinearLayoutManager(context)

viewModel!!.getRepositoriesData().observe(viewLifecycleOwner, {
adapter.setItems(it)
})
// Load filters settings for adapter from memory
// The default shared pref value is based on new ReposFilters() object and it's params
val gson = Gson()
val filtersStr = sharedPreferences!!.getString(getString(R.string.shared_prefs_filters), gson.toJson(ReposFilters()))
filters = gson.fromJson(filtersStr, ReposFilters::class.java)

viewModel!!.updateRepositoriesLiveData()
viewModel!!.getRepositoriesData().observe(viewLifecycleOwner, { repositories ->

val filteredList = getFilteredRepositoriesList(
unfilteredList = repositories,
filters = filters
)

adapter.setItems(filteredList)
})

binding!!.toolbar.inflateMenu(R.menu.menu_toolbar_repos_list)

binding!!.toolbar.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.item_filters -> {
Log.d("DEBUG", "filters clicked")
showFiltersDialog()
}
}

true
}


// Populate the list
viewModel!!.updateRepositoriesLiveData()
}


private fun showFiltersDialog() {
val dialog = RepositoriesFiltersDialog()
val dialog = RepositoriesFiltersDialog(
currentFilters = filters,
filtersUpdateObserver = this
)
dialog.show(requireActivity().supportFragmentManager, "filtersDialog")
}

Expand All @@ -96,4 +111,43 @@ class RepositoriesListFragment : Fragment() {
return builder.create().fromJson(reader, itemsMapType)
}


private fun getFilteredRepositoriesList(unfilteredList: List<RepositoryEntity>, filters: ReposFilters): List<RepositoryEntity> {
val filteredList = ArrayList<RepositoryEntity>()

when (filters.filterPrivacy) {
ReposFilters.FilterPrivacy.PUBLIC_REPOS_ONLY -> unfilteredList.forEach {
if (! it.isPrivate) {
filteredList.add(it)
}
}

ReposFilters.FilterPrivacy.PRIVATE_REPOS_ONLY -> unfilteredList.forEach {
if (it.isPrivate) {
filteredList.add(it)
}
}

else -> filteredList.addAll(unfilteredList)
}

return filteredList
}


// Called when "apply" button is clicked in filters dialog
override fun onFiltersUpdated(filters: ReposFilters) {
Log.d("DEBUG", "update filters $filters")
this.filters = filters

// FIXME
viewModel!!.updateRepositoriesLiveData()

// Save changes in memory
val gson = Gson()
sharedPreferences!!.edit()
.putString(getString(R.string.shared_prefs_filters), gson.toJson(filters))
.apply()
}

}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/filters_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
app:layout_constraintStart_toStartOf="parent">

<RadioButton
android:id="@+id/sortByRepositoryNameBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:buttonTint="@color/gray_500"
android:text="Repository name" />

<RadioButton
android:id="@+id/sortByCreationDateBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/gray_500"
Expand Down Expand Up @@ -71,13 +73,15 @@
app:layout_constraintStart_toStartOf="parent">

<RadioButton
android:id="@+id/sortAscendingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:buttonTint="@color/gray_500"
android:text="Ascending" />

<RadioButton
android:id="@+id/sortDescendingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/gray_500"
Expand Down Expand Up @@ -114,20 +118,23 @@
app:layout_constraintStart_toStartOf="parent">

<RadioButton
android:id="@+id/includeAllBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:buttonTint="@color/gray_500"
android:text="All" />

<RadioButton
android:id="@+id/includePublicOnlyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:buttonTint="@color/gray_500"
android:text="Public only" />

<RadioButton
android:id="@+id/includePrivateOnlyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/gray_500"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@
<string name="more">More</string>
<string name="filters">Filters</string>

<string name="shared_prefs_filters">SP_filters</string>

</resources>

0 comments on commit 192f92d

Please sign in to comment.