Created
April 17, 2019 07:30
-
-
Save Mojtaba-Shafaei/2d63f313b1057b6f5f122fbddb3108be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.util; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.graphics.Typeface; | |
import android.graphics.drawable.Drawable; | |
import android.view.Gravity; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import androidx.annotation.ColorRes; | |
import androidx.annotation.StringRes; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.swiperefreshlayout.widget.CircularProgressDrawable; | |
import app.application.ArmangarApplication; | |
import app.util.constant.Colors; | |
import com.armangar.app.armangar.R; | |
import com.google.android.material.snackbar.Snackbar; | |
import com.mojtaba_shafaei.android.androidBottomDialog.BottomDialog; | |
import com.mojtaba_shafaei.persianFont.PersianFont; | |
import es.dmoral.toasty.Toasty; | |
import java.text.Collator; | |
import java.util.Calendar; | |
import org.apache.commons.lang3.ObjectUtils; | |
import org.apache.commons.lang3.StringUtils; | |
import timber.log.Timber; | |
public class AndroidUtil { | |
public static Typeface IRANSANS_NORMAL; | |
public static Typeface IRANSANS_MEDIUM; | |
public static int _2dip, _4dip, _6dip, _8dip, _12dip, _16dip, _24dip, _28dip, _32dip, _36dip, _56dip, SPACE_BETWEEN_DASHBOARD_SECTIONS; | |
public static int ENDORSERS_OVERLAP_MARGIN_SIZE; | |
public static int ENDORSERS_IMAGE_SIZE; | |
public static Collator persianCollator; | |
private AndroidUtil(Context context) { | |
IRANSANS_NORMAL = PersianFont.IRANSANS_NORMAL(context); | |
IRANSANS_MEDIUM = PersianFont.IRANSANS_MEDIUM(context); | |
_2dip = Utility.dpToPx(2); | |
_4dip = Utility.dpToPx(4); | |
_6dip = Utility.dpToPx(6); | |
_8dip = Utility.dpToPx(8); | |
_12dip = Utility.dpToPx(12); | |
_16dip = Utility.dpToPx(16); | |
_24dip = Utility.dpToPx(24); | |
_28dip = Utility.dpToPx(28); | |
_32dip = Utility.dpToPx(32); | |
_36dip = Utility.dpToPx(36); | |
_56dip = Utility.dpToPx(56); | |
SPACE_BETWEEN_DASHBOARD_SECTIONS = _32dip; | |
persianCollator = Collator.getInstance(LocaleUtil.fa()); | |
ENDORSERS_OVERLAP_MARGIN_SIZE = -Utility.dpToPx(8); | |
ENDORSERS_IMAGE_SIZE = Utility.dpToPx(32); | |
} | |
public static void init(Context context) { | |
new AndroidUtil(context); | |
} | |
public static void showDeleteDialog(AppCompatActivity activity, String content, | |
String negativeButtonText, Runnable runnable) { | |
try { | |
BottomDialog.builder() | |
.withDirection(BottomDialog.RTL) | |
.withIcon(R.drawable.ic_delete_forever_red_a100_24dp) | |
.withTitle(R.string.delete_title) | |
.withCancelable(true) | |
.withOnNegative(bottomDialog -> { | |
bottomDialog.dismiss(); | |
runnable.run(); | |
}) | |
.withPositiveText(R.string.cancel) | |
.withNegativeText(ObjectUtils.defaultIfNull(negativeButtonText, | |
activity.getString(R.string.yes_delete_it)) | |
, R.color.text_colors_red_flat_button_light) | |
.withDefaultTypeface(IRANSANS_NORMAL) | |
.withContent(String.format(activity.getString(R.string.do_you_delete_1p) | |
, content) | |
) | |
.build() | |
.show(activity); | |
} catch (Exception e) { | |
Timber.e(e); | |
Toast.makeText(activity.getApplicationContext(), R.string.retry_again, Toast.LENGTH_SHORT) | |
.show(); | |
} | |
} | |
public static void showDeleteDialog(AppCompatActivity activity | |
, String content | |
, Runnable action) { | |
showDeleteDialog(activity, content, null, action); | |
} | |
public static void showDeleteDialog(AppCompatActivity activity, @StringRes int content, | |
Runnable runnable) { | |
showDeleteDialog(activity, activity.getString(content), null, runnable); | |
} | |
public static void showInternetErrorDialog(Activity activity | |
, boolean cancellable | |
, @StringRes Integer btnPositiveText | |
, Runnable btnPositiveAction | |
, Runnable btnNegativeAction) { | |
showInternetErrorDialog(activity | |
, cancellable | |
, btnPositiveText == null ? activity.getString(R.string.i_got_it) : activity.getString(btnPositiveText) | |
, btnPositiveAction | |
, btnNegativeAction); | |
} | |
public static void showInternetErrorDialog(Activity activity | |
, boolean cancellable | |
, CharSequence btnPositiveText | |
, Runnable btnPositiveAction | |
, Runnable btnNegativeAction) { | |
try { | |
if (activity.getFragmentManager().findFragmentByTag("BottomDialog") != null) { | |
return; | |
} | |
new BottomDialog.Builder() | |
.withDirection(BottomDialog.RTL) | |
.withIcon(R.drawable.ic_cloud_off_red_a100_24dp) | |
.withTitle(R.string.error) | |
.withCancelable(cancellable) | |
.withHiddenNegativeButton(cancellable || btnNegativeAction == null) | |
.withNegativeText(R.string.cancel) | |
.withPositiveText(StringUtils.isBlank(btnPositiveText) ? activity.getString(R.string.i_got_it) : btnPositiveText) | |
.withOnNegative(bottomDialog -> { | |
bottomDialog.dismiss(); | |
if (btnNegativeAction != null) { | |
btnNegativeAction.run(); | |
} | |
}) | |
.withDefaultTypeface(IRANSANS_NORMAL) | |
.withContent(activity.getString(R.string.networkDisconnectException)) | |
.withOnPositive(bottomDialog -> { | |
bottomDialog.dismiss(); | |
if (btnPositiveAction != null) { | |
btnPositiveAction.run(); | |
} | |
}) | |
.withDialogInterface(new DialogInterface() { | |
@Override | |
public void cancel() { | |
} | |
@Override | |
public void dismiss() { | |
/* if (btnNegativeAction != null) { | |
btnNegativeAction.run(); | |
}*/ | |
} | |
}) | |
.build() | |
.show((AppCompatActivity) activity); | |
} catch (Exception e) { | |
Timber.e(e); | |
Toast.makeText(activity.getApplicationContext(), R.string.no_internet_connection, | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public static void showInternetErrorDialog(AppCompatActivity activity | |
, boolean cancellable | |
, Runnable btnPositiveAction) { | |
showInternetErrorDialog(activity, cancellable, "", btnPositiveAction, null); | |
} | |
public static void showInternetErrorDialog(Activity activity) { | |
showInternetErrorDialog(activity, false, "", null, null); | |
} | |
public static void showInternetErrorDialog(AppCompatActivity activity, boolean cancellable) { | |
showInternetErrorDialog(activity, cancellable, "", null, null); | |
} | |
public static void showInternetErrorDialog(AppCompatActivity activity, Runnable btnPositiveAction) { | |
showInternetErrorDialog(activity, false, "", btnPositiveAction, null); | |
} | |
public static void showErrorDialog(Activity activity | |
, boolean cancellable | |
, String error | |
, String positiveButtonText | |
, Runnable positiveAction | |
, String negativeButtonText | |
, Runnable negativeAction) { | |
try { | |
new BottomDialog.Builder() | |
.withDirection(BottomDialog.RTL) | |
.withIcon(R.drawable.ic_error_outline_red_a100_24dp) | |
.withTitle(R.string.error) | |
.withCancelable(cancellable) | |
.withHiddenNegativeButton(cancellable || negativeAction == null) | |
.withPositiveText( | |
org.apache.commons.lang3.StringUtils | |
.defaultIfBlank(positiveButtonText, activity.getString(R.string.i_got_it))) | |
.withDefaultTypeface(IRANSANS_NORMAL) | |
.withContent(error) | |
.withOnPositive(bottomDialog -> { | |
bottomDialog.dismiss(); | |
if (positiveAction != null) { | |
positiveAction.run(); | |
} | |
}) | |
.withNegativeText( | |
org.apache.commons.lang3.StringUtils | |
.defaultIfBlank(negativeButtonText, activity.getString(R.string.cancel))) | |
.withOnNegative(b -> { | |
if (negativeAction != null) { | |
negativeAction.run(); | |
} | |
}) | |
.build() | |
.show((AppCompatActivity) activity); | |
} catch (Exception e) { | |
Timber.e(e); | |
Toast.makeText(activity.getApplicationContext(), error, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public static void showErrorDialog(AppCompatActivity activity | |
, boolean cancellable | |
, String error | |
, String positiveButtonText | |
, Runnable positiveAction) { | |
showErrorDialog(activity | |
, cancellable | |
, error | |
, positiveButtonText | |
, positiveAction, null, null); | |
} | |
public static void showErrorDialog(Activity activity | |
, boolean cancellable | |
, String error | |
, Runnable runnable) { | |
showErrorDialog(activity, cancellable, error, null, runnable, null, null); | |
} | |
public static void showErrorDialog(Activity activity, boolean cancellable, String error) { | |
showErrorDialog(activity, cancellable, error, null); | |
} | |
public static void showErrorDialog(Activity activity, String error) { | |
showErrorDialog(activity, false, error, null); | |
} | |
public static void showErrorDialog(Activity activity, String error, | |
Runnable runnable) { | |
showErrorDialog(activity, false, error, runnable); | |
} | |
//////////////////////////// MESSAGE ///////////////////////////////////// | |
public static void showMessageDialog(Activity activity | |
, boolean cancellable | |
, CharSequence message | |
, CharSequence positiveText | |
, Runnable positiveAction | |
, @ColorRes Integer textColorPrimaryBtn | |
, CharSequence negativeButtonText | |
, Runnable negativeAction | |
, @ColorRes Integer textColorSecondaryBtn) { | |
try { | |
new BottomDialog.Builder() | |
.withDirection(BottomDialog.RTL) | |
.withHiddenIcon(true) | |
.withCancelable(cancellable) | |
.withHiddenHeader(true) | |
// .withHiddenNegativeButton(cancellable || negativeAction == null) | |
.withPositiveText(StringUtils.defaultIfBlank(positiveText | |
, activity.getString(R.string.i_got_it)) | |
, org.apache.commons.lang3.ObjectUtils.defaultIfNull(textColorPrimaryBtn, R.color.Color_White)) | |
.withNegativeText(StringUtils.defaultIfBlank(negativeButtonText, activity.getString(R.string.cancel)), | |
ObjectUtils.defaultIfNull(textColorSecondaryBtn, R.color.primaryTextDark)) | |
.withDefaultTypeface(IRANSANS_NORMAL) | |
.withContent(message) | |
.withOnPositive(bottomDialog -> { | |
bottomDialog.dismiss(); | |
if (positiveAction != null) { | |
positiveAction.run(); | |
} | |
}) | |
.withOnNegative(b -> { | |
if (negativeAction != null) { | |
negativeAction.run(); | |
} | |
}) | |
.build() | |
.show((AppCompatActivity) activity); | |
} catch (Exception e) { | |
Timber.e(e); | |
Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public static void showMessageDialog(Activity activity, String message) { | |
showMessageDialog(activity, false, message | |
, null, null | |
, null, null, null, null); | |
} | |
public static void showMessageDialog(AppCompatActivity activity, boolean cancellable, CharSequence message | |
, String positiveText, Runnable positiveAction) { | |
showMessageDialog(activity, cancellable, message, positiveText, positiveAction | |
, null, null, null, null); | |
} | |
public static void showMessageDialog(Activity activity | |
, CharSequence message | |
, Runnable positiveAction) { | |
showMessageDialog(activity, false, message, null, positiveAction | |
, null, null, null, null); | |
} | |
public static void showMessageDialog(Activity activity | |
, boolean cancellable | |
, CharSequence message | |
, Runnable positiveAction) { | |
showMessageDialog(activity, cancellable, message, null, positiveAction | |
, null, null, null, null); | |
} | |
public static void showMessageDialog(AppCompatActivity activity | |
, boolean cancellable | |
, String message | |
, String positiveText | |
, Runnable positiveAction | |
, String negativeText | |
, Runnable negativeAction) { | |
showMessageDialog(activity, cancellable, message, positiveText, positiveAction | |
, null, negativeText, negativeAction, null); | |
} | |
public static Toast showToast(@StringRes int stringRes) { | |
return showToast(ArmangarApplication.getInstance().getString(stringRes)); | |
} | |
public static Toast showToast(String string) { | |
// Toast.makeText(ArmangarApplication.getInstance(), string, Toast.LENGTH_LONG).render(); | |
final Toast lvToast = Toasty.normal(ArmangarApplication.getInstance(), string); | |
lvToast.show(); | |
return lvToast; | |
} | |
public static CircularProgressDrawable createProgressDrawable(Context context) { | |
CircularProgressDrawable placeHolder = new CircularProgressDrawable(context); | |
placeHolder.setColorSchemeColors(Colors.schemeColors); | |
placeHolder.setStyle(CircularProgressDrawable.DEFAULT); | |
placeHolder.start(); | |
return placeHolder; | |
} | |
public static void showBottomSnackBar(android.view.View root, @StringRes int msg, Runnable runnable) { | |
try { | |
final Snackbar lvSnackbar = Snackbar.make(root, msg, Snackbar.LENGTH_INDEFINITE) | |
.setActionTextColor(Colors.accentColor); | |
if (runnable != null) { | |
lvSnackbar.setAction(R.string.display_it, (ignored) -> { | |
runnable.run(); | |
if (lvSnackbar.isShown()) { | |
lvSnackbar.dismiss(); | |
} | |
}); | |
} | |
lvSnackbar.show(); | |
} catch (Exception e) { | |
Timber.e(e); | |
AndroidUtil.showToast(msg); | |
} | |
} | |
/** | |
* @param view, extends TextView | |
* @param gravity, {@code int} one of the {LEFT, TOP, RIGHT, BOTTOM} constants from {@link Gravity} | |
* @param drawable, {@link Drawable} | |
*/ | |
public static void setDrawable(TextView view, int gravity, Drawable drawable) { | |
final Drawable[] lvDrawables = view.getCompoundDrawables(); | |
switch (gravity) { | |
case Gravity.LEFT: | |
lvDrawables[0] = drawable; | |
break; | |
case Gravity.TOP: | |
lvDrawables[1] = drawable; | |
break; | |
case Gravity.RIGHT: | |
lvDrawables[2] = drawable; | |
break; | |
case Gravity.BOTTOM: | |
lvDrawables[3] = drawable; | |
break; | |
} | |
view.setCompoundDrawablesWithIntrinsicBounds(lvDrawables[0], lvDrawables[1], lvDrawables[2], lvDrawables[3]); | |
} | |
public static long getDate() { | |
Calendar c = Calendar.getInstance(); | |
c.set(2019, 5, 01); | |
return c.getTimeInMillis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment