Retrofit2 | #5

MainActivity.kt

import android.os.Bundle
import android.widget.SearchView.OnQueryTextListener
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.MainApi
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)

binding.sv.setOnQueryTextListener(object : OnQueryTextListener{
override fun onQueryTextSubmit(text: String?): Boolean {
CoroutineScope(Dispatchers.IO).launch {
val list = text?.let { mainApi.getProductsByName(it) }
runOnUiThread {
binding.apply {
adapter.submitList(list?.products)
}
}
}
return true
}

override fun onQueryTextChange(text: String?): Boolean {
return true
}
})
}
}

MainApi.kt

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

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

@GET("products/search")
suspend fun getProductsByName(@Query("q") name: String): Products
}

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">

<SearchView
android:id="@+id/sv"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcView"
android:layout_width="0dp"
android:layout_height="0dp"
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_toBottomOf="@+id/sv" />
</androidx.constraintlayout.widget.ConstraintLayout>

1 комментарий для “Retrofit2 | #5”

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

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