Skip to content

Commit cc2a35a

Browse files
authored
Merge pull request #4713 from owncloud/feature/improvements_in_space_quota
[FEATURE REQUEST] Improvements in space quota (creation and edition)
2 parents 4bb98f0 + 0e1c160 commit cc2a35a

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ ownCloud admins and users.
124124

125125
https://github.com/owncloud/android/issues/4607
126126
https://github.com/owncloud/android/issues/4688
127+
https://github.com/owncloud/android/issues/4695
127128
https://github.com/owncloud/android/pull/4687
128129
https://github.com/owncloud/android/pull/4694
130+
https://github.com/owncloud/android/pull/4713
129131

130132
* Enhancement - Disable/Remove a space: [#4611](https://github.com/owncloud/android/issues/4611)
131133

changelog/unreleased/4687

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ with the required permissions when the three-dot menu button is tapped.
55

66
https://github.com/owncloud/android/issues/4607
77
https://github.com/owncloud/android/issues/4688
8+
https://github.com/owncloud/android/issues/4695
89
https://github.com/owncloud/android/pull/4687
910
https://github.com/owncloud/android/pull/4694
11+
https://github.com/owncloud/android/pull/4713

owncloudApp/src/main/java/com/owncloud/android/presentation/spaces/createspace/CreateSpaceDialogFragment.kt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CreateSpaceDialogFragment : DialogFragment() {
6565
updateUI()
6666
createSpaceDialogQuotaNoRestrictionLabel.isVisible = !isChecked
6767
createSpaceDialogQuotaLayout.isVisible = isChecked
68-
createSpaceDialogQuotaGbLabel.isVisible = isChecked
68+
createSpaceDialogQuotaUnitLabel.isVisible = isChecked
6969
}
7070

7171
if (isEditMode) {
@@ -78,7 +78,9 @@ class CreateSpaceDialogFragment : DialogFragment() {
7878
val totalQuota = it.quota?.total ?: 0L
7979
if (totalQuota != 0L) {
8080
createSpaceDialogQuotaSwitch.isChecked = true
81-
createSpaceDialogQuotaValue.setText(DisplayUtils.formatFromBytesToGb(totalQuota))
81+
val formattedQuota = DisplayUtils.formatFromBytes(totalQuota)
82+
createSpaceDialogQuotaValue.setText(formattedQuota.first)
83+
createSpaceDialogQuotaUnitLabel.text = formattedQuota.second
8284
}
8385
}
8486

@@ -91,7 +93,8 @@ class CreateSpaceDialogFragment : DialogFragment() {
9193
createSpaceButton.setOnClickListener {
9294
val spaceName = createSpaceDialogNameValue.text.toString()
9395
val spaceSubtitle = createSpaceDialogSubtitleValue.text.toString()
94-
val spaceQuota = if (createSpaceDialogQuotaSwitch.isChecked) convertToBytes(createSpaceDialogQuotaValue.text.toString()) else 0L
96+
val spaceQuota = if (createSpaceDialogQuotaSwitch.isChecked) convertToBytes(createSpaceDialogQuotaValue.text.toString(),
97+
createSpaceDialogQuotaUnitLabel.text.toString()) else 0L
9598

9699
if (isEditMode) {
97100
currentSpace?.let {
@@ -127,15 +130,16 @@ class CreateSpaceDialogFragment : DialogFragment() {
127130

128131
return when {
129132
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)
133+
spaceQuota.toDouble() == MIN_SPACE_QUOTA_LIMIT -> getString(R.string.create_space_dialog_quota_zero_error)
134+
spaceQuota.toDouble() > MAX_SPACE_QUOTA_LIMIT -> getString(R.string.create_space_dialog_quota_too_large_error)
132135
else -> null
133136
}
134137
}
135138

136139
private fun updateUI() {
137140
val nameError = validateName(binding.createSpaceDialogNameValue.text.toString())
138-
val quotaError = validateQuota(binding.createSpaceDialogQuotaValue.text.toString())
141+
val quotaValue = convertToBytes(binding.createSpaceDialogQuotaValue.text.toString(), binding.createSpaceDialogQuotaUnitLabel.text.toString())
142+
val quotaError = validateQuota(quotaValue.toString())
139143
val noErrors = nameError == null && quotaError == null
140144

141145
val colorButton = if (noErrors) {
@@ -152,9 +156,18 @@ class CreateSpaceDialogFragment : DialogFragment() {
152156
}
153157
}
154158

155-
private fun convertToBytes(spaceQuota: String): Long {
156-
val quotaNumber = spaceQuota.toDoubleOrNull() ?: return 0L
157-
return (quotaNumber * 1_000_000_000L).toLong()
159+
private fun convertToBytes(spaceQuotaValue: String, spaceQuotaUnit: String): Long {
160+
val quotaNumber = spaceQuotaValue.toDoubleOrNull() ?: return 0L
161+
val multiplier = when (spaceQuotaUnit) {
162+
DisplayUtils.sizeSuffixes[0] -> B_MULTIPLIER
163+
DisplayUtils.sizeSuffixes[1] -> KB_MULTIPLIER
164+
DisplayUtils.sizeSuffixes[2] -> MB_MULTIPLIER
165+
DisplayUtils.sizeSuffixes[3] -> GB_MULTIPLIER
166+
DisplayUtils.sizeSuffixes[4] -> TB_MULTIPLIER
167+
DisplayUtils.sizeSuffixes[5] -> PB_MULTIPLIER
168+
else -> B_MULTIPLIER
169+
}
170+
return (quotaNumber * multiplier).toLong()
158171
}
159172

160173
interface CreateSpaceListener {
@@ -167,8 +180,14 @@ class CreateSpaceDialogFragment : DialogFragment() {
167180
private const val ARG_CAN_EDIT_SPACE_QUOTA = "CAN_EDIT_SPACE_QUOTA"
168181
private const val ARG_CURRENT_SPACE = "CURRENT_SPACE"
169182
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
183+
private const val MIN_SPACE_QUOTA_LIMIT = 0.0
184+
private const val MAX_SPACE_QUOTA_LIMIT = 1_000_000_000_000_000.0
185+
private const val B_MULTIPLIER = 1L
186+
private const val KB_MULTIPLIER = 1_000L
187+
private const val MB_MULTIPLIER = 1_000_000L
188+
private const val GB_MULTIPLIER = 1_000_000_000L
189+
private const val TB_MULTIPLIER = 1_000_000_000_000L
190+
private const val PB_MULTIPLIER = 1_000_000_000_000_000L
172191

173192
fun newInstance(
174193
isEditMode: Boolean,

owncloudApp/src/main/java/com/owncloud/android/utils/DisplayUtils.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
* @author Bartek Przybylski
55
* @author David A. Velasco
66
* @author David González Verdugo
7+
* @author Jorge Aguado Recio
8+
*
79
* Copyright (C) 2011 Bartek Przybylski
8-
* Copyright (C) 2020 ownCloud GmbH.
10+
* Copyright (C) 2025 ownCloud GmbH.
911
* <p>
1012
* This program is free software: you can redistribute it and/or modify
1113
* it under the terms of the GNU General Public License version 2,
@@ -27,6 +29,7 @@
2729
import android.content.res.Resources;
2830
import android.text.format.DateUtils;
2931
import android.util.DisplayMetrics;
32+
import android.util.Pair;
3033

3134
import androidx.core.content.ContextCompat;
3235
import com.google.android.material.snackbar.Snackbar;
@@ -35,6 +38,8 @@
3538

3639
import java.io.File;
3740
import java.math.BigDecimal;
41+
import java.math.MathContext;
42+
import java.math.RoundingMode;
3843
import java.net.IDN;
3944
import java.text.DateFormat;
4045
import java.util.Calendar;
@@ -49,7 +54,7 @@ public class DisplayUtils {
4954

5055
private static final String OWNCLOUD_APP_NAME = "ownCloud";
5156

52-
private static final String[] sizeSuffixes = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
57+
public static final String[] sizeSuffixes = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
5358
private static final int[] sizeScales = {0, 0, 1, 1, 1, 2, 2, 2, 2};
5459

5560
private static Map<String, String> mimeType2HumanReadable;
@@ -256,8 +261,22 @@ public static int getDrawerHeaderHeight(int displayCutout, Resources resources)
256261
}
257262
}
258263

259-
public static String formatFromBytesToGb(long bytes) {
260-
BigDecimal valueInGB = new BigDecimal(bytes).divide(BigDecimal.valueOf(1_000_000_000L)).stripTrailingZeros();
261-
return valueInGB.toPlainString();
264+
public static Pair<String, String> formatFromBytes(long bytes) {
265+
BigDecimal value = new BigDecimal(bytes);
266+
BigDecimal baseUnit = new BigDecimal(1000L);
267+
int unitIndex = 0;
268+
269+
while (value.compareTo(baseUnit) >= 0 && unitIndex < sizeSuffixes.length - 1) {
270+
value = value.divide(baseUnit);
271+
unitIndex++;
272+
}
273+
274+
if (value.compareTo(BigDecimal.ONE) >= 0) {
275+
value = value.setScale(1, RoundingMode.HALF_UP);
276+
} else {
277+
value = value.round(new MathContext(1, RoundingMode.HALF_UP));
278+
}
279+
280+
return new Pair<>(value.stripTrailingZeros().toPlainString(), sizeSuffixes[unitIndex]);
262281
}
263282
}

owncloudApp/src/main/res/layout/create_space_dialog.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
</com.google.android.material.textfield.TextInputLayout>
170170

171171
<TextView
172-
android:id="@+id/create_space_dialog_quota_gb_label"
172+
android:id="@+id/create_space_dialog_quota_unit_label"
173173
android:layout_width="wrap_content"
174174
android:layout_height="match_parent"
175175
android:layout_marginTop="@dimen/standard_half_margin"

0 commit comments

Comments
 (0)