Improve dialog extension

This commit is contained in:
Rahul Kumar Patel
2021-03-01 22:14:21 +05:30
parent 90113ab338
commit 6a1e0ec875
2 changed files with 31 additions and 38 deletions

View File

@@ -27,40 +27,39 @@ import androidx.fragment.app.Fragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
fun Context.showDialog(@StringRes titleId: Int, @StringRes messageId: Int) {
runOnUiThread {
val backgroundColor: Int = getStyledAttributeColor(android.R.attr.colorBackground)
val builder = MaterialAlertDialogBuilder(this).apply {
setTitle(titleId)
setMessage(messageId)
setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _ -> dialog.dismiss() }
background = ColorDrawable(backgroundColor)
}.create()
builder.show()
}
showDialog(getString(titleId), getString(messageId), null, null)
}
fun Context.showDialog(title: String?, message: String?) {
runOnUiThread {
val backgroundColor: Int = getStyledAttributeColor(android.R.attr.colorBackground)
val builder = MaterialAlertDialogBuilder(this).apply {
setTitle(title)
setMessage(message)
setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _ -> dialog.dismiss() }
background = ColorDrawable(backgroundColor)
}.create()
builder.show()
}
showDialog(title, message, null, null)
}
fun Context.showDialog(title: String?, message: String?, listener: DialogInterface.OnDismissListener) {
fun Context.showDialog(
title: String?,
message: String?,
positiveListener: DialogInterface.OnClickListener?,
negativeListener: DialogInterface.OnClickListener?
) {
runOnUiThread {
val backgroundColor: Int = getStyledAttributeColor(android.R.attr.colorBackground)
val builder = MaterialAlertDialogBuilder(this).apply {
setTitle(title)
setMessage(message)
setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _ -> listener }
if (positiveListener != null) {
setPositiveButton(android.R.string.ok, positiveListener)
} else {
setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() }
}
positiveListener?.let {
}
negativeListener?.let {
setNegativeButton(android.R.string.cancel, negativeListener)
}
background = ColorDrawable(backgroundColor)
}.create()

View File

@@ -21,7 +21,6 @@ package com.aurora.store
import android.Manifest
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.os.Environment
@@ -63,7 +62,6 @@ import com.aurora.store.view.ui.search.SearchSuggestionActivity
import com.aurora.store.view.ui.spoof.SpoofActivity
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.livinglifetechway.quickpermissions_kotlin.runWithPermissions
import nl.komponents.kovenant.task
import nl.komponents.kovenant.ui.successUi
@@ -288,21 +286,17 @@ class MainActivity : BaseActivity() {
getString(R.string.dialog_desc_self_update)
)
val builder: MaterialAlertDialogBuilder = MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.dialog_title_self_update))
.setMessage(messages.joinToString(separator = "\n"))
.setPositiveButton(getString(R.string.action_update)) { _, _ ->
showDialog(
getString(R.string.dialog_title_self_update),
messages.joinToString(separator = "\n"),
{ _, _ ->
val intent = Intent(this, SelfUpdateService::class.java)
intent.putExtra(Constants.STRING_EXTRA, gson.toJson(selfUpdate))
startService(intent)
}
.setNegativeButton(getString(R.string.action_later)) { dialog, _ ->
},
{ dialog, _ ->
dialog.dismiss()
}
val backGroundColor: Int = getStyledAttributeColor(android.R.attr.colorBackground)
builder.background = ColorDrawable(backGroundColor)
builder.create()
builder.show()
},
)
}
}