Skip to content

Commit cf867e6

Browse files
Hide/show buttons
1 parent 0ce559a commit cf867e6

9 files changed

Lines changed: 115 additions & 82 deletions

File tree

app/src/main/java/org/open311/android/fragments/ProfileFragment.java

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import org.open311.android.R;
2121

22-
import java.text.Normalizer;
23-
2422
/**
2523
* Profile {@link Fragment} subclass.
2624
*/
@@ -29,6 +27,7 @@ public class ProfileFragment extends Fragment {
2927
private SharedPreferences settings;
3028
private EditText inputName, inputEmail, inputPhone;
3129
private TextInputLayout inputLayoutName, inputLayoutEmail, inputLayoutPhone;
30+
private FloatingActionButton mSubmitBtn;
3231

3332
public ProfileFragment() {
3433
// Required empty public constructor
@@ -61,8 +60,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
6160
inputEmail.addTextChangedListener(new MyTextWatcher(inputEmail));
6261
inputPhone.addTextChangedListener(new MyTextWatcher(inputPhone));
6362

64-
FloatingActionButton submit = (FloatingActionButton) view.findViewById(R.id.profile_submit);
65-
submit.setOnClickListener(new View.OnClickListener() {
63+
mSubmitBtn = (FloatingActionButton) view.findViewById(R.id.profile_submit);
64+
mSubmitBtn.setOnClickListener(new View.OnClickListener() {
6665
@Override
6766
public void onClick(View v) {
6867
onSubmitButtonClicked(v);
@@ -75,19 +74,9 @@ public void onClick(View v) {
7574
private void onSubmitButtonClicked(View v) {
7675
SharedPreferences.Editor editor = settings.edit();
7776
String result;
78-
79-
if (!validateName()) {
77+
if (!validate()) {
8078
return;
8179
}
82-
;
83-
if (!validateEmail()) {
84-
return;
85-
}
86-
;
87-
if (!validatePhone()) {
88-
return;
89-
}
90-
;
9180

9281
editor.putString("name", inputName.getText().toString());
9382
editor.putString("phone", inputPhone.getText().toString());
@@ -102,41 +91,31 @@ private void onSubmitButtonClicked(View v) {
10291
.show();
10392
}
10493

105-
private boolean validateName() {
94+
private boolean validate() {
10695
if (inputName.getText().toString().trim().isEmpty()) {
10796
inputLayoutName.setError(getString(R.string.invalid_name));
108-
requestFocus(inputName);
97+
mSubmitBtn.setVisibility(View.INVISIBLE);
10998
return false;
11099
} else {
111100
inputLayoutName.setErrorEnabled(false);
112101
}
113-
114-
return true;
115-
}
116-
117-
private boolean validateEmail() {
118102
String strEmail = inputEmail.getText().toString().trim();
119103

120104
if (strEmail.isEmpty() || !isValidEmail(strEmail)) {
121105
inputLayoutEmail.setError(getString(R.string.invalid_email));
122-
requestFocus(inputEmail);
106+
mSubmitBtn.setVisibility(View.INVISIBLE);
123107
return false;
124108
} else {
125109
inputLayoutEmail.setErrorEnabled(false);
126110
}
127-
128-
return true;
129-
}
130-
131-
private boolean validatePhone() {
132111
if (inputPhone.getText().toString().trim().isEmpty()) {
133112
inputLayoutPhone.setError(getString(R.string.invalid_phone));
134-
requestFocus(inputPhone);
113+
mSubmitBtn.setVisibility(View.INVISIBLE);
135114
return false;
136115
} else {
137116
inputLayoutPhone.setErrorEnabled(false);
138117
}
139-
118+
mSubmitBtn.setVisibility(View.VISIBLE);
140119
return true;
141120
}
142121

@@ -158,24 +137,18 @@ private MyTextWatcher(View view) {
158137
this.view = view;
159138
}
160139

140+
@Override
161141
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
142+
162143
}
163144

145+
@Override
164146
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
147+
165148
}
166149

167150
public void afterTextChanged(Editable editable) {
168-
switch (view.getId()) {
169-
case R.id.input_name:
170-
validateName();
171-
break;
172-
case R.id.input_email:
173-
validateEmail();
174-
break;
175-
case R.id.input_phone:
176-
validatePhone();
177-
break;
178-
}
151+
validate();
179152
}
180153
}
181154
}

app/src/main/java/org/open311/android/fragments/ReportFragment.java

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import android.content.SharedPreferences;
1818
import android.content.pm.PackageManager;
1919
import android.graphics.Bitmap;
20-
import android.graphics.Color;
2120
import android.net.Uri;
2221
import android.os.AsyncTask;
2322
import android.os.Build;
@@ -28,13 +27,14 @@
2827
import android.support.v4.app.Fragment;
2928
import android.support.v4.content.ContextCompat;
3029
import android.support.v7.widget.LinearLayoutCompat;
30+
import android.text.Editable;
31+
import android.text.TextWatcher;
3132
import android.util.Log;
3233
import android.view.LayoutInflater;
3334
import android.view.View;
3435
import android.view.View.OnClickListener;
3536
import android.view.ViewGroup;
3637
import android.view.WindowManager;
37-
import android.view.inputmethod.InputMethodManager;
3838
import android.widget.EditText;
3939
import android.widget.ImageView;
4040

@@ -105,8 +105,9 @@ public class ReportFragment extends Fragment {
105105
private ViewSwitcher photoviewSwitcher;
106106
private ViewSwitcher audioviewSwitcher;
107107
private AudioStatus mAudioStatus;
108+
private FloatingActionButton mSubmitBtn;
109+
private EditText mDescriptionView;
108110
private int mPlayTime = 0;
109-
110111
public static final int CAMERA_REQUEST = 101;
111112
public static final int LOCATION_REQUEST = 102;
112113
public static final int GALLERY_IMAGE_REQUEST = 103;
@@ -172,20 +173,38 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle st
172173
LinearLayoutCompat btnService = (LinearLayoutCompat) view.findViewById(R.id.serviceButton);
173174
LinearLayoutCompat btnLocation = (LinearLayoutCompat) view.findViewById(R.id.locationButton);
174175
playBtn = (ImageView) view.findViewById(R.id.audioView2);
175-
View descriptionView = view.findViewById(R.id.report_description_textbox);
176-
FloatingActionButton btnSubmit = (FloatingActionButton) view.findViewById(R.id.report_submit);
176+
mDescriptionView = (EditText) view.findViewById(R.id.report_description_textbox);
177+
mSubmitBtn = (FloatingActionButton) view.findViewById(R.id.report_submit);
177178
photoviewSwitcher = (ViewSwitcher) view.findViewById(R.id.report_photoviewswitcher);
178179
audioviewSwitcher = (ViewSwitcher) view.findViewById(R.id.report_audioviewswitcher);
179180
ImageView photoPlaceholder = (ImageView) view.findViewById((R.id.photoPlaceholder));
180181

181182
//Hide the keyboard unless the descriptionView is selected
182-
descriptionView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
183+
mDescriptionView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
183184
@Override
184185
public void onFocusChange(View v, boolean hasFocus) {
185186
if (!hasFocus) {
186187
v.clearFocus();
187188
hideKeyBoard(getActivity());
189+
} else {
190+
mDescriptionView.addTextChangedListener(new TextWatcher() {
191+
@Override
192+
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
193+
194+
}
195+
196+
@Override
197+
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
198+
199+
}
200+
201+
@Override
202+
public void afterTextChanged(Editable editable) {
203+
validate();
204+
}
205+
});
188206
}
207+
189208
}
190209
});
191210
photoPlaceholder.setOnClickListener(new OnClickListener() {
@@ -241,7 +260,7 @@ public void onClick(View v) {
241260
}
242261
});
243262

244-
btnSubmit.setOnClickListener(new OnClickListener() {
263+
mSubmitBtn.setOnClickListener(new OnClickListener() {
245264
@Override
246265
public void onClick(View v) {
247266
hideKeyBoard(getActivity());
@@ -259,6 +278,7 @@ private void updateService() {
259278
ImageView icon = (ImageView) getActivity().findViewById(R.id.serviceView);
260279
icon.setColorFilter(ContextCompat.getColor(getContext(), R.color.colorAccent), android.graphics.PorterDuff.Mode.MULTIPLY);
261280
}
281+
validate(); //required, so validate
262282
}
263283

264284
private void updateLocation() {
@@ -275,6 +295,7 @@ private void updateLocation() {
275295
} else {
276296
text.setText(R.string.report_hint_location);
277297
}
298+
validate(); //required, so validate
278299
} catch (Exception e) {
279300
e.printStackTrace();
280301
}
@@ -434,7 +455,6 @@ public void onCreate(Bundle savedInstanceState) {
434455
attributes = new LinkedList<Attribute>();
435456
attrInfoList = new LinkedList<AttributeInfo>();
436457
installationId = ((MainActivity) getActivity()).getInstallationId();
437-
438458
new RetrieveServicesTask().execute(); // Load services-list in the background
439459

440460
// Don't show the keyboard if it isn't already shown,
@@ -545,52 +565,30 @@ private boolean validate() {
545565

546566
boolean isValid = true;
547567

548-
EditText description = (EditText) getActivity().findViewById(R.id.report_description_textbox);
549-
550-
String descText = description.getText().toString();
551-
552-
// Filter description text. Replace everything that is not a word character,
553-
// whitespace or one of the symbols .?(),!:;@
554-
String pattern = "[^\\w\\s\\.\\?\\(\\),!:;@]";
555-
description.setText(
556-
Normalizer.normalize(descText, Normalizer.Form.NFD).replaceAll(pattern, "")
557-
);
558-
568+
if (mDescriptionView.getText().length() == 0) {
569+
isValid = false;
570+
}
559571
if (serviceCode == null) {
560572
isValid = false;
561-
resetLocation();
562573
}
563574

564575
if (latitude == null || longitude == null) {
565576
isValid = false;
566-
resetLocation();
567577
}
568578

569-
if (description.getText().length() == 0) {
570-
isValid = false;
571-
description.setHintTextColor(Color.RED);
572-
}
573579

574580
if (attrInfoList.size() != 0) {
575581
isValid = (attrInfoList.size() == attributes.size());
576582

577583
}
578584
if (!isValid) {
579-
String result = getString(R.string.failure_posting_service);
580-
Snackbar.make(getView(), result, Snackbar.LENGTH_SHORT)
581-
.show();
585+
mSubmitBtn.setVisibility(View.INVISIBLE);
582586
} else {
583-
showFab();
587+
mSubmitBtn.setVisibility(View.VISIBLE);
584588
}
585-
586589
return isValid;
587590
}
588591

589-
private void showFab() {
590-
View fab = getActivity().findViewById(R.id.report_submit);
591-
fab.setVisibility(View.VISIBLE);
592-
}
593-
594592
private Boolean checkAnonymous() {
595593
SharedPreferences settings = getActivity().getPreferences(Context.MODE_PRIVATE);
596594
String email = settings.getString("email", null);
@@ -859,15 +857,18 @@ private void handleCamera() {
859857
}
860858

861859
private void onSubmitButtonClicked() {
862-
Log.d(LOG_TAG, "Submit Button was clicked.");
863-
860+
Log.d(LOG_TAG, "onSubmitButtonClicked");
861+
final String pattern = "[^\\w\\s\\.\\?\\(\\),!:;@]";
864862
// Check the form result and post the service request
865863
if (!validate()) {
864+
String result = getString(R.string.failure_posting_service);
865+
Snackbar.make(getView(), result, Snackbar.LENGTH_SHORT)
866+
.show();
866867
return;
867868
}
868869

870+
869871
final TextView address = (TextView) getActivity().findViewById(R.id.location_text);
870-
final EditText description = (EditText) getActivity().findViewById(R.id.report_description_textbox);
871872

872873
final POSTServiceRequestDataWrapper data = new POSTServiceRequestDataWrapper(
873874
serviceCode,
@@ -884,9 +885,13 @@ private void onSubmitButtonClicked() {
884885
if (name != null) data.setName(name);
885886
if (email != null) data.setEmail(email);
886887
if (phone != null) data.setPhone(phone);
888+
887889
data.setDeviceId(installationId)
888890
.setAddress(address.getText().toString())
889-
.setDescription(description.getText().toString());
891+
.setDescription(
892+
Normalizer.normalize(
893+
mDescriptionView.getText().toString(), Normalizer.Form.NFD)
894+
.replaceAll(pattern, ""));
890895

891896
if (imageUri != null) {
892897
Glide.with(getActivity().getApplicationContext())
@@ -923,7 +928,9 @@ public void onClick(DialogInterface dialog, int which) {
923928
public void onClick(DialogInterface dialog, int which) {
924929
data.setDeviceId(installationId)
925930
.setAddress(address.getText().toString())
926-
.setDescription(description.getText().toString());
931+
.setDescription(Normalizer.normalize(
932+
mDescriptionView.getText().toString(), Normalizer.Form.NFD)
933+
.replaceAll(pattern, ""));
927934

928935
if (imageUri != null) {
929936
Glide.with(getActivity().getApplicationContext())

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
android:hint="@string/hint_name"
3030
android:inputType="textPersonName"
3131
android:maxLength="50"
32-
android:singleLine="true" />
32+
android:maxLines="1" />
3333
</android.support.design.widget.TextInputLayout>
3434

3535
<android.support.design.widget.TextInputLayout
@@ -69,9 +69,14 @@
6969
android:id="@+id/profile_submit"
7070
android:layout_width="wrap_content"
7171
android:layout_height="match_parent"
72+
android:layout_alignParentBottom="true"
73+
android:layout_alignParentEnd="true"
74+
android:layout_alignParentRight="true"
7275
android:layout_gravity="end|bottom"
7376
android:layout_marginBottom="10dp"
77+
android:layout_marginEnd="10dp"
7478
android:layout_marginRight="10dp"
7579
android:src="@drawable/ic_done"
80+
android:visibility="invisible"
7681
app:backgroundTint="@color/colorPrimary" />
7782
</android.support.design.widget.CoordinatorLayout>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,15 @@
299299
android:id="@+id/report_submit"
300300
android:layout_width="wrap_content"
301301
android:layout_height="match_parent"
302+
android:layout_alignParentBottom="true"
303+
android:layout_alignParentEnd="true"
304+
android:layout_alignParentRight="true"
302305
android:layout_gravity="end|bottom"
303306
android:layout_marginBottom="10dp"
304307
android:layout_marginEnd="10dp"
305308
android:layout_marginRight="10dp"
306309
android:src="@drawable/ic_done"
310+
android:visibility="visible"
307311
app:backgroundTint="@color/colorPrimary" />
312+
308313
</android.support.design.widget.CoordinatorLayout>

0 commit comments

Comments
 (0)