From 6621ac945316e356d353a040aff853b4caa04037 Mon Sep 17 00:00:00 2001 From: Namyunsuk <84739562+Namyunsuk@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:05:50 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=B7=B0=EB=AA=A8=EB=8D=B8=20?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A6=AC=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20(#130)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 뷰모델 팩토리를 뷰모델의 동반객체로 이동 * style: lint적용 --- .../view/detail/OfferingDetailActivity.kt | 2 +- .../view/detail/OfferingDetailViewModel.kt | 20 ++++++++++++++++++- .../detail/OfferingDetailViewModelFactory.kt | 17 ---------------- .../presentation/view/home/HomeFragment.kt | 2 +- .../view/home/OfferingViewModeFactory.kt | 13 ------------ .../view/home/OfferingViewModel.kt | 15 ++++++++++++++ 6 files changed, 36 insertions(+), 33 deletions(-) delete mode 100644 android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModelFactory.kt delete mode 100644 android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModeFactory.kt diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailActivity.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailActivity.kt index c8ebfaac1..33dc360a1 100644 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailActivity.kt +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailActivity.kt @@ -20,7 +20,7 @@ class OfferingDetailActivity : AppCompatActivity() { obtainOfferingId() } private val viewModel: OfferingDetailViewModel by viewModels { - OfferingDetailViewModelFactory( + OfferingDetailViewModel.getFactory( offeringId, OfferingDetailRepositoryImpl( OfferingDetailDataSourceImpl(NetworkManager.offeringDetailService()), diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModel.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModel.kt index eb6ba9f24..e275f3bc4 100644 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModel.kt +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModel.kt @@ -4,7 +4,9 @@ import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.CreationExtras import com.zzang.chongdae.BuildConfig import com.zzang.chongdae.domain.model.OfferingCondition import com.zzang.chongdae.domain.model.OfferingCondition.Companion.isAvailable @@ -71,7 +73,7 @@ class OfferingDetailViewModel( ).onSuccess { _isParticipated.value = true _isAvailable.value = false - _commentDetailEvent.setValue(offeringDetail.value?.title ?: "") + _commentDetailEvent.setValue(offeringDetail.value?.title ?: DEFAULT_TITLE) }.onFailure { Log.e("Error", it.message.toString()) } @@ -88,5 +90,21 @@ class OfferingDetailViewModel( companion object { private const val DEFAULT_TITLE = "" + + @Suppress("UNCHECKED_CAST") + fun getFactory( + articleId: Long, + offeringDetailRepository: OfferingDetailRepository, + ) = object : ViewModelProvider.Factory { + override fun create( + modelClass: Class, + extras: CreationExtras, + ): T { + return OfferingDetailViewModel( + articleId, + offeringDetailRepository, + ) as T + } + } } } diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModelFactory.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModelFactory.kt deleted file mode 100644 index a92071b9e..000000000 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/detail/OfferingDetailViewModelFactory.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.zzang.chongdae.presentation.view.detail - -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.zzang.chongdae.domain.repository.OfferingDetailRepository - -class OfferingDetailViewModelFactory( - private val articleId: Long, - private val offeringDetailRepository: OfferingDetailRepository, -) : ViewModelProvider.Factory { - override fun create(modelClass: Class): T { - return OfferingDetailViewModel( - articleId, - offeringDetailRepository, - ) as T - } -} diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt index 045d67ac8..6c4ba5bbf 100644 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/HomeFragment.kt @@ -21,7 +21,7 @@ class HomeFragment : Fragment(), OnArticleClickListener { private val binding get() = _binding!! private lateinit var offeringAdapter: OfferingAdapter private val viewModel: OfferingViewModel by viewModels { - OfferingViewModeFactory( + OfferingViewModel.getFactory( OfferingsRepositoryImpl( OfferingsDataSourceImpl(NetworkManager.offeringsService()), ), diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModeFactory.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModeFactory.kt deleted file mode 100644 index a1efb21b6..000000000 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModeFactory.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.zzang.chongdae.presentation.view.home - -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.zzang.chongdae.domain.repository.OfferingsRepository - -class OfferingViewModeFactory( - private val offeringsRepository: OfferingsRepository, -) : ViewModelProvider.Factory { - override fun create(modelClass: Class): T { - return OfferingViewModel(offeringsRepository) as T - } -} diff --git a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModel.kt b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModel.kt index 959c4bff3..fc8bf0e4f 100644 --- a/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModel.kt +++ b/android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModel.kt @@ -2,7 +2,9 @@ package com.zzang.chongdae.presentation.view.home import androidx.lifecycle.LiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.CreationExtras import androidx.paging.Pager import androidx.paging.PagingConfig import androidx.paging.PagingData @@ -24,5 +26,18 @@ class OfferingViewModel( companion object { private const val PAGE_SIZE = 10 + + @Suppress("UNCHECKED_CAST") + fun getFactory(offeringsRepository: OfferingsRepository) = + object : ViewModelProvider.Factory { + override fun create( + modelClass: Class, + extras: CreationExtras, + ): T { + return OfferingViewModel( + offeringsRepository, + ) as T + } + } } }