Skip to content

Commit

Permalink
Fix filter, remove filter button in other screens
Browse files Browse the repository at this point in the history
  • Loading branch information
Vi Anh committed Dec 12, 2021
1 parent f9432a2 commit 07ab321
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 40 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ class HomeFragment: BaseFragment<HomeFragmentBinding>(), ItemClick<MangaItem> {
}

override fun onApplyWindowInsets(v: View?, insets: WindowInsetsCompat): WindowInsetsCompat {
super.onApplyWindowInsets(v, insets)
val barInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
requireBinding.btnFilter.updateLayoutParams<ViewGroup.MarginLayoutParams> {
rightMargin = barInsets.right + 10.0.toPx.toInt()
bottomMargin = barInsets.bottom + 10.0.toPx.toInt()
rightMargin = barInsets.right + 10.toPx.toInt()
bottomMargin = barInsets.bottom + 10.toPx.toInt()
}
return super.onApplyWindowInsets(v, insets)
return insets
}

private fun observe() {
Expand Down Expand Up @@ -97,6 +98,7 @@ class HomeFragment: BaseFragment<HomeFragmentBinding>(), ItemClick<MangaItem> {
viewModel.loadPage(1)
}

requireBinding.btnFilter.show()
requireBinding.btnFilter.setOnClickListener {
filterDialogFragment.show(childFragmentManager, null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.vianh.blogtruyen.features.base.list.items.LoadingFooterItem
import com.vianh.blogtruyen.features.list.data.CategoryRepo
import com.vianh.blogtruyen.features.list.filter.FilterCategoryItem
import com.vianh.blogtruyen.utils.SingleLiveEvent
import com.vianh.blogtruyen.utils.asLiveDataDistinct
import com.vianh.blogtruyen.utils.mapToSet
import com.vianh.blogtruyen.utils.withPrevious
import kotlinx.coroutines.Dispatchers
Expand All @@ -35,10 +36,14 @@ class HomeViewModel(
filterManga(manga, filters)
}.shareIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, 1)

val categoryItems = combine(filterCategories, categoryRepo.observeAll())
val categories = categoryRepo.observeAll().distinctUntilChanged()
val categoryItems = combine(filterCategories, categories)
{ filterCategories, categories ->
categories.map { FilterCategoryItem(it, filterCategories.contains(it.name)) }
}.asLiveData(Dispatchers.Default)
val newList = categories
.mapTo(ArrayList()) { FilterCategoryItem(it, filterCategories.contains(it.name)) }
newList.sortBy { it.category.name.lowercase() }
return@combine newList
}.asLiveDataDistinct(Dispatchers.Default)

val content = combine(filterManga, listMode)
{ mangaList, mode ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.vianh.blogtruyen.features.list

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.vianh.blogtruyen.databinding.HomeFragmentBinding
import com.vianh.blogtruyen.features.base.BaseFragment

abstract class MangaListFragment: BaseFragment<HomeFragmentBinding>() {
abstract val isFilterEnable: Boolean

override fun createBinding(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): HomeFragmentBinding {
return HomeFragmentBinding.inflate(inflater, container, false)
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.vianh.blogtruyen.databinding.FilterItemBinding
import timber.log.Timber


class FilterAdapter: ListAdapter<FilterCategoryItem, FilterAdapter.FilterItemVH>(DiffCallback()) {
Expand Down Expand Up @@ -44,14 +43,13 @@ class FilterAdapter: ListAdapter<FilterCategoryItem, FilterAdapter.FilterItemVH>

}

class FilterItemVH(val binding: FilterItemBinding): RecyclerView.ViewHolder(binding.root) {

var boundItem: FilterCategoryItem? = null
inner class FilterItemVH(val binding: FilterItemBinding): RecyclerView.ViewHolder(binding.root) {

init {

fun onItemClick() {
boundItem?.let {
val item = getItem(bindingAdapterPosition)
item?.let {
val isSelected = !it.isSelected
it.isSelected = isSelected
binding.checkBox.isChecked = isSelected
Expand All @@ -69,7 +67,6 @@ class FilterAdapter: ListAdapter<FilterCategoryItem, FilterAdapter.FilterItemVH>


fun bind(item: FilterCategoryItem) {
boundItem = item
binding.categoryName.text = item.category.name
binding.checkBox.isChecked = item.isSelected
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package com.vianh.blogtruyen.features.list.filter

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.graphics.Insets
import androidx.core.view.OnApplyWindowInsetsListener
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.vianh.blogtruyen.R
import com.vianh.blogtruyen.databinding.FilterDialogFragmentBinding
import com.vianh.blogtruyen.features.list.HomeViewModel
import com.vianh.blogtruyen.utils.toPx
import org.koin.androidx.viewmodel.ext.android.getViewModel
import timber.log.Timber

class FilterDialogFragment: BottomSheetDialogFragment() {
class FilterDialogFragment: BottomSheetDialogFragment(), OnApplyWindowInsetsListener {
lateinit var binding: FilterDialogFragmentBinding
private val viewModel: HomeViewModel by lazy { requireParentFragment().getViewModel() }
private var filterAdapter: FilterAdapter? = null
private var lastInsets: Insets? = null

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
val behavior = (dialog as BottomSheetDialog).behavior
behavior.state = BottomSheetBehavior.STATE_EXPANDED
return dialog
}

Expand All @@ -34,9 +42,25 @@ class FilterDialogFragment: BottomSheetDialogFragment() {
savedInstanceState: Bundle?
): View {
binding = FilterDialogFragmentBinding.inflate(inflater, container, false)
ViewCompat.setOnApplyWindowInsetsListener(binding.filterRecycler, this)
return binding.root
}

override fun onApplyWindowInsets(v: View?, insets: WindowInsetsCompat?): WindowInsetsCompat {
if (insets == null) return WindowInsetsCompat.CONSUMED
val barInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
if (lastInsets == barInsets) return WindowInsetsCompat.CONSUMED

lastInsets = barInsets
Timber.e(barInsets.toString())
v?.updatePadding(
bottom = barInsets.bottom + 10.toPx.toInt(),
left = barInsets.left,
right = barInsets.right
)
return WindowInsetsCompat.CONSUMED
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setup()
Expand All @@ -55,7 +79,6 @@ class FilterDialogFragment: BottomSheetDialogFragment() {
}

private fun setup() {
binding.root.setBackgroundColor(Color.BLACK)
filterAdapter = FilterAdapter()
binding.clearFilter.setOnClickListener {
filterAdapter?.clearFilters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PageItemVH(
init {
with(binding.page) {
setMaxTileSize(tileSize)
setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)
setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM)
setMinimumDpi(90)
setMinimumTileDpi(180)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,10 @@ fun Context.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
val Number.toPx get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics)
Resources.getSystem().displayMetrics)

val screenHeight
get() = Resources.getSystem().displayMetrics.heightPixels

val screenWidth
get() = Resources.getSystem().displayMetrics.widthPixels
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.vianh.blogtruyen.views

import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Interpolator
import androidx.core.view.doOnLayout
import com.vianh.blogtruyen.utils.gone
import com.vianh.blogtruyen.utils.visible

Expand All @@ -14,13 +14,9 @@ class ViewHeightAnimator(val view: View) {
if (view.height > 0) {
height = view.height
} else {
view.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
height = view.height
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
view.doOnLayout {
height = it.height
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions app/src/main/res/layout/filter_dialog_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="false"
android:minHeight="500dp"
android:clipToPadding="false"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">

<FrameLayout
Expand Down Expand Up @@ -47,9 +46,10 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/filter_recycler"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header_container"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/home_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/btn_filter"
android:layout_marginEnd="10dp"
android:layout_marginBottom="40dp"
app:backgroundTint="@color/colorAccent"
app:icon="@drawable/ic_filter_list"
app:iconTint="?attr/colorOnPrimary"
android:textColor="?attr/colorOnPrimary"
android:text="@string/filter"
tools:visibility="visible"
android:visibility="gone"
android:textAllCaps="false"
app:elevation="8dp"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/manga_page_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<com.vianh.blogtruyen.views.MangaPage
android:id="@+id/page"

android:layout_width="match_parent"
android:layout_height="wrap_content" />

Expand Down
11 changes: 5 additions & 6 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
</style>

<!-- Bottom sheet-->
<style name="Theme.BottomSheetTheme" parent="@style/Theme.Design.BottomSheetDialog">
<item name="colorAccent">@color/colorAccent</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="android:windowIsFloating">false</item>
<item name="android:navigationBarColor">?attr/colorPrimary</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<style name="Theme.BottomSheetTheme" parent="ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="android:windowIsFloating">true</item>
<item name="colorSurface">@color/colorPrimary</item>

<!-- <item name="android:navigationBarColor">@color/colorPrimary</item>-->
</style>

<style name="SearchViewTheme" parent="Widget.AppCompat.SearchView">
Expand Down

0 comments on commit 07ab321

Please sign in to comment.