Skip to content

Commit

Permalink
- changed the ItemListAdapter.kt to have checkadapterinterface for an…
Browse files Browse the repository at this point in the history
…ime showing favorites and possible adding it to press and hold in manga
  • Loading branch information
jakepurple13 committed Apr 8, 2021
1 parent 0cb7125 commit 65c2b9d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
19 changes: 18 additions & 1 deletion UIViews/src/main/java/com/programmersbox/uiviews/AllFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import com.google.android.material.textfield.TextInputLayout
import com.jakewharton.rxbinding2.widget.textChanges
import com.programmersbox.dragswipe.DragSwipeAdapter
import com.programmersbox.dragswipe.DragSwipeDiffUtil
import com.programmersbox.favoritesdatabase.DbModel
import com.programmersbox.favoritesdatabase.ItemDatabase
import com.programmersbox.helpfulutils.runOnUIThread
import com.programmersbox.models.ApiService
import com.programmersbox.models.ItemModel
import com.programmersbox.models.sourcePublish
import com.programmersbox.uiviews.utils.EndlessScrollingListener
import com.programmersbox.uiviews.utils.FirebaseDb
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.Flowables
import io.reactivex.rxkotlin.addTo
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
Expand All @@ -32,7 +36,7 @@ import java.util.concurrent.TimeUnit
* Use the [AllFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class AllFragment() : BaseListFragment() {
class AllFragment : BaseListFragment() {

private val disposable: CompositeDisposable = CompositeDisposable()
private var count = 1
Expand All @@ -41,12 +45,24 @@ class AllFragment() : BaseListFragment() {

private val currentList = mutableListOf<ItemModel>()

private val dao by lazy { ItemDatabase.getInstance(requireContext()).itemDao() }
private val itemListener = FirebaseDb.FirebaseListener()

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)

Flowables.combineLatest(
itemListener.getAllShowsFlowable(),
dao.getAllFavorites()
) { f, d -> (f + d).groupBy(DbModel::url).map { it.value.maxByOrNull(DbModel::numChapters)!! } }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { adapter.update(it) { s, d -> s.url == d.url } }
.addTo(disposable)

rv?.apply {
adapter = this@AllFragment.adapter
layoutManager = createLayoutManager(this@AllFragment.requireContext())
Expand Down Expand Up @@ -125,6 +141,7 @@ class AllFragment() : BaseListFragment() {
override fun onDestroy() {
super.onDestroy()
disposable.dispose()
itemListener.unregister()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import android.content.Context
import android.view.View
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.programmersbox.dragswipe.CheckAdapter
import com.programmersbox.dragswipe.CheckAdapterInterface
import com.programmersbox.dragswipe.DragSwipeAdapter
import com.programmersbox.loggingutils.Loged
import com.programmersbox.favoritesdatabase.DbModel
import com.programmersbox.models.ItemModel

abstract class ItemListAdapter<VH : RecyclerView.ViewHolder>(
protected val context: Context,
private val baseListFragment: BaseListFragment
) : DragSwipeAdapter<ItemModel, VH>() {
private val baseListFragment: BaseListFragment,
check: CheckAdapter<ItemModel, DbModel> = CheckAdapter()
) : DragSwipeAdapter<ItemModel, VH>(), CheckAdapterInterface<ItemModel, DbModel> by check {
init {
check.adapter = this
}

protected fun onClick(v: View, itemModel: ItemModel) {
val direction = if(baseListFragment is RecentFragment) RecentFragmentDirections.actionRecentFragment2ToDetailsFragment2(itemModel)
val direction = if (baseListFragment is RecentFragment) RecentFragmentDirections.actionRecentFragment2ToDetailsFragment2(itemModel)
else AllFragmentDirections.actionAllFragment2ToDetailsFragment3(itemModel)
v.findNavController().navigate(direction)
}
Expand Down
17 changes: 17 additions & 0 deletions UIViews/src/main/java/com/programmersbox/uiviews/RecentFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.programmersbox.favoritesdatabase.DbModel
import com.programmersbox.favoritesdatabase.ItemDatabase
import com.programmersbox.models.ApiService
import com.programmersbox.models.sourcePublish
import com.programmersbox.uiviews.utils.EndlessScrollingListener
import com.programmersbox.uiviews.utils.FirebaseDb
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.Flowables
import io.reactivex.rxkotlin.addTo
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
Expand All @@ -27,12 +31,24 @@ class RecentFragment : BaseListFragment() {

private var count = 1

private val dao by lazy { ItemDatabase.getInstance(requireContext()).itemDao() }
private val itemListener = FirebaseDb.FirebaseListener()

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)

Flowables.combineLatest(
itemListener.getAllShowsFlowable(),
dao.getAllFavorites()
) { f, d -> (f + d).groupBy(DbModel::url).map { it.value.maxByOrNull(DbModel::numChapters)!! } }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { adapter.update(it) { s, d -> s.url == d.url } }
.addTo(disposable)

rv?.apply {
adapter = this@RecentFragment.adapter
layoutManager = createLayoutManager(this@RecentFragment.requireContext())
Expand Down Expand Up @@ -76,6 +92,7 @@ class RecentFragment : BaseListFragment() {
override fun onDestroy() {
super.onDestroy()
disposable.dispose()
itemListener.unregister()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.programmersbox.animeworld.databinding.AnimeListItemBinding
import com.programmersbox.dragswipe.CheckAdapter
import com.programmersbox.dragswipe.CheckAdapterInterface
import com.programmersbox.favoritesdatabase.DbModel
import com.programmersbox.helpfulutils.layoutInflater
import com.programmersbox.models.ItemModel
Expand All @@ -17,12 +15,7 @@ import com.programmersbox.uiviews.ItemListAdapter
class AnimeAdapter(
context: Context,
baseListFragment: BaseListFragment,
check: CheckAdapter<ItemModel, DbModel> = CheckAdapter()
) : ItemListAdapter<AnimeHolder>(context, baseListFragment), CheckAdapterInterface<ItemModel, DbModel> by check {

init {
check.adapter = this
}
) : ItemListAdapter<AnimeHolder>(context, baseListFragment) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnimeHolder =
AnimeHolder(AnimeListItemBinding.inflate(context.layoutInflater, parent, false))
Expand Down
1 change: 1 addition & 0 deletions animeworld/src/main/res/layout/anime_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:minLines="2"
android:text="@{show.title}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MangaApp : OtakuApp() {
override fun onCreated() {

logo = R.mipmap.ic_launcher
notificationLogo = R.drawable.manga_world_round_logo//R.mipmap.ic_launcher_foreground
notificationLogo = R.drawable.manga_world_round_logo

MangaContext.context = this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.programmersbox.favoritesdatabase.DbModel
import com.programmersbox.helpfulutils.layoutInflater
import com.programmersbox.mangaworld.databinding.MangaGalleryItemBinding
import com.programmersbox.models.ItemModel
Expand Down Expand Up @@ -42,6 +43,10 @@ class MangaGalleryAdapter(context: Context, baseListFragment: BaseListFragment)
}
}

override val currentList: MutableList<DbModel> get() = mutableListOf()
override val previousList: MutableList<DbModel> get() = mutableListOf()
override fun update(list: List<DbModel>, check: (ItemModel, DbModel) -> Boolean) {}

}

class GalleryHolder(private val binding: MangaGalleryItemBinding) : RecyclerView.ViewHolder(binding.root) {
Expand Down

0 comments on commit 65c2b9d

Please sign in to comment.