From ed29c3955de2d3e61f0344203c0d28b692072b4e Mon Sep 17 00:00:00 2001 From: minseonglove Date: Mon, 20 Feb 2023 01:37:12 +0900 Subject: [PATCH] =?UTF-8?q?REFACTOR=20:=20AlbumListViewModel=EC=9D=98=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A3=BC=EC=9E=85=EC=9D=84=20Ass?= =?UTF-8?q?istedInject=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../albumList/AlbumListFragment.kt | 15 ++++++++++++++- .../albumList/AlbumListViewModel.kt | 19 +++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListFragment.kt b/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListFragment.kt index d4f14507..ec89a933 100644 --- a/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListFragment.kt +++ b/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListFragment.kt @@ -9,6 +9,8 @@ import androidx.activity.OnBackPressedCallback import androidx.core.content.ContextCompat import androidx.core.view.isVisible import androidx.fragment.app.viewModels +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.navArgs import androidx.recyclerview.widget.ItemTouchHelper @@ -20,13 +22,24 @@ import com.fakedevelopers.presentation.ui.productEditor.DragAndDropCallback import com.fakedevelopers.presentation.ui.util.repeatOnStarted import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.collectLatest +import javax.inject.Inject @AndroidEntryPoint class AlbumListFragment : BaseFragment( R.layout.fragment_album_list ) { - private val viewModel: AlbumListViewModel by viewModels() + @Inject + lateinit var viewModelFactory: AlbumListViewModel.PathAssistedFactory + + private val viewModel by viewModels { + object : ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + return viewModelFactory.create(args.albumPath) as T + } + } + } + private val args: AlbumListFragmentArgs by navArgs() private val backPressedCallback by lazy { diff --git a/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListViewModel.kt b/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListViewModel.kt index b92dfdda..117547a5 100644 --- a/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListViewModel.kt +++ b/presentation/src/main/java/com/fakedevelopers/presentation/ui/productEditor/albumList/AlbumListViewModel.kt @@ -1,6 +1,5 @@ package com.fakedevelopers.presentation.ui.productEditor.albumList -import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.fakedevelopers.domain.model.AlbumItem @@ -13,22 +12,22 @@ import com.fakedevelopers.presentation.ui.productEditor.SelectedPictureListAdapt import com.fakedevelopers.presentation.ui.util.MutableEventFlow import com.fakedevelopers.presentation.ui.util.ROTATE_DEGREE import com.fakedevelopers.presentation.ui.util.asEventFlow -import dagger.hilt.android.lifecycle.HiltViewModel +import dagger.assisted.Assisted +import dagger.assisted.AssistedFactory +import dagger.assisted.AssistedInject import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import java.util.Collections -import javax.inject.Inject import kotlin.math.roundToInt -@HiltViewModel -class AlbumListViewModel @Inject constructor( - args: SavedStateHandle, +class AlbumListViewModel @AssistedInject constructor( isValidUriUseCase: IsValidUriUseCase, private val getDateModifiedFromUriUseCase: GetDateModifiedByUriUseCase, private val getValidUrisUseCase: GetValidUrisUseCase, private val getImagesUseCase: GetImagesUseCase, - private val getImageObserverUseCase: GetImageObserverUseCase + private val getImageObserverUseCase: GetImageObserverUseCase, + @Assisted private val path: String ) : ViewModel() { private val _albumViewMode = MutableStateFlow(AlbumViewState.GRID) val albumViewMode: StateFlow get() = _albumViewMode @@ -51,9 +50,13 @@ class AlbumListViewModel @Inject constructor( var currentViewPagerIdx = 0 private set - private val path = args.get("albumPath") ?: "" val title = path.substringAfterLast('/') + @AssistedFactory + interface PathAssistedFactory { + fun create(path: String): AlbumListViewModel + } + init { viewModelScope.launch { _albumTitle.emit(title)