Retrofit2 | #4

MainActivity.kt

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.ListAdapter
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.retrofitlesson.adapter.ProductAdapter
import com.example.retrofitlesson.databinding.ActivityMainBinding
import com.example.retrofitlesson.retrofit.AuthRequest
import com.example.retrofitlesson.retrofit.MainApi
import com.squareup.picasso.Picasso
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MainActivity : AppCompatActivity() {
private lateinit var adapter: ProductAdapter
lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

adapter = ProductAdapter()
binding.rcView.layoutManager = LinearLayoutManager(this)
binding.rcView.adapter = adapter


val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY

val client = OkHttpClient.Builder()
.addInterceptor(interceptor)
.build()

val retrofit = Retrofit.Builder()
.baseUrl("https://dummyjson.com").client(client)
.addConverterFactory(GsonConverterFactory.create()).build()
val mainApi = retrofit.create(MainApi::class.java)

CoroutineScope(Dispatchers.IO).launch {
val list = mainApi.getAllProducts()
runOnUiThread {
binding.apply {
adapter.submitList(list.products)
}
}
}
}
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />

<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextView" />
</LinearLayout>
</androidx.cardview.widget.CardView>

ProductAdapter.kt

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.retrofitlesson.R
import com.example.retrofitlesson.databinding.ListItemBinding
import com.example.retrofitlesson.retrofit.Product

class ProductAdapter : ListAdapter<Product, ProductAdapter.Holder>(Comparator()) {

class Holder(view: View) : RecyclerView.ViewHolder(view){
private val binding = ListItemBinding.bind(view)

fun bind(product: Product)= with(binding){
title.text = product.title
description.text = product.description
}
}

class Comparator : DiffUtil.ItemCallback<Product>(){
override fun areItemsTheSame(oldItem: Product, newItem: Product): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(oldItem: Product, newItem: Product): Boolean {
return oldItem == newItem
}

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return Holder(view)
}

override fun onBindViewHolder(holder: Holder, position: Int) {
holder.bind(getItem(position))
}
}

MainApi.kt

import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path

interface MainApi {
@GET("products/{id}")
suspend fun getProductById(@Path("id") id: Int): Product

@POST("auth/login")
suspend fun auth(@Body authRequest: AuthRequest): User

@GET("products")
suspend fun getAllProducts(): Products
}

Products.kt

data class Products(
val products: List<Product>
)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B8B8B8"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="3dp"
android:layout_marginEnd="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Добавить комментарий

Ваш адрес email не будет опубликован.