Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
MFlisarWork committed Nov 12, 2024
1 parent 27d2d59 commit e387091
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class MainActivity : DemoActivity() {

// optional - here you can apply some customisations like changelog resource id, localized texts, styles, filter, sorter, ...
val setup = ChangelogDefaults.setup(
context = context,
//changelogResourceId = R.raw.changelog, // this is the default
versionFormatter = Constants.CHANGELOG_FORMATTER
)

Expand Down Expand Up @@ -99,6 +101,7 @@ class MainActivity : DemoActivity() {
// eventually show a full changelog dialog
if (showChangelog.value) {
val setup = ChangelogDefaults.setup(
context = context,
filter = if (filterDogs.value) ChangelogDefaults.filter(
"dogs",
false
Expand Down Expand Up @@ -170,7 +173,7 @@ class MainActivity : DemoActivity() {
Column {
Text("App Version", fontWeight = FontWeight.Bold)
Text(
"Code: ${ChangelogUtil.getAppVersionCode(context, Constants.CHANGELOG_FORMATTER)}",
"Code: ${ChangelogUtil.getAppVersionCode(context)}",
style = MaterialTheme.typography.bodySmall
)
Text(
Expand Down
2 changes: 1 addition & 1 deletion demo/desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
id("io.github.mflisar.composechangelog.gradle-plugin")
}

val version = "1.0.6"
val version = "1.0.4"
val code = Changelog.buildVersionCode(version, DefaultVersionFormatter(DefaultVersionFormatter.Format.MajorMinorPatchCandidate))

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.michaelflisar.composechangelog

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.compose.runtime.Composable
Expand All @@ -8,6 +9,7 @@ import com.michaelflisar.composechangelog.interfaces.IChangelogFilter

@Composable
fun ChangelogDefaults.setup(
context: Context,
changelogResourceId: Int = R.raw.changelog,
texts: ChangelogSetup.Texts = texts(),
useShowMoreButtons: Boolean = true,
Expand All @@ -16,7 +18,15 @@ fun ChangelogDefaults.setup(
filter: IChangelogFilter? = null,
renderer: ChangelogSetup.Renderer = renderer()
) = ChangelogSetup(
changelogId = changelogResourceId,
logFileReader = {
val resourceType: String = context.resources.getResourceTypeName(changelogResourceId)
if (resourceType == "raw") {
val inputStream = context.resources.openRawResource(changelogResourceId)
val bytes = inputStream.readBytes()
inputStream.close()
bytes
} else throw RuntimeException("Wrong changelog resource type, provide a raw resource!")
},
texts = texts,
useShowMoreButtons = useShowMoreButtons,
versionFormatter = versionFormatter,
Expand Down Expand Up @@ -49,7 +59,7 @@ fun ChangelogUtil.getAppVersionName(context: Context): String {
* @return the app version code
*/
@Suppress("DEPRECATION")
fun ChangelogUtil.getAppVersionCode(context: Context, versionFormatter: ChangelogVersionFormatter): Long {
fun ChangelogUtil.getAppVersionCode(context: Context): Long {
try {
val info = context.packageManager.getPackageInfo(context.packageName, 0)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ import com.michaelflisar.composechangelog.classes.DataItemRelease
import com.michaelflisar.composechangelog.composables.Changelog
import com.michaelflisar.composechangelog.internal.ChangelogParserUtil

actual typealias Context = android.content.Context
actual typealias ChangelogID = Int

@Composable
actual fun stringOk() = stringResource(android.R.string.ok)

@Composable
internal actual fun LocalContext(): Context {
return androidx.compose.ui.platform.LocalContext.current
}

@Composable
internal actual fun ShowChangelogDialog(
visible: MutableState<Boolean>,
Expand Down Expand Up @@ -62,11 +54,10 @@ internal actual fun LazyScrollContainer(state: LazyListState, content: LazyListS
}
}

internal actual suspend fun ChangelogUtil.read(
context: Context,
changelogID: ChangelogID,
internal actual suspend fun ChangelogUtil.readFile(
logFileReader: suspend () -> ByteArray,
versionFormatter: ChangelogVersionFormatter,
sorter: Comparator<DataItemRelease>?
): ChangelogData {
return ChangelogParserUtil.parse(context, changelogID, versionFormatter, sorter)
return ChangelogParserUtil.parse(logFileReader, versionFormatter, sorter)
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package com.michaelflisar.composechangelog.internal

import android.content.Context
import android.util.Log
import android.util.Xml
import com.michaelflisar.composechangelog.ChangelogVersionFormatter
import com.michaelflisar.composechangelog.classes.ChangelogData
import com.michaelflisar.composechangelog.classes.DataItemRelease
import com.michaelflisar.composechangelog.classes.DataItem
import com.michaelflisar.composechangelog.classes.DataItemRelease
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import java.io.IOException
import java.io.InputStream

internal object ChangelogParserUtil {

suspend fun parse(
context: Context,
resourceFile: Int,
logFileReader: suspend () -> ByteArray,
versionFormatter: ChangelogVersionFormatter,
sorter: Comparator<DataItemRelease>? = null
): ChangelogData {
return withContext(Dispatchers.IO) {
try {
val parser: XmlPullParser
val resourceType: String = context.resources.getResourceTypeName(resourceFile)
if (resourceType == "raw") {
val `in`: InputStream = context.resources.openRawResource(resourceFile)
parser = Xml.newPullParser()
parser.setInput(`in`, null)
} else if (resourceType == "xml") {
parser = context.resources.getXml(resourceFile)
} else throw RuntimeException("Wrong changelog resource type, provide xml or raw resource!")
val bytes = logFileReader()
val inputStream = bytes.inputStream()
val parser = Xml.newPullParser()
parser.setInput(inputStream, null)

var id: Int = 1
val idProvider = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ import org.jetbrains.compose.resources.stringResource

object ChangelogDefaults {

@Composable
fun setup(
logFileReader: suspend () -> ByteArray,
texts: ChangelogSetup.Texts = ChangelogDefaults.texts(),
useShowMoreButtons: Boolean = true,
versionFormatter: ChangelogVersionFormatter = DefaultVersionFormatter(),
sorter: ((items: List<DataItem>) -> List<DataItem>)? = ChangelogDefaults.sorter(),
filter: IChangelogFilter? = null,
renderer: ChangelogSetup.Renderer = ChangelogDefaults.renderer()
) = ChangelogSetup(
logFileReader = logFileReader,
texts = texts,
useShowMoreButtons = useShowMoreButtons,
versionFormatter = versionFormatter,
filter = filter,
sorter = sorter,
renderer = renderer
)

@Composable
fun tagNameFormatter() = @Composable { tag: String ->
tag.uppercase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.michaelflisar.composechangelog.classes.DataItemRelease
import com.michaelflisar.composechangelog.interfaces.IChangelogFilter

data class ChangelogSetup internal constructor(
val changelogId: ChangelogID,
val logFileReader: suspend () -> ByteArray,
val texts: Texts,
val useShowMoreButtons: Boolean,
val versionFormatter: ChangelogVersionFormatter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import androidx.compose.runtime.MutableState
import com.michaelflisar.composechangelog.classes.ChangelogData
import com.michaelflisar.composechangelog.classes.DataItemRelease

expect abstract class Context
expect class ChangelogID

@Composable
expect fun stringOk() : String

@Composable
internal expect fun LocalContext(): Context

@Composable
internal expect fun ShowChangelogDialog(
visible: MutableState<Boolean>,
Expand All @@ -27,9 +21,8 @@ internal expect fun ShowChangelogDialog(
@Composable
internal expect fun LazyScrollContainer(state: LazyListState, content: LazyListScope.() -> Unit)

internal expect suspend fun ChangelogUtil.read(
context: Context,
changelogID: ChangelogID,
internal expect suspend fun ChangelogUtil.readFile(
logFileReader: suspend () -> ByteArray,
versionFormatter: ChangelogVersionFormatter,
sorter: Comparator<DataItemRelease>? = null
): ChangelogData
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ fun ShowChangelogDialog(
setup: ChangelogSetup,
onDismiss: () -> Unit = {}
) {
val context = LocalContext()
val changelog = remember { mutableStateOf<ChangelogData?>(null) }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
val data = ChangelogUtil.read(context, setup.changelogId, setup.versionFormatter)
val data = ChangelogUtil.readFile(setup.logFileReader, setup.versionFormatter)
changelog.value = filterAndSort(data, setup)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fun ChangelogDefaults.setup(
filter: IChangelogFilter? = null,
renderer: ChangelogSetup.Renderer = ChangelogDefaults.renderer()
) = ChangelogSetup(
changelogId = file,
logFileReader = { file.readBytes() },
texts = texts,
useShowMoreButtons = useShowMoreButtons,
versionFormatter = versionFormatter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,9 @@ import com.michaelflisar.composechangelog.composables.Changelog
import com.michaelflisar.composechangelog.internal.ChangelogParserUtil
import java.io.File

actual abstract class Context
actual typealias ChangelogID = File

@Composable
actual fun stringOk() = "OK"

object NoContext: Context()

@Composable
internal actual fun LocalContext(): Context {
return NoContext
}

@Composable
internal actual fun ShowChangelogDialog(
visible: MutableState<Boolean>,
Expand Down Expand Up @@ -80,11 +70,10 @@ internal actual fun LazyScrollContainer(state: LazyListState, content: LazyListS
}
}

internal actual suspend fun ChangelogUtil.read(
context: Context,
changelogID: ChangelogID,
internal actual suspend fun ChangelogUtil.readFile(
logFileReader: suspend () -> ByteArray,
versionFormatter: ChangelogVersionFormatter,
sorter: Comparator<DataItemRelease>?
): ChangelogData {
return ChangelogParserUtil.parse(changelogID, versionFormatter, sorter)
return ChangelogParserUtil.parse(logFileReader, versionFormatter, sorter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import javax.xml.parsers.DocumentBuilderFactory
internal object ChangelogParserUtil {

suspend fun parse(
file: File,
logFileReader: suspend () -> ByteArray,
versionFormatter: ChangelogVersionFormatter,
sorter: Comparator<DataItemRelease>? = null
): ChangelogData {
Expand All @@ -27,7 +27,9 @@ internal object ChangelogParserUtil {
val dbf = DocumentBuilderFactory.newInstance()
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
val db = dbf.newDocumentBuilder()
val doc = db.parse(file)
val bytes = logFileReader()
val inputStream = bytes.inputStream()
val doc = db.parse(inputStream)

var id: Int = 1
val idProvider = {
Expand Down

0 comments on commit e387091

Please sign in to comment.