I need a little help.
Previously, my app used MediaStore or File API based on the device SDK, and every time I wanted to get photos or delete some, I created it as a field in my fragments by calling FileManagerFactory(requireContext())
class FileManagerFactory(context: Context) : PhotoManagerInterface {
private var manager: PhotoManagerInterface =
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
MediaStoreManager(context)
} else {
FileManager()
}
[...]
but now I am trying to get user input before I create FilesManagerFactory to decide if I want to see photos in the system gallery app. How do I do it?
I had many attempts with DataStore and wanted to everywhere use viewModel just for getting photoManager in my fragments, but it didn’t work:
@HiltViewModel
class MainViewModel @Inject constructor(
application: Application
) : AndroidViewModel(application) {
val dataStoreRepository = DataStoreRepository(application)
private val readSettings = dataStoreRepository.readSettings
private var selectedFileManager = NOT_SET
lateinit var photoManager: PhotoManagerInterface
init {
viewModelScope.launch {
readSettings.collect { settings ->
selectedFileManager = settings.selectedFileManager
Log.d(TAG, "init collect: $selectedFileManager")
photoManager = FileManagerFactory(application, selectedFileManager)
}
}
}
fun saveSettings(settings: Settings) =
viewModelScope.launch(Dispatchers.IO) {
dataStoreRepository.setSettings(settings)
}
}
Maybe I can stay with my original FileManagerFactory(requireContext())
case and try to get value from DataStore inside FileManagerFactory to choose which one to create? Would that make sense and would it work?