|
| 1 | +package smith.lib.alerts.toast; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.res.Configuration; |
| 6 | +import android.graphics.Color; |
| 7 | +import android.graphics.drawable.GradientDrawable; |
| 8 | +import android.os.CountDownTimer; |
| 9 | +import android.transition.AutoTransition; |
| 10 | +import android.transition.TransitionManager; |
| 11 | +import android.view.animation.AlphaAnimation; |
| 12 | +import android.view.View; |
| 13 | +import android.animation.ObjectAnimator; |
| 14 | +import android.view.ViewGroup; |
| 15 | +import android.widget.ImageView; |
| 16 | +import android.widget.LinearLayout; |
| 17 | +import android.widget.TextView; |
| 18 | +import android.widget.Toast; |
| 19 | +import java.util.Timer; |
| 20 | + |
| 21 | +public class SToast { |
| 22 | + |
| 23 | + private static final String DARK = "#212121"; |
| 24 | + private static final String LIGHT = "#FFFFFF"; |
| 25 | + |
| 26 | + public static final int MODE_OK = 0; |
| 27 | + public static final int MODE_WARN = 1; |
| 28 | + public static final int MODE_DONE = 2; |
| 29 | + public static final int MODE_ERROR = 3; |
| 30 | + public static final int MODE_INFO = 4; |
| 31 | + public static final int MODE_HEART = 5; |
| 32 | + public static final int MODE_CONFUSE = 6; |
| 33 | + private static int type = MODE_INFO; |
| 34 | + |
| 35 | + private static Toast toast; |
| 36 | + private static Context context; |
| 37 | + private static String titl, text; |
| 38 | + |
| 39 | + private static int color = 0xFFFF5252; |
| 40 | + private static int icon = R.drawable.heart_img; |
| 41 | + |
| 42 | + public static final int LONG = Toast.LENGTH_LONG; |
| 43 | + public static final int SHORT = Toast.LENGTH_SHORT; |
| 44 | + private static int length = Toast.LENGTH_SHORT; |
| 45 | + |
| 46 | + public static class Adaptive { |
| 47 | + public Adaptive(Context ctx, int dur) { |
| 48 | + context = ctx; |
| 49 | + length = dur; |
| 50 | + } |
| 51 | + |
| 52 | + public Adaptive setTitle(String ttl) { |
| 53 | + titl = ttl; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public Adaptive setText(String txt) { |
| 58 | + text = txt; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public Adaptive setIconAndColor(int ic, int clr) { |
| 63 | + icon = ic; |
| 64 | + color = clr; |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public SToast show() { |
| 69 | + toast = new Toast(context); |
| 70 | + |
| 71 | + View inflate = ((Activity) context).getLayoutInflater().inflate(R.layout.stoast_adaptive, null); |
| 72 | + LinearLayout main = (LinearLayout) inflate.findViewById(R.id.main); |
| 73 | + LinearLayout view = (LinearLayout) inflate.findViewById(R.id.main2); |
| 74 | + ImageView img = (ImageView) inflate.findViewById(R.id.img); |
| 75 | + TextView title = (TextView) inflate.findViewById(R.id.title); |
| 76 | + TextView desc = (TextView) inflate.findViewById(R.id.desc); |
| 77 | + |
| 78 | + ui(main, color, 30); |
| 79 | + if (nightModeON()) { |
| 80 | + ui(view, Color.parseColor(DARK), 30); |
| 81 | + title.setTextColor(Color.parseColor(LIGHT)); |
| 82 | + desc.setTextColor(Color.parseColor(LIGHT)); |
| 83 | + } else { |
| 84 | + ui(view, Color.parseColor(LIGHT), 30); |
| 85 | + title.setTextColor(color); |
| 86 | + desc.setTextColor(Color.parseColor(DARK)); |
| 87 | + } |
| 88 | + |
| 89 | + animate(img, 230); |
| 90 | + |
| 91 | + main.setClipToOutline(true); |
| 92 | + title.setText(titl); |
| 93 | + desc.setText(text); |
| 94 | + img.setImageResource(icon); |
| 95 | + |
| 96 | + toast.setDuration(length); |
| 97 | + toast.setView(inflate); |
| 98 | + toast.show(); |
| 99 | + |
| 100 | + return new SToast(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public static class Mode { |
| 105 | + |
| 106 | + public Mode(Context ctx, int dur) { |
| 107 | + context = ctx; |
| 108 | + length = dur; |
| 109 | + } |
| 110 | + |
| 111 | + public Mode setTitle(String ttl) { |
| 112 | + titl = ttl; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public Mode setText(String txt) { |
| 117 | + text = txt; |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + public Mode setMode(int mode) { |
| 122 | + type = mode; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public SToast show() { |
| 127 | + toast = new Toast(context); |
| 128 | + |
| 129 | + View inflate = ((Activity)context).getLayoutInflater().inflate(R.layout.stoast_modes, null); |
| 130 | + LinearLayout main = (LinearLayout) inflate.findViewById(R.id.main); |
| 131 | + ImageView img = (ImageView) inflate.findViewById(R.id.img); |
| 132 | + TextView title = (TextView) inflate.findViewById(R.id.title); |
| 133 | + TextView desc = (TextView) inflate.findViewById(R.id.desc); |
| 134 | + |
| 135 | + switch (type) { |
| 136 | + case MODE_OK: |
| 137 | + change(main, img, "#1877F2"); |
| 138 | + img.setImageResource(R.drawable.ok_img); |
| 139 | + break; |
| 140 | + case MODE_WARN: |
| 141 | + change(main, img, "#FF6801"); |
| 142 | + img.setImageResource(R.drawable.warn_img); |
| 143 | + break; |
| 144 | + case MODE_DONE: |
| 145 | + change(main, img, "#43A047"); |
| 146 | + img.setImageResource(R.drawable.true_img); |
| 147 | + break; |
| 148 | + case MODE_ERROR: |
| 149 | + change(main, img, "#FF5252"); |
| 150 | + img.setImageResource(R.drawable.false_img); |
| 151 | + break; |
| 152 | + case MODE_INFO: |
| 153 | + change(main, img, "#0D47A1"); |
| 154 | + img.setImageResource(R.drawable.default_img); |
| 155 | + break; |
| 156 | + case MODE_HEART: |
| 157 | + change(main, img, "#FF5252"); |
| 158 | + img.setImageResource(R.drawable.heart_img); |
| 159 | + break; |
| 160 | + case MODE_CONFUSE: |
| 161 | + change(main, img, "#FF6801"); |
| 162 | + img.setImageResource(R.drawable.confuse_img); |
| 163 | + break; |
| 164 | + } |
| 165 | + |
| 166 | + main.setClipToOutline(true); |
| 167 | + ui(img, Color.parseColor("#ffffff"), 100); |
| 168 | + title.setText(titl); |
| 169 | + desc.setText(text); |
| 170 | + |
| 171 | + animate(img, 230); |
| 172 | + |
| 173 | + toast.setView(inflate); |
| 174 | + toast.setDuration(length); |
| 175 | + toast.show(); |
| 176 | + |
| 177 | + return new SToast(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private static void change(View v, ImageView im, String col) { |
| 182 | + ui(v, Color.parseColor(col), 30); |
| 183 | + im.setColorFilter(Color.parseColor(col)); |
| 184 | + } |
| 185 | + |
| 186 | + private static void animate(View v, int dur) { |
| 187 | + new CountDownTimer(dur, 1) { |
| 188 | + @Override public void onTick(long arg0) {} |
| 189 | + @Override public void onFinish() { |
| 190 | + ObjectAnimator animator = new ObjectAnimator(); |
| 191 | + animator.setDuration(1000); |
| 192 | + animator.setFloatValues(0.0f, 1.0f); |
| 193 | + animator.setPropertyName("alpha"); |
| 194 | + animator.setTarget(v); |
| 195 | + animator.start(); |
| 196 | + } |
| 197 | + }.start(); |
| 198 | + } |
| 199 | + |
| 200 | + private static boolean nightModeON() { |
| 201 | + int flags = |
| 202 | + context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; |
| 203 | + boolean isNightModeOn = flags == Configuration.UI_MODE_NIGHT_YES; |
| 204 | + if (isNightModeOn) return true; |
| 205 | + else return false; |
| 206 | + } |
| 207 | + |
| 208 | + private static void ui(View view, int str, int i) { |
| 209 | + GradientDrawable gradientDrawable = new GradientDrawable(); |
| 210 | + gradientDrawable.setColor(str); |
| 211 | + gradientDrawable.setCornerRadius((float) i); |
| 212 | + view.setBackground(gradientDrawable); |
| 213 | + } |
| 214 | + |
| 215 | + /* |
| 216 | + * |
| 217 | + * |
| 218 | + * THIS LIBRARY CREATED BY HUSSEIN SHAKIR (SMITH) |
| 219 | + * |
| 220 | + * TELEGRAM : @SMITHDEV |
| 221 | + * YOUTUBE : HUSSEIN SMITH |
| 222 | + * |
| 223 | + * YOU GUYS ARE NOT ALLOWED TO MODIFY THIS LIBRARY, |
| 224 | + * WITHOT ANY PERMISSION FROM ME PERSONALLY.. |
| 225 | + * ALL RIGHTS RESERVED © HUSSEIN SHAKIR, 2022. |
| 226 | + * |
| 227 | + * |
| 228 | + */ |
| 229 | +} |
0 commit comments