Skip to content

Commit

Permalink
cr - some improvements in vms and fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitorbp committed Nov 21, 2023
1 parent 461e351 commit b6bcc88
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 75 deletions.
Empty file added changelog/unreleased/4214
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import com.owncloud.android.domain.spaces.usecases.GetProjectSpacesWithSpecialsF
import com.owncloud.android.domain.spaces.usecases.GetSpaceWithSpecialsByIdForAccountUseCase
import com.owncloud.android.domain.spaces.usecases.GetSpacesFromEveryAccountUseCaseAsStream
import com.owncloud.android.domain.spaces.usecases.RefreshSpacesFromServerAsyncUseCase
import com.owncloud.android.domain.spaces.usecases.GetSpaceByIdUseCase
import com.owncloud.android.domain.spaces.usecases.GetSpaceByIdForAccountUseCase
import com.owncloud.android.domain.transfers.usecases.ClearSuccessfulTransfersUseCase
import com.owncloud.android.domain.transfers.usecases.GetAllTransfersAsStreamUseCase
import com.owncloud.android.domain.transfers.usecases.GetAllTransfersUseCase
Expand Down Expand Up @@ -215,7 +215,7 @@ val useCaseModule = module {
factoryOf(::GetSpacesFromEveryAccountUseCaseAsStream)
factoryOf(::GetWebDavUrlForSpaceUseCase)
factoryOf(::RefreshSpacesFromServerAsyncUseCase)
factoryOf(::GetSpaceByIdUseCase)
factoryOf(::GetSpaceByIdForAccountUseCase)

// Transfers
factoryOf(::CancelDownloadForFileUseCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,7 @@ class SettingsPictureUploadsFragment : PreferenceFragmentCompat() {
enablePictureUploads(pictureUploadsConfiguration != null)
pictureUploadsConfiguration?.let {
prefPictureUploadsAccount?.value = it.accountName
prefPictureUploadsPath?.summary = picturesViewModel.modifyUploadPath(
uploadPath = it.uploadPath,
spaceId = it.spaceId,
spaceName = it.spaceName
)
prefPictureUploadsPath?.summary = picturesViewModel.getUploadPathString()
prefPictureUploadsSourcePath?.summary = DisplayUtils.getPathWithoutLastSlash(it.sourcePath.toUri().path)
prefPictureUploadsOnWifi?.isChecked = it.wifiOnly
prefPictureUploadsOnCharging?.isChecked = it.chargingOnly
Expand Down Expand Up @@ -175,10 +171,9 @@ class SettingsPictureUploadsFragment : PreferenceFragmentCompat() {
uploadPath += File.separator
}
val intent = Intent(activity, FolderPickerActivity::class.java).apply {
val accountName = picturesViewModel.getPictureUploadsAccount()
putExtra(FolderPickerActivity.EXTRA_PICKER_MODE, FolderPickerActivity.PickerMode.UPLOAD_PATH)
putExtra(FolderPickerActivity.KEY_PERSONAL_SPACE_ID, spaceId)
putExtra(FolderPickerActivity.KEY_ACCOUNT_NAME, accountName)
putExtra(FolderPickerActivity.EXTRA_PICKER_MODE, FolderPickerActivity.PickerMode.CAMERA_FOLDER)
putExtra(FolderPickerActivity.KEY_SPACE_ID, spaceId)
putExtra(FolderPickerActivity.KEY_ACCOUNT_NAME, picturesViewModel.getPictureUploadsAccount())
}
selectPictureUploadsPathLauncher.launch(intent)
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author Juan Carlos Garrote Gascón
* @author Aitor Ballesteros Pavón
*
* Copyright (C) 2021 ownCloud GmbH.
* Copyright (C) 2023 ownCloud GmbH.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand All @@ -25,6 +25,7 @@ import android.content.Intent
import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.owncloud.android.R
import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_UPLOADS_DEFAULT_PATH
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration.Companion.pictureUploadsName
Expand All @@ -35,8 +36,9 @@ import com.owncloud.android.domain.camerauploads.usecases.SavePictureUploadsConf
import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.domain.spaces.model.OCSpace
import com.owncloud.android.domain.spaces.usecases.GetPersonalSpaceForAccountUseCase
import com.owncloud.android.domain.spaces.usecases.GetSpaceByIdUseCase
import com.owncloud.android.domain.spaces.usecases.GetSpaceByIdForAccountUseCase
import com.owncloud.android.providers.AccountProvider
import com.owncloud.android.providers.ContextProvider
import com.owncloud.android.providers.CoroutinesDispatcherProvider
import com.owncloud.android.providers.WorkManagerProvider
import com.owncloud.android.ui.activity.FolderPickerActivity
Expand All @@ -53,15 +55,16 @@ class SettingsPictureUploadsViewModel(
private val getPictureUploadsConfigurationStreamUseCase: GetPictureUploadsConfigurationStreamUseCase,
private val resetPictureUploadsUseCase: ResetPictureUploadsUseCase,
private val getPersonalSpaceForAccountUseCase: GetPersonalSpaceForAccountUseCase,
private val getSpaceByIdUseCase: GetSpaceByIdUseCase,
private val getSpaceByIdForAccountUseCase: GetSpaceByIdForAccountUseCase,
private val workManagerProvider: WorkManagerProvider,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider,
private val contextProvider: ContextProvider,
) : ViewModel() {

private val _pictureUploads: MutableStateFlow<FolderBackUpConfiguration?> = MutableStateFlow(null)
val pictureUploads: StateFlow<FolderBackUpConfiguration?> = _pictureUploads

private val _spaceFlow: MutableStateFlow<OCSpace?> = MutableStateFlow(null)
private var _space: OCSpace? = null

init {
initPictureUploads()
Expand All @@ -70,6 +73,9 @@ class SettingsPictureUploadsViewModel(
private fun initPictureUploads() {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
getPictureUploadsConfigurationStreamUseCase(Unit).collect { pictureUploadsConfiguration ->
if(pictureUploadsConfiguration?.accountName != null) {
getSpaceById(spaceId = pictureUploadsConfiguration.spaceId, accountName = pictureUploadsConfiguration.accountName)
}
_pictureUploads.update { pictureUploadsConfiguration }
}
}
Expand All @@ -84,7 +90,7 @@ class SettingsPictureUploadsViewModel(
SavePictureUploadsConfigurationUseCase.Params(
composePictureUploadsConfiguration(
accountName = name,
spaceId = _spaceFlow.value?.id,
spaceId = _space?.id,
)
)
)
Expand Down Expand Up @@ -128,13 +134,12 @@ class SettingsPictureUploadsViewModel(
val folderToUpload = data?.getParcelableExtra<OCFile>(FolderPickerActivity.EXTRA_FOLDER)
folderToUpload?.remotePath?.let {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
getSpaceById(_pictureUploads.value?.accountName!!, folderToUpload.spaceId)
getSpaceById(spaceId = folderToUpload.spaceId, accountName = _pictureUploads.value?.accountName!!)
savePictureUploadsConfigurationUseCase(
SavePictureUploadsConfigurationUseCase.Params(
composePictureUploadsConfiguration(
uploadPath = it,
spaceId = _spaceFlow.value?.id,
spaceName = _spaceFlow.value?.name,
spaceId = _space?.id,
)
)
)
Expand All @@ -150,7 +155,7 @@ class SettingsPictureUploadsViewModel(
composePictureUploadsConfiguration(
accountName = accountName,
uploadPath = null,
spaceId = _spaceFlow.value?.id,
spaceId = _space?.id,
)
)
)
Expand Down Expand Up @@ -196,8 +201,6 @@ class SettingsPictureUploadsViewModel(
behavior: UploadBehavior? = _pictureUploads.value?.behavior,
timestamp: Long? = _pictureUploads.value?.lastSyncTimestamp,
spaceId: String? = _pictureUploads.value?.spaceId,
spaceName: String? = _spaceFlow.value?.name,

): FolderBackUpConfiguration = FolderBackUpConfiguration(
accountName = accountName ?: accountProvider.getCurrentOwnCloudAccount()!!.name,
behavior = behavior ?: UploadBehavior.COPY,
Expand All @@ -208,20 +211,24 @@ class SettingsPictureUploadsViewModel(
lastSyncTimestamp = timestamp ?: System.currentTimeMillis(),
name = _pictureUploads.value?.name ?: pictureUploadsName,
spaceId = spaceId,
spaceName = handleSpaceName(spaceName),
).also {
Timber.d("Picture uploads configuration updated. New configuration: $it")
}

private fun handleSpaceName(spaceName: String?): String? {
return if (_spaceFlow.value?.isPersonal == true) {
PERSONAL_SPACE_NAME
return if (_space?.isPersonal == true) {
contextProvider.getString(R.string.bottom_nav_personal)
} else {
spaceName
}
}

fun modifyUploadPath(uploadPath: String?, spaceId: String?, spaceName: String?): String {
fun getUploadPathString(): String {

val spaceName = handleSpaceName(_space?.name)
val uploadPath = pictureUploads.value?.uploadPath
val spaceId = pictureUploads.value?.spaceId

return if (uploadPath != null) {
if (spaceId != null) {
"$spaceName: $uploadPath"
Expand All @@ -243,20 +250,16 @@ class SettingsPictureUploadsViewModel(
accountName = accountName
)
)
_spaceFlow.value = result
_space = result
}

private fun getSpaceById(accountName: String, spaceId: String?) {
val result = getSpaceByIdUseCase(
GetSpaceByIdUseCase.Params(
private fun getSpaceById(spaceId: String?, accountName: String) {
val result = getSpaceByIdForAccountUseCase(
GetSpaceByIdForAccountUseCase.Params(
accountName = accountName,
spaceId = spaceId
)
)
_spaceFlow.value = result
}

companion object {
private const val PERSONAL_SPACE_NAME = "Personal"
_space = result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ class SettingsVideoUploadsFragment : PreferenceFragmentCompat() {
enableVideoUploads(videoUploadsConfiguration != null)
videoUploadsConfiguration?.let {
prefVideoUploadsAccount?.value = it.accountName
prefVideoUploadsPath?.summary = videosViewModel.modifyUploadPath(
uploadPath = it.uploadPath,
spaceId = it.spaceId,
spaceName = it.spaceName)
prefVideoUploadsPath?.summary = videosViewModel.getUploadPathString()
prefVideoUploadsSourcePath?.summary = DisplayUtils.getPathWithoutLastSlash(it.sourcePath.toUri().path)
prefVideoUploadsOnWifi?.isChecked = it.wifiOnly
prefVideoUploadsOnCharging?.isChecked = it.chargingOnly
Expand Down Expand Up @@ -171,10 +168,9 @@ class SettingsVideoUploadsFragment : PreferenceFragmentCompat() {
uploadPath += File.separator
}
val intent = Intent(activity, FolderPickerActivity::class.java).apply {
val accountName = videosViewModel.getVideoUploadsAccount()
putExtra(FolderPickerActivity.EXTRA_PICKER_MODE, FolderPickerActivity.PickerMode.UPLOAD_PATH)
putExtra(FolderPickerActivity.KEY_PERSONAL_SPACE_ID, spaceId)
putExtra(FolderPickerActivity.KEY_ACCOUNT_NAME, accountName)
putExtra(FolderPickerActivity.EXTRA_PICKER_MODE, FolderPickerActivity.PickerMode.CAMERA_FOLDER)
putExtra(FolderPickerActivity.KEY_SPACE_ID, spaceId)
putExtra(FolderPickerActivity.KEY_ACCOUNT_NAME, videosViewModel.getVideoUploadsAccount())
}
selectVideoUploadsPathLauncher.launch(intent)
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.content.Intent
import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.owncloud.android.R
import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_UPLOADS_DEFAULT_PATH
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration.Companion.videoUploadsName
Expand All @@ -35,8 +36,9 @@ import com.owncloud.android.domain.camerauploads.usecases.SaveVideoUploadsConfig
import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.domain.spaces.model.OCSpace
import com.owncloud.android.domain.spaces.usecases.GetPersonalSpaceForAccountUseCase
import com.owncloud.android.domain.spaces.usecases.GetSpaceByIdUseCase
import com.owncloud.android.domain.spaces.usecases.GetSpaceByIdForAccountUseCase
import com.owncloud.android.providers.AccountProvider
import com.owncloud.android.providers.ContextProvider
import com.owncloud.android.providers.CoroutinesDispatcherProvider
import com.owncloud.android.providers.WorkManagerProvider
import com.owncloud.android.ui.activity.FolderPickerActivity
Expand All @@ -53,23 +55,26 @@ class SettingsVideoUploadsViewModel(
private val getVideoUploadsConfigurationStreamUseCase: GetVideoUploadsConfigurationStreamUseCase,
private val resetVideoUploadsUseCase: ResetVideoUploadsUseCase,
private val getPersonalSpaceForAccountUseCase: GetPersonalSpaceForAccountUseCase,
private val getSpaceByIdUseCase: GetSpaceByIdUseCase,
private val getSpaceByIdForAccountUseCase: GetSpaceByIdForAccountUseCase,
private val workManagerProvider: WorkManagerProvider,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider,
private val contextProvider: ContextProvider,
) : ViewModel() {

private val _videoUploads: MutableStateFlow<FolderBackUpConfiguration?> = MutableStateFlow(null)
val videoUploads: StateFlow<FolderBackUpConfiguration?> = _videoUploads

private val _spaceFlow: MutableStateFlow<OCSpace?> = MutableStateFlow(null)

private var _space: OCSpace? = null
init {
initVideoUploads()
}

private fun initVideoUploads() {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
getVideoUploadsConfigurationStreamUseCase(Unit).collect { videoUploadsConfiguration ->
if(videoUploadsConfiguration?.accountName != null) {
getSpaceById(spaceId = videoUploadsConfiguration.spaceId, accountName = videoUploadsConfiguration.accountName)
}
_videoUploads.update { videoUploadsConfiguration }
}
}
Expand All @@ -84,7 +89,7 @@ class SettingsVideoUploadsViewModel(
SaveVideoUploadsConfigurationUseCase.Params(
composeVideoUploadsConfiguration(
accountName = name,
spaceId = _spaceFlow.value?.id,
spaceId = _space?.id,
)
)
)
Expand Down Expand Up @@ -128,13 +133,12 @@ class SettingsVideoUploadsViewModel(
val folderToUpload = data?.getParcelableExtra<OCFile>(FolderPickerActivity.EXTRA_FOLDER)
folderToUpload?.remotePath?.let {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
getSpaceById(_videoUploads.value?.accountName!!, folderToUpload.spaceId)
getSpaceById(spaceId = folderToUpload.spaceId, accountName = _videoUploads.value?.accountName!!)
saveVideoUploadsConfigurationUseCase(
SaveVideoUploadsConfigurationUseCase.Params(
composeVideoUploadsConfiguration(
uploadPath = it,
spaceId = _spaceFlow.value?.id,
spaceName = _spaceFlow.value?.name,
spaceId = _space?.id,
)
)
)
Expand All @@ -150,7 +154,7 @@ class SettingsVideoUploadsViewModel(
composeVideoUploadsConfiguration(
accountName = accountName,
uploadPath = null,
spaceId = _spaceFlow.value?.id,
spaceId = _space?.id,
)
)
)
Expand Down Expand Up @@ -196,7 +200,6 @@ class SettingsVideoUploadsViewModel(
behavior: UploadBehavior? = _videoUploads.value?.behavior,
timestamp: Long? = _videoUploads.value?.lastSyncTimestamp,
spaceId: String? = _videoUploads.value?.spaceId,
spaceName: String? = _spaceFlow.value?.name,
): FolderBackUpConfiguration =
FolderBackUpConfiguration(
accountName = accountName ?: accountProvider.getCurrentOwnCloudAccount()!!.name,
Expand All @@ -208,20 +211,24 @@ class SettingsVideoUploadsViewModel(
lastSyncTimestamp = timestamp ?: System.currentTimeMillis(),
name = _videoUploads.value?.name ?: videoUploadsName,
spaceId = spaceId,
spaceName = handleSpaceName(spaceName),
).also {
Timber.d("Video uploads configuration updated. New configuration: $it")
}

private fun handleSpaceName(spaceName: String?): String? {
return if (_spaceFlow.value?.isPersonal == true) {
PERSONAL_SPACE_NAME
return if (_space?.isPersonal == true) {
contextProvider.getString(R.string.bottom_nav_personal)
} else {
spaceName
}
}

fun modifyUploadPath(uploadPath: String?, spaceId: String?, spaceName: String?): String {
fun getUploadPathString(): String {

val spaceName = handleSpaceName(_space?.name)
val uploadPath = videoUploads.value?.uploadPath
val spaceId = videoUploads.value?.spaceId

return if (uploadPath != null) {
if (spaceId != null) {
"$spaceName: $uploadPath"
Expand All @@ -243,20 +250,16 @@ class SettingsVideoUploadsViewModel(
accountName = accountName
)
)
_spaceFlow.value = result
_space = result
}

private fun getSpaceById(accountName: String, spaceId: String?) {
val result = getSpaceByIdUseCase(
GetSpaceByIdUseCase.Params(
private fun getSpaceById(spaceId: String?, accountName: String) {
val result = getSpaceByIdForAccountUseCase(
GetSpaceByIdForAccountUseCase.Params(
accountName = accountName,
spaceId = spaceId
)
)
_spaceFlow.value = result
}

companion object {
private const val PERSONAL_SPACE_NAME = "Personal"
_space = result
}
}
Loading

0 comments on commit b6bcc88

Please sign in to comment.