Skip to content

Commit 99201b0

Browse files
authored
Merge pull request #4694 from owncloud/feature/custom_value_for_space_quota
[FEATURE REQUEST] Custom value in space quota (creation and edition)
2 parents ae6d7b2 + f29c1d6 commit 99201b0

File tree

9 files changed

+129
-66
lines changed

9 files changed

+129
-66
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,19 @@ ownCloud admins and users.
120120
been implemented and, it is shown when the floating action button is tapped.
121121

122122
https://github.com/owncloud/android/issues/4606
123+
https://github.com/owncloud/android/issues/4688
123124
https://github.com/owncloud/android/pull/4683
125+
https://github.com/owncloud/android/pull/4694
124126

125127
* Enhancement - Edit a space: [#4607](https://github.com/owncloud/android/issues/4607)
126128

127129
A new edit space option has been added to the bottom sheet, available only to
128130
users with the required permissions when the three-dot menu button is tapped.
129131

130132
https://github.com/owncloud/android/issues/4607
133+
https://github.com/owncloud/android/issues/4688
131134
https://github.com/owncloud/android/pull/4687
135+
https://github.com/owncloud/android/pull/4694
132136

133137
* Enhancement - Update test in GitHub Actions: [#4663](https://github.com/owncloud/android/pull/4663)
134138

changelog/unreleased/4683

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ A new floating action button has been added to the spaces list, visible only to
44
In addition, a new dialog for space creation has been implemented and, it is shown when the floating action button is tapped.
55

66
https://github.com/owncloud/android/issues/4606
7+
https://github.com/owncloud/android/issues/4688
78
https://github.com/owncloud/android/pull/4683
9+
https://github.com/owncloud/android/pull/4694

changelog/unreleased/4687

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ A new edit space option has been added to the bottom sheet, available only to us
44
with the required permissions when the three-dot menu button is tapped.
55

66
https://github.com/owncloud/android/issues/4607
7+
https://github.com/owncloud/android/issues/4688
78
https://github.com/owncloud/android/pull/4687
9+
https://github.com/owncloud/android/pull/4694

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import androidx.fragment.app.DialogFragment
3030
import com.owncloud.android.R
3131
import com.owncloud.android.databinding.CreateSpaceDialogBinding
3232
import com.owncloud.android.domain.spaces.model.OCSpace
33+
import com.owncloud.android.utils.DisplayUtils
3334

3435
class 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,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,9 @@ public static int getDrawerHeaderHeight(int displayCutout, Resources resources)
255255
return (int) resources.getDimension(R.dimen.nav_drawer_header_height);
256256
}
257257
}
258+
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();
262+
}
258263
}

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

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,84 @@
119119

120120
</androidx.constraintlayout.widget.ConstraintLayout>
121121

122-
<LinearLayout
122+
<androidx.constraintlayout.widget.ConstraintLayout
123123
android:id="@+id/create_space_dialog_quota_section"
124124
android:layout_width="match_parent"
125-
android:layout_height="wrap_content"
126-
android:orientation="horizontal"
127-
android:gravity="center_vertical"
128-
android:layout_marginTop="@dimen/standard_half_padding"
129-
android:layout_marginEnd="@dimen/standard_half_padding">
125+
android:layout_height="wrap_content">
130126

131127
<TextView
132128
android:id="@+id/create_space_dialog_quota"
133129
android:layout_width="wrap_content"
134130
android:layout_height="wrap_content"
131+
android:layout_marginTop="@dimen/standard_half_margin"
135132
android:padding="@dimen/standard_padding"
136133
android:text="@string/create_space_dialog_quota"
137134
android:textColor="@color/black"
138-
android:textSize="15sp" />
135+
android:textSize="15sp"
136+
android:labelFor="@+id/create_space_dialog_quota_value"
137+
app:layout_constraintTop_toTopOf="parent"
138+
app:layout_constraintStart_toStartOf="parent"/>
139139

140-
<Spinner
141-
android:id="@+id/create_space_dialog_quota_unit"
142-
android:layout_width="0dp"
140+
<com.google.android.material.switchmaterial.SwitchMaterial
141+
android:id="@+id/create_space_dialog_quota_switch"
142+
android:layout_width="wrap_content"
143143
android:layout_height="wrap_content"
144-
android:layout_weight="1"
145-
android:spinnerMode="dropdown"
146-
android:entries="@array/create_space_dialog_quota_units"/>
144+
android:layout_marginTop="@dimen/standard_half_margin"
145+
android:contentDescription="@string/content_description_quota_switch"
146+
app:layout_constraintTop_toTopOf="parent"
147+
app:layout_constraintBottom_toBottomOf="parent"
148+
app:layout_constraintStart_toEndOf="@+id/create_space_dialog_quota"/>
147149

