Skip to content

Commit

Permalink
refactor: 뷰모델 팩토리 방식 변경 (#130)
Browse files Browse the repository at this point in the history
* refactor: 뷰모델 팩토리를 뷰모델의 동반객체로 이동

* style: lint적용
  • Loading branch information
Namyunsuk authored and ChooSeoyeon committed Oct 11, 2024
1 parent 47fcb07 commit 6621ac9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class OfferingDetailActivity : AppCompatActivity() {
obtainOfferingId()
}
private val viewModel: OfferingDetailViewModel by viewModels {
OfferingDetailViewModelFactory(
OfferingDetailViewModel.getFactory(
offeringId,
OfferingDetailRepositoryImpl(
OfferingDetailDataSourceImpl(NetworkManager.offeringDetailService()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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 <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras,
): T {
return OfferingDetailViewModel(
articleId,
offeringDetailRepository,
) as T
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras,
): T {
return OfferingViewModel(
offeringsRepository,
) as T
}
}
}
}

0 comments on commit 6621ac9

Please sign in to comment.