binding.searchingTabBtn.setOnClickListener {
if(fragmentIndex == 0) return@setOnClickListener
fragmentIndex = 0
supportFragmentManager.beginTransaction()
.replace(R.id.main_fragment_container, SearchingFragment()).commitNow()
}
binding.archiveTabBtn.setOnClickListener {
if(fragmentIndex == 1) return@setOnClickListener
fragmentIndex = 1
supportFragmentManager.beginTransaction()
.replace(R.id.main_fragment_container, MyArchiveFragment()).commitNow()
}
여기까지 view layer 작업을 마쳤다.
class RetrofitController {
private val client = RetrofitClient.getInstance().create(RetrofitService::class.java)
suspend fun getImages(query: String): List<ImageData>
= client.getImages(query).documents.map {
ImageData(
imageUrl = it.image_url,
from = it.display_sitename,
time = it.datetime
)
}
}
object RetrofitClient {
private var instance: Retrofit? = null
private val gson = GsonBuilder().setLenient().create()
private const val BASE_URL = "https://dapi.kakao.com/v2/"
private const val CONNECT_TIMEOUT_SEC = 20000L
private const val KAKAO_APP_KEY = BuildConfig.KAKAO_API_KEY
private const val KAKAO_REST_API_KEY = BuildConfig.KAKAO_REST_KEY
fun getInstance() : Retrofit {
if(instance == null) {
instance = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkhttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return instance!!
}
private fun getOkhttpClient()
= OkHttpClient()
.newBuilder()
.addInterceptor { chain ->
val originalRequest = chain.request()
val requestBuilder = originalRequest.newBuilder()
.header("Authorization", "KakaoAK $KAKAO_REST_API_KEY")
val request = requestBuilder.build()
chain.proceed(request)
}.build()
}
RecyclerView Adapter View Holder에 추가
fun bind(item: ImageData) {
Glide.with(binding.root)
.load(item.imageUrl)
.into(binding.image)
with(binding) {
fromText.text = item.from
timeText.text = item.time
}
}