Skip to content

Commit

Permalink
#14: It's now possible to use a font size with a decimal number and n…
Browse files Browse the repository at this point in the history
…ot only an integer.
  • Loading branch information
Keidan committed May 17, 2021
1 parent c3ae4aa commit 082eac0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
20 changes: 10 additions & 10 deletions app/src/main/java/fr/ralala/hexviewer/ApplicationCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,21 @@ public void setHexRowHeight(int number) {

/**
* Returns the font size for the hex listview.
* @return int
* @return float
*/
public int getHexFontSize() {
public float getHexFontSize() {
try {
return Integer.parseInt(mSharedPreferences.getString(CFG_HEX_FONT_SIZE, mDefaultHexFontSize));
return Float.parseFloat(mSharedPreferences.getString(CFG_HEX_FONT_SIZE, mDefaultHexFontSize));
} catch(Exception ignore) {
return Integer.parseInt(mDefaultHexFontSize);
return Float.parseFloat(mDefaultHexFontSize);
}
}

/**
* Change the the font size for the hex listview.
* @param number The new number.
*/
public void setHexFontSize(int number) {
public void setHexFontSize(float number) {
SharedPreferences.Editor e = mSharedPreferences.edit();
e.putString(CFG_HEX_FONT_SIZE, String.valueOf(number));
e.apply();
Expand Down Expand Up @@ -206,21 +206,21 @@ public void setPlainRowHeight(int number) {

/**
* Returns the font size for the hex listview.
* @return int
* @return float
*/
public int getPlainFontSize() {
public float getPlainFontSize() {
try {
return Integer.parseInt(mSharedPreferences.getString(CFG_PLAIN_FONT_SIZE, mDefaultPlainFontSize));
return Float.parseFloat(mSharedPreferences.getString(CFG_PLAIN_FONT_SIZE, mDefaultPlainFontSize));
} catch(Exception ignore) {
return Integer.parseInt(mDefaultPlainFontSize);
return Float.parseFloat(mDefaultPlainFontSize);
}
}

/**
* Change the the font size for the hex listview.
* @param number The new number.
*/
public void setPlainFontSize(int number) {
public void setPlainFontSize(float number) {
SharedPreferences.Editor e = mSharedPreferences.edit();
e.putString(CFG_PLAIN_FONT_SIZE, String.valueOf(number));
e.apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void onCreate(final Bundle savedInstanceState) {

mAdapterHex = new SearchableListArrayAdapter(this, DisplayCharPolicy.DISPLAY_ALL, new ArrayList<>(), new UserConfig() {
@Override
public int getFontSize() {
public float getFontSize() {
return mApp.getHexFontSize();
}

Expand All @@ -112,7 +112,7 @@ public boolean isRowHeightAuto() {

mAdapterPlain = new SearchableListArrayAdapter(this, DisplayCharPolicy.IGNORE_NON_DISPLAYED_CHAR, new ArrayList<>(), new UserConfig() {
@Override
public int getFontSize() {
public float getFontSize() {
return mApp.getPlainFontSize();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum DisplayCharPolicy {
}

public interface UserConfig {
int getFontSize();
float getFontSize();
int getRowHeight();
boolean isRowHeightAuto();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
Expand Down Expand Up @@ -127,7 +129,7 @@ public boolean onPreferenceClick(Preference preference) {
mApp.getHexFontSize(),
MIN_HEX_FONT_SIZE,
MAX_HEX_FONT_SIZE,
mApp::setHexFontSize);
mApp::setHexFontSize, true);
} else if (preference.equals(mPlainRowHeightAuto)) {
mPlainRowHeight.setEnabled(!mPlainRowHeightAuto.isChecked());
} else if (preference.equals(mPlainRowHeight)) {
Expand All @@ -141,14 +143,14 @@ public boolean onPreferenceClick(Preference preference) {
mApp.getPlainFontSize(),
MIN_PLAIN_FONT_SIZE,
MAX_PLAIN_FONT_SIZE,
mApp::setPlainFontSize);
mApp::setPlainFontSize, true);
}
return false;
}

/* ----------------------------- */
private interface InputValidated {
void onValidated(int n);
private interface InputValidated<T> {
void onValidated(T n);
}

/**
Expand All @@ -161,7 +163,20 @@ private interface InputValidated {
* @param iv Callback which will be called if the entry is valid.
*/
@SuppressLint("InflateParams")
private void displayDialog(CharSequence title, int defaultValue, int minValue, int maxValue, InputValidated iv) {
private void displayDialog(CharSequence title, int defaultValue, int minValue, int maxValue, InputValidated<Integer> iv) {
displayDialog(title, defaultValue, minValue, maxValue, (v) -> iv.onValidated(v.intValue()), false);
}
/**
* Displays the input dialog box.
*
* @param title Dialog title.
* @param defaultValue Default value.
* @param minValue Min value.
* @param maxValue Max value.
* @param iv Callback which will be called if the entry is valid.
*/
@SuppressLint("InflateParams")
private void displayDialog(CharSequence title, float defaultValue, float minValue, float maxValue, InputValidated<Float> iv, boolean decimal) {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setCancelable(false)
.setTitle(title)
Expand All @@ -174,7 +189,20 @@ private void displayDialog(CharSequence title, int defaultValue, int minValue, i
dialog.show();
EditText et = dialog.findViewById(R.id.editText);
if (et != null) {
et.setText(String.valueOf(defaultValue));
int inputType = InputType.TYPE_CLASS_NUMBER;
String def;
int maxLen;
if(decimal) {
maxLen = 5;
inputType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
def = String.valueOf(defaultValue);
} else {
maxLen = 3;
def = String.valueOf((int)defaultValue);
}
et.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLen)});
et.setInputType(inputType);
et.setText(def);
et.requestFocus();
Editable text = et.getText();
if (text.length() > 0) {
Expand All @@ -185,7 +213,7 @@ private void displayDialog(CharSequence title, int defaultValue, int minValue, i
final InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener((v) -> {
if (et != null && validInput(et, defaultValue, minValue, maxValue, iv)) {
if (et != null && validInput(et, defaultValue, minValue, maxValue, iv, decimal)) {
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
dialog.dismiss();
}
Expand All @@ -197,6 +225,7 @@ private void displayDialog(CharSequence title, int defaultValue, int minValue, i
});
}


/**
* Validation of the input.
*
Expand All @@ -207,32 +236,34 @@ private void displayDialog(CharSequence title, int defaultValue, int minValue, i
* @param iv Callback
* @return False on error.
*/
private boolean validInput(EditText et, int defaultValue, int minValue, int maxValue, InputValidated iv) {
private boolean validInput(EditText et, float defaultValue, float minValue, float maxValue, InputValidated<Float> iv, boolean decimal) {
try {
Editable s = et.getText();
int nb = Integer.parseInt(s.toString());
float nb = Float.parseFloat(s.toString());
if (s.length() == 0) {
et.setText(String.valueOf(minValue));
et.setText(String.valueOf(!decimal ? (int)minValue : minValue));
et.selectAll();
return false;
} else {
if (nb < minValue) {
UIHelper.shakeError(et, mActivity.getString(R.string.error_less_than) + ": " + minValue);
et.setText(String.valueOf(minValue));
et.setText(String.valueOf(!decimal ? (int)minValue : minValue));
et.selectAll();
return false;
} else if (nb > maxValue) {
UIHelper.shakeError(et, mActivity.getString(R.string.error_greater_than) + ": " + maxValue);
et.setText(String.valueOf(maxValue));
et.setText(String.valueOf(!decimal ? (int)maxValue : maxValue));
et.selectAll();
return false;
} else
et.setError(null);
iv.onValidated(nb);
return true;
}
} catch (Exception ignored) {
iv.onValidated(defaultValue);
} catch (Exception ex) {
UIHelper.shakeError(et, ex.getMessage());
et.setText(String.valueOf(!decimal ? (int)defaultValue : defaultValue));
et.selectAll();
return false;
}
}
Expand Down

0 comments on commit 082eac0

Please sign in to comment.