Skip to content

Commit

Permalink
Provide dispatchers instead of harcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuwu committed Nov 28, 2023
1 parent f0cbed2 commit a999a69
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ import dev.arkbuilders.navigator.presentation.screen.resources.adapter.FileItemV
import dev.arkbuilders.navigator.presentation.screen.resources.adapter.ResourcesGridPresenter
import dev.arkbuilders.navigator.presentation.screen.settings.SettingsFragment
import dev.arkbuilders.arkfilepicker.folders.FoldersRepo
import dev.arkbuilders.navigator.di.modules.DispatcherModule
import javax.inject.Singleton

@Singleton
@Component(
modules = [
AppModule::class,
CiceroneModule::class,
RepoModule::class
RepoModule::class,
DispatcherModule::class,
]
)

Expand Down Expand Up @@ -63,7 +65,7 @@ interface AppComponent {
fun create(
@BindsInstance app: App,
@BindsInstance context: Context,
@BindsInstance foldersRepo: FoldersRepo
@BindsInstance foldersRepo: FoldersRepo,
): AppComponent
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.arkbuilders.navigator.di.modules

import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import javax.inject.Qualifier
@Module
class DispatcherModule {

@DefaultDispatcher
@Provides
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default

@IoDispatcher
@Provides
fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO

@MainDispatcher
@Provides
fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
}

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class DefaultDispatcher

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class IoDispatcher

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class MainDispatcher
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class FoldersViewModelFactory @AssistedInject constructor(
private val resourcesIndexRepo: ResourceIndexRepo,
private val preferences: Preferences,
private val permsHelper: PermissionsHelper,
private val devicePathsExtractor: DevicePathsExtractor
private val devicePathsExtractor: DevicePathsExtractor,
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return FoldersViewModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package dev.arkbuilders.navigator.presentation.screen.resources

import android.util.Log
import dev.arkbuilders.arkfilepicker.folders.FoldersRepo
import dev.arkbuilders.arkfilepicker.folders.RootAndFav
import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.data.Message
import dev.arkbuilders.arklib.data.index.ResourceIndex
import dev.arkbuilders.arklib.data.index.ResourceIndexRepo
import dev.arkbuilders.arklib.data.meta.MetadataProcessor
import dev.arkbuilders.arklib.data.meta.MetadataProcessorRepo
import dev.arkbuilders.arklib.data.preview.PreviewProcessor
import dev.arkbuilders.arklib.data.preview.PreviewProcessorRepo
import dev.arkbuilders.arklib.data.storage.StorageException
import dev.arkbuilders.arklib.user.score.ScoreStorage
import dev.arkbuilders.arklib.user.score.ScoreStorageRepo
import dev.arkbuilders.arklib.user.tags.Tag
import dev.arkbuilders.arklib.user.tags.TagStorage
import dev.arkbuilders.arklib.user.tags.TagsStorageRepo
import dev.arkbuilders.components.tagselector.QueryMode
import dev.arkbuilders.components.tagselector.TagSelectorController
import dev.arkbuilders.components.tagselector.TagsSorting
Expand All @@ -10,11 +26,14 @@ import dev.arkbuilders.navigator.data.stats.StatsStorage
import dev.arkbuilders.navigator.data.stats.StatsStorageRepo
import dev.arkbuilders.navigator.data.utils.LogTags.RESOURCES_SCREEN
import dev.arkbuilders.navigator.data.utils.findNotExistCopyName
import dev.arkbuilders.navigator.di.modules.DefaultDispatcher
import dev.arkbuilders.navigator.di.modules.IoDispatcher
import dev.arkbuilders.navigator.di.modules.MainDispatcher
import dev.arkbuilders.navigator.presentation.App
import dev.arkbuilders.navigator.presentation.navigation.AppRouter
import dev.arkbuilders.navigator.presentation.screen.resources.adapter.ResourcesGridPresenter
import dev.arkbuilders.navigator.presentation.utils.StringProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -25,22 +44,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import moxy.MvpPresenter
import moxy.presenterScope
import dev.arkbuilders.arkfilepicker.folders.FoldersRepo
import dev.arkbuilders.arkfilepicker.folders.RootAndFav
import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.data.Message
import dev.arkbuilders.arklib.data.index.ResourceIndex
import dev.arkbuilders.arklib.data.index.ResourceIndexRepo
import dev.arkbuilders.arklib.data.meta.MetadataProcessor
import dev.arkbuilders.arklib.data.meta.MetadataProcessorRepo
import dev.arkbuilders.arklib.data.preview.PreviewProcessor
import dev.arkbuilders.arklib.data.preview.PreviewProcessorRepo
import dev.arkbuilders.arklib.data.storage.StorageException
import dev.arkbuilders.arklib.user.score.ScoreStorage
import dev.arkbuilders.arklib.user.score.ScoreStorageRepo
import dev.arkbuilders.arklib.user.tags.Tag
import dev.arkbuilders.arklib.user.tags.TagStorage
import dev.arkbuilders.arklib.user.tags.TagsStorageRepo
import timber.log.Timber
import java.nio.file.Path
import javax.inject.Inject
Expand Down Expand Up @@ -82,10 +85,19 @@ class ResourcesPresenter(
@Inject
lateinit var stringProvider: StringProvider

@Inject
@MainDispatcher
lateinit var mainDispatcher: CoroutineDispatcher

@Inject
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher

@Inject
@DefaultDispatcher
lateinit var defaultDispatcher: CoroutineDispatcher

private val messageFlow: MutableSharedFlow<Message> = MutableSharedFlow()
private val mainDispatcher = Dispatchers.Main
private val ioDispatcher = Dispatchers.IO
private val defaultDispatcher = Dispatchers.Default

lateinit var index: ResourceIndex
private set
Expand Down

0 comments on commit a999a69

Please sign in to comment.