refactor: 6주차 제로#45
Conversation
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionSha256Sum=b266d5ff6b90eada6dc3b20cb090e3731302e553a27c5d3e4df1f0d76beaff06 | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip |
| @@ -1,4 +1,4 @@ | |||
| package com.example.week1 | |||
| package com.example.week2 | |||
There was a problem hiding this comment.
데이터 처리 로직을 ViewModel로 이동하는 방식이 깔끔하다
| package com.example.week2.di | ||
|
|
||
| import android.content.Context | ||
| import com.example.week2.DataStoreManager | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object DataModule { | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideDataStoreManager(@ApplicationContext context: Context): DataStoreManager { | ||
| return DataStoreManager(context) | ||
| } | ||
| } |
kimdoyeon1234
left a comment
There was a problem hiding this comment.
수고하셨습니다!!
이번 코드는 화면별로 HomeViewModel, ProfileViewModel, AllProductsViewModel,
WishlistViewModel을 분리한 점, repository 패키지를 따로 만들어 ProductRepository와 UserRepository를 나눈 점, 그리고 FollowingAdapter에 DiffUtil을 적용한 점이 정말 좋았어요!
다만 이번 미션의 핵심은 패키지 분리입니다! 현재 Fragment, Model, DataStoreManager, ReqResService 등 대부분의 파일이 루트 패키지(com.example.week2)에 몰려있어요.
data/model/, data/repository/, domain/repository/, di/, ui/home/, ui/profile/, ui/purchase/, ui/wish/ 처럼 역할에 맞게 패키지를 나눠주세요 !
추가로 Hilt를 도입했는데 ApiClient object가 아직 남아있으니 삭제해주시고,
ProductAdapter도 notifyDataSetChanged() 대신 ListAdapter + DiffUtil로
교체해주시면 훨씬 완성도 있는 코드가 될 것 같아요!
전반적으로 ViewModel 분리와 Repository 구조는 잘 잡으셨으니
패키지 구조만 다듬어주시면 더욱 좋아질 것 같습니다. 고생 많으셨어요! 😊
| object ApiClient { | ||
| private const val BASE_URL = "https://reqres.in/" | ||
|
|
||
| private val retrofit: Retrofit by lazy { | ||
| Retrofit.Builder() | ||
| .baseUrl(BASE_URL) | ||
| .addConverterFactory(GsonConverterFactory.create()) | ||
| .build() | ||
| } | ||
|
|
There was a problem hiding this comment.
NetworkModule에서 이미 Retrofit을 Hilt로 제공하고 있는데 ApiClient object가 별도로 남아있어요!
Hilt를 도입했다면 ApiClient는 삭제하고 NetworkModule만 사용해주세요 !
| fun updateList(newProducts: List<Product>) { | ||
| this.products = newProducts | ||
| notifyDataSetChanged() | ||
| } | ||
|
|
There was a problem hiding this comment.
notifyDataSetChanged()는 전체 목록을 다시 그려서 성능이 좋지 않아요! 이전에 말한것처럼 ListAdapter + DiffUtil을 사용하면 변경된 항목만 업데이트해서 훨씬 효율적으로 동작합니다
There was a problem hiding this comment.
이번 미션의 핵심은 패키지 분리입니다! 모델 클래스들은 data/model/, DataStoreManager는 data/, ReqResService는 data/ 패키지로 이동시켜주세요! Models.kt, Product.kt, DataStoreManager.kt, ReqResService.kt 이것들!
There was a problem hiding this comment.
repository 패키지를 만들어서 분리한 점 아주 좋아요!! 다만 domain/repository/에 interface를 선언하고 data/repository/에 구현체를 두는 구조로 분리하면 더 완성도 있는 코드가 됩니다!
|
|
||
| Glide.with(this@ProfileFragment) | ||
| .load(it.avatar) | ||
| .circleCrop() | ||
| .into(binding.ivProfileImg) | ||
| } |
There was a problem hiding this comment.
이미지 로딩을 ProfileBindingAdapters.kt처럼 BindingAdapter로 분리하면 Fragment 코드가 더 깔끔해져요 !
| viewModel.followingList.observe(viewLifecycleOwner) { users -> | ||
| followingAdapter.submitList(users) | ||
| binding.tvFollowingTitle.text = "팔로잉 (${users.size})" | ||
| } | ||
|
|
There was a problem hiding this comment.
"팔로잉 (${users.size})" 같은 팔로잉 수 계산은 비즈니스 로직에 가까운거 같습니다! ViewModel에서 처리하고 Fragment는 결과만 받아서 보여주는 방식으로 바꾸는것도 좋을거 같아요!
📌refactor: 6주차 미션_제로
기존의 MVC 형태의 코드를 안드로이드 권장 아키텍처인 MVVM(Model-View-ViewModel)으로 변환하고, Hilt를 사용하여 의존성 주입을 구현했습니다.
🔗 관련 이슈
Closes #42
✨ 변경 사항
🔍 테스트
📸 스크린샷 (선택)
🚨 추가 이슈