Skip to content

Commit

Permalink
Refactor to use dummy data
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinTCoughlin committed Nov 28, 2024
1 parent f348fc7 commit 4d34306
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cascadiacollections.jamoka.fragment.BinderRecyclerFragment
Expand All @@ -24,48 +23,71 @@ import retrofit2.Response
import retrofit2.Retrofit

class EpisodesFragment : BinderRecyclerFragment<Item, EpisodeViewHolder>(), Callback<Feed?> {

private val feedService: FeedService by lazy { createFeedService() }
private val layoutManager: LinearLayoutManager by lazy { LinearLayoutManager(context) }
private val adapter: BinderRecyclerAdapter<Item, EpisodeViewHolder> by lazy {
BinderRecyclerAdapter(
binder = EpisodeView(),
config = BinderRecyclerAdapterConfig(
enableDiffUtil = true,
adapterCallback = object :
BinderRecyclerAdapter.AdapterCallback<Item, RecyclerView.ViewHolder> {
override fun onItemBound(model: Item, viewHolder: RecyclerView.ViewHolder) {
println("Bound episode: ${model.title}")
}
}
enableDiffUtil = false
)
)
}
override fun getAdapter(): RecyclerView.Adapter<*> = adapter

override fun getLayoutManager(): RecyclerView.LayoutManager = layoutManager
override fun getLayoutManager(): RecyclerView.LayoutManager = LinearLayoutManager(context)

override fun onRefresh() {
fetchEpisodes()
}

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

recyclerView?.apply {
// @todo: fix in base
val linearLayoutManager = layoutManager as? LinearLayoutManager
linearLayoutManager?.let {
recyclerView?.addItemDecoration(
DividerItemDecoration(requireContext(), it.orientation)
)
}
adapter = this@EpisodesFragment.adapter
setHasFixedSize(true)
layoutManager = this@EpisodesFragment.getLayoutManager()
adapter = this@EpisodesFragment.getAdapter()
}
fetchEpisodes()

val dummyData = listOf(
Item(
guid = "1",
title = "Episode 1",
pubDate = "2024-11-01",
description = "This is a description for Episode 1",
duration = "25:00",
summary = "Summary of Episode 1",
origEnclosureLink = "https://example.com/episode1.mp3",
completed = false
),
Item(
guid = "2",
title = "Episode 2",
pubDate = "2024-11-02",
description = "This is a description for Episode 2",
duration = "30:00",
summary = "Summary of Episode 2",
origEnclosureLink = "https://example.com/episode2.mp3",
completed = false
),
Item(
guid = "3",
title = "Episode 3",
pubDate = "2024-11-03",
description = "This is a description for Episode 3",
duration = "40:00",
summary = "Summary of Episode 3",
origEnclosureLink = "https://example.com/episode3.mp3",
completed = false
)
)
adapter.updateItems(dummyData)
// fetchEpisodes()
}

override fun onResponse(call: Call<Feed?>, response: Response<Feed?>) {
response.body()?.channel?.items?.let {
adapter.updateItems(it) // Use the new `updateItems` method for efficient updates
adapter.updateItems(it)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cascadiacollections.jamoka.fragment

import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -16,14 +15,12 @@ import com.cascadiacollections.jamoka.R
* Supports binding adapters, layout managers, and item selection callbacks.
*/
abstract class BinderRecyclerFragment<T, VH : RecyclerView.ViewHolder> : Fragment(), SwipeRefreshLayout.OnRefreshListener {
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
lateinit var recyclerView: RecyclerView

private var swipeRefreshLayout: SwipeRefreshLayout? = null
var recyclerView: RecyclerView? = null
abstract fun getLayoutManager(): RecyclerView.LayoutManager

/**
* Provide the layout manager for the RecyclerView.
*/
protected abstract fun getLayoutManager(): RecyclerView.LayoutManager?
abstract fun getAdapter(): RecyclerView.Adapter<*>

@LayoutRes
protected open fun getLayoutResId(): Int = R.layout.fragment_recycler_layout
Expand All @@ -38,52 +35,9 @@ abstract class BinderRecyclerFragment<T, VH : RecyclerView.ViewHolder> : Fragmen

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

swipeRefreshLayout = view.findViewById(R.id.swipeContainer)
recyclerView = view.findViewById(R.id.list)

setupRecyclerView()
setupSwipeRefresh()
}

/**
* Save the RecyclerView's layout state.
*/
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
getLayoutManager()?.onSaveInstanceState()?.let {
outState.putParcelable(VIEW_STATE_KEY, it)
}
}

/**
* Restore the RecyclerView's layout state.
*/
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
savedInstanceState?.getParcelable(VIEW_STATE_KEY, Parcelable::class.java)?.let { layoutState ->
getLayoutManager()?.onRestoreInstanceState(layoutState)
}
}

/**
* Configures the RecyclerView with the provided adapter and layout manager.
*/
private fun setupRecyclerView() {
recyclerView?.apply {
setHasFixedSize(true)
layoutManager = layoutManager
this.adapter = adapter
}
}

/**
* Configures the SwipeRefreshLayout and its listener.
*/
private fun setupSwipeRefresh() {
swipeRefreshLayout?.setOnRefreshListener(this)
}

companion object {
private const val VIEW_STATE_KEY = "BinderRecyclerFragment.ViewState"
swipeRefreshLayout.setOnRefreshListener(this)
}
}

0 comments on commit 4d34306

Please sign in to comment.