Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
hamid97m committed Oct 28, 2024
2 parents c23db79 + c6ed1bf commit c885c98
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 166 deletions.
11 changes: 11 additions & 0 deletions .github/ci-gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
org.gradle.daemon=false
org.gradle.parallel=true
org.gradle.workers.max=2

kotlin.incremental=false

# Controls KotlinOptions.allWarningsAsErrors.
# This value used in CI and is currently set to false.
# If you want to treat warnings as errors locally, set this property to true
# in your ~/.gradle/gradle.properties file.
warningsAsErrors=false
26 changes: 26 additions & 0 deletions .github/workflows/api-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Api Binary Check

on:
push:
branches: [ master ]
pull_request:

concurrency:
group: build-${{ github.ref }}
cancel-in-progress: true

jobs:
api-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Validate binary api
run: ./gradlew clean apiCheck
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,40 @@ BazaarUpdater.getLastUpdateState(context = context) { result ->
}
is UpdateResult.Error -> {
// Handle the error case
val errorMessage = result.message
val errorMessage = result.getError()?.message
}
is UpdateResult.NeedUpdate -> {
// Handle the case where an update is needed
val targetVersion = result.targetVersion
val targetVersion = result.getTargetVersionCode()
}
}
}
```

<details><summary><b>Java Usage</b></summary>

```java
BazaarUpdater.getLastUpdateState(context, result -> {
if (result.isAlreadyUpdated()) {
// Handle the case where the app is already updated
} else if (result.isUpdateNeeded()) {
// Handle the case where an update is needed
long targetVersion = result.getTargetVersionCode();
} else {
// Handle the error case
String errorMessage = result.getError().getMessage();
}
});
```
</details>

#### Update Result States

##### 1. AlreadyUpdated: Indicates that your application is up-to-date.

##### 2. Error: Indicates an error occurred. Use `result.message` to get the error message.

##### 3. NeedUpdate: Indicates that a new update is available. Use `result.targetVersion` to get the version code of the update.
##### 3. NeedUpdate: Indicates that a new update is available. Use `result.getTargetVersionCode()` to get the version code of the update.

### Updating the Application

Expand Down
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)

debugImplementation(libs.androidx.ui.tooling)
}
Original file line number Diff line number Diff line change
@@ -1,137 +1,46 @@
package com.farsitel.bazaar.bazaarupdaterSample

import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.farsitel.bazaar.bazaarupdaterSample.ui.theme.BazaarupdaterSampleTheme
import com.farsitel.bazaar.bazaarupdaterSample.ui.theme.BazaarUpdaterSampleTheme
import com.farsitel.bazaar.updater.BazaarUpdater
import com.farsitel.bazaar.updater.UpdateResult

class MainActivity : ComponentActivity() {

private val updateState = mutableStateOf<UpdateResult?>(null)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
BazaarupdaterSampleTheme {
BazaarUpdaterSampleTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
UpdateScreen(
modifier = Modifier.padding(innerPadding)
updateState = updateState,
modifier = Modifier.padding(innerPadding),
onUpdateClick = ::updateApplication,
onCheckVersionClick = ::checkUpdateState,
)
}
}
}
}
}

@Composable
private fun UpdateScreen(
modifier: Modifier = Modifier
) {
val updateState = remember { mutableStateOf<UpdateResult?>(null) }
val context = LocalContext.current
fun onResult(result: UpdateResult) {
updateState.value = result
private fun updateApplication() {
BazaarUpdater.updateApplication(context = this)
}
Box(
modifier = modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
when (val state = updateState.value) {
UpdateResult.AlreadyUpdated -> AlreadyUpdatedView()
is UpdateResult.Error -> ErrorView(message = state.throwable.message.orEmpty())
is UpdateResult.NeedUpdate -> NeedUpdateView(
targetVersion = state.targetVersion,
onResult = {
BazaarUpdater.updateApplication(context = context)
}
)

null -> CheckUpdateStateView(context = LocalContext.current, onResult = ::onResult)
private fun checkUpdateState() {
BazaarUpdater.getLastUpdateState(context = this) { result ->
updateState.value = result
}
}
}

@Composable
private fun AlreadyUpdatedView(modifier: Modifier = Modifier) {
Text(modifier = modifier, text = stringResource(R.string.your_application_is_updated))
}

@Composable
private fun ErrorView(message: String, modifier: Modifier = Modifier) {
Column {
Text(modifier = modifier, text = message)
}
}

@Composable
private fun NeedUpdateView(
targetVersion: Long,
modifier: Modifier = Modifier,
onResult: (UpdateResult) -> Unit = {}
) {
UpdateButton(
context = LocalContext.current,
text = stringResource(R.string.there_is_new_update_version, targetVersion),
modifier = modifier,
onResult = onResult,
)
}

@Composable
private fun UpdateButton(
context: Context,
text: String,
modifier: Modifier = Modifier,
onResult: (UpdateResult) -> Unit = {}
) {
Button(
modifier = modifier,
onClick = {
checkUpdateState(
context = context,
onResult = onResult
)
}
) {
Text(text = text)
}
}

@Composable
private fun CheckUpdateStateView(context: Context, onResult: (UpdateResult) -> Unit) {
UpdateButton(
context = context,
onResult = onResult,
text = stringResource(R.string.check_update)
)
}

private fun checkUpdateState(context: Context, onResult: (UpdateResult) -> Unit) {
BazaarUpdater.getLastUpdateState(context = context, onResult = onResult)
}

@Preview(showBackground = true)
@Composable
fun UpdateScreenPreview() {
BazaarupdaterSampleTheme {
UpdateScreen()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.farsitel.bazaar.bazaarupdaterSample

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.farsitel.bazaar.bazaarupdaterSample.ui.theme.BazaarUpdaterSampleTheme
import com.farsitel.bazaar.updater.UpdateResult

@Composable
fun UpdateScreen(
updateState: State<UpdateResult?>,
modifier: Modifier = Modifier,
onUpdateClick: () -> Unit = {},
onCheckVersionClick: () -> Unit = {},
) {
Box(
modifier = modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
) {
when (val state = updateState.value) {
UpdateResult.AlreadyUpdated -> {
AlreadyUpdatedView()
}
is UpdateResult.Error -> {
ErrorView(
message = state.throwable.message.orEmpty(),
)
}
is UpdateResult.NeedUpdate -> {
NeedUpdateView(
targetVersion = state.getTargetVersionCode(),
onClick = onUpdateClick,
)
}
else -> {
CheckUpdateStateView(
onClick = onCheckVersionClick,
)
}
}
}
}

@Composable
private fun AlreadyUpdatedView(
modifier: Modifier = Modifier,
) {
Text(
modifier = modifier,
text = stringResource(R.string.your_application_is_updated),
)
}

@Composable
private fun ErrorView(
message: String,
modifier: Modifier = Modifier,
) {
Text(
modifier = modifier,
text = message,
)
}

@Composable
private fun NeedUpdateView(
targetVersion: Long,
modifier: Modifier = Modifier,
onClick: () -> Unit = {},
) {
UpdateButton(
text = stringResource(R.string.there_is_new_update_version, targetVersion),
modifier = modifier,
onClick = onClick,
)
}

@Composable
private fun CheckUpdateStateView(
modifier: Modifier = Modifier,
onClick: () -> Unit = {},
) {
UpdateButton(
text = stringResource(R.string.check_update),
modifier = modifier,
onClick = onClick,
)
}

@Composable
private fun UpdateButton(
text: String,
modifier: Modifier = Modifier,
onClick: () -> Unit = {},
) {
Button(
modifier = modifier,
onClick = onClick,
) {
Text(text = text)
}
}

@Preview(showBackground = true)
@Composable
private fun UpdateScreenPreview() {
BazaarUpdaterSampleTheme {
UpdateScreen(
updateState = remember {
mutableStateOf(null)
},
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.farsitel.bazaar.bazaarupdaterSample.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
Expand Down Expand Up @@ -34,7 +33,7 @@ private val LightColorScheme = lightColorScheme(
)

@Composable
fun BazaarupdaterSampleTheme(
fun BazaarUpdaterSampleTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ activityCompose = "1.9.1"
composeBom = "2024.06.00"
appcompat = "1.7.0"
material = "1.12.0"
binary-compatibility-validator = "0.16.3"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand All @@ -35,4 +36,5 @@ android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
android-library = { id = "com.android.library", version.ref = "agp" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binary-compatibility-validator" }

Loading

0 comments on commit c885c98

Please sign in to comment.