Skip to content

Commit

Permalink
feat: disabled light users for automatic uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
joragua committed Nov 26, 2024
1 parent 2df421b commit 507a4fe
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*
* @author Juan Carlos Garrote Gascón
* @author Aitor Ballesteros Pavón
* @author Jorge Aguado Recio
*
* Copyright (C) 2023 ownCloud GmbH.
* Copyright (C) 2024 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 Down Expand Up @@ -48,7 +49,9 @@ import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_PICTURE_UPLOADS_PA
import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_PICTURE_UPLOADS_SOURCE
import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_PICTURE_UPLOADS_WIFI_ONLY
import com.owncloud.android.domain.automaticuploads.model.UploadBehavior
import com.owncloud.android.extensions.collectLatestLifecycleFlow
import com.owncloud.android.extensions.showAlertDialog
import com.owncloud.android.presentation.accounts.ManageAccountsViewModel
import com.owncloud.android.ui.activity.FolderPickerActivity
import com.owncloud.android.utils.DisplayUtils
import kotlinx.coroutines.launch
Expand All @@ -59,6 +62,7 @@ class SettingsPictureUploadsFragment : PreferenceFragmentCompat() {

// ViewModel
private val picturesViewModel by viewModel<SettingsPictureUploadsViewModel>()
private val manageAccountsViewModel by viewModel<ManageAccountsViewModel>()

private var prefEnablePictureUploads: SwitchPreferenceCompat? = null
private var prefPictureUploadsPath: Preference? = null
Expand Down Expand Up @@ -103,10 +107,7 @@ class SettingsPictureUploadsFragment : PreferenceFragmentCompat() {
).toTypedArray()
entryValues = listOf(UploadBehavior.COPY.name, UploadBehavior.MOVE.name).toTypedArray()
}
prefPictureUploadsAccount = findPreference<ListPreference>(PREF__CAMERA_PICTURE_UPLOADS_ACCOUNT_NAME)?.apply {
entries = picturesViewModel.getLoggedAccountNames()
entryValues = picturesViewModel.getLoggedAccountNames()
}
prefPictureUploadsAccount = findPreference(PREF__CAMERA_PICTURE_UPLOADS_ACCOUNT_NAME)

