Skip to content

Commit

Permalink
- changed everything over to use ViewBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
jakepurple13 committed May 25, 2021
1 parent 3ee5bf7 commit 1b1608e
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 439 deletions.
1 change: 1 addition & 0 deletions UIViews/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ android {
}
buildFeatures {
dataBinding true
viewBinding true
}
}

Expand Down
43 changes: 19 additions & 24 deletions UIViews/src/main/java/com/programmersbox/uiviews/AllFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package com.programmersbox.uiviews

import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.jakewharton.rxbinding2.widget.textChanges
import com.programmersbox.dragswipe.DragSwipeAdapter
import com.programmersbox.dragswipe.DragSwipeDiffUtil
Expand All @@ -21,6 +16,7 @@ import com.programmersbox.helpfulutils.runOnUIThread
import com.programmersbox.models.ApiService
import com.programmersbox.models.ItemModel
import com.programmersbox.models.sourcePublish
import com.programmersbox.uiviews.databinding.FragmentAllBinding
import com.programmersbox.uiviews.utils.EndlessScrollingListener
import com.programmersbox.uiviews.utils.FirebaseDb
import io.reactivex.android.schedulers.AndroidSchedulers
Expand Down Expand Up @@ -50,12 +46,12 @@ class AllFragment : BaseListFragment() {
private val dao by lazy { ItemDatabase.getInstance(requireContext()).itemDao() }
private val itemListener = FirebaseDb.FirebaseListener()

private lateinit var binding: FragmentAllBinding

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

val rv = view.findViewById<RecyclerView>(R.id.allList)
val refresh = view.findViewById<SwipeRefreshLayout>(R.id.allRefresh)
val editText = view.findViewById<TextInputEditText>(R.id.search_info)
binding = FragmentAllBinding.bind(view)

Flowables.combineLatest(
itemListener.getAllShowsFlowable(),
Expand All @@ -66,14 +62,14 @@ class AllFragment : BaseListFragment() {
.subscribe { adapter.update(it) { s, d -> s.url == d.url } }
.addTo(disposable)

rv?.apply {
binding.allList.apply {
adapter = this@AllFragment.adapter
layoutManager = createLayoutManager(this@AllFragment.requireContext())
addOnScrollListener(object : EndlessScrollingListener(layoutManager!!) {
override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?) {
if (sourcePublish.value!!.canScroll && editText.text.isNullOrEmpty()) {
if (sourcePublish.value!!.canScroll && binding.searchInfo.text.isNullOrEmpty()) {
count++
refresh.isRefreshing = true
binding.allRefresh.isRefreshing = true
sourceLoad(sourcePublish.value!!, count)
}
}
Expand All @@ -84,8 +80,8 @@ class AllFragment : BaseListFragment() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
view.findViewById<RelativeLayout>(R.id.offline_view).visibility = if (it) View.GONE else View.VISIBLE
refresh.visibility = if (it) View.VISIBLE else View.GONE
binding.offlineView.visibility = if (it) View.GONE else View.VISIBLE
binding.allRefresh.visibility = if (it) View.VISIBLE else View.GONE
}
.addTo(disposable)

Expand All @@ -96,27 +92,27 @@ class AllFragment : BaseListFragment() {
count = 1
adapter.setListNotify(emptyList())
sourceLoad(it)
rv?.scrollToPosition(0)
binding.allList.scrollToPosition(0)
}
.addTo(disposable)

view.findViewById<FloatingActionButton>(R.id.scrollToTop).setOnClickListener {
binding.scrollToTop.setOnClickListener {
lifecycleScope.launch {
activity?.runOnUiThread { rv?.smoothScrollToPosition(0) }
activity?.runOnUiThread { binding.allList.smoothScrollToPosition(0) }
delay(500)
activity?.runOnUiThread { rv?.scrollToPosition(0) }
activity?.runOnUiThread { binding.allList.scrollToPosition(0) }
}
}

editText
binding.searchInfo
.textChanges()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.debounce(500, TimeUnit.MILLISECONDS)
.flatMapSingle { sourcePublish.value!!.searchList(it, 1, currentList) }
.subscribe {
adapter.setData(it)
activity?.runOnUiThread { view.findViewById<TextInputLayout>(R.id.search_layout)?.suffixText = "${it.size}" }
activity?.runOnUiThread { binding.searchLayout.suffixText = "${it.size}" }
}
.addTo(disposable)

Expand All @@ -134,19 +130,18 @@ class AllFragment : BaseListFragment() {
}

private fun sourceLoad(sources: ApiService, page: Int = 1) {
val searchLayout = view?.findViewById<TextInputLayout>(R.id.search_layout)
sources.getList(page)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy {
adapter.addItems(it)
currentList.clear()
currentList.addAll(it)
view?.findViewById<SwipeRefreshLayout>(R.id.allRefresh)?.isRefreshing = false
binding.allRefresh.isRefreshing = false
activity?.runOnUiThread {
searchLayout?.editText?.setText("")
searchLayout?.suffixText = "${adapter.dataList.size}"
searchLayout?.hint = getString(R.string.searchFor, sourcePublish.value?.serviceName.orEmpty())
binding.searchLayout.editText?.setText("")
binding.searchLayout.suffixText = "${adapter.dataList.size}"
binding.searchLayout.hint = getString(R.string.searchFor, sourcePublish.value?.serviceName.orEmpty())
}
}
.addTo(disposable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.programmersbox.uiviews

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
Expand All @@ -11,10 +12,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.jakewharton.rxbinding2.widget.textChanges
import com.programmersbox.dragswipe.DragSwipeAdapter
import com.programmersbox.dragswipe.DragSwipeDiffUtil
Expand All @@ -26,6 +24,7 @@ import com.programmersbox.models.ApiService
import com.programmersbox.rxutils.behaviorDelegate
import com.programmersbox.rxutils.toLatestFlowable
import com.programmersbox.uiviews.databinding.FavoriteItemBinding
import com.programmersbox.uiviews.databinding.FragmentFavoriteBinding
import com.programmersbox.uiviews.utils.AutoFitGridLayoutManager
import com.programmersbox.uiviews.utils.FirebaseDb
import io.reactivex.android.schedulers.AndroidSchedulers
Expand All @@ -50,11 +49,14 @@ class FavoriteFragment : BaseFragment() {

override val layoutId: Int get() = R.layout.fragment_favorite

private lateinit var binding: FragmentFavoriteBinding

@SuppressLint("SetTextI18n")
override fun viewCreated(view: View, savedInstanceState: Bundle?) {

uiSetup(view)
binding = FragmentFavoriteBinding.bind(view)

val favRv = view.findViewById<RecyclerView>(R.id.favRv)
uiSetup()

val fired = fireListener.getAllShowsFlowable()

Expand All @@ -70,7 +72,7 @@ class FavoriteFragment : BaseFragment() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()),
source2 = sourcePublisher.toLatestFlowable(),
source3 = view.findViewById<TextInputEditText>(R.id.fav_search_info)
source3 = binding.favSearchInfo
.textChanges()
.debounce(500, TimeUnit.MILLISECONDS)
.toLatestFlowable()
Expand All @@ -79,59 +81,54 @@ class FavoriteFragment : BaseFragment() {
.observeOn(AndroidSchedulers.mainThread())
.map { pair ->
pair.first.sortedBy(DbModel::title)
.filter { it.source in pair.second.map { it.serviceName } && it.title.contains(pair.third, true) }
.filter { it.source in pair.second.map(ApiService::serviceName) && it.title.contains(pair.third, true) }
}
.map { it.size to it.toGroup() }
.distinctUntilChanged()
.subscribe {
adapter.setData(it.second.toList())
view.findViewById<TextInputLayout>(R.id.fav_search_layout)?.hint =
resources.getQuantityString(R.plurals.numFavorites, it.first, it.first)
favRv?.smoothScrollToPosition(0)
binding.favSearchLayout.hint = resources.getQuantityString(R.plurals.numFavorites, it.first, it.first)
binding.favRv.smoothScrollToPosition(0)
}
.addTo(disposable)

val sourceList = view.findViewById<ChipGroup>(R.id.sourceList)

dbFire
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map { it.groupBy { s -> s.source } }
.subscribe { s ->
s.forEach { m -> sourceList.children.filterIsInstance<Chip>().find { it.text == m.key }?.text = "${m.key}: ${m.value.size}" }
s.forEach { m -> binding.sourceList.children.filterIsInstance<Chip>().find { it.text == m.key }?.text = "${m.key}: ${m.value.size}" }
}
.addTo(disposable)

}

private fun List<DbModel>.toGroup() = groupBy(DbModel::title)

private fun uiSetup(view: View) = with(view) {
val favRv = view.findViewById<RecyclerView>(R.id.favRv)
favRv.layoutManager = AutoFitGridLayoutManager(requireContext(), 360).apply { orientation = GridLayoutManager.VERTICAL }
favRv.adapter = adapter
favRv.setItemViewCacheSize(20)
favRv.setHasFixedSize(true)

val sourceList = findViewById<ChipGroup>(R.id.sourceList)
@SuppressLint("SetTextI18n")
private fun uiSetup() {
binding.favRv.layoutManager = AutoFitGridLayoutManager(requireContext(), 360).apply { orientation = GridLayoutManager.VERTICAL }
binding.favRv.adapter = adapter
binding.favRv.setItemViewCacheSize(20)
binding.favRv.setHasFixedSize(true)

sourceList.addView(Chip(requireContext()).apply {
binding.sourceList.addView(Chip(requireContext()).apply {
text = "ALL"
isCheckable = true
isClickable = true
isChecked = true
setOnClickListener { sourceList.children.filterIsInstance<Chip>().forEach { it.isChecked = true } }
setOnClickListener { binding.sourceList.children.filterIsInstance<Chip>().forEach { it.isChecked = true } }
})

sources.forEach {
sourceList.addView(Chip(requireContext()).apply {
binding.sourceList.addView(Chip(requireContext()).apply {
text = it.serviceName
isCheckable = true
isClickable = true
isChecked = true
setOnCheckedChangeListener { _, isChecked -> addOrRemoveSource(isChecked, it) }
setOnLongClickListener {
sourceList.clearCheck()
binding.sourceList.clearCheck()
isChecked = true
true
}
Expand Down
23 changes: 11 additions & 12 deletions UIViews/src/main/java/com/programmersbox/uiviews/RecentFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package com.programmersbox.uiviews

import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.programmersbox.favoritesdatabase.DbModel
import com.programmersbox.favoritesdatabase.ItemDatabase
import com.programmersbox.models.ApiService
import com.programmersbox.models.sourcePublish
import com.programmersbox.uiviews.databinding.FragmentRecentBinding
import com.programmersbox.uiviews.utils.EndlessScrollingListener
import com.programmersbox.uiviews.utils.FirebaseDb
import io.reactivex.android.schedulers.AndroidSchedulers
Expand All @@ -36,11 +35,11 @@ class RecentFragment : BaseListFragment() {
private val dao by lazy { ItemDatabase.getInstance(requireContext()).itemDao() }
private val itemListener = FirebaseDb.FirebaseListener()

private lateinit var binding: FragmentRecentBinding

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

val rv = view.findViewById<RecyclerView>(R.id.recentList)
val refresh = view.findViewById<SwipeRefreshLayout>(R.id.recentRefresh)
binding = FragmentRecentBinding.bind(view)

Flowables.combineLatest(
itemListener.getAllShowsFlowable(),
Expand All @@ -51,14 +50,14 @@ class RecentFragment : BaseListFragment() {
.subscribe { adapter.update(it) { s, d -> s.url == d.url } }
.addTo(disposable)

rv?.apply {
binding.recentList.apply {
adapter = this@RecentFragment.adapter
layoutManager = createLayoutManager(this@RecentFragment.requireContext())
addOnScrollListener(object : EndlessScrollingListener(layoutManager!!) {
override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?) {
if (sourcePublish.value!!.canScroll) {
count++
refresh.isRefreshing = true
binding.recentRefresh.isRefreshing = true
sourceLoad(sourcePublish.value!!, count)
}
}
Expand All @@ -69,12 +68,12 @@ class RecentFragment : BaseListFragment() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
view.findViewById<RelativeLayout>(R.id.offline_view).visibility = if (it) View.GONE else View.VISIBLE
refresh.visibility = if (it) View.VISIBLE else View.GONE
binding.offlineView.visibility = if (it) View.GONE else View.VISIBLE
binding.recentRefresh.visibility = if (it) View.VISIBLE else View.GONE
}
.addTo(disposable)

refresh.setOnRefreshListener { sourceLoad(sourcePublish.value!!) }
binding.recentRefresh.setOnRefreshListener { sourceLoad(sourcePublish.value!!) }

sourcePublish
.subscribeOn(Schedulers.io())
Expand All @@ -83,7 +82,7 @@ class RecentFragment : BaseListFragment() {
count = 1
adapter.setListNotify(emptyList())
sourceLoad(it)
rv?.scrollToPosition(0)
binding.recentList.scrollToPosition(0)
}
.addTo(disposable)

Expand All @@ -96,7 +95,7 @@ class RecentFragment : BaseListFragment() {
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy {
adapter.addItems(it)
view?.findViewById<SwipeRefreshLayout>(R.id.recentRefresh)?.isRefreshing = false
binding.recentRefresh.isRefreshing = false
}
.addTo(disposable)
}
Expand Down
1 change: 1 addition & 0 deletions animeworld/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ android {
}
buildFeatures {
dataBinding true
viewBinding true
}
}

Expand Down
Loading

0 comments on commit 1b1608e

Please sign in to comment.