Skip to content

Commit 635047d

Browse files
vanshikaaroramaskaravivek
authored andcommitted
* Fixed issue commons-app#2291 * resolved codacy issue * settings * removed unwanted commemts
1 parent 73bb0c5 commit 635047d

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

app/src/main/java/fr/free/nrw/commons/settings/SettingsFragment.java

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import android.Manifest;
44
import android.app.AlertDialog;
5+
import android.content.SharedPreferences;
56
import android.net.Uri;
67
import android.os.Bundle;
78
import android.preference.EditTextPreference;
89
import android.preference.Preference;
910
import android.preference.PreferenceFragment;
1011
import android.preference.SwitchPreference;
12+
import android.text.Editable;
13+
import android.text.TextWatcher;
1114

1215
import com.karumi.dexter.Dexter;
1316
import com.karumi.dexter.listener.PermissionGrantedResponse;
@@ -24,12 +27,15 @@
2427
import fr.free.nrw.commons.utils.PermissionUtils;
2528
import fr.free.nrw.commons.utils.ViewUtil;
2629

30+
import static android.R.*;
31+
2732
public class SettingsFragment extends PreferenceFragment {
2833

2934
@Inject
3035
@Named("default_preferences")
3136
BasicKvStore defaultKvStore;
32-
@Inject CommonsLogSender commonsLogSender;
37+
@Inject
38+
CommonsLogSender commonsLogSender;
3339

3440
@Override
3541
public void onCreate(Bundle savedInstanceState) {
@@ -61,32 +67,48 @@ public void onCreate(Bundle savedInstanceState) {
6167
int uploads = defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 100);
6268
uploadLimit.setText(uploads + "");
6369
uploadLimit.setSummary(uploads + "");
64-
uploadLimit.setOnPreferenceChangeListener((preference, newValue) -> {
65-
int value;
66-
try {
67-
value = Integer.parseInt(newValue.toString());
68-
} catch(Exception e) {
69-
value = 100; //Default number
70+
uploadLimit.getEditText().addTextChangedListener(new TextWatcher() {
71+
@Override
72+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
73+
7074
}
71-
if (value > 500) {
72-
new AlertDialog.Builder(getActivity())
73-
.setTitle(R.string.maximum_limit)
74-
.setMessage(R.string.maximum_limit_alert)
75-
.setPositiveButton(android.R.string.yes, (dialog, which) -> {})
76-
.setIcon(android.R.drawable.ic_dialog_alert)
77-
.show();
78-
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, 500);
79-
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
80-
uploadLimit.setSummary(500 + "");
81-
uploadLimit.setText(500 + "");
82-
} else {
83-
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, value);
84-
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
85-
uploadLimit.setSummary(String.valueOf(value));
75+
76+
@Override
77+
public void onTextChanged(CharSequence s, int start, int before, int count) {
8678
}
87-
return true;
88-
});
8979

80+
@Override
81+
public void afterTextChanged(Editable s) {
82+
int value;
83+
if (s.length()>0)
84+
try {
85+
value = Integer.parseInt(s.toString());
86+
if (value > 500) {
87+
uploadLimit.getEditText().setError(getString((R.string.maximum_limit_alert)));
88+
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, 500);
89+
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
90+
uploadLimit.setSummary(500 + "");
91+
uploadLimit.setText(500 + "");
92+
} else if (value == 0) {
93+
uploadLimit.getEditText().setError(getString(R.string.cannot_be_zero));
94+
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, 100);
95+
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
96+
uploadLimit.setSummary(100 + "");
97+
uploadLimit.setText(100 + "");
98+
} else {
99+
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, value);
100+
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
101+
uploadLimit.setSummary(String.valueOf(value));
102+
}
103+
} catch (Exception e) {
104+
uploadLimit.getEditText().setError(getString(R.string.enter_valid));
105+
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, 100);
106+
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
107+
uploadLimit.setSummary(100 + "");
108+
uploadLimit.setText(100 + "");
109+
}
110+
}
111+
});
90112
Preference betaTesterPreference = findPreference("becomeBetaTester");
91113
betaTesterPreference.setOnPreferenceClickListener(preference -> {
92114
Utils.handleWebUrl(getActivity(), Uri.parse(getResources().getString(R.string.beta_opt_in_link)));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@
193193
<string name="_2fa_code">2FA Code</string>
194194
<string name="number_of_uploads">My Recent Upload Limit</string>
195195
<string name="maximum_limit">Maximum Limit</string>
196+
<string name="invalid_zero">Zero is not valid</string>
197+
<string name="invalid_input">Invalid Input</string>
196198
<string name="maximum_limit_alert">Unable to display more than 500</string>
199+
<string name="enter_valid">Enter a valid number</string>
200+
<string name="cannot_be_zero">Upload limit cannot be 0</string>
197201
<string name="set_limit">Set Recent Upload Limit</string>
198202
<string name="login_failed_2fa_not_supported">Two factor authentication is currently not supported.</string>
199203
<string name="logout_verification">Do you really want to logout?</string>

0 commit comments

Comments
 (0)