Advertisement
EdGr87

app

Mar 8th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 7.09 KB | None | 0 0
  1. My podcast model
  2.  
  3. data class Podcast(
  4.     val feed: Feed
  5. )
  6. {
  7.     data class Feed(
  8.         val author: Author,
  9.         val entry: List<Entry>,
  10.         val updated: Updated,
  11.         val rights: Rights2,
  12.         val title: Title2,
  13.         val icon: Icon,
  14.         val link: List<Link2>,
  15.         val id: Id2,
  16.     )
  17.  
  18.     data class Author(
  19.         val name: Name,
  20.         val uri: Uri,
  21.     )
  22.  
  23.     data class Name(
  24.         val label: String,
  25.     )
  26.  
  27.     data class Uri(
  28.         val label: String,
  29.     )
  30.  
  31.     data class Entry(
  32.         @JsonProperty("im:name")
  33.         val imName: ImName,
  34.         @JsonProperty("im:image")
  35.         val imImage: List<ImImage>,
  36.         val summary: Summary,
  37.         @JsonProperty("im:price")
  38.         val imPrice: ImPrice,
  39.         @JsonProperty("im:contentType")
  40.         val imContentType: ImContentType,
  41.         val rights: Rights?,
  42.         val title: Title,
  43.         val link: Link,
  44.         val id: Id,
  45.         @JsonProperty("im:artist")
  46.         val imArtist: ImArtist,
  47.         val category: Category,
  48.         @JsonProperty("im:releaseDate")
  49.         val imReleaseDate: ImReleaseDate,
  50.     )
  51.  
  52.     data class ImName(
  53.         val label: String,
  54.     )
  55.  
  56.     data class ImImage(
  57.         val label: String,
  58.         val attributes: Attributes,
  59.     )
  60.  
  61.     data class Attributes(
  62.         val height: String,
  63.     )
  64.  
  65.     data class Summary(
  66.         val label: String,
  67.     )
  68.  
  69.     data class ImPrice(
  70.         val label: String,
  71.         val attributes: Attributes2,
  72.     )
  73.  
  74.     data class Attributes2(
  75.         val amount: String,
  76.         val currency: String,
  77.     )
  78.  
  79.     data class ImContentType(
  80.         val attributes: Attributes3,
  81.     )
  82.  
  83.     data class Attributes3(
  84.         val term: String,
  85.         val label: String,
  86.     )
  87.  
  88.     data class Rights(
  89.         val label: String,
  90.     )
  91.  
  92.     data class Title(
  93.         val label: String,
  94.     )
  95.  
  96.     data class Link(
  97.         val attributes: Attributes4,
  98.     )
  99.  
  100.     data class Attributes4(
  101.         val rel: String,
  102.         val type: String,
  103.         val href: String,
  104.     )
  105.  
  106.     data class Id(
  107.         val label: String,
  108.         val attributes: Attributes5,
  109.     )
  110.  
  111.     data class Attributes5(
  112.         @JsonProperty("im:id")
  113.         val imId: String,
  114.     )
  115.  
  116.     data class ImArtist(
  117.         val label: String,
  118.         val attributes: Attributes6?,
  119.     )
  120.  
  121.     data class Attributes6(
  122.         val href: String,
  123.     )
  124.  
  125.     data class Category(
  126.         val attributes: Attributes7,
  127.     )
  128.  
  129.     data class Attributes7(
  130.         @JsonProperty("im:id")
  131.         val imId: String,
  132.         val term: String,
  133.         val scheme: String,
  134.         val label: String,
  135.     )
  136.  
  137.     data class ImReleaseDate(
  138.         val label: String,
  139.         val attributes: Attributes8,
  140.     )
  141.  
  142.     data class Attributes8(
  143.         val label: String,
  144.     )
  145.  
  146.     data class Updated(
  147.         val label: String,
  148.     )
  149.  
  150.     data class Rights2(
  151.         val label: String,
  152.     )
  153.  
  154.     data class Title2(
  155.         val label: String,
  156.     )
  157.  
  158.     data class Icon(
  159.         val label: String,
  160.     )
  161.  
  162.     data class Link2(
  163.         val attributes: Attributes9,
  164.     )
  165.  
  166.     data class Attributes9(
  167.         val rel: String,
  168.         val type: String?,
  169.         val href: String,
  170.     )
  171.  
  172.     data class Id2(
  173.         val label: String
  174.     )
  175.  
  176. My api client
  177.  
  178.  private lateinit var retrofit: Retrofit
  179.     private val cacheSize = 10 * 1024 * 1024 // 10 MB
  180.     private val cache = Cache(context.cacheDir, cacheSize.toLong())
  181.  
  182.     private val requestIterceptor = Interceptor { chain ->
  183.         val url = chain.request().url.newBuilder().build()
  184.  
  185.         val request = chain.request().newBuilder()
  186.             .url(url)
  187.             .cacheControl(CacheControl.Builder().maxAge(24, TimeUnit.HOURS).build()) // Set max age to 24 hours
  188.             .build()
  189.  
  190.         val response = chain.proceed(request)
  191.  
  192.         response.newBuilder()
  193.             .removeHeader("Pragma")
  194.             .removeHeader("Cache-Control")
  195.             .header("Cache-Control", "public, max-age=${24 * 60 * 60}") // Set max age to 24 hours
  196.             .build()
  197.     }
  198.  
  199.     private val httpClient = OkHttpClient.Builder()
  200.         .addInterceptor(requestIterceptor)
  201.         .cache(cache)
  202.         .build()
  203.  
  204.     fun getClient(): Retrofit{
  205.         retrofit = Retrofit.Builder()
  206.         .baseUrl("https://itunes.apple.com/")
  207.             .client(httpClient)
  208.             .addConverterFactory(GsonConverterFactory.create())
  209.             .build()
  210.  
  211.         return retrofit
  212.     }
  213.  
  214. my api service
  215.  
  216. interface ApiServices {
  217.     @get:GET("rss/toppodcasts/limit=100/genre=1310/json")
  218.     val getTopPodcasts: Call<Podcast>
  219.  
  220.     @GET("lookup")
  221.     fun getPodcastDetails(@Query("id") podcastId: String?): Call<PodcastDetail>
  222.  
  223.     @GET("lookup")
  224.     fun getPodcastEpisodes(
  225.         @Query("id") podcastId: String?,
  226.     ): Call<PodcastEpisodeDetail>
  227. }
  228.  
  229. and the Main
  230.  
  231.  
  232. class MainActivity : AppCompatActivity() {
  233.  
  234.     private lateinit var binding: ActivityMainBinding
  235.     private val podcastAdapter by lazy { PodcastAdapter() }
  236.     private val api : ApiServices by lazy {
  237.         ApiClient(this).getClient().create(ApiServices::class.java)
  238.     }
  239.     override fun onCreate(savedInstanceState: Bundle?) {
  240.         super.onCreate(savedInstanceState)
  241.         binding = ActivityMainBinding.inflate(layoutInflater)
  242.         setContentView(binding.root)
  243.  
  244.         binding.apply {
  245.             mainProgressBar.visibility = View.VISIBLE
  246.  
  247.             val callPodcastApi = api.getTopPodcasts
  248.             callPodcastApi.enqueue(object : retrofit2.Callback<Podcast>{
  249.                 override fun onResponse(call: Call<Podcast>, response: Response<Podcast>) {
  250.  
  251.                     mainProgressBar.visibility = View.GONE
  252.                     when(response.code()){
  253.                         // successful response
  254.                         in 200..299->{
  255.                             response.body().let { b ->
  256.                                 b?.feed.let {data ->
  257.                                     if (data != null){
  258.                                         podcastAdapter.differ.submitList(MutableList(Podcast))
  259.                                         mainRecycler.apply {
  260.                                             layoutManager = LinearLayoutManager(this@MainActivity)
  261.                                             adapter = podcastAdapter
  262.                                         }
  263.  
  264.                                     }
  265.                                 }
  266.                             }
  267.                         }
  268.                         // redirection response
  269.                         in 300..399->{}
  270.                         // client error response
  271.                         in 400..499 ->{}
  272.                         // server error response
  273.                         in 500..599->{}
  274.                     }
  275.                 }
  276.  
  277.                 override fun onFailure(call: Call<Podcast>, t: Throwable) {
  278.                     mainProgressBar.visibility = View.GONE
  279.                     Log.e("onFailure", "Err : ${t.message}")
  280.                 }
  281.  
  282.             })
  283.         }
  284.  
  285.     }
  286. }
  287.  
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement