Skip to content

Commit

Permalink
Merge pull request #200 from AppDevNext/OptionalDialogDismissCallback
Browse files Browse the repository at this point in the history
Optional dialog dismiss callback
  • Loading branch information
hannesa2 authored Dec 3, 2024
2 parents e7efc12 + 8e72f36 commit cb4fa5d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import java.util.Collections
open class ChangeLog @JvmOverloads constructor(
private val context: Context,
preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context),
protected val css: String = DEFAULT_CSS
protected val css: String = DEFAULT_CSS,
val callback: (() -> Unit)? = null
) {
/**
* Get version code of last installation.
Expand Down Expand Up @@ -208,6 +209,7 @@ open class ChangeLog @JvmOverloads constructor(
) { _, _ ->
// The user clicked "OK" so save the current version code as "last version code".
updateVersionInPreferences()
callback?.invoke()
}

if (!full) {
Expand Down
21 changes: 17 additions & 4 deletions app/src/main/java/info/hannes/changelog/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.ContextThemeWrapper
import android.view.Menu
import android.view.MenuItem
Expand Down Expand Up @@ -36,7 +37,9 @@ class MainActivity : AppCompatActivity() {
setupDrawerContent(it)
}

val changeLog = ChangeLog(this)
val changeLog = ChangeLog(this, callback = {
Log.d("log", "dialog dismiss")
})
if (changeLog.isFirstRun) {
changeLog.logDialog.show()
}
Expand All @@ -54,7 +57,10 @@ class MainActivity : AppCompatActivity() {
private fun selectNavigationItem(itemId: Int) {

when (itemId) {
R.id.nav_full_changelog -> ChangeLog(this).fullLogDialog.show()
R.id.nav_full_changelog -> ChangeLog(this, callback = {
Log.d("log", "dialog dismiss")
}).fullLogDialog.show()

R.id.nav_whats_new -> DarkThemeChangeLog(this).logDialog.show()
R.id.nav_other_github -> {
val url = "https://github.com/hannesa2/ChangeLog"
Expand All @@ -74,15 +80,22 @@ class MainActivity : AppCompatActivity() {
when (item.itemId) {
android.R.id.home -> mDrawerLayout.openDrawer(GravityCompat.START)
R.id.nav_whats_new -> DarkThemeChangeLog(this).logDialog.show()
R.id.nav_full_changelog -> ChangeLog(this).fullLogDialog.show()
R.id.nav_full_changelog -> ChangeLog(this, callback = {
Log.d("log", "dialog dismiss")
}).fullLogDialog.show()
}
return true
}

/**
* Example that shows how to create a themed dialog.
*/
class DarkThemeChangeLog internal constructor(context: Context) : ChangeLog(ContextThemeWrapper(context, R.style.DarkTheme), DARK_THEME_CSS)
class DarkThemeChangeLog internal constructor(context: Context) : ChangeLog(
ContextThemeWrapper(context, R.style.DarkTheme),
css = DARK_THEME_CSS,
callback = {
Log.d("DarkThemeChangeLog", "dialog dismiss")
})

companion object {
internal val DARK_THEME_CSS = "body { color: #ffffff; background-color: #282828; }\n$DEFAULT_CSS"
Expand Down

0 comments on commit cb4fa5d

Please sign in to comment.