helper Version: add valid() function (#670)
Merge branch 'master' into VersionValid format code introduce Version.valid() Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/670 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org>
This commit is contained in:
parent
23d3ba7f42
commit
d677c90b01
@ -42,7 +42,8 @@ public class AddNewAccountActivity extends BaseActivity {
|
|||||||
private enum Protocol {HTTPS, HTTP}
|
private enum Protocol {HTTPS, HTTP}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getLayoutResourceId(){
|
protected int getLayoutResourceId() {
|
||||||
|
|
||||||
return R.layout.activity_add_new_account;
|
return R.layout.activity_add_new_account;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,12 +63,13 @@ public class AddNewAccountActivity extends BaseActivity {
|
|||||||
initCloseListener();
|
initCloseListener();
|
||||||
viewBinding.close.setOnClickListener(onClickListener);
|
viewBinding.close.setOnClickListener(onClickListener);
|
||||||
|
|
||||||
ArrayAdapter<AddNewAccountActivity.Protocol> adapterProtocols = new ArrayAdapter<>(AddNewAccountActivity.this, R.layout.spinner_item, AddNewAccountActivity.Protocol.values());
|
ArrayAdapter<AddNewAccountActivity.Protocol> adapterProtocols = new ArrayAdapter<>(AddNewAccountActivity.this, R.layout.spinner_item,
|
||||||
|
AddNewAccountActivity.Protocol.values());
|
||||||
adapterProtocols.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
adapterProtocols.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
||||||
|
|
||||||
viewBinding.protocolSpinner.setAdapter(adapterProtocols);
|
viewBinding.protocolSpinner.setAdapter(adapterProtocols);
|
||||||
|
|
||||||
viewBinding.addNewAccount.setOnClickListener(login -> {
|
viewBinding.addNewAccount.setOnClickListener(login -> {
|
||||||
|
|
||||||
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
||||||
|
|
||||||
@ -80,7 +82,7 @@ public class AddNewAccountActivity extends BaseActivity {
|
|||||||
|
|
||||||
processLogin();
|
processLogin();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,21 +139,16 @@ public class AddNewAccountActivity extends BaseActivity {
|
|||||||
if(responseVersion.code() == 200) {
|
if(responseVersion.code() == 200) {
|
||||||
|
|
||||||
GiteaVersion version = responseVersion.body();
|
GiteaVersion version = responseVersion.body();
|
||||||
Version giteaVersion;
|
|
||||||
|
|
||||||
assert version != null;
|
assert version != null;
|
||||||
|
if(!Version.valid(version.getVersion())) {
|
||||||
tinyDB.putString("giteaVersion", version.getVersion());
|
|
||||||
|
|
||||||
try {
|
|
||||||
giteaVersion = new Version(version.getVersion());
|
|
||||||
}
|
|
||||||
catch(Exception e) {
|
|
||||||
|
|
||||||
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
|
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tinyDB.putString("giteaVersion", version.getVersion());
|
||||||
|
Version giteaVersion = new Version(version.getVersion());
|
||||||
|
|
||||||
if(giteaVersion.less(getString(R.string.versionLow))) {
|
if(giteaVersion.less(getString(R.string.versionLow))) {
|
||||||
|
|
||||||
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader))
|
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader))
|
||||||
@ -161,7 +158,6 @@ public class AddNewAccountActivity extends BaseActivity {
|
|||||||
alertDialogBuilder.setNegativeButton(getString(R.string.cancelButton), (dialog, which) -> {
|
alertDialogBuilder.setNegativeButton(getString(R.string.cancelButton), (dialog, which) -> {
|
||||||
|
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
//enableProcessButton();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
alertDialogBuilder.setPositiveButton(getString(R.string.textContinue), (dialog, which) -> {
|
alertDialogBuilder.setPositiveButton(getString(R.string.textContinue), (dialog, which) -> {
|
||||||
@ -241,12 +237,12 @@ public class AddNewAccountActivity extends BaseActivity {
|
|||||||
|
|
||||||
case 401:
|
case 401:
|
||||||
|
|
||||||
Toasty.error(ctx,getResources().getString(R.string.unauthorizedApiError));
|
Toasty.error(ctx, getResources().getString(R.string.unauthorizedApiError));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
Toasty.error(ctx,getResources().getString(R.string.genericApiStatusError) + response.code());
|
Toasty.error(ctx, getResources().getString(R.string.genericApiStatusError) + response.code());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -255,7 +251,7 @@ public class AddNewAccountActivity extends BaseActivity {
|
|||||||
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
|
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
|
||||||
|
|
||||||
Log.e("onFailure", t.toString());
|
Log.e("onFailure", t.toString());
|
||||||
Toasty.error(ctx,getResources().getString(R.string.genericError));
|
Toasty.error(ctx, getResources().getString(R.string.genericError));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -276,22 +276,17 @@ public class LoginActivity extends BaseActivity {
|
|||||||
if(responseVersion.code() == 200) {
|
if(responseVersion.code() == 200) {
|
||||||
|
|
||||||
GiteaVersion version = responseVersion.body();
|
GiteaVersion version = responseVersion.body();
|
||||||
Version gitea_version;
|
|
||||||
|
|
||||||
assert version != null;
|
assert version != null;
|
||||||
|
if(!Version.valid(version.getVersion())) {
|
||||||
tinyDB.putString("giteaVersion", version.getVersion());
|
|
||||||
|
|
||||||
try {
|
|
||||||
gitea_version = new Version(version.getVersion());
|
|
||||||
}
|
|
||||||
catch(Exception e) {
|
|
||||||
|
|
||||||
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
|
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
|
||||||
enableProcessButton();
|
enableProcessButton();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tinyDB.putString("giteaVersion", version.getVersion());
|
||||||
|
Version gitea_version = new Version(version.getVersion());
|
||||||
|
|
||||||
if(gitea_version.less(getString(R.string.versionLow))) {
|
if(gitea_version.less(getString(R.string.versionLow))) {
|
||||||
|
|
||||||
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader))
|
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader))
|
||||||
@ -572,7 +567,8 @@ public class LoginActivity extends BaseActivity {
|
|||||||
|
|
||||||
if(checkAccount == 0) {
|
if(checkAccount == 0) {
|
||||||
|
|
||||||
accountId = userAccountsApi.insertNewAccount(accountName, instanceUrl, userDetails.getUsername(), newToken.getSha1(), "");
|
accountId = userAccountsApi
|
||||||
|
.insertNewAccount(accountName, instanceUrl, userDetails.getUsername(), newToken.getSha1(), "");
|
||||||
tinyDB.putInt("currentActiveAccountId", (int) accountId);
|
tinyDB.putInt("currentActiveAccountId", (int) accountId);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -24,6 +24,21 @@ public class Version {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* valid return true if string is a valid version
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean valid(String value) {
|
||||||
|
|
||||||
|
if(value == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final Pattern pattern_valid = Pattern.compile("^[v,V]?(\\d+)+(\\.(\\d+))*([_,\\-,+][\\w,\\d,_,\\-,+]*)?$");
|
||||||
|
return pattern_valid.matcher(value).find();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* init parse and store values for other functions of an Version instance
|
* init parse and store values for other functions of an Version instance
|
||||||
* it use the raw variable as base
|
* it use the raw variable as base
|
||||||
@ -32,10 +47,9 @@ public class Version {
|
|||||||
*/
|
*/
|
||||||
private void init() {
|
private void init() {
|
||||||
|
|
||||||
final Pattern pattern_valid = Pattern.compile("^[v,V]?(\\d+)+(\\.(\\d+))*([_,\\-,+][\\w,\\d,_,\\-,+]*)?$");
|
|
||||||
final Pattern pattern_number_dot_number = Pattern.compile("^\\d+(\\.(\\d)+)*");
|
final Pattern pattern_number_dot_number = Pattern.compile("^\\d+(\\.(\\d)+)*");
|
||||||
|
|
||||||
if(!pattern_valid.matcher(raw).find()) {
|
if(!valid(raw)) {
|
||||||
throw new IllegalArgumentException("Invalid version format");
|
throw new IllegalArgumentException("Invalid version format");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,4 +119,25 @@ public class VersionTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void valid() {
|
||||||
|
|
||||||
|
assertTrue(Version.valid("1.12"));
|
||||||
|
assertTrue(Version.valid("1.12.0+dev-211-g316db0fe7"));
|
||||||
|
assertTrue(Version.valid("v2"));
|
||||||
|
assertTrue(Version.valid("1.12.1-wowowow"));
|
||||||
|
assertTrue(Version.valid("0.2.4"));
|
||||||
|
assertTrue(Version.valid("1"));
|
||||||
|
assertTrue(Version.valid("1.12.0"));
|
||||||
|
|
||||||
|
assertFalse(Version.valid("fdsa21.22.-"));
|
||||||
|
assertFalse(Version.valid("weo2.2.2"));
|
||||||
|
assertFalse(Version.valid(""));
|
||||||
|
assertFalse(Version.valid(" "));
|
||||||
|
assertFalse(Version.valid("\t"));
|
||||||
|
assertFalse(Version.valid("abc"));
|
||||||
|
assertFalse(Version.valid("version1"));
|
||||||
|
assertFalse(Version.valid(null));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user