Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmetic #198

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 48 additions & 56 deletions ChangeLogLib/src/main/java/info/hannes/changelog/ChangeLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ import info.hannes.R
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import java.io.IOException
import java.util.*
import java.util.Collections


/**
* Display a dialog showing a full or partial (What's New) change log.
*/
open class ChangeLog
/**
* Create a `ChangeLog` instance using the supplied `SharedPreferences` instance.
*
Expand All @@ -29,19 +25,19 @@ open class ChangeLog
* @param css CSS styles used to format the change log (excluding `<style>` and
* `</style>`).
*/
@JvmOverloads constructor(
/**
* Context that is used to access the resources and to create the ChangeLog dialogs.
*/
private val context: Context, preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context),
/**
* Contains the CSS rules used to format the change log.
*/
protected val css: String = DEFAULT_CSS) {
open class ChangeLog @JvmOverloads constructor(
private val context: Context, preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context),
/**
* Contains the CSS rules used to format the change log.
*/
protected val css: String = DEFAULT_CSS
) {

/**
* Last version code read from `SharedPreferences` or [.NO_VERSION].
*/

// Get last version code
/**
* Get version code of last installation.
*
Expand All @@ -51,7 +47,7 @@ open class ChangeLog
* `ChangeLog` is instantiated).
* @see [android:versionCode](http://developer.android.com/guide/topics/manifest/manifest-element.html.vcode)
*/
val lastVersionCode: Int
val lastVersionCode: Int = preferences.getInt(VERSION_KEY, NO_VERSION)

/**
* Version code of the current installation.
Expand Down Expand Up @@ -166,14 +162,8 @@ open class ChangeLog
constructor(context: Context, css: String) : this(context, PreferenceManager.getDefaultSharedPreferences(context), css) {}

init {

// Get last version code
lastVersionCode = preferences.getInt(VERSION_KEY, NO_VERSION)

// Get current version code and version name
try {
val packageInfo = context.packageManager.getPackageInfo(
context.packageName, 0)
val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
currentVersionCode = packageInfo.longVersionCode.toInt()
Expand All @@ -192,11 +182,7 @@ open class ChangeLog
/**
* Skip the "What's new" dialog for this app version.
*
*
*
*
* Future calls to [.isFirstRun] and [.isFirstRunEver] will return `false`
* for the current app version.
* Future calls to [.isFirstRun] and [.isFirstRunEver] will return `false` for the current app version.
*
*/
fun skipLogDialog() {
Expand All @@ -217,20 +203,22 @@ open class ChangeLog

val builder = AlertDialog.Builder(context)
builder.setTitle(
context.resources.getString(if (full) R.string.changelog_full_title else R.string.changelog_title))
.setView(webView)
.setCancelable(false)
// OK button
.setPositiveButton(
context.resources.getString(R.string.changelog_ok_button)
) { _, _ ->
// The user clicked "OK" so save the current version code as "last version code".
updateVersionInPreferences()
}
context.resources.getString(if (full) R.string.changelog_full_title else R.string.changelog_title)
)
.setView(webView)
.setCancelable(false)
// OK button
.setPositiveButton(
context.resources.getString(R.string.changelog_ok_button)
) { _, _ ->
// The user clicked "OK" so save the current version code as "last version code".
updateVersionInPreferences()
}

if (!full) {
// Show "More…" button if we're only displaying a partial change log.
builder.setNegativeButton(R.string.changelog_show_full
builder.setNegativeButton(
R.string.changelog_show_full
) { _, _ -> fullLogDialog.show() }
}

Expand Down Expand Up @@ -296,7 +284,7 @@ open class ChangeLog
val changelog = getLocalizedChangeLog(full)

val text = context.resources.openRawResource(R.raw.gitlog)
.bufferedReader().use { it.readText() }.replace("},]", "}]")
.bufferedReader().use { it.readText() }.replace("},]", "}]")

val gitListType = object : TypeToken<List<Gitlog>>() {}.type
var gitList: List<Gitlog>? = Gson().fromJson<List<Gitlog>>(text, gitListType)
Expand All @@ -310,11 +298,11 @@ open class ChangeLog

val mergedChangeLog = ArrayList<ReleaseItem>(masterChangelog.size() + gitGroup.count())
gitGroup.filter { filter -> filter.value.count() > 0 }
.forEach {
val list = it.value.map { item -> item.message.orEmpty() }
val abc = ReleaseItem(99, it.value[0].version.orEmpty(), list)
mergedChangeLog.add(abc)
}
.forEach {
val list = it.value.map { item -> item.message.orEmpty() }
val abc = ReleaseItem(99, it.value[0].version.orEmpty(), list)
mergedChangeLog.add(abc)
}

for (i in 0 until masterChangelog.size()) {
val key = masterChangelog.keyAt(i)
Expand Down Expand Up @@ -473,18 +461,19 @@ open class ChangeLog
* Container used to store information about a release/version.
*/
class ReleaseItem internal constructor(
/**
* Version code of the release.
*/
val versionCode: Int,
/**
* Version name of the release.
*/
val versionName: String,
/**
* List of changes introduced with that release.
*/
val changes: List<String>)
/**
* Version code of the release.
*/
val versionCode: Int,
/**
* Version name of the release.
*/
val versionName: String,
/**
* List of changes introduced with that release.
*/
val changes: List<String>
)

companion object {
/**
Expand All @@ -493,14 +482,17 @@ open class ChangeLog
const val DEFAULT_CSS = "h1 { margin-left: 0px; font-size: 1.2em; }" + "\n" +
"li { margin-left: 0px; }" + "\n" +
"ul { padding-left: 2em; }"

/**
* Tag that is used when sending error/debug messages to the log.
*/
private const val LOG_TAG = "ChangeLog"

/**
* This is the key used when storing the version code in SharedPreferences.
*/
protected const val VERSION_KEY = "ChangeLog_last_version_code"

/**
* Constant that used when no version code is available.
*/
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/info/hannes/changelog/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import info.hannes.changelog.ChangeLog
import info.hannes.changelog.ChangeLog.Companion.DEFAULT_CSS

class MainActivity : AppCompatActivity() {

Expand All @@ -28,8 +29,8 @@ class MainActivity : AppCompatActivity() {
setSupportActionBar(toolbar)

// enable ActionBar app icon to behave as action to toggle nav drawer
supportActionBar!!.setHomeAsUpIndicator(R.drawable.ic_menu)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_menu)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

findViewById<NavigationView>(R.id.nav_view)?.let {
setupDrawerContent(it)
Expand Down Expand Up @@ -81,9 +82,9 @@ class MainActivity : AppCompatActivity() {
/**
* Example that shows how to create a themed dialog.
*/
class DarkThemeChangeLog internal constructor(context: Context) : ChangeLog(ContextThemeWrapper(context, R.style.DarkTheme), DARK_THEME_CSS) {
companion object {
internal val DARK_THEME_CSS = "body { color: #ffffff; background-color: #282828; }\n$DEFAULT_CSS"
}
class DarkThemeChangeLog internal constructor(context: Context) : ChangeLog(ContextThemeWrapper(context, R.style.DarkTheme), DARK_THEME_CSS)

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