Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinTCoughlin committed Nov 29, 2024
1 parent 4d0e467 commit d104ccb
Showing 1 changed file with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,47 @@ import com.cascadiacollections.jamoka.R
* Generic fragment with RecyclerView and SwipeRefreshLayout integration.
* 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
abstract class BinderRecyclerFragment<T, VH : RecyclerView.ViewHolder>(
@LayoutRes private val layoutResId: Int = R.layout.fragment_recycler_layout
) : Fragment(), SwipeRefreshLayout.OnRefreshListener {

protected val swipeRefreshLayout: SwipeRefreshLayout
get() = requireView().findViewById(R.id.swipeContainer)

protected val recyclerView: RecyclerView
get() = requireView().findViewById(R.id.list)

abstract fun getLayoutManager(): RecyclerView.LayoutManager

abstract fun getAdapter(): RecyclerView.Adapter<*>

@LayoutRes
protected open fun getLayoutResId(): Int = R.layout.fragment_recycler_layout

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(getLayoutResId(), container, false)
}
): View? = inflater.inflate(layoutResId, container, false)

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

swipeRefreshLayout = view.findViewById(R.id.swipeContainer)
recyclerView = view.findViewById(R.id.list)
swipeRefreshLayout.setOnRefreshListener(this)
recyclerView.apply {
setHasFixedSize(true)
layoutManager = getLayoutManager()
adapter = getAdapter()
configureRecyclerView(this)
}

swipeRefreshLayout.apply {
setOnRefreshListener(this@BinderRecyclerFragment)
configureSwipeRefresh(this)
}
}

protected open fun configureRecyclerView(recyclerView: RecyclerView) {
// Optional for subclasses
}

protected open fun configureSwipeRefresh(swipeRefreshLayout: SwipeRefreshLayout) {
// Optional for subclasses
}
}

0 comments on commit d104ccb

Please sign in to comment.