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

View File

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

View File

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