Retrofit2 | #6

MainActivity.kt

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.ListAdapter
import android.widget.SearchView.OnQueryTextListener
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.example.retrofitlesson.retrofit.User
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)
supportActionBar?.title = "Гость"
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)




var user: User? = null

CoroutineScope(Dispatchers.IO).launch {
user = mainApi.auth(
AuthRequest(
"kminchelle",
"0lelplR"
)
)
runOnUiThread {
supportActionBar?.title = user?.firstName
}
}


binding.sv.setOnQueryTextListener(object : OnQueryTextListener{
override fun onQueryTextSubmit(text: String?): Boolean {
CoroutineScope(Dispatchers.IO).launch {
val list = text?.let { mainApi.getProductsByNameAuth(user?.token ?: "", 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.Header
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query

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

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

@GET("auth/products")
suspend fun getAllProducts(): Products

@Headers("Content-Type: application/json")
@GET("auth/products/search")
suspend fun getProductsByNameAuth(@Header("Authorization") token: String,
@Query("q") name: String): Products

}

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

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