148-
</LinearLayout>
150+
<com.google.android.material.textfield.TextInputLayout
151+
android:id="@+id/create_space_dialog_quota_layout"
152+
android:layout_width="75dp"
153+
android:layout_height="wrap_content"
154+
android:layout_marginEnd="@dimen/standard_half_margin"
155+
android:layout_marginStart="@dimen/standard_half_margin"
156+
android:visibility="gone"
157+
app:layout_constraintStart_toEndOf="@id/create_space_dialog_quota_switch"
158+
app:layout_constraintTop_toTopOf="parent">
159+
160+
<com.google.android.material.textfield.TextInputEditText
161+
android:id="@+id/create_space_dialog_quota_value"
162+
android:layout_width="75dp"
163+
android:layout_height="wrap_content"
164+
android:inputType="numberDecimal"
165+
android:textSize="15sp"
166+
android:gravity="end"
167+
app:layout_constraintTop_toTopOf="parent"/>
168+
169+
</com.google.android.material.textfield.TextInputLayout>
170+
171+
<TextView
172+
android:id="@+id/create_space_dialog_quota_gb_label"
173+
android:layout_width="wrap_content"
174+
android:layout_height="match_parent"
175+
android:layout_marginTop="@dimen/standard_half_margin"
176+
android:layout_marginStart="@dimen/standard_half_margin"
177+
android:text="@string/create_space_dialog_gb_unit"
178+
android:textColor="@color/black"
179+
android:textSize="15sp"
180+
android:gravity="center_vertical"
181+
android:visibility="gone"
182+
app:layout_constraintTop_toTopOf="parent"
183+
app:layout_constraintBottom_toBottomOf="parent"
184+
app:layout_constraintStart_toEndOf="@+id/create_space_dialog_quota_layout" />
185+
186+
<TextView
187+
android:id="@+id/create_space_dialog_quota_no_restriction_label"
188+
android:layout_width="wrap_content"
189+
android:layout_height="match_parent"
190+
android:layout_marginStart="@dimen/standard_half_margin"
191+
android:layout_marginTop="@dimen/standard_half_margin"
192+
android:text="@string/create_space_dialog_no_restriction"
193+
android:textColor="@color/black"
194+
android:textSize="15sp"
195+
app:layout_constraintTop_toTopOf="parent"
196+
app:layout_constraintBottom_toBottomOf="parent"
197+
app:layout_constraintStart_toEndOf="@+id/create_space_dialog_quota_switch" />
198+
199+
</androidx.constraintlayout.widget.ConstraintLayout>
149200

150201
<LinearLayout
151202
android:layout_width="match_parent"

owncloudApp/src/main/res/values-et-rEE/strings.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,15 +771,6 @@
771771
<string name="create_space_dialog_name">Nimi</string>
772772
<string name="create_space_dialog_subtitle">Alapealkiri</string>
773773
<string name="create_space_dialog_quota">Mahupiir</string>
774-
<string-array name="create_space_dialog_quota_units">
775-
<item>1 GB</item>
776-
<item>2 GB</item>
777-
<item>5 GB</item>
778-
<item>10 GB</item>
779-
<item>50 GB</item>
780-
<item>100 GB</item>
781-
<item>Piiranguteta</item>
782-
</string-array>
783774
<string name="create_space_dialog_empty_error">Ruumi nimi ei tohi olla tühi</string>
784775
<string name="create_space_dialog_length_error">Ruumi nii ei tphi olla pikem kui 255 märki</string>
785776
<string name="create_space_dialog_characters_error">Keelatud märgid: / \\ . : ? * \" \' &gt; &lt; |</string>

owncloudApp/src/main/res/values-sq/strings.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -769,15 +769,6 @@
769769
<string name="create_space_dialog_name">Emër</string>
770770
<string name="create_space_dialog_subtitle">Nëntitull</string>
771771
<string name="create_space_dialog_quota">Kuota</string>
772-
<string-array name="create_space_dialog_quota_units">
773-
<item>1 GB</item>
774-
<item>2 GB</item>
775-
<item>5 GB</item>
776-
<item>10 GB</item>
777-
<item>50 GB</item>
778-
<item>100 GB</item>
779-
<item>Pa kufizim</item>
780-
</string-array>
781772
<string name="create_space_dialog_empty_error">Emri i hapësirës s’mund të jetë i zbrazët</string>
782773
<string name="create_space_dialog_length_error">Emri i hapësirës s’mund të jetë më i gjatë se 255 shenja</string>
783774
<string name="create_space_dialog_characters_error">Shenja të ndaluara: / \\ . : ? * \" \' &gt; &lt; |</string>

owncloudApp/src/main/res/values/strings.xml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@
826826
<string name="content_description_space_subtitle">%1$s space subtitle</string>
827827
<string name="content_description_create_button">Create button</string>
828828
<string name="content_description_cancel_button">Cancel button</string>
829+
<string name="content_description_quota_switch">Space quota</string>
829830

830831
<string name="create_shortcut_dialog_title">Create a shortcut</string>
831832
<string name="create_shortcut_dialog_url">URL</string>
@@ -839,18 +840,14 @@
839840
<string name="create_space_dialog_name">Name</string>
840841
<string name="create_space_dialog_subtitle">Subtitle</string>
841842
<string name="create_space_dialog_quota">Quota</string>
842-
<string-array name="create_space_dialog_quota_units">
843-
<item>1 GB</item>
844-
<item>2 GB</item>
845-
<item>5 GB</item>
846-
<item>10 GB</item>
847-
<item>50 GB</item>
848-
<item>100 GB</item>
849-
<item>No restriction</item>
850-
</string-array>
843+
<string name="create_space_dialog_no_restriction">No restriction</string>
844+
<string name="create_space_dialog_gb_unit">GB</string>
851845
<string name="create_space_dialog_empty_error">Space name must not be empty</string>
852846
<string name="create_space_dialog_length_error">Space name must not be longer than 255 characters</string>
853847
<string name="create_space_dialog_characters_error">Forbidden characters: / \\ . : ? * &quot; ' &gt; &lt; |</string>
848+
<string name="create_space_dialog_quota_empty_error">Space quota must not be empty</string>
849+
<string name="create_space_dialog_quota_zero_error">Space quota must be greater than zero</string>
850+
<string name="create_space_dialog_quota_too_large_error">Space quota cannot exceed 1 PB</string>
854851
<string name="create_space_correctly">Space created correctly</string>
855852
<string name="create_space_failed">Space could not be created</string>
856853
<string name="edit_space">Edit space</string>

0 commit comments

Comments
 (0)