val comment = getString(R.string.prefs_camera_upload_source_path_title_required)
prefPictureUploadsSourcePath?.title = String.format(prefPictureUploadsSourcePath?.title.toString(), comment)
Expand All @@ -123,18 +124,30 @@ class SettingsPictureUploadsFragment : PreferenceFragmentCompat() {
private fun initStateObservers() {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
picturesViewModel.pictureUploads.collect { pictureUploadsConfiguration ->
enablePictureUploads(pictureUploadsConfiguration != null)
pictureUploadsConfiguration?.let {
prefPictureUploadsAccount?.value = it.accountName
prefPictureUploadsPath?.summary = picturesViewModel.getUploadPathString()
prefPictureUploadsSourcePath?.summary = DisplayUtils.getPathWithoutLastSlash(it.sourcePath.toUri().path)
prefPictureUploadsOnWifi?.isChecked = it.wifiOnly
prefPictureUploadsOnCharging?.isChecked = it.chargingOnly
prefPictureUploadsBehaviour?.value = it.behavior.name
prefPictureUploadsLastSync?.summary = DisplayUtils.unixTimeToHumanReadable(it.lastSyncTimestamp)
spaceId = it.spaceId
} ?: resetFields()
collectLatestLifecycleFlow(manageAccountsViewModel.userQuotas) { listUserQuotas ->
val availableAccounts = listUserQuotas.filter { it.available != -4L }
prefPictureUploadsAccount?.apply {
entries = availableAccounts.map { it.accountName }.toTypedArray()
entryValues = availableAccounts.map { it.accountName }.toTypedArray()
}

if (availableAccounts.isEmpty()) {
disableFields()
} else {
picturesViewModel.pictureUploads.collect { pictureUploadsConfiguration ->
enablePictureUploads(pictureUploadsConfiguration != null)
pictureUploadsConfiguration?.let {
prefPictureUploadsAccount?.value = it.accountName
prefPictureUploadsPath?.summary = picturesViewModel.getUploadPathString()
prefPictureUploadsSourcePath?.summary = DisplayUtils.getPathWithoutLastSlash(it.sourcePath.toUri().path)
prefPictureUploadsOnWifi?.isChecked = it.wifiOnly
prefPictureUploadsOnCharging?.isChecked = it.chargingOnly
prefPictureUploadsBehaviour?.value = it.behavior.name
prefPictureUploadsLastSync?.summary = DisplayUtils.unixTimeToHumanReadable(it.lastSyncTimestamp)
spaceId = it.spaceId
} ?: resetFields()
}
}
}
}
}
Expand Down Expand Up @@ -248,4 +261,16 @@ class SettingsPictureUploadsFragment : PreferenceFragmentCompat() {
prefPictureUploadsBehaviour?.value = UploadBehavior.COPY.name
prefPictureUploadsLastSync?.summary = null
}

private fun disableFields() {
prefEnablePictureUploads?.isChecked = false
prefEnablePictureUploads?.isEnabled = false
prefPictureUploadsAccount?.isEnabled = false
prefPictureUploadsPath?.isEnabled = false
prefPictureUploadsSourcePath?.isEnabled = false
prefPictureUploadsOnWifi?.isEnabled = false
prefPictureUploadsOnCharging?.isEnabled = false
prefPictureUploadsBehaviour?.isEnabled = false
prefPictureUploadsLastSync?.isEnabled = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*
* @author Juan Carlos Garrote Gascón
* @author Aitor Ballesteros Pavón
* @author Jorge Aguado Recio
*
* Copyright (C) 2023 ownCloud GmbH.
* Copyright (C) 2024 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 Down Expand Up @@ -124,8 +125,6 @@ class SettingsPictureUploadsViewModel(

fun getPictureUploadsAccount() = _pictureUploads.value?.accountName

fun getLoggedAccountNames(): Array<String> = accountProvider.getLoggedAccounts().map { it.name }.toTypedArray()

fun getPictureUploadsPath() = _pictureUploads.value?.uploadPath ?: PREF__CAMERA_UPLOADS_DEFAULT_PATH

fun getPictureUploadsSourcePath(): String? = _pictureUploads.value?.sourcePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*
* @author Juan Carlos Garrote Gascón
* @author Aitor Ballesteros Pavón
* @author Jorge Aguado Recio
*
* Copyright (C) 2023 ownCloud GmbH.
* Copyright (C) 2024 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 Down Expand Up @@ -48,7 +49,9 @@ import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_VIDEO_UPLOADS_PATH
import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_VIDEO_UPLOADS_SOURCE
import com.owncloud.android.db.PreferenceManager.PREF__CAMERA_VIDEO_UPLOADS_WIFI_ONLY
import com.owncloud.android.domain.automaticuploads.model.UploadBehavior
import com.owncloud.android.extensions.collectLatestLifecycleFlow
import com.owncloud.android.extensions.showAlertDialog
import com.owncloud.android.presentation.accounts.ManageAccountsViewModel
import com.owncloud.android.ui.activity.FolderPickerActivity
import com.owncloud.android.utils.DisplayUtils
import kotlinx.coroutines.launch
Expand All @@ -59,6 +62,7 @@ class SettingsVideoUploadsFragment : PreferenceFragmentCompat() {

// ViewModel
private val videosViewModel by viewModel<SettingsVideoUploadsViewModel>()
private val manageAccountsViewModel by viewModel<ManageAccountsViewModel>()

private var prefEnableVideoUploads: SwitchPreferenceCompat? = null
private var prefVideoUploadsPath: Preference? = null
Expand Down Expand Up @@ -100,10 +104,7 @@ class SettingsVideoUploadsFragment : PreferenceFragmentCompat() {
entries = listOf(getString(R.string.pref_behaviour_entries_keep_file), getString(R.string.pref_behaviour_entries_remove_original_file)).toTypedArray()
entryValues = listOf(UploadBehavior.COPY.name, UploadBehavior.MOVE.name).toTypedArray()
}
prefVideoUploadsAccount = findPreference<ListPreference>(PREF__CAMERA_VIDEO_UPLOADS_ACCOUNT_NAME)?.apply {
entries = videosViewModel.getLoggedAccountNames()
entryValues = videosViewModel.getLoggedAccountNames()
}
prefVideoUploadsAccount = findPreference<ListPreference>(PREF__CAMERA_VIDEO_UPLOADS_ACCOUNT_NAME)

val comment = getString(R.string.prefs_camera_upload_source_path_title_required)
prefVideoUploadsSourcePath?.title = String.format(prefVideoUploadsSourcePath?.title.toString(), comment)
Expand All @@ -120,18 +121,30 @@ class SettingsVideoUploadsFragment : PreferenceFragmentCompat() {
private fun initLiveDataObservers() {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
videosViewModel.videoUploads.collect { videoUploadsConfiguration ->
enableVideoUploads(videoUploadsConfiguration != null)
videoUploadsConfiguration?.let {
prefVideoUploadsAccount?.value = it.accountName
prefVideoUploadsPath?.summary = videosViewModel.getUploadPathString()
prefVideoUploadsSourcePath?.summary = DisplayUtils.getPathWithoutLastSlash(it.sourcePath.toUri().path)
prefVideoUploadsOnWifi?.isChecked = it.wifiOnly
prefVideoUploadsOnCharging?.isChecked = it.chargingOnly
prefVideoUploadsBehaviour?.value = it.behavior.name
prefVideoUploadsLastSync?.summary = DisplayUtils.unixTimeToHumanReadable(it.lastSyncTimestamp)
spaceId = it.spaceId
} ?: resetFields()
collectLatestLifecycleFlow(manageAccountsViewModel.userQuotas) { listUserQuotas ->
val availableAccounts = listUserQuotas.filter { it.available != -4L }
prefVideoUploadsAccount?.apply {
entries = availableAccounts.map { it.accountName }.toTypedArray()
entryValues = availableAccounts.map { it.accountName }.toTypedArray()
}

if (availableAccounts.isEmpty()) {
disableFields()
} else {
videosViewModel.videoUploads.collect { videoUploadsConfiguration ->
enableVideoUploads(videoUploadsConfiguration != null)
videoUploadsConfiguration?.let {
prefVideoUploadsAccount?.value = it.accountName
prefVideoUploadsPath?.summary = videosViewModel.getUploadPathString()
prefVideoUploadsSourcePath?.summary = DisplayUtils.getPathWithoutLastSlash(it.sourcePath.toUri().path)
prefVideoUploadsOnWifi?.isChecked = it.wifiOnly
prefVideoUploadsOnCharging?.isChecked = it.chargingOnly
prefVideoUploadsBehaviour?.value = it.behavior.name
prefVideoUploadsLastSync?.summary = DisplayUtils.unixTimeToHumanReadable(it.lastSyncTimestamp)
spaceId = it.spaceId
} ?: resetFields()
}
}
}
}
}
Expand Down Expand Up @@ -245,4 +258,16 @@ class SettingsVideoUploadsFragment : PreferenceFragmentCompat() {
prefVideoUploadsBehaviour?.value = UploadBehavior.COPY.name
prefVideoUploadsLastSync?.summary = null
}

private fun disableFields() {
prefEnableVideoUploads?.isChecked = false
prefEnableVideoUploads?.isEnabled = false
prefVideoUploadsAccount?.isEnabled = false
prefVideoUploadsPath?.isEnabled = false
prefVideoUploadsSourcePath?.isEnabled = false
prefVideoUploadsOnWifi?.isEnabled = false
prefVideoUploadsOnCharging?.isEnabled = false
prefVideoUploadsBehaviour?.isEnabled = false
prefVideoUploadsLastSync?.isEnabled = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*
* @author Juan Carlos Garrote Gascón
* @author Aitor Ballesteros Pavón
* @author Jorge Aguado Recio
*
* Copyright (C) 2023 ownCloud GmbH.
* Copyright (C) 2024 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 Down Expand Up @@ -124,8 +125,6 @@ class SettingsVideoUploadsViewModel(

fun getVideoUploadsAccount() = _videoUploads.value?.accountName

fun getLoggedAccountNames(): Array<String> = accountProvider.getLoggedAccounts().map { it.name }.toTypedArray()

fun getVideoUploadsPath() = _videoUploads.value?.uploadPath ?: PREF__CAMERA_UPLOADS_DEFAULT_PATH

fun getVideoUploadsSourcePath(): String? = _videoUploads.value?.sourcePath
Expand Down

0 comments on commit 507a4fe

Please sign in to comment.