Android/Retrofit
Retrofit구조 변경(Android 앱 권장 아키텍처)
${Harvey}
2022. 11. 22. 17:56

안드로이드 앱 권장 아키텍처에 따라 지난 번에 만든 코드를 수정하였다.
build.gradle
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1")
implementation 'com.github.bumptech.glide:glide:4.13.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
CustomAdapter.kt
class CustomAdapter(val context :Context, val dataSet : List<Plant>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
class ViewHolder(view : View) : RecyclerView.ViewHolder(view) {
val textView : TextView = view.findViewById(R.id.textArea)
val imageView : ImageView = view.findViewById(R.id.imageArea)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.text_row_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.textView.text = dataSet[position].name
Glide.with(context)
.load(dataSet[position].imageUrl)
.into(holder.imageView)
}
override fun getItemCount(): Int {
return dataSet.size
}
}
RetrofitInstance.kt
object RetrofitInstance {
val BASE_URL = "https://raw.githubusercontent.com/"
val client = Retrofit
.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
fun getInstance() : Retrofit {
return client
}
}
Plant.kt
data class Plant (
val plantId : String,
val name : String,
val description : String,
val growZoneNumber : Int,
val wateringInterval : Int,
val imageUrl : String
)
MainViewModel.kt
class MainViewModel : ViewModel() {
// val retrofitInstance : MyApi = RetrofitInstance.getInstance().create(MyApi::class.java)
private val repository = Repository()
private val _result = MutableLiveData<List<Plant>>()
val result : LiveData<List<Plant>>
get() = _result
fun getAllData() = viewModelScope.launch {
Log.d("MainViewModel", repository.getAllData().toString())
_result.value = repository.getAllData()
}
}
MainActivity.kt
// https://raw.githubusercontent.com/googlecodelabs/kotlin-coroutines/master/advanced-coroutines-codelab/sunflower/src/main/assets/plants.json
// https://developer.android.com/training/dependency-injection/manual?hl=ko
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
viewModel.getAllData()
val rv = findViewById<RecyclerView>(R.id.rv)
viewModel.result.observe(this, Observer {
val customAdapter = CustomAdapter(this, it)
rv.adapter = customAdapter
rv.layoutManager = LinearLayoutManager(this)
})
}
}
MyApi.kt
interface MyApi {
@GET("googlecodelabs/kotlin-coroutines/master/advanced-coroutines-codelab/sunflower/src/main/assets/plants.json")
suspend fun getAllPlants() : List<Plant>
}
RetrofitInstance.kt
object RetrofitInstance {
val BASE_URL = "https://raw.githubusercontent.com/"
val client = Retrofit
.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
fun getInstance() : Retrofit {
return client
}
}
Repository.kt
class Repository {
private val client = RetrofitInstance.getInstance().create(MyApi::class.java)
suspend fun getAllData() = client.getAllPlants()
}