diff --git a/ChangeLogLib/src/main/java/info/hannes/changelog/ChangeLog.kt b/ChangeLogLib/src/main/java/info/hannes/changelog/ChangeLog.kt index 22d7544..76df852 100644 --- a/ChangeLogLib/src/main/java/info/hannes/changelog/ChangeLog.kt +++ b/ChangeLogLib/src/main/java/info/hannes/changelog/ChangeLog.kt @@ -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. * @@ -29,19 +25,19 @@ open class ChangeLog * @param css CSS styles used to format the change log (excluding ``). */ -@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. * @@ -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. @@ -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() @@ -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() { @@ -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() } } @@ -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>() {}.type var gitList: List? = Gson().fromJson>(text, gitListType) @@ -310,11 +298,11 @@ open class ChangeLog val mergedChangeLog = ArrayList(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) @@ -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) + /** + * 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 + ) companion object { /** @@ -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. */ diff --git a/app/src/main/java/info/hannes/changelog/sample/MainActivity.kt b/app/src/main/java/info/hannes/changelog/sample/MainActivity.kt index 4d7dfab..17470e1 100644 --- a/app/src/main/java/info/hannes/changelog/sample/MainActivity.kt +++ b/app/src/main/java/info/hannes/changelog/sample/MainActivity.kt @@ -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() { @@ -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(R.id.nav_view)?.let { setupDrawerContent(it) @@ -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" } }