Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit dc69c82

Browse files
author
Eduard Ablekimov
committed
UpdateDialog formatting
1 parent 82a6150 commit dc69c82

1 file changed

Lines changed: 51 additions & 31 deletions

File tree

status-resolver/src/main/java/com/ackee/versioupdatehandler/UpdateDialog.kt

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.ackee.versioupdatehandler.model.DialogSettings
1212
/**
1313
* Dialog indicating that user should update application.
1414
*
15-
*
1615
* Texts and package name are customizable via [DialogSettings] class
1716
*/
1817
class UpdateDialog : DialogFragment() {
@@ -31,30 +30,42 @@ class UpdateDialog : DialogFragment() {
3130
}
3231

3332
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
34-
val builder = AlertDialog.Builder(activity!!)
35-
val settings: DialogSettings =
36-
arguments!!.getParcelable(DIALOG_SETTINGS_KEY) ?: throw IllegalArgumentException("Dialog settings cannot be null")
37-
val isForceUpdate = arguments!!.getBoolean(FORCE_UPDATE_KEY, false)
33+
val builder = AlertDialog.Builder(requireActivity())
34+
val settings = requireArguments().getParcelable<DialogSettings>(DIALOG_SETTINGS_KEY)
35+
?: throw IllegalArgumentException("Dialog settings cannot be null")
36+
37+
val isForceUpdate = requireArguments().getBoolean(FORCE_UPDATE_KEY, false)
3838
if (isForceUpdate) {
3939
isCancelable = false
4040
builder.setCancelable(false)
4141
}
42-
var title: String? = "Update app"
43-
if (settings.title != null) {
44-
title = settings.title
45-
}
46-
if (settings.titleRes > 0) {
47-
title = getString(settings.titleRes)
42+
43+
setupTitle(settings, builder)
44+
setupMessage(settings, builder)
45+
setupPositiveButton(settings, builder, isForceUpdate)
46+
setupNegativeButton(settings, builder, isForceUpdate)
47+
48+
return builder.create()
49+
}
50+
51+
private fun setupNegativeButton(settings: DialogSettings, builder: AlertDialog.Builder,
52+
isForceUpdate: Boolean) {
53+
var negButton: String? = "Cancel"
54+
if (settings.negativeButton != null) {
55+
negButton = settings.negativeButton
4856
}
49-
builder.setTitle(title)
50-
var message: String? = "Your application is outdated. Please update to the newest version"
51-
if (settings.message != null) {
52-
message = settings.message
57+
if (settings.negativeButtonRes > 0) {
58+
negButton = getString(settings.negativeButtonRes)
5359
}
54-
if (settings.messageRes > 0) {
55-
message = getString(settings.messageRes)
60+
builder.setNegativeButton(negButton) { _, _ ->
61+
if (isForceUpdate) {
62+
requireActivity().moveTaskToBack(true)
63+
}
5664
}
57-
builder.setMessage(message)
65+
}
66+
67+
private fun setupPositiveButton(settings: DialogSettings, builder: AlertDialog.Builder,
68+
isForceUpdate: Boolean) {
5869
var posButton: String? = "Update"
5970
if (settings.positiveButton != null) {
6071
posButton = settings.positiveButton
@@ -64,25 +75,34 @@ class UpdateDialog : DialogFragment() {
6475
}
6576
builder.setPositiveButton(posButton) { _, _ ->
6677
if (isForceUpdate) {
67-
activity!!.finish()
78+
requireActivity().finish()
6879
}
69-
val appPackageName =
70-
if (settings.packageName == null) activity!!.packageName else settings.packageName // getPackageName() from Context or Activity object
80+
// getPackageName() from Context or Activity object
81+
val appPackageName = if (settings.packageName == null) requireActivity().packageName else settings.packageName
7182
appPackageName?.let { openPlayStore(it) }
7283
}
73-
var negButton: String? = "Cancel"
74-
if (settings.negativeButton != null) {
75-
negButton = settings.negativeButton
84+
}
85+
86+
private fun setupMessage(settings: DialogSettings, builder: AlertDialog.Builder) {
87+
var message: String? = "Your application is outdated. Please update to the newest version"
88+
if (settings.message != null) {
89+
message = settings.message
7690
}
77-
if (settings.negativeButtonRes > 0) {
78-
negButton = getString(settings.negativeButtonRes)
91+
if (settings.messageRes > 0) {
92+
message = getString(settings.messageRes)
7993
}
80-
builder.setNegativeButton(negButton) { _, _ ->
81-
if (isForceUpdate) {
82-
activity!!.moveTaskToBack(true)
83-
}
94+
builder.setMessage(message)
95+
}
96+
97+
private fun setupTitle(settings: DialogSettings, builder: AlertDialog.Builder) {
98+
var title: String? = "Update app"
99+
if (settings.title != null) {
100+
title = settings.title
84101
}
85-
return builder.create()
102+
if (settings.titleRes > 0) {
103+
title = getString(settings.titleRes)
104+
}
105+
builder.setTitle(title)
86106
}
87107

88108
/**

0 commit comments

Comments
 (0)