Skip to content

Commit

Permalink
refactor: transform utility classes to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
JuancaG05 committed Jan 28, 2025
1 parent 3493a1d commit e1ae328
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import com.owncloud.android.presentation.authentication.AccountUtils.getAccounts
import com.owncloud.android.presentation.authentication.AccountUtils.getUsernameOfAccount
import com.owncloud.android.presentation.authentication.oauth.OAuthUtils
import com.owncloud.android.presentation.common.UIResult
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.Companion.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.security.LockType
import com.owncloud.android.presentation.security.SecurityEnforced
import com.owncloud.android.presentation.settings.SettingsActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import android.content.Context
import android.provider.DocumentsContract
import com.owncloud.android.R

class DocumentsProviderUtils {
companion object {
/**
* Notify Document Provider to refresh roots
*/
fun notifyDocumentsProviderRoots(context: Context) {
val authority = context.resources.getString(R.string.document_provider_authority)
val rootsUri = DocumentsContract.buildRootsUri(authority)
context.contentResolver.notifyChange(rootsUri, null)
}
object DocumentsProviderUtils {
/**
* Notify Document Provider to refresh roots
*/
fun notifyDocumentsProviderRoots(context: Context) {
val authority = context.resources.getString(R.string.document_provider_authority)
val rootsUri = DocumentsContract.buildRootsUri(authority)
context.contentResolver.notifyChange(rootsUri, null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import com.owncloud.android.R
import com.owncloud.android.databinding.PasscodelockBinding
import com.owncloud.android.domain.utils.Event
import com.owncloud.android.extensions.showBiometricDialog
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.Companion.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.security.biometric.BiometricStatus
import com.owncloud.android.presentation.security.biometric.BiometricViewModel
import com.owncloud.android.presentation.security.biometric.EnableBiometrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import com.owncloud.android.R
import com.owncloud.android.data.providers.implementation.OCSharedPreferencesProvider
import com.owncloud.android.databinding.ActivityPatternLockBinding
import com.owncloud.android.extensions.showBiometricDialog
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.Companion.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.security.PREFERENCE_LAST_UNLOCK_TIMESTAMP
import com.owncloud.android.presentation.security.biometric.BiometricStatus
import com.owncloud.android.presentation.security.biometric.BiometricViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import androidx.preference.PreferenceScreen
import com.owncloud.android.R
import com.owncloud.android.extensions.avoidScreenshotsIfNeeded
import com.owncloud.android.extensions.showMessageInSnackbar
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.Companion.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.documentsprovider.DocumentsProviderUtils.notifyDocumentsProviderRoots
import com.owncloud.android.presentation.security.LockTimeout
import com.owncloud.android.presentation.security.PREFERENCE_LOCK_TIMEOUT
import com.owncloud.android.presentation.security.biometric.BiometricActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,80 +24,78 @@ package com.owncloud.android.utils
import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation

class RemoteFileUtils {
companion object {
/**
* Checks if remotePath does not exist in the server and returns it, or adds
* a suffix to it in order to avoid the server file is overwritten.
*
* @param ownCloudClient
* @param remotePath
* @return
*/
fun getAvailableRemotePath(
ownCloudClient: OwnCloudClient,
remotePath: String,
spaceWebDavUrl: String? = null,
isUserLogged: Boolean,
): String {
var checkExistsFile = existsFile(
ownCloudClient = ownCloudClient,
remotePath = remotePath,
spaceWebDavUrl = spaceWebDavUrl,
isUserLogged = isUserLogged,
)
if (!checkExistsFile) {
return remotePath
}
val pos = remotePath.lastIndexOf(".")
var suffix: String
var extension = ""
if (pos >= 0) {
extension = remotePath.substring(pos + 1)
remotePath.apply {
substring(0, pos)
}
}
var count = 1
do {
suffix = " ($count)"
checkExistsFile = if (pos >= 0) {
existsFile(
ownCloudClient = ownCloudClient,
remotePath = "${remotePath.substringBeforeLast('.', "")}$suffix.$extension",
spaceWebDavUrl = spaceWebDavUrl,
isUserLogged = isUserLogged,
)
} else {
existsFile(
ownCloudClient = ownCloudClient,
remotePath = remotePath + suffix,
spaceWebDavUrl = spaceWebDavUrl,
isUserLogged = isUserLogged,
)
}
count++
} while (checkExistsFile)
return if (pos >= 0) {
"${remotePath.substringBeforeLast('.', "")}$suffix.$extension"
} else {
remotePath + suffix
object RemoteFileUtils {
/**
* Checks if remotePath does not exist in the server and returns it, or adds
* a suffix to it in order to avoid the server file is overwritten.
*
* @param ownCloudClient
* @param remotePath
* @return
*/
fun getAvailableRemotePath(
ownCloudClient: OwnCloudClient,
remotePath: String,
spaceWebDavUrl: String? = null,
isUserLogged: Boolean,
): String {
var checkExistsFile = existsFile(
ownCloudClient = ownCloudClient,
remotePath = remotePath,
spaceWebDavUrl = spaceWebDavUrl,
isUserLogged = isUserLogged,
)
if (!checkExistsFile) {
return remotePath
}
val pos = remotePath.lastIndexOf(".")
var suffix: String
var extension = ""
if (pos >= 0) {
extension = remotePath.substring(pos + 1)
remotePath.apply {
substring(0, pos)
}
}

private fun existsFile(
ownCloudClient: OwnCloudClient,
remotePath: String,
spaceWebDavUrl: String?,
isUserLogged: Boolean,
): Boolean {
val existsOperation =
CheckPathExistenceRemoteOperation(
remotePath = remotePath,
isUserLoggedIn = isUserLogged,
var count = 1
do {
suffix = " ($count)"
checkExistsFile = if (pos >= 0) {
existsFile(
ownCloudClient = ownCloudClient,
remotePath = "${remotePath.substringBeforeLast('.', "")}$suffix.$extension",
spaceWebDavUrl = spaceWebDavUrl,
isUserLogged = isUserLogged,
)
return existsOperation.execute(ownCloudClient).isSuccess
} else {
existsFile(
ownCloudClient = ownCloudClient,
remotePath = remotePath + suffix,
spaceWebDavUrl = spaceWebDavUrl,
isUserLogged = isUserLogged,
)
}
count++
} while (checkExistsFile)
return if (pos >= 0) {
"${remotePath.substringBeforeLast('.', "")}$suffix.$extension"
} else {
remotePath + suffix
}
}

private fun existsFile(
ownCloudClient: OwnCloudClient,
remotePath: String,
spaceWebDavUrl: String?,
isUserLogged: Boolean,
): Boolean {
val existsOperation =
CheckPathExistenceRemoteOperation(
remotePath = remotePath,
isUserLoggedIn = isUserLogged,
spaceWebDavUrl = spaceWebDavUrl,
)
return existsOperation.execute(ownCloudClient).isSuccess
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import com.owncloud.android.lib.resources.files.chunks.ChunkedUploadFromFileSyst
import com.owncloud.android.lib.resources.files.services.implementation.OCChunkService
import com.owncloud.android.presentation.authentication.AccountUtils
import com.owncloud.android.utils.NotificationUtils
import com.owncloud.android.utils.RemoteFileUtils.Companion.getAvailableRemotePath
import com.owncloud.android.utils.RemoteFileUtils.getAvailableRemotePath
import com.owncloud.android.utils.SecurityUtils
import com.owncloud.android.utils.UPLOAD_NOTIFICATION_CHANNEL_ID
import kotlinx.coroutines.CoroutineScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import com.owncloud.android.lib.resources.files.chunks.ChunkedUploadFromFileSyst
import com.owncloud.android.lib.resources.files.services.implementation.OCChunkService
import com.owncloud.android.presentation.authentication.AccountUtils
import com.owncloud.android.utils.NotificationUtils
import com.owncloud.android.utils.RemoteFileUtils.Companion.getAvailableRemotePath
import com.owncloud.android.utils.RemoteFileUtils.getAvailableRemotePath
import com.owncloud.android.utils.SecurityUtils
import com.owncloud.android.utils.UPLOAD_NOTIFICATION_CHANNEL_ID
import kotlinx.coroutines.CoroutineScope
Expand Down

0 comments on commit e1ae328

Please sign in to comment.