|
| 1 | +package kv.vension.fastframe.utils |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Typeface |
| 5 | +import android.os.Build |
| 6 | +import android.view.Gravity |
| 7 | +import android.view.LayoutInflater |
| 8 | +import android.widget.ImageView |
| 9 | +import android.widget.LinearLayout |
| 10 | +import android.widget.TextView |
| 11 | +import android.widget.Toast |
| 12 | +import androidx.cardview.widget.CardView |
| 13 | +import androidx.core.content.ContextCompat |
| 14 | +import kv.vension.fastframe.R |
| 15 | +import kv.vension.fastframe.VFrame |
| 16 | + |
| 17 | +/** |
| 18 | + * ======================================================================== |
| 19 | + * 作 者:Vension |
| 20 | + * 主 页:Github: https://github.com/Vension |
| 21 | + * 日 期:2019/9/3 10:58 |
| 22 | + * ______ _____ _______ /\/|_____ _____ __ __ ______ |
| 23 | + * | ____/\ / ____|__ __|/\/ ____| __ \ /\ | \/ | ____| |
| 24 | + * | |__ / \ | (___ | | | |__ | |__) | / \ | \ / | |__ |
| 25 | + * | __/ /\ \ \___ \ | | | __| | _ / / /\ \ | |\/| | __| |
| 26 | + * | | / ____ \ ____) | | | | | | | \ \ / ____ \| | | | |____ |
| 27 | + * |_|/_/ \_\_____/ |_| |_| |_| \_\/_/ \_\_| |_|______| |
| 28 | + * |
| 29 | + * Take advantage of youth and toss about ! |
| 30 | + * ------------------------------------------------------------------------ |
| 31 | + * 描述:自定义吐司弹窗 |
| 32 | + * ======================================================================== |
| 33 | + */ |
| 34 | +class ToastHelper private constructor(){ |
| 35 | + |
| 36 | + private var icon = R.drawable.ic_warning_white_24dp |
| 37 | + private var message = "No text!" |
| 38 | + private var cardBackgroundColor = android.R.color.black |
| 39 | + private var cardElevation = 4f |
| 40 | + private var cardCornerRadius = 8f |
| 41 | + private var textSize = 16f |
| 42 | + private var typeface = Typeface.DEFAULT |
| 43 | + private var gravity = -91 |
| 44 | + private var xOffset: Int = 0 |
| 45 | + private var yOffset: Int = 0 |
| 46 | + |
| 47 | + fun setIcon(icon: Int): ToastHelper { |
| 48 | + this.icon = icon |
| 49 | + return this |
| 50 | + } |
| 51 | + |
| 52 | + fun setMessage(message: String): ToastHelper { |
| 53 | + this.message = message |
| 54 | + return this |
| 55 | + } |
| 56 | + |
| 57 | + fun setCardBackgroundColor(cardBackgroundColor: Int): ToastHelper { |
| 58 | + this.cardBackgroundColor = cardBackgroundColor |
| 59 | + return this |
| 60 | + } |
| 61 | + |
| 62 | + fun setCardElevation(elevation: Float): ToastHelper { |
| 63 | + this.cardElevation = elevation |
| 64 | + return this |
| 65 | + } |
| 66 | + |
| 67 | + fun setCardRadius(radius: Float): ToastHelper { |
| 68 | + this.cardCornerRadius = radius |
| 69 | + return this |
| 70 | + } |
| 71 | + |
| 72 | + fun setTextSize(textSize: Float): ToastHelper { |
| 73 | + this.textSize = textSize |
| 74 | + return this |
| 75 | + } |
| 76 | + |
| 77 | + fun setTypeFace(typeface: Typeface): ToastHelper { |
| 78 | + this.typeface = typeface |
| 79 | + return this |
| 80 | + } |
| 81 | + |
| 82 | + fun setGravity(gravity: Int, xOffset: Int, yOffset: Int): ToastHelper { |
| 83 | + this.gravity = gravity |
| 84 | + this.xOffset = xOffset |
| 85 | + this.yOffset = yOffset |
| 86 | + return this |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates the toast with initialized variables. And returns the custom toast. |
| 91 | + * @param duration Duration of the toast message. |
| 92 | + * @param isVertical |
| 93 | + * @return Toast object. |
| 94 | + */ |
| 95 | + fun createToast(duration: Int, isVertical: Boolean): Toast { |
| 96 | + val inflater = VFrame.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater |
| 97 | + |
| 98 | + val view = inflater.inflate(R.layout.layout_toast_default, null) |
| 99 | + val cardView = view.findViewById<CardView>(R.id.cv_toast) |
| 100 | + val layout = view.findViewById<LinearLayout>(R.id.ll_toast) |
| 101 | + val toastIcon = view.findViewById<ImageView>(R.id.iv_toastIcon) |
| 102 | + val toastMessage = view.findViewById<TextView>(R.id.tv_toastMsg) |
| 103 | + |
| 104 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 105 | + cardView.setCardBackgroundColor(VFrame.getColor(cardBackgroundColor)) |
| 106 | + } else { |
| 107 | + cardView.setBackgroundColor(VFrame.getColor(cardBackgroundColor)) |
| 108 | + } |
| 109 | + |
| 110 | + cardView.cardElevation = cardElevation |
| 111 | + cardView.radius = cardCornerRadius |
| 112 | + |
| 113 | + if (isVertical) {//竖向 |
| 114 | + cardView.setContentPadding(30, 40, 30, 40) |
| 115 | + layout.orientation = LinearLayout.VERTICAL |
| 116 | + val params = toastMessage.layoutParams as LinearLayout.LayoutParams |
| 117 | + params.setMargins(0, 10, 0, 0) |
| 118 | + toastMessage.layoutParams = params |
| 119 | + } |
| 120 | + toastIcon.setImageDrawable(ContextCompat.getDrawable(VFrame.mContext, icon)) |
| 121 | + toastMessage.textSize = textSize |
| 122 | + toastMessage.text = message |
| 123 | + toastMessage.typeface = typeface |
| 124 | + |
| 125 | + val toast = Toast(VFrame.mContext) |
| 126 | + toast.view = view |
| 127 | + toast.duration = duration |
| 128 | + if (gravity != -91) toast.setGravity(gravity, xOffset, yOffset) |
| 129 | + return toast |
| 130 | + } |
| 131 | + |
| 132 | + companion object { |
| 133 | + |
| 134 | + fun normal(message: String): Toast { |
| 135 | + return normal(message,false) |
| 136 | + } |
| 137 | + fun normal(gravity: Int,message: String): Toast { |
| 138 | + return normal(Gravity.CENTER,message,false) |
| 139 | + } |
| 140 | + fun normal(message: String,isVertical: Boolean): Toast { |
| 141 | + return normal(Gravity.CENTER,message,isVertical) |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Pre-Set Centered Info toast |
| 146 | + * @param gravity |
| 147 | + * @param icon |
| 148 | + * @param message Message that will be on the toast. |
| 149 | + * @param bgColor |
| 150 | + * @param duration Duration of the toast message. |
| 151 | + * @param isVertical |
| 152 | + * @return Toast. |
| 153 | + */ |
| 154 | + fun normal(gravity: Int, message: String, isVertical: Boolean): Toast { |
| 155 | + return ToastHelper() |
| 156 | + .setGravity(gravity,0, 0) |
| 157 | + .setMessage(message) |
| 158 | + .createToast(Toast.LENGTH_LONG, isVertical) |
| 159 | + } |
| 160 | + |
| 161 | + fun info(message: String): Toast { |
| 162 | + return info(message,false) |
| 163 | + } |
| 164 | + fun info(gravity: Int,message: String): Toast { |
| 165 | + return info(gravity,message,Toast.LENGTH_LONG,false) |
| 166 | + } |
| 167 | + fun info(message: String,isVertical: Boolean): Toast { |
| 168 | + return info(Gravity.CENTER,message,Toast.LENGTH_LONG,isVertical) |
| 169 | + } |
| 170 | + fun info(gravity: Int, message: String, duration: Int,isVertical: Boolean): Toast { |
| 171 | + return info(gravity,R.drawable.ic_info_black_24dp,message,R.color.color_deep_purple,duration,isVertical) |
| 172 | + } |
| 173 | + /** |
| 174 | + * Pre-Set Centered Info toast |
| 175 | + * @param gravity |
| 176 | + * @param icon |
| 177 | + * @param message Message that will be on the toast. |
| 178 | + * @param bgColor |
| 179 | + * @param duration Duration of the toast message. |
| 180 | + * @param isVertical |
| 181 | + * @return Toast. |
| 182 | + */ |
| 183 | + fun info(gravity: Int,icon:Int, message: String,bgColor:Int, duration: Int,isVertical: Boolean): Toast { |
| 184 | + return ToastHelper() |
| 185 | + .setGravity(gravity,0, 0) |
| 186 | + .setIcon(icon) |
| 187 | + .setMessage(message) |
| 188 | + .setCardBackgroundColor(bgColor) |
| 189 | + .createToast(duration, isVertical) |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | + fun warning(message: String): Toast { |
| 194 | + return warning(message,false) |
| 195 | + } |
| 196 | + fun warning(gravity: Int,message: String): Toast { |
| 197 | + return warning(gravity,message,Toast.LENGTH_LONG,false) |
| 198 | + } |
| 199 | + fun warning(message: String,isVertical: Boolean): Toast { |
| 200 | + return warning(Gravity.CENTER,message,Toast.LENGTH_LONG,isVertical) |
| 201 | + } |
| 202 | + fun warning(gravity: Int, message: String, duration: Int,isVertical: Boolean): Toast { |
| 203 | + return warning(gravity,R.drawable.ic_warning_white_24dp,message,R.color.color_orange,duration,isVertical) |
| 204 | + } |
| 205 | + /** |
| 206 | + * Pre-Set Centered warning toast |
| 207 | + * @param gravity |
| 208 | + * @param icon |
| 209 | + * @param message Message that will be on the toast. |
| 210 | + * @param bgColor |
| 211 | + * @param duration Duration of the toast message. |
| 212 | + * @param isVertical |
| 213 | + * @return Toast. |
| 214 | + */ |
| 215 | + fun warning(gravity: Int,icon:Int, message: String,bgColor:Int, duration: Int,isVertical: Boolean): Toast { |
| 216 | + return ToastHelper() |
| 217 | + .setGravity(gravity,0, 0) |
| 218 | + .setIcon(icon) |
| 219 | + .setMessage(message) |
| 220 | + .setCardBackgroundColor(bgColor) |
| 221 | + .createToast(duration, isVertical) |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | + fun error(message: String): Toast { |
| 226 | + return error(message,false) |
| 227 | + } |
| 228 | + fun error(gravity: Int,message: String): Toast { |
| 229 | + return error(gravity,message,Toast.LENGTH_LONG,false) |
| 230 | + } |
| 231 | + fun error(message: String,isVertical: Boolean): Toast { |
| 232 | + return error(Gravity.CENTER,message,Toast.LENGTH_LONG,isVertical) |
| 233 | + } |
| 234 | + fun error(gravity: Int, message: String, duration: Int,isVertical: Boolean): Toast { |
| 235 | + return error(gravity,R.drawable.ic_error_black_24dp,message,R.color.color_red,duration,isVertical) |
| 236 | + } |
| 237 | + /** |
| 238 | + * Pre-Set Centered warning toast |
| 239 | + * @param gravity |
| 240 | + * @param icon |
| 241 | + * @param message Message that will be on the toast. |
| 242 | + * @param bgColor |
| 243 | + * @param duration Duration of the toast message. |
| 244 | + * @param isVertical |
| 245 | + * @return Toast. |
| 246 | + */ |
| 247 | + fun error(gravity: Int,icon:Int, message: String,bgColor:Int, duration: Int,isVertical: Boolean): Toast { |
| 248 | + return ToastHelper() |
| 249 | + .setGravity(gravity,0, 0) |
| 250 | + .setIcon(icon) |
| 251 | + .setMessage(message) |
| 252 | + .setCardBackgroundColor(bgColor) |
| 253 | + .createToast(duration, isVertical) |
| 254 | + } |
| 255 | + |
| 256 | + |
| 257 | + fun success(message: String): Toast { |
| 258 | + return success(message,false) |
| 259 | + } |
| 260 | + fun success(gravity: Int,message: String): Toast { |
| 261 | + return success(gravity,message,Toast.LENGTH_LONG,false) |
| 262 | + } |
| 263 | + fun success(message: String,isVertical: Boolean): Toast { |
| 264 | + return success(Gravity.CENTER,message,Toast.LENGTH_LONG,isVertical) |
| 265 | + } |
| 266 | + fun success(gravity: Int, message: String, duration: Int,isVertical: Boolean): Toast { |
| 267 | + return success(gravity,R.drawable.ic_success_circle_black_24dp,message,R.color.color_green,duration,isVertical) |
| 268 | + } |
| 269 | + /** |
| 270 | + * Pre-Set Centered error toast |
| 271 | + * @param gravity |
| 272 | + * @param icon |
| 273 | + * @param message Message that will be on the toast. |
| 274 | + * @param bgColor |
| 275 | + * @param duration Duration of the toast message. |
| 276 | + * @param isVertical |
| 277 | + * @return Toast. |
| 278 | + */ |
| 279 | + fun success(gravity: Int,icon:Int, message: String,bgColor:Int, duration: Int,isVertical: Boolean): Toast { |
| 280 | + return ToastHelper() |
| 281 | + .setGravity(gravity,0, 0) |
| 282 | + .setIcon(icon) |
| 283 | + .setMessage(message) |
| 284 | + .setCardBackgroundColor(bgColor) |
| 285 | + .createToast(duration, isVertical) |
| 286 | + } |
| 287 | + |
| 288 | + |
| 289 | + |
| 290 | + /** |
| 291 | + * Customizable toast |
| 292 | + * @param context Context |
| 293 | + * @param message Message that will be on the toast. |
| 294 | + * @param icon Icon that will be on the left side of the toast message. |
| 295 | + * @param cardBackgroundColor Background color of toast. |
| 296 | + * @param duration Duration of the toast message. |
| 297 | + * @return Toast. |
| 298 | + */ |
| 299 | + fun custom(gravity: Int, message: String, icon: Int, cardBackgroundColor: Int, duration: Int): Toast { |
| 300 | + return ToastHelper() |
| 301 | + .setGravity(gravity,0,0) |
| 302 | + .setIcon(icon) |
| 303 | + .setMessage(message) |
| 304 | + .setCardBackgroundColor(cardBackgroundColor) |
| 305 | + .createToast(duration, false) |
| 306 | + } |
| 307 | + |
| 308 | + |
| 309 | + /** |
| 310 | + * Fully customizable toast |
| 311 | + * @param context Context |
| 312 | + * @param message Message that will be on the toast. |
| 313 | + * @param icon Icon that will be on the left side of the toast message. |
| 314 | + * @param cardBackgroundColor Background color of toast. |
| 315 | + * @param cornerRadius Corner radius of the toast. |
| 316 | + * @param elevation Elevation of the toast. |
| 317 | + * @param textSize Text size of the toast message. |
| 318 | + * @param typeface Typeface of the toast message. |
| 319 | + * @param gravity Gravity of the toast. |
| 320 | + * @param xOffset X axis offset for toast gravity. |
| 321 | + * @param yOffset Y axis offset for toast gravity. |
| 322 | + * @param duration Duration of the toast message. |
| 323 | + * @return Toast. |
| 324 | + */ |
| 325 | + fun customAll(message: String, icon: Int, cardBackgroundColor: Int, |
| 326 | + cornerRadius: Float, elevation: Float, |
| 327 | + textSize: Float, typeface: Typeface, gravity: Int, |
| 328 | + xOffset: Int, yOffset: Int, duration: Int): Toast { |
| 329 | + val toast = ToastHelper() |
| 330 | + .setIcon(icon) |
| 331 | + .setMessage(message) |
| 332 | + .setCardBackgroundColor(cardBackgroundColor) |
| 333 | + .setCardElevation(elevation) |
| 334 | + .setCardRadius(cornerRadius) |
| 335 | + .setTextSize(textSize) |
| 336 | + .setTypeFace(typeface) |
| 337 | + .createToast(duration, false) |
| 338 | + if (gravity != -1) toast.setGravity(gravity, xOffset, yOffset) |
| 339 | + return toast |
| 340 | + } |
| 341 | + |
| 342 | + /** |
| 343 | + * Simple way to create ToastieActivity. You can use it to fully customize toast. |
| 344 | + * @return ToastieActivity object |
| 345 | + */ |
| 346 | + fun makeCustom(): ToastHelper { |
| 347 | + return ToastHelper() |
| 348 | + } |
| 349 | + |
| 350 | + } |
| 351 | + |
| 352 | +} |
0 commit comments