Skip to content

Commit e5acb58

Browse files
committed
Support readonly in Single choice inputs
1 parent ebef00e commit e5acb58

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed

app/src/main/java/org/bspb/smartbirds/pro/ui/utils/FormUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.bspb.smartbirds.pro.SmartBirdsApplication;
1111
import org.bspb.smartbirds.pro.ui.exception.ViewValidationException;
12+
import org.bspb.smartbirds.pro.ui.views.SupportReadOnly;
1213
import org.bspb.smartbirds.pro.ui.views.SupportRequiredView;
1314
import org.bspb.smartbirds.pro.ui.views.SupportStorage;
1415

@@ -120,7 +121,10 @@ public boolean validateFields() {
120121
}
121122

122123
public static void serialize(Map<String, String> storage, String field, View view) {
123-
if (!view.isEnabled()) {
124+
// Check if view is readonly - readonly views should persist even when disabled
125+
boolean isReadOnly = (view instanceof SupportReadOnly) && ((SupportReadOnly) view).isReadOnly();
126+
127+
if (!view.isEnabled() && !isReadOnly) {
124128
storage.put(field, "");
125129
return;
126130
}

app/src/main/java/org/bspb/smartbirds/pro/ui/views/SingleChoiceFormInput.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@
4646
/**
4747
* Created by groupsky on 14-10-10.
4848
*/
49-
public class SingleChoiceFormInput extends TextViewFormInput implements SupportStorage {
49+
public class SingleChoiceFormInput extends TextViewFormInput implements SupportStorage, SupportReadOnly {
5050

5151
private CharSequence key;
52+
private boolean mReadOnly = false;
5253

5354
NomenclaturesManager nomenclatures = NomenclaturesManager.Companion.getInstance();
5455

@@ -77,10 +78,16 @@ public SingleChoiceFormInput(Context context, AttributeSet attrs, int defStyle)
7778
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SingleChoiceFormInput, defStyle, 0);
7879
try {
7980
key = a.getText(R.styleable.SingleChoiceFormInput_entriesType);
81+
boolean readonly = a.getBoolean(R.styleable.SingleChoiceFormInput_readonly, false);
8082
SmartArrayAdapter<NomenclatureItem> adapter = new SmartArrayAdapter<>(context,
8183
R.layout.item_dialog_single_choice, new ArrayList<NomenclatureItem>());
8284
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
8385
setAdapter(adapter);
86+
87+
// Apply readonly state if specified in XML
88+
if (readonly) {
89+
setReadOnly(true);
90+
}
8491
} finally {
8592
a.recycle();
8693
}
@@ -208,8 +215,31 @@ public void setSelection(NomenclatureItem item) {
208215
if (onSelectionChangeListener != null) onSelectionChangeListener.onSelectionChange(this);
209216
}
210217

218+
@Override
219+
public void setReadOnly(boolean readOnly) {
220+
mReadOnly = readOnly;
221+
if (readOnly) {
222+
// Prevent interaction with the view
223+
setFocusable(false);
224+
setClickable(false);
225+
} else {
226+
// Restore normal interaction
227+
setFocusable(true);
228+
setClickable(true);
229+
}
230+
}
231+
232+
@Override
233+
public boolean isReadOnly() {
234+
return mReadOnly;
235+
}
236+
211237
@Override
212238
public boolean performClick() {
239+
// Block clicks when readonly
240+
if (mReadOnly) {
241+
return false;
242+
}
213243
super.performClick();
214244
new PopupDialog().show();
215245
return true;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.bspb.smartbirds.pro.ui.views;
2+
3+
/**
4+
* Interface for views that support read-only mode.
5+
*/
6+
public interface SupportReadOnly {
7+
/**
8+
* Set whether this view is in read-only mode.
9+
* When read-only, the view should:
10+
* - Display its current value
11+
* - Prevent user editing/interaction
12+
*
13+
* @param readOnly true to make the view read-only, false to allow editing
14+
*/
15+
void setReadOnly(boolean readOnly);
16+
17+
/**
18+
* Check if this view is currently in read-only mode.
19+
*
20+
* @return true if read-only, false otherwise
21+
*/
22+
boolean isReadOnly();
23+
}

app/src/main/kotlin/org/bspb/smartbirds/pro/ui/fragment/NewBearsEntryMainFormFragment.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ class NewBearsEntryMainFormFragment : BaseFormFragment() {
6363
species.label.labelId.equals("Ursus arctos", ignoreCase = true)
6464
}
6565
bearSpecies?.let { nomenclature ->
66-
nomenclature.localeLabel =
67-
nomenclature.label.get(context?.getString(R.string.locale))
6866
speciesInput?.setSelectionIfAvailable(NomenclatureItem(nomenclature))
6967
}
7068
} catch (t: Throwable) {

app/src/main/res/layout-land/fragment_monitoring_form_new_bears_entry.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
android:hint="@string/monitoring_bears_name"
5353
android:tag="@string/tag_species_scientific_name"
5454
app:entriesType="species_mammals"
55-
android:enabled="false"
55+
app:readonly="true"
5656
app:required="true" />
5757
</com.google.android.material.textfield.TextInputLayout>
5858
</TableRow>

app/src/main/res/layout/fragment_monitoring_form_new_bears_entry.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
android:hint="@string/monitoring_bears_name"
5555
android:tag="@string/tag_species_scientific_name"
5656
app:entriesType="species_mammals"
57-
android:enabled="false"
57+
app:readonly="true"
5858
app:required="true" />
5959
</com.google.android.material.textfield.TextInputLayout>
6060

app/src/main/res/values/attrs.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<attr name="required" format="boolean" />
2828
</declare-styleable>
2929

30+
<declare-styleable name="SupportReadOnly">
31+
<attr name="readonly" format="boolean" />
32+
</declare-styleable>
33+
3034
<attr name="entriesType" format="string" />
3135
<attr name="hint" format="string" />
3236
<!-- Value of the enum represent configs index in FormsConfig -->
@@ -44,6 +48,7 @@
4448
<!-- Stylable attributes for SingleChoiceFormInput -->
4549
<declare-styleable name="SingleChoiceFormInput">
4650
<attr name="entriesType" />
51+
<attr name="readonly" />
4752
</declare-styleable>
4853

4954
<!-- Stylable attributes for MultipleChoiceFormInput -->

0 commit comments

Comments
 (0)