Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinTCoughlin committed Dec 15, 2024
1 parent 1aad13e commit 674423f
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 119 deletions.

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

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

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

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

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
@@ -1,5 +1,6 @@
package com.kevintcoughlin.smodr.viewholders

import BinderRecyclerAdapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.HtmlCompat
Expand All @@ -10,9 +11,9 @@ import java.text.SimpleDateFormat
import java.util.Locale

/**
* Implementation of ItemBinder for binding Episode data to EpisodeViewHolder.
* Implementation of ViewHolderBinder for binding Episode data to EpisodeViewHolder.
*/
class EpisodeView : BinderRecyclerAdapter.ItemBinder<Item, EpisodeViewHolder> {
class EpisodeView : BinderRecyclerAdapter.ViewHolderBinder<Item, EpisodeViewHolder> {

override fun bind(model: Item, viewHolder: EpisodeViewHolder) = with(viewHolder.binding) {
title.text = model.title
Expand All @@ -24,7 +25,7 @@ class EpisodeView : BinderRecyclerAdapter.ItemBinder<Item, EpisodeViewHolder> {
)
}

override fun createViewHolder(parent: ViewGroup, viewType: Int) =
override fun createViewHolder(parent: ViewGroup, viewType: Int): EpisodeViewHolder =
EpisodeViewHolder(ItemListEpisodeLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.kevintcoughlin.smodr.views.fragments

import BinderRecyclerAdapter
import BinderRecyclerAdapterConfig
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cascadiacollections.jamoka.fragment.BinderRecyclerFragment
import com.kevintcoughlin.smodr.models.Channel
import com.kevintcoughlin.smodr.models.Feed
Expand All @@ -20,78 +18,51 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import BinderRecyclerAdapter
import BinderRecyclerAdapterConfig

class EpisodesFragment : BinderRecyclerFragment(), Callback<Feed?> {
private val feedService: FeedService by lazy { createFeedService() }
private val adapter: BinderRecyclerAdapter<Item, EpisodeViewHolder> by lazy {
BinderRecyclerAdapter(
binder = EpisodeView(),
config = BinderRecyclerAdapterConfig(
enableDiffUtil = false
)
viewHolderBinder = EpisodeView(),
config = BinderRecyclerAdapterConfig.Builder<Item>()
.enableDiffUtil(false)
.build()
)
}

override fun configureRecyclerView(recyclerView: RecyclerView) {
recyclerView.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = this@EpisodesFragment.adapter
}
}

override fun onRefresh() {
fetchEpisodes()
}

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

recyclerView.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = adapter
}

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()
fetchEpisodes()
}

override fun onResponse(call: Call<Feed?>, response: Response<Feed?>) {
swipeRefreshLayout.isRefreshing = false
response.body()?.channel?.items?.let {
adapter.updateItems(it)
}
}

override fun onFailure(call: Call<Feed?>, t: Throwable) {
swipeRefreshLayout.isRefreshing = false
Toast.makeText(context, t.message, Toast.LENGTH_SHORT).show()
}

private fun fetchEpisodes() {
swipeRefreshLayout.isRefreshing = true
arguments?.getString(EPISODE_FEED_URL)?.let { feedUrl ->
feedService.feed(feedUrl).enqueue(this)
}
Expand All @@ -114,7 +85,7 @@ class EpisodesFragment : BinderRecyclerFragment(), Callback<Feed?> {
val TAG: String = EpisodesFragment::class.java.simpleName

@JvmStatic
fun create(channel: Channel): Fragment {
fun create(channel: Channel): EpisodesFragment {
return EpisodesFragment().apply {
arguments = Bundle().apply {
putString(EPISODE_FEED_URL, channel.link)
Expand Down
Loading

0 comments on commit 674423f

Please sign in to comment.