@@ -30,6 +30,7 @@ import androidx.fragment.app.DialogFragment
3030import com.owncloud.android.R
3131import com.owncloud.android.databinding.CreateSpaceDialogBinding
3232import com.owncloud.android.domain.spaces.model.OCSpace
33+ import com.owncloud.android.utils.DisplayUtils
3334
3435class CreateSpaceDialogFragment : DialogFragment () {
3536 private var _binding : CreateSpaceDialogBinding ? = null
@@ -51,9 +52,20 @@ class CreateSpaceDialogFragment : DialogFragment() {
5152 val canEditQuota = requireArguments().getBoolean(ARG_CAN_EDIT_SPACE_QUOTA )
5253 binding.apply {
5354 cancelCreateSpaceButton.setOnClickListener { dialog?.dismiss() }
54- createSpaceDialogNameValue.doOnTextChanged { name, _, _, _ ->
55- val errorMessage = validateName(name.toString())
56- updateUI(errorMessage)
55+ createSpaceDialogNameValue.doOnTextChanged { _, _, _, _ ->
56+ updateUI()
57+ }
58+
59+ createSpaceDialogQuotaValue.doOnTextChanged { _, _, _, _ ->
60+ updateUI()
61+ }
62+
63+ createSpaceDialogQuotaSwitch.setOnCheckedChangeListener { _, isChecked ->
64+ if (isChecked) { createSpaceDialogQuotaValue.setText(" 1" ) }
65+ updateUI()
66+ createSpaceDialogQuotaNoRestrictionLabel.isVisible = ! isChecked
67+ createSpaceDialogQuotaLayout.isVisible = isChecked
68+ createSpaceDialogQuotaGbLabel.isVisible = isChecked
5769 }
5870
5971 if (isEditMode) {
@@ -63,7 +75,11 @@ class CreateSpaceDialogFragment : DialogFragment() {
6375 currentSpace?.let {
6476 createSpaceDialogNameValue.setText(it.name)
6577 createSpaceDialogSubtitleValue.setText(it.description)
66- createSpaceDialogQuotaUnit.setSelection(getQuotaValueForSpinner(it.quota!! .total))
78+ val totalQuota = it.quota?.total ? : 0L
79+ if (totalQuota != 0L ) {
80+ createSpaceDialogQuotaSwitch.isChecked = true
81+ createSpaceDialogQuotaValue.setText(DisplayUtils .formatFromBytesToGb(totalQuota))
82+ }
6783 }
6884
6985 createSpaceButton.apply {
@@ -75,7 +91,7 @@ class CreateSpaceDialogFragment : DialogFragment() {
7591 createSpaceButton.setOnClickListener {
7692 val spaceName = createSpaceDialogNameValue.text.toString()
7793 val spaceSubtitle = createSpaceDialogSubtitleValue.text.toString()
78- val spaceQuota = convertToBytes(createSpaceDialogQuotaUnit.selectedItem .toString())
94+ val spaceQuota = if (createSpaceDialogQuotaSwitch.isChecked) convertToBytes(createSpaceDialogQuotaValue.text .toString()) else 0L
7995
8096 if (isEditMode) {
8197 currentSpace?.let {
@@ -106,37 +122,39 @@ class CreateSpaceDialogFragment : DialogFragment() {
106122 else -> null
107123 }
108124
109- private fun updateUI (errorMessage : String? ) {
110- val colorButton = if (errorMessage == null ) {
125+ private fun validateQuota (spaceQuota : String ): String? {
126+ if (! binding.createSpaceDialogQuotaSwitch.isChecked) return null
127+
128+ return when {
129+ spaceQuota.isEmpty() -> getString(R .string.create_space_dialog_quota_empty_error)
130+ spaceQuota.toDouble() == MIN_SPACE_QUOTA_GB -> getString(R .string.create_space_dialog_quota_zero_error)
131+ spaceQuota.toDouble() > MAX_SPACE_QUOTA_GB -> getString(R .string.create_space_dialog_quota_too_large_error)
132+ else -> null
133+ }
134+ }
135+
136+ private fun updateUI () {
137+ val nameError = validateName(binding.createSpaceDialogNameValue.text.toString())
138+ val quotaError = validateQuota(binding.createSpaceDialogQuotaValue.text.toString())
139+ val noErrors = nameError == null && quotaError == null
140+
141+ val colorButton = if (noErrors) {
111142 resources.getColor(R .color.primary_button_background_color, null )
112143 } else {
113144 resources.getColor(R .color.grey, null )
114145 }
115146
116- binding.createSpaceDialogNameValue.error = errorMessage
147+ binding.createSpaceDialogNameValue.error = nameError
148+ binding.createSpaceDialogQuotaValue.error = quotaError
117149 binding.createSpaceButton.apply {
118150 setTextColor(colorButton)
119- isEnabled = errorMessage == null
151+ isEnabled = noErrors
120152 }
121153 }
122154
123155 private fun convertToBytes (spaceQuota : String ): Long {
124- val quotaNumber = spaceQuota.removeSuffix(" GB" ).toLongOrNull() ? : return 0L
125- return quotaNumber * 1_000_000_000L
126- }
127-
128- private fun getQuotaValueForSpinner (spaceQuota : Long ): Int {
129- val totalGB = spaceQuota / 1_000_000_000.0
130- return when (totalGB) {
131- 1.0 -> 0
132- 2.0 -> 1
133- 5.0 -> 2
134- 10.0 -> 3
135- 50.0 -> 4
136- 100.0 -> 5
137- 0.0 -> 6
138- else -> 0
139- }
156+ val quotaNumber = spaceQuota.toDoubleOrNull() ? : return 0L
157+ return (quotaNumber * 1_000_000_000L ).toLong()
140158 }
141159
142160 interface CreateSpaceListener {
@@ -149,6 +167,8 @@ class CreateSpaceDialogFragment : DialogFragment() {
149167 private const val ARG_CAN_EDIT_SPACE_QUOTA = " CAN_EDIT_SPACE_QUOTA"
150168 private const val ARG_CURRENT_SPACE = " CURRENT_SPACE"
151169 private const val FORBIDDEN_CHARACTERS = """ [/\\.:?*"'><|]"""
170+ private const val MIN_SPACE_QUOTA_GB = 0.0
171+ private const val MAX_SPACE_QUOTA_GB = 1_000_000.0
152172
153173 fun newInstance (
154174 isEditMode : Boolean ,
0 commit comments