Introduce snackbar for toast messages (#352)

Merge branch 'master' into 302-snackbar

Added snackbar to login screen

Co-authored-by: 6543 <6543@noreply.gitea.io>
Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/352
This commit is contained in:
M M Arif 2020-04-04 19:19:44 +00:00
parent 36ebfff529
commit c402046699
5 changed files with 104 additions and 49 deletions

View File

@ -15,6 +15,7 @@ import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
@ -23,7 +24,7 @@ import com.tooltip.Tooltip;
import org.mian.gitnex.R;
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.helpers.NetworkObserver;
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.SnackBar;
import org.mian.gitnex.helpers.VersionCheck;
import org.mian.gitnex.models.GiteaVersion;
import org.mian.gitnex.models.UserInfo;
@ -53,6 +54,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
private RadioGroup loginMethod;
final Context ctx = this;
private String device_id = "token";
private ScrollView layoutView;
@Override
protected int getLayoutResourceId(){
@ -78,6 +80,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
protocolSpinner = findViewById(R.id.httpsSpinner);
loginMethod = findViewById(R.id.loginMethod);
loginTokenCode = findViewById(R.id.loginTokenCode);
layoutView = findViewById(R.id.loginForm);
viewTextAppVersion.setText(AppUtil.getAppVersion(getApplicationContext()));
@ -96,7 +99,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
String value = getResources().getStringArray(R.array.protocolValues)[pos];
if(value.toLowerCase().equals("http")) {
Toasty.info(getApplicationContext(), getResources().getString(R.string.protocolError));
SnackBar.warning(getApplicationContext(), layoutView,getResources().getString(R.string.protocolError));
}
}
@ -135,10 +138,11 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
if(isAvailable) {
enableProcessButton();
SnackBar.success(getApplicationContext(), layoutView, getResources().getString(R.string.netConnectionIsBack));
}
else {
disableProcessButton();
Toasty.info(getApplicationContext(), getResources().getString(R.string.checkNetConnection));
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.checkNetConnection));
}
});
@ -189,20 +193,16 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
};
private View.OnClickListener infoListener = new View.OnClickListener() {
public void onClick(View v) {
new Tooltip.Builder(v)
.setText(R.string.urlInfoTooltip)
.setTextColor(getResources().getColor(R.color.white))
.setBackgroundColor(getResources().getColor(R.color.tooltipBackground))
.setCancelable(true)
.setDismissOnClick(true)
.setPadding(30)
.setCornerRadius(R.dimen.tooltipCornor)
.setGravity(Gravity.BOTTOM)
.show();
}
};
private View.OnClickListener infoListener = v -> new Tooltip.Builder(v)
.setText(R.string.urlInfoTooltip)
.setTextColor(getResources().getColor(R.color.white))
.setBackgroundColor(getResources().getColor(R.color.tooltipBackground))
.setCancelable(true)
.setDismissOnClick(true)
.setPadding(30)
.setCornerRadius(R.dimen.tooltipCornor)
.setGravity(Gravity.BOTTOM)
.show();
@SuppressLint("ResourceAsColor")
private void login() {
@ -269,7 +269,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
if(instanceUrlET.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL));
SnackBar.warning(getApplicationContext(), layoutView, getResources().getString(R.string.emptyFieldURL));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
@ -277,7 +277,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
if(loginUid.equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldUsername));
SnackBar.warning(getApplicationContext(), layoutView, getResources().getString(R.string.emptyFieldUsername));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
@ -285,7 +285,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
if(loginPassword.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldPassword));
SnackBar.warning(getApplicationContext(), layoutView, getResources().getString(R.string.emptyFieldPassword));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
@ -301,7 +301,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else {
Toasty.info(getApplicationContext(), getString(R.string.loginOTPTypeError));
SnackBar.warning(getApplicationContext(), layoutView, getResources().getString(R.string.loginOTPTypeError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
@ -315,7 +315,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else {
Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection));
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.checkNetConnection));
}
@ -358,7 +358,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
if (instanceUrlET.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL));
SnackBar.warning(getApplicationContext(), layoutView, getResources().getString(R.string.emptyFieldURL));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
@ -366,7 +366,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
if (loginToken_.equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.loginTokenError));
SnackBar.warning(getApplicationContext(), layoutView, getResources().getString(R.string.loginTokenError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
@ -377,7 +377,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else {
Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection));
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.checkNetConnection));
}
@ -406,7 +406,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
switch (vt) {
case UNSUPPORTED_NEW:
//Toasty.info(getApplicationContext(), getString(R.string.versionUnsupportedNew));
//SnackBar.warning(getApplicationContext(), layoutView, getResources().getString(R.string.versionUnsupportedNew));
case SUPPORTED_LATEST:
case SUPPORTED_OLD:
case DEVELOPMENT:
@ -439,7 +439,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
alertDialog.show();
return;
default: // UNKNOWN
Toasty.info(getApplicationContext(), getString(R.string.versionUnknow));
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.versionUnknow));
enableProcessButton();
}
@ -510,18 +510,14 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else if(response.code() == 401) {
String toastError = getResources().getString(R.string.unauthorizedApiError);
Toasty.info(getApplicationContext(), toastError);
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.unauthorizedApiError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.genericApiStatusError) + response.code());
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -533,7 +529,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError));
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.genericError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -676,18 +672,14 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else if(response.code() == 401) {
String toastError = getResources().getString(R.string.unauthorizedApiError);
Toasty.info(getApplicationContext(), toastError);
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.unauthorizedApiError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.genericApiStatusError) + response.code());
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -699,7 +691,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError));
SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.genericError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -713,8 +705,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else if(responseCreate.code() == 500) {
String toastError = getResources().getString(R.string.genericApiStatusError) + responseCreate.code();
Toasty.info(getApplicationContext(), toastError);
SnackBar.error(getApplicationContext(), layoutView,getResources().getString(R.string.genericApiStatusError) + responseCreate.code());
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -744,16 +735,14 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else if(response.code() == 500) {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
SnackBar.error(getApplicationContext(), layoutView,getResources().getString(R.string.genericApiStatusError) + response.code());
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
SnackBar.error(getApplicationContext(), layoutView,getResources().getString(R.string.genericApiStatusError) + response.code());
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -765,7 +754,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
public void onFailure(@NonNull Call<List<UserTokens>> call, @NonNull Throwable t) {
Log.e("onFailure-login", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson));
SnackBar.error(getApplicationContext(), layoutView,getResources().getString(R.string.malformedJson));
enableProcessButton();
loginButton.setText(R.string.btnLogin);

View File

@ -0,0 +1,63 @@
package org.mian.gitnex.helpers;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar;
import org.mian.gitnex.R;
/**
* Author M M Arif
*/
public class SnackBar {
public static void info(Context context, View createRepository, String message) {
Snackbar snackBar = Snackbar.make(createRepository, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.lightBlue));
snackBar.show();
}
public static void success(Context context, View createRepository, String message) {
Snackbar snackBar = Snackbar.make(createRepository, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.white));
snackBar.show();
}
public static void warning(Context context, View createRepository, String message) {
Snackbar snackBar = Snackbar.make(createRepository, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.lightYellow));
snackBar.show();
}
public static void error(Context context, View createRepository, String message) {
Snackbar snackBar = Snackbar.make(createRepository, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.darkRed));
snackBar.show();
}
}

View File

@ -5,7 +5,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:background="?attr/primaryBackgroundColor"
android:orientation="vertical"
android:gravity="center">
android:gravity="center"
android:id="@+id/loginForm">
<LinearLayout
android:layout_width="match_parent"

View File

@ -29,6 +29,7 @@
<color name="diffRemovedColor">#ffe0e0</color>
<color name="diffAddedColor">#d6fcd6</color>
<color name="dividerColorDark">#1d1d1d</color>
<color name="lightYellow">#efd34a</color>
<color name="lightThemeTextColor">#646565</color>
<color name="lightThemeBackground">#f9f9f9</color>

View File

@ -100,6 +100,7 @@
<string name="emptyFieldPassword">Password is required</string>
<string name="checkNetConnection">Cannot access network, please check your Internet connection</string>
<string name="netConnectionIsBack">Yay, Internet connection is back</string>
<string name="repoNameErrorEmpty">Repository name is empty</string>
<string name="repoNameErrorInvalid">Repository name is not valid. [a&#8211;z A&#8211;Z 0&#8211;9 &#8211; _]</string>