Skip to content

Commit

Permalink
Init state, simplify code flow
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuwu committed Jun 14, 2024
1 parent 3848f7e commit 71532b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ class GalleryUpliftFragment : Fragment() {
lateinit var factory: GalleryUpliftViewModelFactory.Factory
private val viewModel: GalleryUpliftViewModel by viewModels {
factory.create(
requireArguments().getBoolean(SELECTING_ENABLED_KEY)
)
selectingEnabled =requireArguments().getBoolean(SELECTING_ENABLED_KEY),
rootAndFav = requireArguments()[ROOT_AND_FAV_KEY] as RootAndFav,
resourcesIds = requireArguments().getParcelableArray(RESOURCES_KEY)!!
.toList() as List<ResourceId>
).apply {
App.instance.appComponent.inject(this@GalleryUpliftFragment)
}
}

private val scoreWidget by lazy {
Expand Down Expand Up @@ -103,11 +108,7 @@ class GalleryUpliftFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Timber.d(LogTags.GALLERY_SCREEN, "view created in GalleryFragment")
App.instance.appComponent.inject(this)
viewModel.initialize(
rootAndFav = requireArguments()[ROOT_AND_FAV_KEY] as RootAndFav,
resourcesIds = requireArguments().getParcelableArray(RESOURCES_KEY)!!
.toList() as List<ResourceId>
)
viewModel.onFirstViewAttach()
super.onViewCreated(view, savedInstanceState)
Timber.d(
LogTags.GALLERY_SCREEN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ import kotlin.io.path.getLastModifiedTime
import kotlin.io.path.notExists

class GalleryUpliftViewModel(
selectorNotEdit: Boolean,
selectingEnabled: Boolean,
private val rootAndFav: RootAndFav,
private val resourcesIds: List<ResourceId>,
val preferences: Preferences,
val router: AppRouter,
val indexRepo: ResourceIndexRepo,
Expand All @@ -82,11 +84,9 @@ class GalleryUpliftViewModel(
private lateinit var metadataStorage: MetadataProcessor
private lateinit var statsStorage: StatsStorage
private lateinit var scoreStorage: ScoreStorage
private lateinit var rootAndFav: RootAndFav
private lateinit var resourcesIds: List<ResourceId>

override val container: Container<GalleryState, GallerySideEffect> =
container(GalleryState())
container(GalleryState(selectingEnabled = selectingEnabled))

var galleryItems: MutableList<GalleryPresenter.GalleryItem> = mutableListOf()
var diffResult: DiffUtil.DiffResult? = null
Expand All @@ -108,15 +108,6 @@ class GalleryUpliftViewModel(
private val currentItem: GalleryPresenter.GalleryItem
get() = galleryItems[container.stateFlow.value.currentPos]

fun initialize(
rootAndFav: RootAndFav,
resourcesIds: List<ResourceId>,
) {
this.rootAndFav = rootAndFav
this.resourcesIds = resourcesIds
onFirstViewAttach()
}

fun onPreviewsItemClick() {
intent {
postSideEffect(GallerySideEffect.ControlVisible(isVisible = true))
Expand Down Expand Up @@ -185,7 +176,7 @@ class GalleryUpliftViewModel(
}
}

private fun onFirstViewAttach() {
fun onFirstViewAttach() {
analytics.trackScreen()
Timber.d(LogTags.GALLERY_SCREEN, "first view attached in GalleryPresenter")
viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModelProvider
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dev.arkbuilders.arkfilepicker.folders.RootAndFav
import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.data.index.ResourceIndexRepo
import dev.arkbuilders.arklib.data.meta.MetadataProcessorRepo
import dev.arkbuilders.arklib.data.preview.PreviewProcessorRepo
Expand All @@ -16,7 +18,9 @@ import dev.arkbuilders.navigator.data.stats.StatsStorageRepo
import dev.arkbuilders.navigator.presentation.navigation.AppRouter

class GalleryUpliftViewModelFactory @AssistedInject constructor(
@Assisted val selectorNotEdit: Boolean,
@Assisted val selectingEnabled: Boolean,
@Assisted private val rootAndFav: RootAndFav,
@Assisted private val resourcesIds: List<ResourceId>,
val preferences: Preferences,
val router: AppRouter,
val indexRepo: ResourceIndexRepo,
Expand All @@ -29,7 +33,9 @@ class GalleryUpliftViewModelFactory @AssistedInject constructor(
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return GalleryUpliftViewModel(
selectorNotEdit = selectorNotEdit,
selectingEnabled = selectingEnabled,
rootAndFav = rootAndFav,
resourcesIds = resourcesIds,
preferences = preferences,
router = router,
indexRepo = indexRepo,
Expand All @@ -45,7 +51,9 @@ class GalleryUpliftViewModelFactory @AssistedInject constructor(
@AssistedFactory
interface Factory {
fun create(
@Assisted selectorNotEdit: Boolean,
@Assisted selectingEnabled: Boolean,
@Assisted rootAndFav: RootAndFav,
@Assisted resourcesIds: List<ResourceId>,
): GalleryUpliftViewModelFactory
}
}

0 comments on commit 71532b4

Please sign in to comment.