Skip to content

Instantly share code, notes, and snippets.

@josinSbazin
Created June 25, 2018 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josinSbazin/a653fafbf476d0a7aa4cf14723459e38 to your computer and use it in GitHub Desktop.
Save josinSbazin/a653fafbf476d0a7aa4cf14723459e38 to your computer and use it in GitHub Desktop.
class MessageManager @Inject constructor(private val context: Context) {
fun showMessage(@StringRes message: Int, duration: Duration = Duration.Short) {
Toast.makeText(context, message, toToastDuration(duration)).show()
}
fun showMessage(message: CharSequence, duration: Duration = Duration.Short) {
Toast.makeText(context, message, toToastDuration(duration)).show()
}
fun showMessageAtTop(@StringRes message: Int, duration: Duration = Duration.Short) {
showMessageAtTop(context.getText(message), duration)
}
fun showMessageAtTop(message: CharSequence, duration: Duration = Duration.Short) {
val toast = Toast.makeText(context, message, toToastDuration(duration))
val view = toast.view
val textView = view.findViewById(android.R.id.message) as TextView
val background = view.background
if (background is GradientDrawable) {
background.setColor(ContextCompat.getColor(context, R.color.toast_bg_color))
}
val tv = TypedValue()
var actionBarHeight = DEFAULT_ACTION_BAR_HEIGHT
if (context.theme?.resolveAttribute(android.R.attr.actionBarSize, tv, true) == true) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.resources.displayMetrics)
}
val margin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
DEFAULT_TOAST_TOP_MARGIN, context.resources.displayMetrics).toInt()
textView.setTextColor(ContextCompat.getColor(context, R.color.baseWhite))
toast.setGravity(Gravity.TOP, 0, actionBarHeight + margin)
toast.show()
}
fun showInteractiveMessage(view: View, message: CharSequence, duration: Duration = Duration.Short, actionMessage: CharSequence, action: (v: View) -> Unit) {
val snackbar = Snackbar.make(view, message, toSnackbarDuration(duration))
snackbar.setAction(actionMessage, action)
snackbar.show()
}
fun showSnackbar(view: View, message: CharSequence, duration: Duration = Duration.Short) {
Snackbar.make(view, message, toSnackbarDuration(duration)).show()
}
fun showNeedAuthDialog(activityContext: Context, navigationPresenter: CommonNavigation.Presenter) {
val alertDialog = AlertDialog.Builder(activityContext)
.setTitle(R.string.do_login)
.setMessage(R.string.is_not_auth_message)
.setPositiveButton(R.string.continue_act) { _, _ ->
navigationPresenter.navigateToLogin()
}
.setNegativeButton(R.string.cancel) {dialog, _ ->
dialog.cancel()
}
.create()
alertDialog.show()
}
private fun toToastDuration(duration: Duration): Int = when(duration) {
Duration.Long -> Toast.LENGTH_LONG
Duration.Short -> Toast.LENGTH_SHORT
}
private fun toSnackbarDuration(duration: Duration): Int = when(duration) {
Duration.Long -> Snackbar.LENGTH_LONG
Duration.Short -> Snackbar.LENGTH_SHORT
}
companion object {
private const val DEFAULT_ACTION_BAR_HEIGHT = 60
private const val DEFAULT_TOAST_TOP_MARGIN = 16f
}
enum class Duration {
Long, Short
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment