Skip to content

Commit

Permalink
Inject TransferFileUtils using Hilt
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Oct 3, 2024
1 parent a2a5e2f commit 7148bc6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@
*/
package com.infomaniak.swisstransfer.ui.screen.newtransfer

import android.content.Context
import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.infomaniak.swisstransfer.ui.screen.newtransfer.TransferFileUtils.getTransferFile
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class NewTransferViewModel @Inject constructor(@ApplicationContext private val appContext: Context) : ViewModel() {
class NewTransferViewModel @Inject constructor(private val transferFileUtils: TransferFileUtils) : ViewModel() {

val transferFiles = MutableStateFlow<List<TransferFile>>(emptyList())
val failedTransferFileCount = MutableSharedFlow<Int>()
Expand All @@ -41,7 +38,7 @@ class NewTransferViewModel @Inject constructor(@ApplicationContext private val a

var failedFileCount = 0
uris.forEach { uri ->
appContext.getTransferFile(uri, alreadyUsedFileNames)?.let { transferFile ->
transferFileUtils.getTransferFile(uri, alreadyUsedFileNames)?.let { transferFile ->
transferFiles.value += transferFile
} ?: run {
failedFileCount++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ import android.database.Cursor
import android.net.Uri
import android.provider.OpenableColumns
import com.infomaniak.swisstransfer.ui.utils.FileNameUtils
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

object TransferFileUtils {
fun Context.getTransferFile(uri: Uri, alreadyUsedFileNames: Set<String>): TransferFile? {
val contentResolver: ContentResolver = contentResolver
@Singleton
class TransferFileUtils @Inject constructor(@ApplicationContext private val appContext: Context) {
fun getTransferFile(uri: Uri, alreadyUsedFileNames: Set<String>): TransferFile? {
val contentResolver: ContentResolver = appContext.contentResolver
val cursor: Cursor? = contentResolver.query(uri, null, null, null, null)

return cursor?.getFileNameAndSize()?.let { (name, size) ->
Expand Down

0 comments on commit 7148bc6

Please sign in to comment.