Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 뷰모델 팩토리 방식 변경 #130

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
}
Loading